add base testSerializeDeserializeThenCompare test, modify concrete TestCases so that they pass this test (or when necessary, skip them)
this tests the Comparator.equals contract, among others git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@130922 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
a11c82f3bc
commit
a55fe3e992
|
@ -1,13 +1,13 @@
|
|||
/*
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestBeanMap.java,v 1.7 2002/08/10 02:05:20 pjack Exp $
|
||||
* $Revision: 1.7 $
|
||||
* $Date: 2002/08/10 02:05:20 $
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestBeanMap.java,v 1.8 2003/01/07 23:44:19 rwaldhoff Exp $
|
||||
* $Revision: 1.8 $
|
||||
* $Date: 2003/01/07 23:44:19 $
|
||||
*
|
||||
* ====================================================================
|
||||
*
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 1999-2001 The Apache Software Foundation. All rights
|
||||
* Copyright (c) 1999-2003 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
|
@ -60,9 +60,10 @@
|
|||
*/
|
||||
package org.apache.commons.collections;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Map;
|
||||
|
||||
import junit.framework.*;
|
||||
import junit.framework.Test;
|
||||
|
||||
/**
|
||||
* Test cases for BeanMap
|
||||
|
@ -98,7 +99,7 @@ public class TestBeanMap extends TestMap {
|
|||
*/
|
||||
|
||||
|
||||
public static class BeanWithProperties {
|
||||
public static class BeanWithProperties implements Serializable {
|
||||
private int someInt;
|
||||
private long someLong;
|
||||
private double someDouble;
|
||||
|
@ -328,7 +329,9 @@ public class TestBeanMap extends TestMap {
|
|||
"TestBeanMap.bulkTestMapValues.testCanonicalEmptyCollectionExists",
|
||||
"TestBeanMap.bulkTestMapValues.testCanonicalFullCollectionExists",
|
||||
"TestBeanMap.bulkTestMapEntrySet.testSimpleSerialization",
|
||||
"TestBeanMap.bulkTestMapKeySet.testSimpleSerialization"
|
||||
"TestBeanMap.bulkTestMapKeySet.testSimpleSerialization",
|
||||
"TestBeanMap.bulkTestMapEntrySet.testSerializeDeserializeThenCompare",
|
||||
"TestBeanMap.bulkTestMapKeySet.testSerializeDeserializeThenCompare"
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
/*
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestCursorableLinkedList.java,v 1.6 2002/06/21 03:32:06 mas Exp $
|
||||
* $Revision: 1.6 $
|
||||
* $Date: 2002/06/21 03:32:06 $
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestCursorableLinkedList.java,v 1.7 2003/01/07 23:44:19 rwaldhoff Exp $
|
||||
* $Revision: 1.7 $
|
||||
* $Date: 2003/01/07 23:44:19 $
|
||||
*
|
||||
* ====================================================================
|
||||
*
|
||||
|
@ -66,7 +66,7 @@ import java.util.*;
|
|||
|
||||
/**
|
||||
* @author Rodney Waldhoff
|
||||
* @version $Id: TestCursorableLinkedList.java,v 1.6 2002/06/21 03:32:06 mas Exp $
|
||||
* @version $Id: TestCursorableLinkedList.java,v 1.7 2003/01/07 23:44:19 rwaldhoff Exp $
|
||||
*/
|
||||
public class TestCursorableLinkedList extends TestList {
|
||||
public TestCursorableLinkedList(String testName) {
|
||||
|
@ -966,7 +966,8 @@ public class TestCursorableLinkedList extends TestList {
|
|||
".testFullListCompatibility",
|
||||
".testSimpleSerialization",
|
||||
".testCanonicalEmptyCollectionExists",
|
||||
".testCanonicalFullCollectionExists"
|
||||
".testCanonicalFullCollectionExists",
|
||||
".testSerializeDeserializeThenCompare"
|
||||
};
|
||||
for (int i = 0; i < ignored.length; i++) {
|
||||
list.add(prefix + bulk + ignored[i]);
|
||||
|
|
|
@ -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.16 2003/01/07 15:18:15 rwaldhoff Exp $
|
||||
* $Revision: 1.16 $
|
||||
* $Date: 2003/01/07 15:18:15 $
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/Attic/TestObject.java,v 1.17 2003/01/07 23:44:19 rwaldhoff Exp $
|
||||
* $Revision: 1.17 $
|
||||
* $Date: 2003/01/07 23:44:19 $
|
||||
*
|
||||
* ====================================================================
|
||||
*
|
||||
|
@ -84,7 +84,7 @@ import java.io.Serializable;
|
|||
* test case (method) your {@link Object} fails.
|
||||
*
|
||||
* @author Rodney Waldhoff
|
||||
* @version $Id: TestObject.java,v 1.16 2003/01/07 15:18:15 rwaldhoff Exp $
|
||||
* @version $Revision: 1.17 $ $Date: 2003/01/07 23:44:19 $
|
||||
*/
|
||||
public abstract class TestObject extends BulkTest {
|
||||
public TestObject(String testName) {
|
||||
|
@ -141,6 +141,21 @@ public abstract class TestObject extends BulkTest {
|
|||
}
|
||||
}
|
||||
|
||||
public void testSerializeDeserializeThenCompare() throws Exception {
|
||||
Object obj = makeObject();
|
||||
if(obj instanceof Serializable) {
|
||||
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
||||
ObjectOutputStream out = new ObjectOutputStream(buffer);
|
||||
out.writeObject(obj);
|
||||
out.close();
|
||||
|
||||
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
|
||||
Object dest = in.readObject();
|
||||
in.close();
|
||||
assertEquals("obj != deserialize(serialize(obj))",obj,dest);
|
||||
}
|
||||
}
|
||||
|
||||
private void writeExternalFormToStream(Serializable o, OutputStream stream)
|
||||
throws IOException {
|
||||
ObjectOutputStream oStream = new ObjectOutputStream(stream);
|
||||
|
|
|
@ -1,3 +1,59 @@
|
|||
/*
|
||||
* $Id: TestComparatorChain.java,v 1.4 2003/01/07 23:44:20 rwaldhoff Exp $
|
||||
* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2001-2003 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution,
|
||||
* if any, must include the following acknowledgment:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowledgment may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgments normally appear.
|
||||
*
|
||||
* 4. The names "Apache" and "Apache Software Foundation" and
|
||||
* "Apache Commons" must not be used to endorse or promote products
|
||||
* derived from this software without prior written permission. For
|
||||
* written permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache",
|
||||
* "Apache Turbine", nor may "Apache" appear in their name, without
|
||||
* prior written permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
|
||||
package org.apache.commons.collections.comparators;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
@ -18,10 +74,6 @@ public class TestComparatorChain extends TestComparator {
|
|||
return new TestSuite(TestComparatorChain.class);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Comparator makeComparator() {
|
||||
ComparatorChain chain = new ComparatorChain(new ColumnComparator(0));
|
||||
chain.addComparator(new ColumnComparator(1),true); // reverse the second column
|
||||
|
@ -159,5 +211,19 @@ public class TestComparatorChain extends TestComparator {
|
|||
|
||||
return 0;
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return colIndex;
|
||||
}
|
||||
|
||||
public boolean equals(Object that) {
|
||||
if(that instanceof ColumnComparator) {
|
||||
return colIndex == ((ColumnComparator)that).colIndex;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static final long serialVersionUID = -2284880866328872105L;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -65,7 +65,7 @@ import junit.framework.TestSuite;
|
|||
* Test the NullComparator
|
||||
*
|
||||
* @author <a href="mailto:mas@apache.org">Michael A. Smith</a>
|
||||
* @version $Id: TestNullComparator.java,v 1.3 2002/10/12 22:36:23 scolebourne Exp $
|
||||
* @version $Id: TestNullComparator.java,v 1.4 2003/01/07 23:44:20 rwaldhoff Exp $
|
||||
**/
|
||||
public abstract class TestNullComparator extends TestComparator {
|
||||
|
||||
|
@ -74,10 +74,10 @@ public abstract class TestNullComparator extends TestComparator {
|
|||
}
|
||||
|
||||
public static Test suite() {
|
||||
TestSuite suite = new TestSuite(TestNullComparator.class.getName());
|
||||
suite.addTest(new TestSuite(TestNullComparator1.class));
|
||||
suite.addTest(new TestSuite(TestNullComparator2.class));
|
||||
return suite;
|
||||
TestSuite suite = new TestSuite(TestNullComparator.class.getName());
|
||||
suite.addTest(new TestSuite(TestNullComparator1.class));
|
||||
suite.addTest(new TestSuite(TestNullComparator2.class));
|
||||
return suite;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -94,7 +94,7 @@ public abstract class TestNullComparator extends TestComparator {
|
|||
}
|
||||
|
||||
public List getComparableObjectsOrdered() {
|
||||
List list = new LinkedList();
|
||||
List list = new LinkedList();
|
||||
list.add(new Integer(1));
|
||||
list.add(new Integer(2));
|
||||
list.add(new Integer(3));
|
||||
|
@ -113,28 +113,28 @@ public abstract class TestNullComparator extends TestComparator {
|
|||
* Test the NullComparator with nulls low using the comparable comparator
|
||||
**/
|
||||
public static class TestNullComparator2 extends TestNullComparator {
|
||||
|
||||
public TestNullComparator2(String testName) {
|
||||
super(testName);
|
||||
}
|
||||
|
||||
public Comparator makeComparator() {
|
||||
return new NullComparator(false);
|
||||
}
|
||||
|
||||
public List getComparableObjectsOrdered() {
|
||||
List list = new LinkedList();
|
||||
list.add(null);
|
||||
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;
|
||||
}
|
||||
|
||||
public String getCanonicalComparatorName(Object object) {
|
||||
return super.getCanonicalComparatorName(object) + "2";
|
||||
}
|
||||
|
||||
public TestNullComparator2(String testName) {
|
||||
super(testName);
|
||||
}
|
||||
|
||||
public Comparator makeComparator() {
|
||||
return new NullComparator(false);
|
||||
}
|
||||
|
||||
public List getComparableObjectsOrdered() {
|
||||
List list = new LinkedList();
|
||||
list.add(null);
|
||||
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;
|
||||
}
|
||||
|
||||
public String getCanonicalComparatorName(Object object) {
|
||||
return super.getCanonicalComparatorName(object) + "2";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,65 @@
|
|||
/*
|
||||
* $Id: TestReverseComparator.java,v 1.4 2003/01/07 23:44:20 rwaldhoff Exp $
|
||||
* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2001-2003 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution,
|
||||
* if any, must include the following acknowledgment:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowledgment may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgments normally appear.
|
||||
*
|
||||
* 4. The names "Apache" and "Apache Software Foundation" and
|
||||
* "Apache Commons" must not be used to endorse or promote products
|
||||
* derived from this software without prior written permission. For
|
||||
* written permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache",
|
||||
* "Apache Turbine", nor may "Apache" appear in their name, without
|
||||
* prior written permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
|
||||
package org.apache.commons.collections.comparators;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.LinkedList;
|
||||
|
@ -21,7 +81,7 @@ public class TestReverseComparator extends TestComparator {
|
|||
/**
|
||||
* For the purposes of this test, return a
|
||||
* ReverseComparator that wraps the java.util.Collections.reverseOrder()
|
||||
* Comparator. The resulting comparator shouls
|
||||
* Comparator. The resulting comparator should
|
||||
* sort according to natural Order. (Note: we wrap
|
||||
* a Comparator taken from the JDK so that we can
|
||||
* save a "canonical" form in CVS.
|
||||
|
@ -42,4 +102,23 @@ public class TestReverseComparator extends TestComparator {
|
|||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Override this inherited test since Collections.reverseOrder
|
||||
* doesn't adhere to the "soft" Comparator contract, and we've
|
||||
* already "cannonized" the comparator returned by makeComparator.
|
||||
*/
|
||||
public void testSerializeDeserializeThenCompare() throws Exception {
|
||||
Comparator comp = new ReverseComparator(new ComparableComparator());
|
||||
|
||||
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
||||
ObjectOutputStream out = new ObjectOutputStream(buffer);
|
||||
out.writeObject(comp);
|
||||
out.close();
|
||||
|
||||
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
|
||||
Object dest = in.readObject();
|
||||
in.close();
|
||||
assertEquals("obj != deserialize(serialize(obj))",comp,dest);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue