blob: 926135fcb0b74051bf59f1c6575ceb198a5f0a36 [file] [log] [blame]
Ray Milkeyd5a1b202014-05-07 13:44:44 -07001package net.onrc.onos.core.datastore.utils;
2
3import org.hamcrest.MatcherAssert;
4import org.junit.Test;
5
6import static net.onrc.onos.core.util.UtilityClassChecker.assertThatClassIsUtility;
7import static org.hamcrest.Matchers.equalTo;
8import static org.hamcrest.Matchers.is;
9import static org.hamcrest.Matchers.notNullValue;
10
11/**
12 * Test cases for the ByteArrayUtil class.
13 */
14
15public class ByteArrayUtilTest {
16
17 /**
18 * Make sure that the ByteArrayUtil class is a utility class.
19 */
20 @Test
21 public void testForWellDefinedUtilityClass() throws Exception {
22 assertThatClassIsUtility(ByteArrayUtil.class);
23 }
24
25 /**
26 * Test that when given a null array for the bytes to convert, the hex
27 * conversion routines return an empty StringBuilder.
28 */
29 @Test
30 public void testToHexStringBuilderNullParameter() {
31 final StringBuilder allocatedBuilder =
32 ByteArrayUtil.toHexStringBuilder(null, "");
33 MatcherAssert.assertThat(allocatedBuilder, is(notNullValue()));
34 MatcherAssert.assertThat(allocatedBuilder.toString(), is(equalTo("")));
35
36 final StringBuilder providedBuilder = new StringBuilder();
37 final StringBuilder returnedBuilder = ByteArrayUtil.toHexStringBuilder
38 (null, "", providedBuilder);
39 MatcherAssert.assertThat(providedBuilder, is(notNullValue()));
40 MatcherAssert.assertThat(returnedBuilder, is(equalTo(providedBuilder)));
41 MatcherAssert.assertThat(providedBuilder.toString(), is(equalTo("")));
42 }
43
44 /**
45 * Test the array byte to String conversion routines to be sure the data
46 * is converted properly.
47 */
48 @Test
49 public void testToHexStringBuilder() {
50 final byte[] testData = {0x1, 0x2, 0x3, 0x4, 0x5,
51 0x3b, 0x3c, 0x3d, 0x3e, 0x3f};
52 final String expectedResult = "1:2:3:4:5:3b:3c:3d:3e:3f";
53
54 final StringBuilder result =
55 ByteArrayUtil.toHexStringBuilder(testData, ":");
56
57 MatcherAssert.assertThat(result, is(notNullValue()));
58 MatcherAssert.assertThat(result.toString(),
59 is(equalTo(expectedResult)));
60
61 final StringBuilder providedBuilder = new StringBuilder();
62 final StringBuilder returnedBuilder =
63 ByteArrayUtil.toHexStringBuilder(testData, ":", providedBuilder);
64
65 MatcherAssert.assertThat(returnedBuilder, is(notNullValue()));
66 MatcherAssert.assertThat(returnedBuilder, is(equalTo(providedBuilder)));
67 MatcherAssert.assertThat(returnedBuilder.toString(),
68 is(equalTo(expectedResult)));
69 }
70}