KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quilt > reg > TestArrComparator


1 /* TestArrComparator.java */
2
3 package org.quilt.reg;
4
5 import java.util.*;
6 import junit.framework.*;
7
8 public class TestArrComparator extends TestCase {
9
10     private Registry reg = new Registry();
11     private Comparator cmp = reg.comparator();
12    
13     int compare (String JavaDoc[] s1, String JavaDoc [] s2) {
14         return cmp.compare(s1, s2);
15     }
16     int objCompare (Object JavaDoc o1, Object JavaDoc o2) {
17         return cmp.compare(o1, o2);
18     }
19     final String JavaDoc key0[] = {"Quilt"};
20     final String JavaDoc key1[] = {"Quilt", "ARG0"};
21     final String JavaDoc key2[] = {"Elvis", "lives"};
22     final String JavaDoc key20[] = {"Elvis", "lives", "forever"};
23     final String JavaDoc key22[] = {"Elvis", "lives", "forever", "in", "Graceland"};
24     final String JavaDoc key3[] = {"One plus ", "one"};
25     final String JavaDoc key4[] = {"Elvis", "wombat"};
26     final String JavaDoc key5[] = {"Elvis", "heart-throb"};
27     final String JavaDoc key6[] = {"rg2", "msg"};
28     final String JavaDoc key7[] = {"rg2", "test"};
29     final String JavaDoc key8[] = {"rg2", "test", "a"};
30     final String JavaDoc key9[] = {"rg2", "test", "b"};
31
32     public void testSelfCompare () {
33         // self-comparisons should always return 0
34
assertEquals ("compare to self does not return 0",
35                                                 0, compare(key0, key0) );
36         assertEquals ("compare to self does not return 0",
37                                                 0, compare(key1, key1) );
38         assertEquals ("compare to self does not return 0",
39                                                 0, compare(key20, key20));
40         assertEquals ("compare to self does not return 0",
41                                                 0, compare(key22, key22));
42     }
43     public void testDiffLen () {
44         // shorter to otherwise identical but longer string returns -1
45
assertEquals ("compare to longer but otherwise identical",
46                                                 -1, compare(key0, key1) );
47         assertEquals ("compare to longer but otherwise identical",
48                                                 -1, compare(key2, key20) );
49         assertEquals ("compare to longer but otherwise identical",
50                                                 -1, compare(key20, key22));
51             
52         // longer to otherwise identical but shorter string returns +1
53
assertEquals ("compare to longer but otherwise identical",
54                                                 1, compare(key1, key0) );
55         assertEquals ("compare to longer but otherwise identical",
56                                                 1, compare(key20, key2) );
57         assertEquals ("compare to longer but otherwise identical",
58                                                 1, compare(key22, key20) );
59     }
60     public void testMiscCompare () {
61         // seem to fail in the field
62
assertEquals ("rg2/msg vs rg2/test", -1, compare(key6, key7));
63         assertEquals ("rg2/msg vs rg2/test", 1, compare(key7, key6));
64
65         // two-string arrays
66
assertEquals ("Elvis/heart-throb vs lives", -1, compare(key5, key2));
67         assertEquals ("Elvis/lives vs wombat", -1, compare(key2, key4));
68         assertEquals ("Elvis/lives vs heart-throb", 1, compare(key2, key5));
69         assertEquals ("Elvis/wombat vs lives", 1, compare(key4, key2));
70
71         // three-string arrays
72
assertEquals ("rg2/test/a vs b", -1, compare(key8, key9));
73         assertEquals ("rg2/test/b vs a", 1, compare(key9, key8));
74     }
75     public void testExceptions () {
76         try {
77             objCompare ("this is a string", new Boolean JavaDoc(true) );
78             fail (
79             "args not String[], Comparator did not throw ClassCastException");
80         } catch (ClassCastException JavaDoc e) {
81             // success
82
}
83     }
84 }
85         
86
Popular Tags