blob: 27652123a838c9122b9cbf9f8840d88388072fc0 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska24c849c2014-10-27 09:53:05 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
Thomas Vachuska24c849c2014-10-27 09:53:05 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Thomas Vachuska24c849c2014-10-27 09:53:05 -070015 */
weibit38c42ed2014-10-09 19:03:54 -070016package org.onlab.util;
17
18import org.junit.Test;
19
20import com.esotericsoftware.minlog.Log;
21
22import junit.framework.TestCase;
23
24/**
25 * Test of the Hexstring.
26 *
27 */
28
29public class HexStringTest extends TestCase {
30
31 @Test
32 public void testMarshalling() throws Exception {
33 String dpidStr = "00:00:00:23:20:2d:16:71";
34 long dpid = HexString.toLong(dpidStr);
35 String testStr = HexString.toHexString(dpid);
36 TestCase.assertEquals(dpidStr, testStr);
37 }
38
39 @Test
40 public void testToLong() {
41 String dpidStr = "3e:1f:01:fc:72:8c:63:31";
42 long valid = 0x3e1f01fc728c6331L;
43 long testLong = HexString.toLong(dpidStr);
44 TestCase.assertEquals(valid, testLong);
45 }
46
47 @Test
48 public void testToLongMSB() {
49 String dpidStr = "ca:7c:5e:d1:64:7a:95:9b";
50 long valid = -3856102927509056101L;
51 long testLong = HexString.toLong(dpidStr);
52 TestCase.assertEquals(valid, testLong);
53 }
54
55 @Test
56 public void testToLongError() {
57 String dpidStr = "09:08:07:06:05:04:03:02:01";
58 try {
59 HexString.toLong(dpidStr);
60 fail("HexString.toLong() should have thrown a NumberFormatException");
61 } catch (NumberFormatException expected) {
62 Log.info("HexString.toLong() have thrown a NumberFormatException");
63 }
64 }
65
66 @Test
67 public void testToStringBytes() {
68 byte[] dpid = {0, 0, 0, 0, 0, 0, 0, -1 };
69 String valid = "00:00:00:00:00:00:00:ff";
70 String testString = HexString.toHexString(dpid);
71 TestCase.assertEquals(valid, testString);
72 }
73
74 @Test
75 public void testFromHexStringError() {
76 String invalidStr = "00:00:00:00:00:00:ffff";
77 try {
78 HexString.fromHexString(invalidStr);
79 fail("HexString.fromHexString() should have thrown a NumberFormatException");
80 } catch (NumberFormatException expected) {
81 Log.info("HexString.toLong() have thrown a NumberFormatException");
82 }
83 }
84}
85