added backward compatibility tests for Serialization

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@130626 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Morgan James Delagrange 2002-03-01 19:42:42 +00:00
parent 61445ae559
commit f8d081df0d
1 changed files with 52 additions and 0 deletions

View File

@ -1,5 +1,6 @@
package org.apache.commons.collections.comparators; package org.apache.commons.collections.comparators;
import java.io.IOException;
import java.io.Serializable; import java.io.Serializable;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
@ -22,6 +23,15 @@ public abstract class TestComparator extends TestObject {
return makeComparator(); return makeComparator();
} }
/**
* There were no Comparators in version 1.x.
*
* @return 2
*/
public int getCompatibilityVersion() {
return 2;
}
public void reverseObjects(List list) { public void reverseObjects(List list) {
Collections.reverse(list); Collections.reverse(list);
} }
@ -79,4 +89,46 @@ public abstract class TestComparator extends TestObject {
comparator instanceof Serializable); comparator instanceof Serializable);
} }
public String getCanonicalComparatorName(Object object) {
StringBuffer retval = new StringBuffer();
retval.append("data/test/");
String colName = object.getClass().getName();
colName = colName.substring(colName.lastIndexOf(".")+1,colName.length());
retval.append(colName);
retval.append(".version");
retval.append(getCompatibilityVersion());
retval.append(".obj");
return retval.toString();
}
/**
* Compare the current serialized form of the Comparator
* against the canonical version in CVS.
*/
public void testComparatorCompatibility() throws IOException, ClassNotFoundException {
Comparator comparator = null;
/*
* Create canonical objects with this code
comparator = makeComparator();
writeExternalFormToDisk((Serializable) comparator,
getCanonicalComparatorName(comparator));
*/
// test to make sure the canonical form has been preserved
comparator =
(Comparator) readExternalFormFromDisk(getCanonicalComparatorName(makeComparator()));
// make sure the canonical form produces the ordering we currently
// expect
List randomList = getComparableObjectsOrdered();
reverseObjects(randomList);
sortObjects(randomList,comparator);
List orderedList = getComparableObjectsOrdered();
assertTrue("Comparator did not reorder the List correctly",
orderedList.equals(randomList));
}
} }