001: /*
002: * Copyright 2001-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections.comparators;
017:
018: import java.io.FileNotFoundException;
019: import java.io.IOException;
020: import java.io.Serializable;
021: import java.util.Collections;
022: import java.util.Comparator;
023: import java.util.LinkedList;
024: import java.util.List;
025:
026: import org.apache.commons.collections.AbstractTestObject;
027:
028: /**
029: * Abstract test class for testing the Comparator interface.
030: * <p>
031: * Concrete subclasses declare the comparator to be tested.
032: * They also declare certain aspects of the tests.
033: *
034: * @author Stephen Colebourne
035: */
036: public abstract class AbstractTestComparator extends AbstractTestObject {
037:
038: /**
039: * JUnit constructor.
040: *
041: * @param testName the test class name
042: */
043: public AbstractTestComparator(String testName) {
044: super (testName);
045: }
046:
047: //-----------------------------------------------------------------------
048: /**
049: * Implement this method to return the comparator to test.
050: *
051: * @return the comparator to test
052: */
053: public abstract Comparator makeComparator();
054:
055: /**
056: * Implement this method to return a list of sorted objects.
057: *
058: * @return sorted objects
059: */
060: public abstract List getComparableObjectsOrdered();
061:
062: //-----------------------------------------------------------------------
063: /**
064: * Implements the abstract superclass method to return the comparator.
065: *
066: * @return a full iterator
067: */
068: public Object makeObject() {
069: return makeComparator();
070: }
071:
072: /**
073: * Overrides superclass to block tests.
074: */
075: public boolean supportsEmptyCollections() {
076: return false;
077: }
078:
079: /**
080: * Overrides superclass to block tests.
081: */
082: public boolean supportsFullCollections() {
083: return false;
084: }
085:
086: /**
087: * Overrides superclass to set the compatability to version 2
088: * as there were no Comparators in version 1.x.
089: */
090: public String getCompatibilityVersion() {
091: return "2";
092: }
093:
094: //-----------------------------------------------------------------------
095: /**
096: * Reverse the list.
097: */
098: protected void reverseObjects(List list) {
099: Collections.reverse(list);
100: }
101:
102: /**
103: * Randomize the list.
104: */
105: protected void randomizeObjects(List list) {
106: Collections.shuffle(list);
107: }
108:
109: /**
110: * Sort the list.
111: */
112: protected void sortObjects(List list, Comparator comparator) {
113: Collections.sort(list, comparator);
114:
115: }
116:
117: //-----------------------------------------------------------------------
118: /**
119: * Test sorting an empty list
120: */
121: public void testEmptyListSort() {
122: List list = new LinkedList();
123: sortObjects(list, makeComparator());
124:
125: List list2 = new LinkedList();
126:
127: assertTrue("Comparator cannot sort empty lists", list2
128: .equals(list));
129: }
130:
131: /**
132: * Test sorting a reversed list.
133: */
134: public void testReverseListSort() {
135: Comparator comparator = makeComparator();
136:
137: List randomList = getComparableObjectsOrdered();
138: reverseObjects(randomList);
139: sortObjects(randomList, comparator);
140:
141: List 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 comparator = makeComparator();
153:
154: List randomList = getComparableObjectsOrdered();
155: randomizeObjects(randomList);
156: sortObjects(randomList, comparator);
157:
158: List 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 comparator = makeComparator();
177: assertTrue("This comparator should be Serializable.",
178: comparator instanceof Serializable);
179: }
180:
181: public String getCanonicalComparatorName(Object object) {
182: StringBuffer retval = new StringBuffer();
183: retval.append("data/test/");
184: String colName = object.getClass().getName();
185: colName = colName.substring(colName.lastIndexOf(".") + 1,
186: colName.length());
187: retval.append(colName);
188: retval.append(".version");
189: retval.append(getCompatibilityVersion());
190: retval.append(".obj");
191: return retval.toString();
192: }
193:
194: /**
195: * Compare the current serialized form of the Comparator
196: * against the canonical version in CVS.
197: */
198: public void testComparatorCompatibility() throws IOException,
199: ClassNotFoundException {
200: if (!skipSerializedCanonicalTests()) {
201: Comparator comparator = null;
202:
203: // test to make sure the canonical form has been preserved
204: try {
205: comparator = (Comparator) readExternalFormFromDisk(getCanonicalComparatorName(makeComparator()));
206: } catch (FileNotFoundException exception) {
207:
208: boolean autoCreateSerialized = false;
209:
210: if (autoCreateSerialized) {
211: comparator = makeComparator();
212: String fileName = getCanonicalComparatorName(comparator);
213: writeExternalFormToDisk((Serializable) comparator,
214: fileName);
215: fail("Serialized form could not be found. A serialized version "
216: + "has now been written (and should be added to CVS): "
217: + fileName);
218: } else {
219: fail("The Serialized form could be located to test serialization "
220: + "compatibility: "
221: + exception.getMessage());
222: }
223: }
224:
225: // make sure the canonical form produces the ordering we currently
226: // expect
227: List randomList = getComparableObjectsOrdered();
228: reverseObjects(randomList);
229: sortObjects(randomList, comparator);
230:
231: List orderedList = getComparableObjectsOrdered();
232:
233: assertTrue("Comparator did not reorder the List correctly",
234: orderedList.equals(randomList));
235: }
236: }
237:
238: }
|