KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > optimistic > ComparatorTest


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 JavaDoc;
8 import java.util.List JavaDoc;
9
10 /**
11  * Tests {@link FqnComparator}.
12  *
13  * @author xenephon
14  */

15 public class ComparatorTest extends TestCase
16 {
17    FqnComparator comp = new FqnComparator();
18
19    public void testSingleCompare()
20    {
21       Fqn fqn1 = Fqn.fromString("one");
22       Fqn fqn2 = Fqn.fromString("two");
23
24       assertTrue(comp.compare(fqn1, fqn2) < 0);
25       assertTrue(comp.compare(fqn2, fqn1) > 0);
26       assertTrue(comp.compare(fqn1, fqn1) == 0);
27       assertTrue(comp.compare(fqn2, fqn2) == 0);
28    }
29
30    public void testNullCompare()
31    {
32       Fqn fqn1 = new Fqn(new ArrayList JavaDoc());
33       Fqn fqn2 = new Fqn(new ArrayList JavaDoc());
34
35       assertTrue(comp.compare(fqn1, fqn2) == 0);
36       assertTrue(comp.compare(fqn2, fqn1) == 0);
37       assertTrue(comp.compare(fqn1, fqn1) == 0);
38       assertTrue(comp.compare(fqn2, fqn2) == 0);
39    }
40
41    public void testOneNullCompare()
42    {
43       Fqn fqn1 = new Fqn(new ArrayList JavaDoc());
44       List JavaDoc temp = new ArrayList JavaDoc();
45       temp.add("one");
46       Fqn fqn2 = new Fqn(temp);
47
48       assertTrue(comp.compare(fqn1, fqn2) < 0);
49       assertTrue(comp.compare(fqn2, fqn1) > 0);
50    }
51
52    public void testNotComparableCompare()
53    {
54       Fqn fqn1 = new Fqn(new ArrayList JavaDoc());
55
56       List JavaDoc temp = new ArrayList JavaDoc();
57       temp.add("one");
58       Fqn fqn2 = new Fqn(temp);
59
60       assertTrue(comp.compare(fqn1, fqn2) < 0);
61       assertTrue(comp.compare(fqn2, fqn1) > 0);
62    }
63
64    public void testMultiChildCompare()
65    {
66
67       Fqn fqn1 = Fqn.fromString("/one/two");
68
69       Fqn fqn2 = Fqn.fromString("/one/two/three");
70
71       assertTrue(comp.compare(fqn1, fqn2) < 0);
72       assertTrue(comp.compare(fqn2, fqn1) > 0);
73
74       assertTrue(comp.compare(fqn2, fqn2) == 0);
75
76       assertTrue(comp.compare(fqn1, fqn1) == 0);
77    }
78
79    public void testMultiNotChildCompare()
80    {
81
82       Fqn fqn1 = Fqn.fromString("/one/two");
83
84       Fqn fqn2 = Fqn.fromString("/three/four");
85
86       assertTrue(comp.compare(fqn1, fqn2) < 0);
87       assertTrue(comp.compare(fqn2, fqn1) > 0);
88
89       assertTrue(comp.compare(fqn2, fqn2) == 0);
90
91       assertTrue(comp.compare(fqn1, fqn1) == 0);
92    }
93
94    public void testPartialMultiNotChildCompare()
95    {
96
97       Fqn fqn1 = Fqn.fromString("/one/two");
98
99       Fqn fqn2 = Fqn.fromString("/three");
100
101       assertTrue(comp.compare(fqn1, fqn2) < 0);
102       assertTrue(comp.compare(fqn2, fqn1) > 0);
103
104       assertTrue(comp.compare(fqn2, fqn2) == 0);
105
106       assertTrue(comp.compare(fqn1, fqn1) == 0);
107    }
108
109    public void testEqualsMultidCompare()
110    {
111
112       Fqn fqn1 = Fqn.fromString("/one/two");
113
114       Fqn fqn2 = Fqn.fromString("/one/two");
115
116       assertTrue(comp.compare(fqn1, fqn2) == 0);
117       assertTrue(comp.compare(fqn2, fqn1) == 0);
118
119       assertTrue(comp.compare(fqn2, fqn2) == 0);
120
121       assertTrue(comp.compare(fqn1, fqn1) == 0);
122    }
123
124    public void testStringIntMultidCompare()
125    {
126       Fqn fqn1 = Fqn.fromString("/one/two");
127
128       List JavaDoc temp = new ArrayList JavaDoc();
129       temp.add(1234);
130       Fqn fqn2 = new Fqn(temp);
131
132       assertTrue(comp.compare(fqn1, fqn2) > 0);
133       assertTrue(comp.compare(fqn2, fqn1) < 0);
134
135       assertTrue(comp.compare(fqn2, fqn2) == 0);
136
137       assertTrue(comp.compare(fqn1, fqn1) == 0);
138    }
139
140    public void testOrdinaryObjectCompare()
141    {
142       Fqn fqn1 = new Fqn(new Object JavaDoc[]{new XYZ(), new ABC()});
143       Fqn fqn2 = new Fqn(new Object JavaDoc[]{"XYZ", "ABC"});
144       Fqn fqn3 = new Fqn(new Object JavaDoc[]{"XYZ", new ABC()});
145
146       Fqn fqn4 = new Fqn(new Object JavaDoc[]{"XYZ", new XYZ()});
147
148       assertEquals(0, comp.compare(fqn1, fqn2));
149       assertEquals(0, comp.compare(fqn1, fqn3));
150       assertEquals(0, comp.compare(fqn2, fqn3));
151       assertEquals(true, comp.compare(fqn1, fqn4) < 0);
152       assertEquals(true, comp.compare(fqn4, fqn1) > 0);
153    }
154
155    /*
156    public void testNullComparator()
157    {
158        Fqn fqn1 = new Fqn(new Object[] {"a", null, "c"});
159        Fqn fqn2 = new Fqn(new Object[] {"a", "d", "c"});
160        Fqn fqn3 = new Fqn(new Object[] {"a", null});
161
162        assertEquals(0, comp.compare(fqn1, fqn1));
163        assertEquals(true, comp.compare(fqn1, fqn2) > 0);
164        assertEquals(true, comp.compare(fqn2, fqn1) < 0);
165        assertEquals(true, comp.compare(fqn1, fqn3) < 0);
166        assertEquals(true, comp.compare(fqn3, fqn1) > 0);
167    }
168    */

169
170    private static class XYZ
171    {
172       public String JavaDoc toString()
173       {
174          return "XYZ";
175       }
176    }
177
178    private static class ABC
179    {
180       public String JavaDoc toString()
181       {
182          return "ABC";
183       }
184    }
185
186 }
187
Popular Tags