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