added unit tests for Comparators

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@130619 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Morgan James Delagrange 2002-03-01 18:36:21 +00:00
parent 468110ea08
commit 1311750c18
4 changed files with 156 additions and 8 deletions

View File

@ -1,7 +1,7 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestAll.java,v 1.20 2002/02/26 17:31:51 morgand Exp $
* $Revision: 1.20 $
* $Date: 2002/02/26 17:31:51 $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestAll.java,v 1.21 2002/03/01 18:36:21 morgand Exp $
* $Revision: 1.21 $
* $Date: 2002/03/01 18:36:21 $
*
* ====================================================================
*
@ -61,12 +61,13 @@
package org.apache.commons.collections;
import org.apache.commons.collections.comparators.*;
import junit.framework.*;
/**
* Entry point for all Collections tests.
* @author Rodney Waldhoff
* @version $Id: TestAll.java,v 1.20 2002/02/26 17:31:51 morgand Exp $
* @version $Id: TestAll.java,v 1.21 2002/03/01 18:36:21 morgand Exp $
*/
public class TestAll extends TestCase {
public TestAll(String testName) {
@ -80,6 +81,7 @@ public class TestAll extends TestCase {
suite.addTest(TestArrayStack.suite());
suite.addTest(TestBeanMap.suite());
suite.addTest(TestCollectionUtils.suite());
suite.addTest(TestComparableComparator.suite());
suite.addTest(TestCursorableLinkedList.suite());
suite.addTest(TestDoubleOrderedMap.suite());
suite.addTest(TestExtendedProperties.suite());

View File

@ -1,7 +1,7 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/Attic/TestObject.java,v 1.9 2002/02/26 20:52:17 morgand Exp $
* $Revision: 1.9 $
* $Date: 2002/02/26 20:52:17 $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/Attic/TestObject.java,v 1.10 2002/03/01 18:36:21 morgand Exp $
* $Revision: 1.10 $
* $Date: 2002/03/01 18:36:21 $
*
* ====================================================================
*
@ -91,7 +91,7 @@ import java.util.NoSuchElementException;
* test case (method) your {@link Object} fails.
*
* @author Rodney Waldhoff
* @version $Id: TestObject.java,v 1.9 2002/02/26 20:52:17 morgand Exp $
* @version $Id: TestObject.java,v 1.10 2002/03/01 18:36:21 morgand Exp $
*/
public abstract class TestObject extends TestCase {
public TestObject(String testName) {
@ -261,6 +261,27 @@ public abstract class TestObject extends TestCase {
return retval.toString();
}
/**
* Override this method if a subclass is testing a
* Collections that cannot serialize an "empty" Collection
* (e.g. Comparators have no contents)
*
* @return true
*/
public boolean supportsEmptyCollections() {
return true;
}
/**
* Override this method if a subclass is testing a
* Collections that cannot serialize a "full" Collection
* (e.g. Comparators have no contents)
*
* @return true
*/
public boolean supportsFullCollections() {
return true;
}
/**
* If the test object is serializable, confirm that
@ -268,6 +289,10 @@ public abstract class TestObject extends TestCase {
*
*/
public void testCanonicalEmptyCollectionExists() {
if (!supportsEmptyCollections()) {
return;
}
Object object = makeObject();
if (!(object instanceof Serializable)) {
return;
@ -283,6 +308,10 @@ public abstract class TestObject extends TestCase {
*
*/
public void testCanonicalFullCollectionExists() {
if (!supportsFullCollections()) {
return;
}
Object object = makeObject();
if (!(object instanceof Serializable)) {
return;

View File

@ -0,0 +1,35 @@
package org.apache.commons.collections.comparators;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
public class TestComparableComparator extends TestComparator {
public TestComparableComparator(String testName) {
super(testName);
}
public static Test suite() {
return new TestSuite(TestComparableComparator.class);
}
public Comparator makeComparator() {
return new ComparableComparator();
}
public List getComparableObjectsOrdered() {
List list = new LinkedList();
list.add(new Integer(1));
list.add(new Integer(2));
list.add(new Integer(3));
list.add(new Integer(4));
list.add(new Integer(5));
return list;
}
}

View File

@ -0,0 +1,82 @@
package org.apache.commons.collections.comparators;
import java.io.Serializable;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.TestObject;
public abstract class TestComparator extends TestObject {
public TestComparator(String testName) {
super(testName);
}
public abstract Comparator makeComparator();
public abstract List getComparableObjectsOrdered();
public Object makeObject() {
return makeComparator();
}
public void reverseObjects(List list) {
Collections.reverse(list);
}
/**
* Sort object according to the given Comparator.
*
* @param list List to sort
* @param comparator sorting comparator
*/
public void sortObjects(List list, Comparator comparator) {
Collections.sort(list,comparator);
}
public boolean supportsEmptyCollections() {
return false;
}
public boolean supportsFullCollections() {
return false;
}
public void testEmptyListSort() {
List list = new LinkedList();
sortObjects(list,makeComparator());
List list2 = new LinkedList();
assertTrue("Comparator cannot sort empty lists",
list2.equals(list));
}
public void testRandomListSort() {
Comparator comparator = makeComparator();
List randomList = getComparableObjectsOrdered();
reverseObjects(randomList);
sortObjects(randomList,comparator);
List orderedList = getComparableObjectsOrdered();
assertTrue("Comparator did not reorder the List correctly",
orderedList.equals(randomList));
}
/**
* Nearly all Comparators should be Serializable.
*/
public void testComparatorIsSerializable() {
Comparator comparator = makeComparator();
assertTrue("This comparator should be Serializable.",
comparator instanceof Serializable);
}
}