weibit | 38c42ed | 2014-10-09 19:03:54 -0700 | [diff] [blame^] | 1 | package org.onlab.util; |
| 2 | |
| 3 | import org.junit.Test; |
| 4 | |
| 5 | import com.esotericsoftware.minlog.Log; |
| 6 | |
| 7 | import junit.framework.TestCase; |
| 8 | |
| 9 | /** |
| 10 | * Test of the Hexstring. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | public class HexStringTest extends TestCase { |
| 15 | |
| 16 | @Test |
| 17 | public void testMarshalling() throws Exception { |
| 18 | String dpidStr = "00:00:00:23:20:2d:16:71"; |
| 19 | long dpid = HexString.toLong(dpidStr); |
| 20 | String testStr = HexString.toHexString(dpid); |
| 21 | TestCase.assertEquals(dpidStr, testStr); |
| 22 | } |
| 23 | |
| 24 | @Test |
| 25 | public void testToLong() { |
| 26 | String dpidStr = "3e:1f:01:fc:72:8c:63:31"; |
| 27 | long valid = 0x3e1f01fc728c6331L; |
| 28 | long testLong = HexString.toLong(dpidStr); |
| 29 | TestCase.assertEquals(valid, testLong); |
| 30 | } |
| 31 | |
| 32 | @Test |
| 33 | public void testToLongMSB() { |
| 34 | String dpidStr = "ca:7c:5e:d1:64:7a:95:9b"; |
| 35 | long valid = -3856102927509056101L; |
| 36 | long testLong = HexString.toLong(dpidStr); |
| 37 | TestCase.assertEquals(valid, testLong); |
| 38 | } |
| 39 | |
| 40 | @Test |
| 41 | public void testToLongError() { |
| 42 | String dpidStr = "09:08:07:06:05:04:03:02:01"; |
| 43 | try { |
| 44 | HexString.toLong(dpidStr); |
| 45 | fail("HexString.toLong() should have thrown a NumberFormatException"); |
| 46 | } catch (NumberFormatException expected) { |
| 47 | Log.info("HexString.toLong() have thrown a NumberFormatException"); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | @Test |
| 52 | public void testToStringBytes() { |
| 53 | byte[] dpid = {0, 0, 0, 0, 0, 0, 0, -1 }; |
| 54 | String valid = "00:00:00:00:00:00:00:ff"; |
| 55 | String testString = HexString.toHexString(dpid); |
| 56 | TestCase.assertEquals(valid, testString); |
| 57 | } |
| 58 | |
| 59 | @Test |
| 60 | public void testFromHexStringError() { |
| 61 | String invalidStr = "00:00:00:00:00:00:ffff"; |
| 62 | try { |
| 63 | HexString.fromHexString(invalidStr); |
| 64 | fail("HexString.fromHexString() should have thrown a NumberFormatException"); |
| 65 | } catch (NumberFormatException expected) { |
| 66 | Log.info("HexString.toLong() have thrown a NumberFormatException"); |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |