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