001: /*
002: * Copyright 1999-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;
017:
018: import java.util.ArrayList;
019: import java.util.Collections;
020: import java.util.Comparator;
021: import java.util.List;
022:
023: public abstract class TestComparator extends TestObject {
024:
025: public abstract Comparator makeComparator();
026:
027: public abstract List getComparableObjectsOrdered();
028:
029: public Object makeObject() {
030: return makeComparator();
031: }
032:
033: /**
034: * There were no Comparators in version 1.x.
035: *
036: * @return 2
037: */
038: public int getCompatibilityVersion() {
039: return 2;
040: }
041:
042: public void reverseObjects(List list) {
043: Collections.reverse(list);
044: }
045:
046: /**
047: * Sort object according to the given Comparator.
048: *
049: * @param list List to sort
050: * @param comparator sorting comparator
051: */
052: public void sortObjects(List list, Comparator comparator) {
053:
054: Collections.sort(list, comparator);
055:
056: }
057:
058: public boolean supportsEmptyCollections() {
059: return false;
060: }
061:
062: public boolean supportsFullCollections() {
063: return false;
064: }
065:
066: public void testEmptyListSort() {
067: List list = new ArrayList();
068: sortObjects(list, makeComparator());
069:
070: List list2 = new ArrayList();
071:
072: assertTrue("Comparator cannot sort empty lists", list2
073: .equals(list));
074: }
075:
076: public void testReverseListSort() {
077: Comparator comparator = makeComparator();
078:
079: List randomList = getComparableObjectsOrdered();
080: reverseObjects(randomList);
081: sortObjects(randomList, comparator);
082:
083: List orderedList = getComparableObjectsOrdered();
084:
085: assertTrue("Comparator did not reorder the List correctly",
086: orderedList.equals(randomList));
087:
088: }
089:
090: public String getCanonicalComparatorName(Object object) {
091: StringBuffer retval = new StringBuffer();
092: retval.append("data/test/");
093: String colName = object.getClass().getName();
094: colName = colName.substring(colName.lastIndexOf(".") + 1,
095: colName.length());
096: retval.append(colName);
097: retval.append(".version");
098: retval.append(getCompatibilityVersion());
099: retval.append(".obj");
100: return retval.toString();
101: }
102:
103: /**
104: * Compare the current serialized form of the Comparator
105: * against the canonical version in CVS.
106: */
107: public void testComparatorCompatibility() {
108: Comparator comparator = null;
109:
110: // make sure the canonical form produces the ordering we currently
111: // expect
112: List randomList = getComparableObjectsOrdered();
113: reverseObjects(randomList);
114: sortObjects(randomList, comparator);
115:
116: List orderedList = getComparableObjectsOrdered();
117:
118: assertTrue("Comparator did not reorder the List correctly:"
119: + randomList, orderedList.equals(randomList));
120: }
121:
122: }
|