Fix problem with Serialization and Cursors hidden from original tests.

Reported by Rodney


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131170 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2003-09-20 14:03:57 +00:00
parent be72e12a5d
commit d4b4a8f7bf
2 changed files with 40 additions and 12 deletions

View File

@ -1,5 +1,5 @@
/* /*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/CursorableLinkedList.java,v 1.16 2003/08/31 17:26:43 scolebourne Exp $ * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/CursorableLinkedList.java,v 1.17 2003/09/20 14:03:57 scolebourne Exp $
* ==================================================================== * ====================================================================
* *
* The Apache Software License, Version 1.1 * The Apache Software License, Version 1.1
@ -84,7 +84,7 @@ import java.util.NoSuchElementException;
* *
* @see java.util.LinkedList * @see java.util.LinkedList
* @since Commons Collections 1.0 * @since Commons Collections 1.0
* @version $Revision: 1.16 $ $Date: 2003/08/31 17:26:43 $ * @version $Revision: 1.17 $ $Date: 2003/09/20 14:03:57 $
* *
* @author Rodney Waldhoff * @author Rodney Waldhoff
* @author Janek Bogucki * @author Janek Bogucki
@ -92,6 +92,9 @@ import java.util.NoSuchElementException;
public class CursorableLinkedList implements List, Serializable { public class CursorableLinkedList implements List, Serializable {
// TODO: use weak references to cursors in case they aren't closed directly // TODO: use weak references to cursors in case they aren't closed directly
/** Ensure serialization compatability */
private static final long serialVersionUID = 8836393098519411393L;
//--- public methods --------------------------------------------- //--- public methods ---------------------------------------------
/** /**
@ -897,7 +900,7 @@ public class CursorableLinkedList implements List, Serializable {
out.defaultWriteObject(); out.defaultWriteObject();
out.writeInt(_size); out.writeInt(_size);
Listable cur = _head.next(); Listable cur = _head.next();
while(cur != null) { while (cur != null) {
out.writeObject(cur.value()); out.writeObject(cur.value());
cur = cur.next(); cur = cur.next();
} }
@ -906,9 +909,11 @@ public class CursorableLinkedList implements List, Serializable {
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject(); in.defaultReadObject();
_size = 0; _size = 0;
_modCount = 0;
_cursors = new ArrayList();
_head = new Listable(null,null,null); _head = new Listable(null,null,null);
int size = in.readInt(); int size = in.readInt();
for(int i=0;i<size;i++) { for (int i=0;i<size;i++) {
this.add(in.readObject()); this.add(in.readObject());
} }
} }
@ -916,7 +921,7 @@ public class CursorableLinkedList implements List, Serializable {
//--- protected attributes --------------------------------------- //--- protected attributes ---------------------------------------
/** The number of elements in me. */ /** The number of elements in me. */
transient protected int _size = 0; protected transient int _size = 0;
/** /**
* A sentry node. * A sentry node.
@ -930,16 +935,16 @@ public class CursorableLinkedList implements List, Serializable {
* {@link org.apache.commons.collections.CursorableLinkedList.Listable} * {@link org.apache.commons.collections.CursorableLinkedList.Listable}
* is the first or last element in the list. * is the first or last element in the list.
*/ */
transient protected Listable _head = new Listable(null,null,null); protected transient Listable _head = new Listable(null,null,null);
/** Tracks the number of structural modifications to me. */ /** Tracks the number of structural modifications to me. */
protected int _modCount = 0; protected transient int _modCount = 0;
/** /**
* A list of the currently {@link CursorableLinkedList.Cursor}s currently * A list of the currently {@link CursorableLinkedList.Cursor}s currently
* open in this list. * open in this list.
*/ */
protected List _cursors = new ArrayList(); protected transient List _cursors = new ArrayList();
//--- inner classes ---------------------------------------------- //--- inner classes ----------------------------------------------

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/TestCursorableLinkedList.java,v 1.9 2003/08/31 17:28:43 scolebourne Exp $ * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestCursorableLinkedList.java,v 1.10 2003/09/20 14:03:57 scolebourne Exp $
* $Revision: 1.9 $ * $Revision: 1.10 $
* $Date: 2003/08/31 17:28:43 $ * $Date: 2003/09/20 14:03:57 $
* *
* ==================================================================== * ====================================================================
* *
@ -71,7 +71,7 @@ import junit.framework.Test;
/** /**
* @author Rodney Waldhoff * @author Rodney Waldhoff
* @version $Id: TestCursorableLinkedList.java,v 1.9 2003/08/31 17:28:43 scolebourne Exp $ * @version $Id: TestCursorableLinkedList.java,v 1.10 2003/09/20 14:03:57 scolebourne Exp $
*/ */
public class TestCursorableLinkedList extends TestList { public class TestCursorableLinkedList extends TestList {
public TestCursorableLinkedList(String testName) { public TestCursorableLinkedList(String testName) {
@ -932,6 +932,29 @@ public class TestCursorableLinkedList extends TestList {
assertTrue(list.equals(list2)); assertTrue(list.equals(list2));
} }
public void testSerializationWithOpenCursor() throws Exception {
list.add("A");
list.add("B");
list.add("C");
list.add("D");
list.add("E");
CursorableLinkedList.Cursor cursor = list.cursor();
java.io.ByteArrayOutputStream buf = new java.io.ByteArrayOutputStream();
java.io.ObjectOutputStream out = new java.io.ObjectOutputStream(buf);
out.writeObject(list);
out.flush();
out.close();
java.io.ByteArrayInputStream bufin = new java.io.ByteArrayInputStream(buf.toByteArray());
java.io.ObjectInputStream in = new java.io.ObjectInputStream(bufin);
Object list2 = in.readObject();
assertTrue(list != list2);
assertTrue(list2.equals(list));
assertTrue(list.equals(list2));
}
public void testLongSerialization() throws Exception { public void testLongSerialization() throws Exception {
// recursive serialization will cause a stack // recursive serialization will cause a stack
// overflow exception with long lists // overflow exception with long lists