1 |
| package org.jboss.cache.optimistic; |
2 |
| |
3 |
| import junit.framework.TestCase; |
4 |
| import org.jboss.cache.Fqn; |
5 |
| import org.jboss.cache.FqnComparator; |
6 |
| |
7 |
| import java.util.ArrayList; |
8 |
| import java.util.List; |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| public class ComparatorTest extends TestCase |
16 |
| { |
17 |
| FqnComparator comp = new FqnComparator(); |
18 |
| |
19 |
1
| public void testSingleCompare()
|
20 |
| { |
21 |
1
| Fqn fqn1 = Fqn.fromString("one");
|
22 |
1
| Fqn fqn2 = Fqn.fromString("two");
|
23 |
| |
24 |
1
| assertTrue(comp.compare(fqn1, fqn2) < 0);
|
25 |
1
| assertTrue(comp.compare(fqn2, fqn1) > 0);
|
26 |
1
| assertTrue(comp.compare(fqn1, fqn1) == 0);
|
27 |
1
| assertTrue(comp.compare(fqn2, fqn2) == 0);
|
28 |
| } |
29 |
| |
30 |
1
| public void testNullCompare()
|
31 |
| { |
32 |
1
| Fqn fqn1 = new Fqn(new ArrayList());
|
33 |
1
| Fqn fqn2 = new Fqn(new ArrayList());
|
34 |
| |
35 |
1
| assertTrue(comp.compare(fqn1, fqn2) == 0);
|
36 |
1
| assertTrue(comp.compare(fqn2, fqn1) == 0);
|
37 |
1
| assertTrue(comp.compare(fqn1, fqn1) == 0);
|
38 |
1
| assertTrue(comp.compare(fqn2, fqn2) == 0);
|
39 |
| } |
40 |
| |
41 |
1
| public void testOneNullCompare()
|
42 |
| { |
43 |
1
| Fqn fqn1 = new Fqn(new ArrayList());
|
44 |
1
| List temp = new ArrayList();
|
45 |
1
| temp.add("one");
|
46 |
1
| Fqn fqn2 = new Fqn(temp);
|
47 |
| |
48 |
1
| assertTrue(comp.compare(fqn1, fqn2) < 0);
|
49 |
1
| assertTrue(comp.compare(fqn2, fqn1) > 0);
|
50 |
| } |
51 |
| |
52 |
1
| public void testNotComparableCompare()
|
53 |
| { |
54 |
1
| Fqn fqn1 = new Fqn(new ArrayList());
|
55 |
| |
56 |
1
| List temp = new ArrayList();
|
57 |
1
| temp.add("one");
|
58 |
1
| Fqn fqn2 = new Fqn(temp);
|
59 |
| |
60 |
1
| assertTrue(comp.compare(fqn1, fqn2) < 0);
|
61 |
1
| assertTrue(comp.compare(fqn2, fqn1) > 0);
|
62 |
| } |
63 |
| |
64 |
1
| public void testMultiChildCompare()
|
65 |
| { |
66 |
| |
67 |
1
| Fqn fqn1 = Fqn.fromString("/one/two");
|
68 |
| |
69 |
1
| Fqn fqn2 = Fqn.fromString("/one/two/three");
|
70 |
| |
71 |
1
| assertTrue(comp.compare(fqn1, fqn2) < 0);
|
72 |
1
| assertTrue(comp.compare(fqn2, fqn1) > 0);
|
73 |
| |
74 |
1
| assertTrue(comp.compare(fqn2, fqn2) == 0);
|
75 |
| |
76 |
1
| assertTrue(comp.compare(fqn1, fqn1) == 0);
|
77 |
| } |
78 |
| |
79 |
1
| public void testMultiNotChildCompare()
|
80 |
| { |
81 |
| |
82 |
1
| Fqn fqn1 = Fqn.fromString("/one/two");
|
83 |
| |
84 |
1
| Fqn fqn2 = Fqn.fromString("/three/four");
|
85 |
| |
86 |
1
| assertTrue(comp.compare(fqn1, fqn2) < 0);
|
87 |
1
| assertTrue(comp.compare(fqn2, fqn1) > 0);
|
88 |
| |
89 |
1
| assertTrue(comp.compare(fqn2, fqn2) == 0);
|
90 |
| |
91 |
1
| assertTrue(comp.compare(fqn1, fqn1) == 0);
|
92 |
| } |
93 |
| |
94 |
1
| public void testPartialMultiNotChildCompare()
|
95 |
| { |
96 |
| |
97 |
1
| Fqn fqn1 = Fqn.fromString("/one/two");
|
98 |
| |
99 |
1
| Fqn fqn2 = Fqn.fromString("/three");
|
100 |
| |
101 |
1
| assertTrue(comp.compare(fqn1, fqn2) < 0);
|
102 |
1
| assertTrue(comp.compare(fqn2, fqn1) > 0);
|
103 |
| |
104 |
1
| assertTrue(comp.compare(fqn2, fqn2) == 0);
|
105 |
| |
106 |
1
| assertTrue(comp.compare(fqn1, fqn1) == 0);
|
107 |
| } |
108 |
| |
109 |
1
| public void testEqualsMultidCompare()
|
110 |
| { |
111 |
| |
112 |
1
| Fqn fqn1 = Fqn.fromString("/one/two");
|
113 |
| |
114 |
1
| Fqn fqn2 = Fqn.fromString("/one/two");
|
115 |
| |
116 |
1
| assertTrue(comp.compare(fqn1, fqn2) == 0);
|
117 |
1
| assertTrue(comp.compare(fqn2, fqn1) == 0);
|
118 |
| |
119 |
1
| assertTrue(comp.compare(fqn2, fqn2) == 0);
|
120 |
| |
121 |
1
| assertTrue(comp.compare(fqn1, fqn1) == 0);
|
122 |
| } |
123 |
| |
124 |
1
| public void testStringIntMultidCompare()
|
125 |
| { |
126 |
1
| Fqn fqn1 = Fqn.fromString("/one/two");
|
127 |
| |
128 |
1
| List temp = new ArrayList();
|
129 |
1
| temp.add(1234);
|
130 |
1
| Fqn fqn2 = new Fqn(temp);
|
131 |
| |
132 |
1
| assertTrue(comp.compare(fqn1, fqn2) > 0);
|
133 |
1
| assertTrue(comp.compare(fqn2, fqn1) < 0);
|
134 |
| |
135 |
1
| assertTrue(comp.compare(fqn2, fqn2) == 0);
|
136 |
| |
137 |
1
| assertTrue(comp.compare(fqn1, fqn1) == 0);
|
138 |
| } |
139 |
| |
140 |
1
| public void testOrdinaryObjectCompare()
|
141 |
| { |
142 |
1
| Fqn fqn1 = new Fqn(new Object[]{new XYZ(), new ABC()});
|
143 |
1
| Fqn fqn2 = new Fqn(new Object[]{"XYZ", "ABC"});
|
144 |
1
| Fqn fqn3 = new Fqn(new Object[]{"XYZ", new ABC()});
|
145 |
| |
146 |
1
| Fqn fqn4 = new Fqn(new Object[]{"XYZ", new XYZ()});
|
147 |
| |
148 |
1
| assertEquals(0, comp.compare(fqn1, fqn2));
|
149 |
1
| assertEquals(0, comp.compare(fqn1, fqn3));
|
150 |
1
| assertEquals(0, comp.compare(fqn2, fqn3));
|
151 |
1
| assertEquals(true, comp.compare(fqn1, fqn4) < 0);
|
152 |
1
| assertEquals(true, comp.compare(fqn4, fqn1) > 0);
|
153 |
| } |
154 |
| |
155 |
| |
156 |
| |
157 |
| |
158 |
| |
159 |
| |
160 |
| |
161 |
| |
162 |
| |
163 |
| |
164 |
| |
165 |
| |
166 |
| |
167 |
| |
168 |
| |
169 |
| |
170 |
| private static class XYZ |
171 |
| { |
172 |
6
| public String toString()
|
173 |
| { |
174 |
6
| return "XYZ";
|
175 |
| } |
176 |
| } |
177 |
| |
178 |
| private static class ABC |
179 |
| { |
180 |
6
| public String toString()
|
181 |
| { |
182 |
6
| return "ABC";
|
183 |
| } |
184 |
| } |
185 |
| |
186 |
| } |