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
@ -84,7 +84,7 @@ import java.util.NoSuchElementException;
*
* @see java.util.LinkedList
* @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 Janek Bogucki
@ -92,6 +92,9 @@ import java.util.NoSuchElementException;
public class CursorableLinkedList implements List, Serializable {
// TODO: use weak references to cursors in case they aren't closed directly
/** Ensure serialization compatability */
private static final long serialVersionUID = 8836393098519411393L;
//--- public methods ---------------------------------------------
/**
@ -906,6 +909,8 @@ public class CursorableLinkedList implements List, Serializable {
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
_size = 0;
_modCount = 0;
_cursors = new ArrayList();
_head = new Listable(null,null,null);
int size = in.readInt();
for (int i=0;i<size;i++) {
@ -916,7 +921,7 @@ public class CursorableLinkedList implements List, Serializable {
//--- protected attributes ---------------------------------------
/** The number of elements in me. */
transient protected int _size = 0;
protected transient int _size = 0;
/**
* A sentry node.
@ -930,16 +935,16 @@ public class CursorableLinkedList implements List, Serializable {
* {@link org.apache.commons.collections.CursorableLinkedList.Listable}
* 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. */
protected int _modCount = 0;
protected transient int _modCount = 0;
/**
* A list of the currently {@link CursorableLinkedList.Cursor}s currently
* open in this list.
*/
protected List _cursors = new ArrayList();
protected transient List _cursors = new ArrayList();
//--- 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 $
* $Revision: 1.9 $
* $Date: 2003/08/31 17:28:43 $
* $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.10 $
* $Date: 2003/09/20 14:03:57 $
*
* ====================================================================
*
@ -71,7 +71,7 @@ import junit.framework.Test;
/**
* @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 TestCursorableLinkedList(String testName) {
@ -932,6 +932,29 @@ public class TestCursorableLinkedList extends TestList {
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 {
// recursive serialization will cause a stack
// overflow exception with long lists