Further unit tests: Util and Delta.
[cfb.git] / test / net / jaekl / cfb / util / UtilTest.java
1 package net.jaekl.cfb.util;
2
3 import static org.junit.Assert.*;
4
5 import java.sql.SQLException;
6 import java.util.Arrays;
7 import java.util.List;
8
9 import org.junit.Test;
10
11 public class UtilTest {
12         @Test
13         public void testListsAreEqual()
14         {
15                 Object[][][] equal = {
16                                 {
17                                         null,
18                                         null
19                                 },
20                                 {
21                                         {},
22                                         {},
23                                 },
24                                 {
25                                         { "one", "two", "three" },
26                                         { "one", "two", "three" }
27                                 },
28                                 {
29                                         { Integer.valueOf(1), Integer.valueOf(2048), Integer.valueOf(3) },
30                                         { Integer.valueOf(1), Integer.valueOf(2048), Integer.valueOf(3) }
31                                 },
32                                 {
33                                         { Integer.valueOf(1), Integer.valueOf(2048), Integer.valueOf(3) },
34                                         { Integer.valueOf(1), Integer.valueOf(2048), Long.valueOf(3) }
35                                 }                               
36                 };
37                 Object[][][] unequal = {
38                                 {
39                                         null,
40                                         { "one", "two", "three" }
41                                 },
42                                 {
43                                         { Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3) },
44                                         null
45                                 },
46                                 {
47                                         { "1", "1", "2" },
48                                         { "1", "2", "1" }
49                                 },
50                                 {
51                                         { "1", "1", "2" },
52                                         { "1", "2", "2" }
53                                 },
54                                 {
55                                         { "1", "1", "2" },
56                                         { "1", "2" }
57                                 },
58                                 {
59                                         { "1", "2", "3" },
60                                         { Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3) }
61                                 },
62                                 { 
63                                         {},
64                                         { "1", "2", "3"}
65                                 },
66                                 {
67                                         { "1", "2", "3"},
68                                         {}
69                                 },
70                                 {
71                                         { "alpha", "beta" },
72                                         { "a", "b" }
73                                 }
74                 };
75
76                 for (Object[][] lists : equal) {
77                         List<?> a = (null == lists[0]) ? null : Arrays.asList(lists[0]);
78                         List<?> b = (null == lists[1]) ? null : Arrays.asList(lists[1]);
79                         boolean result = Util.listsAreEqual(a, b);
80                         assertTrue(result);
81                 }
82                 
83                 for (Object[][] lists : unequal) {
84                         List<?> a = (null == lists[0]) ? null : Arrays.asList(lists[0]);
85                         List<?> b = (null == lists[1]) ? null : Arrays.asList(lists[1]);
86                         boolean result = Util.listsAreEqual(a, b);
87                         assertFalse(result);
88                 }
89         }
90         
91         @Test
92         public void testObjHashCode()
93         {
94                 Object[] data = {
95                                 Integer.valueOf(12),
96                                 "arma virumque cano, Troiae qui primus ab oris",
97                                 new String[] { null, null, null },
98                                 new Util()
99                 };
100                 
101                 int code = 0;
102                 
103                 code = Util.objHashCode(null);
104                 assertEquals(1, code);
105                 
106                 for (Object datum : data) {
107                         code = Util.objHashCode(datum);
108                         assertEquals(datum.hashCode(), code);
109                 }
110         }
111         
112         @Test
113         public void testStringify() 
114         {
115                 final String msg = "Foo bar baz bat bam";
116                 SQLException exc = new SQLException(msg);
117                 String actual = Util.stringify(exc);
118                 
119                 assertNotNull(actual);
120                 assertTrue(actual.contains(msg));
121                 assertTrue(actual.contains("SQLException"));
122                 assertTrue(actual.contains("testStringify"));
123         }
124 }