blob: f4aadd1589506d5fb87ce3b81e0e4b412f7cccfb [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
Sho SHIMIZU8dc81ea2015-09-10 10:59:43 -070024import static org.junit.Assert.fail;
25
weibit38c42ed2014-10-09 19:03:54 -070026/**
27 * Test of the Hexstring.
28 *
29 */
30
Sho SHIMIZU8dc81ea2015-09-10 10:59:43 -070031public class HexStringTest {
weibit38c42ed2014-10-09 19:03:54 -070032
33 @Test
34 public void testMarshalling() throws Exception {
35 String dpidStr = "00:00:00:23:20:2d:16:71";
36 long dpid = HexString.toLong(dpidStr);
37 String testStr = HexString.toHexString(dpid);
38 TestCase.assertEquals(dpidStr, testStr);
39 }
40
41 @Test
42 public void testToLong() {
43 String dpidStr = "3e:1f:01:fc:72:8c:63:31";
44 long valid = 0x3e1f01fc728c6331L;
45 long testLong = HexString.toLong(dpidStr);
46 TestCase.assertEquals(valid, testLong);
47 }
48
49 @Test
Jonathan Hartd9df7bd2015-11-10 17:10:25 -080050 public void testToLongMsb() {
weibit38c42ed2014-10-09 19:03:54 -070051 String dpidStr = "ca:7c:5e:d1:64:7a:95:9b";
52 long valid = -3856102927509056101L;
53 long testLong = HexString.toLong(dpidStr);
54 TestCase.assertEquals(valid, testLong);
55 }
56
57 @Test
58 public void testToLongError() {
59 String dpidStr = "09:08:07:06:05:04:03:02:01";
60 try {
61 HexString.toLong(dpidStr);
62 fail("HexString.toLong() should have thrown a NumberFormatException");
63 } catch (NumberFormatException expected) {
64 Log.info("HexString.toLong() have thrown a NumberFormatException");
65 }
66 }
67
68 @Test
69 public void testToStringBytes() {
70 byte[] dpid = {0, 0, 0, 0, 0, 0, 0, -1 };
71 String valid = "00:00:00:00:00:00:00:ff";
72 String testString = HexString.toHexString(dpid);
73 TestCase.assertEquals(valid, testString);
74 }
75
76 @Test
77 public void testFromHexStringError() {
78 String invalidStr = "00:00:00:00:00:00:ffff";
79 try {
80 HexString.fromHexString(invalidStr);
81 fail("HexString.fromHexString() should have thrown a NumberFormatException");
82 } catch (NumberFormatException expected) {
83 Log.info("HexString.toLong() have thrown a NumberFormatException");
84 }
85 }
86}
87