1 16 package org.apache.commons.collections.comparators; 17 18 import java.io.FileNotFoundException ; 19 import java.io.IOException ; 20 import java.io.Serializable ; 21 import java.util.Collections ; 22 import java.util.Comparator ; 23 import java.util.LinkedList ; 24 import java.util.List ; 25 26 import org.apache.commons.collections.AbstractTestObject; 27 28 36 public abstract class AbstractTestComparator extends AbstractTestObject { 37 38 43 public AbstractTestComparator(String testName) { 44 super(testName); 45 } 46 47 53 public abstract Comparator makeComparator(); 54 55 60 public abstract List getComparableObjectsOrdered(); 61 62 68 public Object makeObject() { 69 return makeComparator(); 70 } 71 72 75 public boolean supportsEmptyCollections() { 76 return false; 77 } 78 79 82 public boolean supportsFullCollections() { 83 return false; 84 } 85 86 90 public String getCompatibilityVersion() { 91 return "2"; 92 } 93 94 98 protected void reverseObjects(List list) { 99 Collections.reverse(list); 100 } 101 102 105 protected void randomizeObjects(List list) { 106 Collections.shuffle(list); 107 } 108 109 112 protected void sortObjects(List list, Comparator comparator) { 113 Collections.sort(list,comparator); 114 115 } 116 117 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", 128 list2.equals(list)); 129 } 130 131 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 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 166 167 assertTrue("Comparator did not reorder the List correctly", 168 orderedList.equals(randomList)); 169 170 } 171 172 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,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 197 public void testComparatorCompatibility() throws IOException , ClassNotFoundException { 198 if(!skipSerializedCanonicalTests()) { 199 Comparator comparator = null; 200 201 try { 203 comparator = (Comparator ) readExternalFormFromDisk(getCanonicalComparatorName(makeComparator())); 204 } catch (FileNotFoundException exception) { 205 206 boolean autoCreateSerialized = false; 207 208 if(autoCreateSerialized) { 209 comparator = makeComparator(); 210 String fileName = getCanonicalComparatorName(comparator); 211 writeExternalFormToDisk((Serializable ) 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 List randomList = getComparableObjectsOrdered(); 224 reverseObjects(randomList); 225 sortObjects(randomList,comparator); 226 227 List orderedList = getComparableObjectsOrdered(); 228 229 assertTrue("Comparator did not reorder the List correctly", 230 orderedList.equals(randomList)); 231 } 232 } 233 234 } 235 | Popular Tags |