KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.FileNotFoundException JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.Serializable JavaDoc;
21 import java.util.Collections JavaDoc;
22 import java.util.Comparator JavaDoc;
23 import java.util.LinkedList JavaDoc;
24 import java.util.List JavaDoc;
25
26 import org.apache.commons.collections.AbstractTestObject;
27
28 /**
29  * Abstract test class for testing the Comparator interface.
30  * <p>
31  * Concrete subclasses declare the comparator to be tested.
32  * They also declare certain aspects of the tests.
33  *
34  * @author Stephen Colebourne
35  */

36 public abstract class AbstractTestComparator extends AbstractTestObject {
37
38     /**
39      * JUnit constructor.
40      *
41      * @param testName the test class name
42      */

43     public AbstractTestComparator(String JavaDoc testName) {
44         super(testName);
45     }
46
47     //-----------------------------------------------------------------------
48
/**
49      * Implement this method to return the comparator to test.
50      *
51      * @return the comparator to test
52      */

53     public abstract Comparator JavaDoc makeComparator();
54     
55     /**
56      * Implement this method to return a list of sorted objects.
57      *
58      * @return sorted objects
59      */

60     public abstract List JavaDoc getComparableObjectsOrdered();
61
62     //-----------------------------------------------------------------------
63
/**
64      * Implements the abstract superclass method to return the comparator.
65      *
66      * @return a full iterator
67      */

68     public Object JavaDoc makeObject() {
69         return makeComparator();
70     }
71
72     /**
73      * Overrides superclass to block tests.
74      */

75     public boolean supportsEmptyCollections() {
76         return false;
77     }
78
79     /**
80      * Overrides superclass to block tests.
81      */

82     public boolean supportsFullCollections() {
83         return false;
84     }
85
86     /**
87      * Overrides superclass to set the compatability to version 2
88      * as there were no Comparators in version 1.x.
89      */

90     public String JavaDoc getCompatibilityVersion() {
91         return "2";
92     }
93
94     //-----------------------------------------------------------------------
95
/**
96      * Reverse the list.
97      */

98     protected void reverseObjects(List JavaDoc list) {
99         Collections.reverse(list);
100     }
101
102     /**
103      * Randomize the list.
104      */

105     protected void randomizeObjects(List JavaDoc list) {
106         Collections.shuffle(list);
107     }
108
109     /**
110      * Sort the list.
111      */

112     protected void sortObjects(List JavaDoc list, Comparator JavaDoc comparator) {
113         Collections.sort(list,comparator);
114
115     }
116
117     //-----------------------------------------------------------------------
118
/**
119      * Test sorting an empty list
120      */

121     public void testEmptyListSort() {
122         List JavaDoc list = new LinkedList JavaDoc();
123         sortObjects(list, makeComparator());
124
125         List JavaDoc list2 = new LinkedList JavaDoc();
126         
127         assertTrue("Comparator cannot sort empty lists",
128                    list2.equals(list));
129     }
130
131     /**
132      * Test sorting a reversed list.
133      */

134     public void testReverseListSort() {
135         Comparator JavaDoc comparator = makeComparator();
136
137         List JavaDoc randomList = getComparableObjectsOrdered();
138         reverseObjects(randomList);
139         sortObjects(randomList,comparator);
140
141         List JavaDoc orderedList = getComparableObjectsOrdered();
142
143         assertTrue("Comparator did not reorder the List correctly",
144                    orderedList.equals(randomList));
145
146     }
147
148     /**
149      * Test sorting a random list.
150      */

151     public void testRandomListSort() {
152         Comparator JavaDoc comparator = makeComparator();
153
154         List JavaDoc randomList = getComparableObjectsOrdered();
155         randomizeObjects(randomList);
156         sortObjects(randomList,comparator);
157
158         List JavaDoc orderedList = getComparableObjectsOrdered();
159
160         /* debug
161         Iterator i = randomList.iterator();
162         while (i.hasNext()) {
163             System.out.println(i.next());
164         }
165         */

166
167         assertTrue("Comparator did not reorder the List correctly",
168                    orderedList.equals(randomList));
169
170     }
171
172     /**
173      * Nearly all Comparators should be Serializable.
174      */

175     public void testComparatorIsSerializable() {
176         Comparator JavaDoc comparator = makeComparator();
177         assertTrue("This comparator should be Serializable.",
178                    comparator instanceof Serializable JavaDoc);
179     }
180
181     public String JavaDoc getCanonicalComparatorName(Object JavaDoc object) {
182         StringBuffer JavaDoc retval = new StringBuffer JavaDoc();
183         retval.append("data/test/");
184         String JavaDoc colName = object.getClass().getName();
185         colName = colName.substring(colName.lastIndexOf(".")+1,colName.length());
186         retval.append(colName);
187         retval.append(".version");
188         retval.append(getCompatibilityVersion());
189         retval.append(".obj");
190         return retval.toString();
191     }
192
193     /**
194      * Compare the current serialized form of the Comparator
195      * against the canonical version in CVS.
196      */

197     public void testComparatorCompatibility() throws IOException JavaDoc, ClassNotFoundException JavaDoc {
198         if(!skipSerializedCanonicalTests()) {
199             Comparator JavaDoc comparator = null;
200     
201             // test to make sure the canonical form has been preserved
202
try {
203                 comparator = (Comparator JavaDoc) readExternalFormFromDisk(getCanonicalComparatorName(makeComparator()));
204             } catch (FileNotFoundException JavaDoc exception) {
205     
206                 boolean autoCreateSerialized = false;
207     
208                 if(autoCreateSerialized) {
209                     comparator = makeComparator();
210                     String JavaDoc fileName = getCanonicalComparatorName(comparator);
211                     writeExternalFormToDisk((Serializable JavaDoc) comparator, fileName);
212                     fail("Serialized form could not be found. A serialized version " +
213                          "has now been written (and should be added to CVS): " + fileName);
214                 } else {
215                     fail("The Serialized form could be located to test serialization " +
216                         "compatibility: " + exception.getMessage());
217                 }
218             }
219     
220             
221             // make sure the canonical form produces the ordering we currently
222
// expect
223
List JavaDoc randomList = getComparableObjectsOrdered();
224             reverseObjects(randomList);
225             sortObjects(randomList,comparator);
226     
227             List JavaDoc orderedList = getComparableObjectsOrdered();
228     
229             assertTrue("Comparator did not reorder the List correctly",
230                        orderedList.equals(randomList));
231         }
232     }
233
234 }
235
Popular Tags