KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > comparators > TestBooleanComparator


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.collections.comparators;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Comparator JavaDoc;
20 import java.util.List JavaDoc;
21
22 import junit.framework.Test;
23 import junit.framework.TestSuite;
24
25 /**
26  * Tests for {@link BooleanComparator}.
27  *
28  * @version $Revision: 1.9 $ $Date: 2004/02/18 01:20:34 $
29  *
30  * @author Rodney Waldhoff
31  */

32 public class TestBooleanComparator extends AbstractTestComparator {
33
34     // conventional
35
// ------------------------------------------------------------------------
36

37     public TestBooleanComparator(String JavaDoc testName) {
38         super(testName);
39     }
40
41     public static Test suite() {
42         return new TestSuite(TestBooleanComparator.class);
43     }
44
45     // collections testing framework
46
// ------------------------------------------------------------------------
47

48     public Comparator JavaDoc makeComparator() {
49         return new BooleanComparator();
50     }
51
52     public List JavaDoc getComparableObjectsOrdered() {
53         List JavaDoc list = new ArrayList JavaDoc();
54         list.add(new Boolean JavaDoc(false));
55         list.add(Boolean.FALSE);
56         list.add(new Boolean JavaDoc(false));
57         list.add(Boolean.TRUE);
58         list.add(new Boolean JavaDoc(true));
59         list.add(Boolean.TRUE);
60         return list;
61     }
62     
63     public String JavaDoc getCompatibilityVersion() {
64         return "3";
65     }
66
67     // tests
68
// ------------------------------------------------------------------------
69

70     public void testConstructors() {
71         allTests(false,new BooleanComparator());
72         allTests(false,new BooleanComparator(false));
73         allTests(true,new BooleanComparator(true));
74     }
75     
76     public void testStaticFactoryMethods() {
77         allTests(false,BooleanComparator.getFalseFirstComparator());
78         allTests(false,BooleanComparator.getBooleanComparator(false));
79         allTests(true,BooleanComparator.getTrueFirstComparator());
80         allTests(true,BooleanComparator.getBooleanComparator(true));
81     }
82     
83     public void testEqualsCompatibleInstance() {
84         assertEquals(new BooleanComparator(),new BooleanComparator(false));
85         assertEquals(new BooleanComparator(false),new BooleanComparator(false));
86         assertEquals(new BooleanComparator(false),BooleanComparator.getFalseFirstComparator());
87         assertSame(BooleanComparator.getFalseFirstComparator(),BooleanComparator.getBooleanComparator(false));
88
89         assertEquals(new BooleanComparator(true),new BooleanComparator(true));
90         assertEquals(new BooleanComparator(true),BooleanComparator.getTrueFirstComparator());
91         assertSame(BooleanComparator.getTrueFirstComparator(),BooleanComparator.getBooleanComparator(true));
92
93         assertTrue(!(new BooleanComparator().equals(new BooleanComparator(true))));
94         assertTrue(!(new BooleanComparator(true).equals(new BooleanComparator(false))));
95     }
96     
97     // utilities
98
// ------------------------------------------------------------------------
99

100     protected void allTests(boolean trueFirst, BooleanComparator comp) {
101         orderIndependentTests(comp);
102         if(trueFirst) {
103             trueFirstTests(comp);
104         } else {
105             falseFirstTests(comp);
106         }
107     }
108
109     protected void trueFirstTests(BooleanComparator comp) {
110         assertNotNull(comp);
111         assertEquals(0,comp.compare(Boolean.TRUE,Boolean.TRUE));
112         assertEquals(0,comp.compare(Boolean.FALSE,Boolean.FALSE));
113         assertTrue(comp.compare(Boolean.FALSE,Boolean.TRUE) > 0);
114         assertTrue(comp.compare(Boolean.TRUE,Boolean.FALSE) < 0);
115
116         assertEquals(0,comp.compare((Object JavaDoc)(Boolean.TRUE),(Object JavaDoc)(Boolean.TRUE)));
117         assertEquals(0,comp.compare((Object JavaDoc)(Boolean.FALSE),(Object JavaDoc)(Boolean.FALSE)));
118         assertTrue(comp.compare((Object JavaDoc)(Boolean.FALSE),(Object JavaDoc)(Boolean.TRUE)) > 0);
119         assertTrue(comp.compare((Object JavaDoc)(Boolean.TRUE),(Object JavaDoc)(Boolean.FALSE)) < 0);
120     }
121
122     protected void falseFirstTests(BooleanComparator comp) {
123         assertNotNull(comp);
124         assertEquals(0,comp.compare(Boolean.TRUE,Boolean.TRUE));
125         assertEquals(0,comp.compare(Boolean.FALSE,Boolean.FALSE));
126         assertTrue(comp.compare(Boolean.FALSE,Boolean.TRUE) < 0);
127         assertTrue(comp.compare(Boolean.TRUE,Boolean.FALSE) > 0);
128
129         assertEquals(0,comp.compare((Object JavaDoc)(Boolean.TRUE),(Object JavaDoc)(Boolean.TRUE)));
130         assertEquals(0,comp.compare((Object JavaDoc)(Boolean.FALSE),(Object JavaDoc)(Boolean.FALSE)));
131         assertTrue(comp.compare((Object JavaDoc)(Boolean.FALSE),(Object JavaDoc)(Boolean.TRUE)) < 0);
132         assertTrue(comp.compare((Object JavaDoc)(Boolean.TRUE),(Object JavaDoc)(Boolean.FALSE)) > 0);
133     }
134
135     protected void orderIndependentTests(BooleanComparator comp) {
136         nullArgumentTests(comp);
137         nonBooleanArgumentTests(comp);
138         nullAndNonBooleanArgumentsTests(comp);
139     }
140     
141     protected void nullArgumentTests(BooleanComparator comp) {
142         assertNotNull(comp);
143         try {
144             comp.compare(null,null);
145             fail("Expected NullPointerException");
146         } catch(NullPointerException JavaDoc e) {
147             // expected
148
}
149         try {
150             comp.compare(Boolean.TRUE,null);
151             fail("Expected NullPointerException");
152         } catch(NullPointerException JavaDoc e) {
153             // expected
154
}
155         try {
156             comp.compare(Boolean.FALSE,null);
157             fail("Expected NullPointerException");
158         } catch(NullPointerException JavaDoc e) {
159             // expected
160
}
161         try {
162             comp.compare(null,Boolean.TRUE);
163             fail("Expected NullPointerException");
164         } catch(NullPointerException JavaDoc e) {
165             // expected
166
}
167         try {
168             comp.compare(null,Boolean.FALSE);
169             fail("Expected NullPointerException");
170         } catch(NullPointerException JavaDoc e) {
171             // expected
172
}
173     }
174     
175     protected void nonBooleanArgumentTests(BooleanComparator comp) {
176         assertNotNull(comp);
177         try {
178             comp.compare("string","string");
179             fail("Expected ClassCastException");
180         } catch(ClassCastException JavaDoc e) {
181             // expected
182
}
183         try {
184             comp.compare(Boolean.TRUE,"string");
185             fail("Expected ClassCastException");
186         } catch(ClassCastException JavaDoc e) {
187             // expected
188
}
189         try {
190             comp.compare("string",Boolean.TRUE);
191             fail("Expected ClassCastException");
192         } catch(ClassCastException JavaDoc e) {
193             // expected
194
}
195         try {
196             comp.compare("string",new Integer JavaDoc(3));
197             fail("Expected ClassCastException");
198         } catch(ClassCastException JavaDoc e) {
199             // expected
200
}
201         try {
202             comp.compare(new Integer JavaDoc(3),"string");
203             fail("Expected ClassCastException");
204         } catch(ClassCastException JavaDoc e) {
205             // expected
206
}
207     }
208     
209     protected void nullAndNonBooleanArgumentsTests(BooleanComparator comp) {
210         assertNotNull(comp);
211         try {
212             comp.compare(null,"string");
213             fail("Expected ClassCast or NullPointer Exception");
214         } catch(ClassCastException JavaDoc e) {
215             // expected
216
} catch(NullPointerException JavaDoc e) {
217             // expected
218
}
219         try {
220             comp.compare("string",null);
221             fail("Expected ClassCast or NullPointer Exception");
222         } catch(ClassCastException JavaDoc e) {
223             // expected
224
} catch(NullPointerException JavaDoc e) {
225             // expected
226
}
227     }
228
229 }
230
Popular Tags