apply Rich Dougherty's patch fixing NodeCachingLinkedList unit tests

this also adds CommonsLinkedList and tests


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@130914 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Rodney Waldhoff 2003-01-07 15:18:15 +00:00
parent 8f77a02066
commit 37b1a12876
15 changed files with 1136 additions and 910 deletions

View File

@ -0,0 +1,539 @@
package org.apache.commons.collections;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.lang.reflect.Array;
import java.util.AbstractSequentialList;
import java.util.Collection;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.NoSuchElementException;
/**
* An implementation of {@link java.util.List} which duplicates the behaviour
* of {@link java.util.LinkedList}, but which provides a more open interface for
* subclasses to extend.
*
* @author <a href="mailto:rich@rd.gen.nz">Rich Dougherty</a>
*/
class CommonsLinkedList extends LinkedList
implements List, Serializable {
/*
* Implementation notes:
* - a standard circular doubly-linked list
* - a marker node is stored to mark the start and the end of the list
* - node creation and removal always occurs through createNode() and
* removeNode().
* - a modification count is kept, with the same semantics as
* {@link java.util.LinkedList}.
* - respects {@link AbstractList#modCount}
*/
/**
* A node within the {@link CommonsLinkedList}.
*
* @author <a href="mailto:rich@nil.co.nz">Rich Dougherty</a>
*/
protected static class Node {
/**
* A pointer to the node before this node.
*/
public Node previous;
/**
* A pointer to the node after this node.
*/
public Node next;
/**
* The object contained within this node.
*/
public Object element;
public Node() {
}
public Node(Node previous, Node next, Object element) {
this.previous = previous;
this.next = next;
this.element = element;
}
/**
* Checks if a value is equal to this node's element.
*
* @return True if the elements are both null or both equal according
* to {@link Object#equals()}.
*/
public boolean elementEquals(Object otherElement) {
if (element == null) {
return otherElement == null;
} else {
return element.equals(otherElement);
}
}
}
/**
* A {@link java.util.ListIterator} {@link CommonsLinkedList}.
*
* @author <a href="mailto:rich@nil.co.nz">Rich Dougherty</a>
*/
protected class ListIteratorImpl implements ListIterator {
/**
* The node that will be returned by {@link #next()}. If this is equal
* to {@link #marker} then there are no more elements to return.
*/
protected Node nextNode;
/**
* The index of {@link #nextNode}.
*/
protected int nextIndex;
/**
* The last node that was returned by {@link #next()} or {@link
* #previous()}. Set to <code>null</code> if {@link #next()} or {@link
* #previous()} haven't been called, or if the node has been removed
* with {@link #remove()} or a new node added with {@link #add(Object)}.
* Should be accesed through {@link #getLastNodeReturned()} to enforce
* this behaviour.
*/
protected Node lastNodeReturned;
/**
* The modification count that the list is expected to have. If the list
* doesn't have this count, then a
* {@link java.util.ConcurrentModificationException} may be thrown by
* the operations.
*/
protected int expectedModCount;
/**
* Create a ListIterator for a list, starting at the first element in
* the list.
*/
public ListIteratorImpl() throws IndexOutOfBoundsException {
this(0);
}
/**
* Create a ListIterator for a list.
*
* @param startIndex The index to start at.
*/
public ListIteratorImpl(int startIndex)
throws IndexOutOfBoundsException {
expectedModCount = modCount;
nextNode = getNode(startIndex, true);
nextIndex = startIndex;
}
/**
* Checks the modification count of the list is the value that this
* object expects.
*
* @throws ConcurrentModificationException If the list's modification
* count isn't the value that was expected.
*/
protected void checkModCount()
throws ConcurrentModificationException {
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
}
/**
* Gets the last node returned.
*
* @throws IllegalStateException If {@link #next()} or {@link
* #previous()} haven't been called, or if the node has been removed
* with {@link #remove()} or a new node added with {@link #add(Object)}.
*/
protected Node getLastNodeReturned() throws IllegalStateException {
if (lastNodeReturned == null) {
throw new IllegalStateException();
}
return lastNodeReturned;
}
public boolean hasNext() {
return nextNode != marker;
}
public Object next() {
checkModCount();
if (!hasNext()) {
throw new NoSuchElementException("No element at index " +
nextIndex + ".");
}
Object element = nextNode.element;
lastNodeReturned = nextNode;
nextNode = nextNode.next;
nextIndex++;
return element;
}
public boolean hasPrevious() {
return nextNode.previous != marker;
}
public Object previous() {
checkModCount();
if (!hasPrevious()) {
throw new NoSuchElementException("Already at start of list.");
}
nextNode = nextNode.previous;
Object element = nextNode.element;
lastNodeReturned = nextNode;
nextIndex--;
return element;
}
public int nextIndex() {
return nextIndex;
}
public int previousIndex() {
return nextIndex - 1;
}
public void remove() {
checkModCount();
removeNode(getLastNodeReturned());
lastNodeReturned = null;
nextIndex--;
expectedModCount++;
}
public void set(Object o) {
checkModCount();
getLastNodeReturned().element = o;
}
public void add(Object o) {
checkModCount();
addNodeBefore(nextNode, o);
lastNodeReturned = null;
nextIndex++;
expectedModCount++;
}
}
private static final long serialVersionUID = 1L;
/**
* A {@link Node} which ndicates the start and end of the list and does not
* hold a value. The value of <code>next</code> is the first item in the
* list. The value of of <code>previous</code> is the last item in the list.
*/
protected transient Node marker;
/**
* The size of the list.
*/
protected transient int size;
public CommonsLinkedList() {
initializeEmptyList();
}
public CommonsLinkedList(Collection c) {
initializeEmptyList();
addAll(c);
}
/**
* The equivalent of a default constructor, broken out so it can be called
* by any constructor and by {@link #readObject(ObjectInputStream)}.
* Subclasses which override this method should make sure they call it so
* the list is initialised properly.
*/
protected void initializeEmptyList() {
marker = createNode();
marker.next = marker;
marker.previous = marker;
}
// Operations on nodes
protected Node createNode() {
return new Node();
}
protected Node createNode(Node next, Node previous, Object element) {
return new Node(next, previous, element);
}
private void addNodeBefore(Node node, Object o) {
Node newNode = createNode(node.previous, node, o);
node.previous.next = newNode;
node.previous = newNode;
size++;
modCount++;
}
protected void addNodeAfter(Node node, Object o) {
Node newNode = createNode(node, node.next, o);
node.next.previous = newNode;
node.next = newNode;
size++;
modCount++;
}
protected void removeNode(Node node) {
node.previous.next = node.next;
node.next.previous = node.previous;
size--;
modCount++;
}
protected void removeAllNodes() {
marker.next = marker;
marker.previous = marker;
size = 0;
modCount++;
}
/**
* Gets the node at a particular index.
*
* @param index The index, starting from 0.
* @param endMarkerAllowd Whether or not the end marker can be returned if
* startIndex is set to the list's size.
* @throws IndexOutOfBoundsException If the index is less than 0; equal to
* the size of the list and endMakerAllowed is false; or greater than the
* size of the list.
*/
protected Node getNode(int index, boolean endMarkerAllowed) throws IndexOutOfBoundsException {
// Check the index is within the bounds
if (index < 0) {
throw new IndexOutOfBoundsException("Couldn't get the node: " +
"index (" + index + ") less than zero.");
}
if (!endMarkerAllowed && index == size) {
throw new IndexOutOfBoundsException("Couldn't get the node: " +
"index (" + index + ") is the size of the list.");
}
if (index > size) {
throw new IndexOutOfBoundsException("Couldn't get the node: " +
"index (" + index + ") greater than the size of the " +
"list (" + size + ").");
}
// Search the list and get the node
Node node;
if (index < (size / 2)) {
// Search forwards
node = marker.next;
for (int currentIndex = 0; currentIndex < index; currentIndex++) {
node = node.next;
}
} else {
// Search backwards
node = marker;
for (int currentIndex = size; currentIndex > index; currentIndex--) {
node = node.previous;
}
}
return node;
}
// List implementation required by AbstractSequentialList
public ListIterator listIterator() {
return new ListIteratorImpl();
}
public ListIterator listIterator(int startIndex) {
return new ListIteratorImpl(startIndex);
}
public int size() {
return size;
}
// List implementation not required by AbstractSequentialList, but provided
// for efficiency or to override LinkedList's implementation.
public void clear() {
removeAllNodes();
}
public boolean add(Object o) {
addLast(o);
return true;
}
public void add(int index, Object element) {
Node node = getNode(index, true);
addNodeBefore(node, element);
}
public boolean addAll(Collection c) {
return addAll(size, c);
}
public boolean addAll(int index, Collection c) {
Node node = getNode(index, true);
for (Iterator itr = c.iterator(); itr.hasNext();) {
Object element = itr.next();
addNodeBefore(node, element);
}
return true;
}
public Object get(int index) {
Node node = getNode(index, false);
return node.element;
}
public Object set(int index, Object element) {
Node node = getNode(index, false);
Object oldElement = node.element;
node.element = element;
return oldElement;
}
public Object remove(int index) {
Node node = getNode(index, false);
Object oldElement = node.element;
removeNode(node);
return oldElement;
}
public boolean remove(Object element) {
for (Node node = marker.next; node != marker; node = node.next) {
if (node.elementEquals(element)) {
removeNode(node);
return true;
}
}
return false;
}
public int indexOf(Object element) {
int i = 0;
for (Node node = marker.next; node != marker; node = node.next) {
if (node.elementEquals(element)) {
return i;
}
i++;
}
return -1;
}
public int lastIndexOf(Object element) {
int i = size - 1;
for (Node node = marker.previous; node != marker; node = node.previous) {
if (node.elementEquals(element)) {
return i;
}
i--;
}
return -1;
}
public boolean contains(Object element) {
return indexOf(element) != -1;
}
public Object[] toArray() {
return toArray(new Object[size]);
}
public Object[] toArray(Object[] array) {
// Extend the array if needed
if (array.length < size) {
Class componentType = array.getClass().getComponentType();
array = (Object[]) Array.newInstance(componentType, size);
}
// Copy the values into the array
Node node = marker.next;
for (int i = 0; i < size; i++) {
array[i] = node.element;
node = node.next;
}
// Set the value after the last element to null
if (array.length > size) {
array[size] = null;
}
return array;
}
// Extra methods compatible with java.util.LinkedList.
public Object getFirst() {
Node node = marker.next;
if (node == marker) {
throw new NoSuchElementException();
}
return node.element;
}
public Object getLast() {
Node node = marker.previous;
if (node == marker) {
throw new NoSuchElementException();
}
return node.element;
}
public void addFirst(Object o) {
addNodeAfter(marker, o);
}
public void addLast(Object o) {
addNodeBefore(marker, o);
}
public Object removeFirst() {
Node node = marker.next;
if (node == marker) {
throw new NoSuchElementException();
}
Object oldElement = node.element;
removeNode(node);
return oldElement;
}
public Object removeLast() {
Node node = marker.previous;
if (node == marker) {
throw new NoSuchElementException();
}
Object oldElement = node.element;
removeNode(node);
return oldElement;
}
// Serialization methods
private void writeObject(ObjectOutputStream outputStream)
throws IOException, ClassNotFoundException {
outputStream.defaultWriteObject();
// Write the size so we know how many nodes to read back
outputStream.writeInt(size());
for (Iterator itr = iterator(); itr.hasNext();) {
outputStream.writeObject(itr.next());
}
}
private void readObject(ObjectInputStream inputStream)
throws IOException, ClassNotFoundException {
inputStream.defaultReadObject();
initializeEmptyList();
int size = inputStream.readInt();
for (int i = 0; i < size; i++) {
add(inputStream.readObject());
}
}
}

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.38 2003/01/07 13:24:52 rwaldhoff Exp $ * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestAll.java,v 1.39 2003/01/07 15:18:14 rwaldhoff Exp $
* $Revision: 1.38 $ * $Revision: 1.39 $
* $Date: 2003/01/07 13:24:52 $ * $Date: 2003/01/07 15:18:14 $
* *
* ==================================================================== * ====================================================================
* *
@ -66,7 +66,7 @@ import junit.framework.*;
/** /**
* Entry point for all Collections tests. * Entry point for all Collections tests.
* @author Rodney Waldhoff * @author Rodney Waldhoff
* @version $Id: TestAll.java,v 1.38 2003/01/07 13:24:52 rwaldhoff Exp $ * @version $Id: TestAll.java,v 1.39 2003/01/07 15:18:14 rwaldhoff Exp $
*/ */
public class TestAll extends TestCase { public class TestAll extends TestCase {
public TestAll(String testName) { public TestAll(String testName) {
@ -81,6 +81,7 @@ public class TestAll extends TestCase {
suite.addTest(TestBoundedFifoBuffer.suite()); suite.addTest(TestBoundedFifoBuffer.suite());
suite.addTest(TestBoundedFifoBuffer2.suite()); suite.addTest(TestBoundedFifoBuffer2.suite());
suite.addTest(TestCollectionUtils.suite()); suite.addTest(TestCollectionUtils.suite());
suite.addTest(TestCommonsLinkedList.suite());
suite.addTest(TestBufferUtils.suite()); suite.addTest(TestBufferUtils.suite());
suite.addTest(TestSetUtils.suite()); suite.addTest(TestSetUtils.suite());
suite.addTest(TestListUtils.suite()); suite.addTest(TestListUtils.suite());

View File

@ -0,0 +1,90 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/Attic/TestCommonsLinkedList.java,v 1.1 2003/01/07 15:18:14 rwaldhoff Exp $
* $Revision: 1.1 $
* $Date: 2003/01/07 15:18:14 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 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 acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" 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"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* 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;
import java.util.*;
import junit.framework.Test;
/**
* Test case for {@link CommonsLinkedList}.
*
* @author <a href="mailto:rich@rd.gen.nz">Rich Dougherty</a>
*/
public class TestCommonsLinkedList extends TestLinkedList {
public TestCommonsLinkedList(String testName) {
super(testName);
}
public LinkedList makeEmptyLinkedList() {
return new CommonsLinkedList();
}
public static Test suite() {
return BulkTest.makeSuite(TestCommonsLinkedList.class);
}
public String getCompatibilityVersion() {
return "2.2";
}
}

View File

@ -0,0 +1,288 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestLinkedList.java,v 1.1 2003/01/07 15:18:14 rwaldhoff Exp $
* $Revision: 1.1 $
* $Date: 2003/01/07 15:18:14 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2001 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 acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" 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"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* 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;
import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.AbstractCollection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.NoSuchElementException;
/**
* Tests base {@link java.util.LinkedList} methods and contracts.
* <p>
* To use, simply extend this class, and implement
* the {@link #makeLinkedList} method.
* <p>
* If your {@link LinkedList} fails one of these tests by design,
* you may still use this base set of cases. Simply override the
* test case (method) your {@link List} fails.
*
* @author <a href="mailto:rich@rd.gen.nz">Rich Dougherty</a>
* @version $Id: TestLinkedList.java,v 1.1 2003/01/07 15:18:14 rwaldhoff Exp $
*/
public abstract class TestLinkedList extends TestList {
public TestLinkedList(String testName) {
super(testName);
}
protected List makeEmptyList() {
return makeEmptyLinkedList();
}
protected List makeFullList() {
return makeFullLinkedList();
}
/**
* Return a new, empty {@link LinkedList} to be used for testing.
*
* @return an empty list for testing.
*/
protected abstract LinkedList makeEmptyLinkedList();
/**
* Return a new, full {@link List} to be used for testing.
*
* @return a full list for testing
*/
protected LinkedList makeFullLinkedList() {
// only works if list supports optional "addAll(Collection)"
LinkedList list = makeEmptyLinkedList();
list.addAll(Arrays.asList(getFullElements()));
return list;
}
/**
* Returns the {@link #collection} field cast to a {@link LinkedList}.
*
* @return the collection field as a List
*/
protected LinkedList getLinkedList() {
return (LinkedList)collection;
}
/**
* Returns the {@link #confirmed} field cast to a {@link LinkedList}.
*
* @return the confirmed field as a List
*/
protected LinkedList getConfirmedLinkedList() {
return (LinkedList)confirmed;
}
/**
* Tests {@link LinkedList#addFirst(Object)}.
*/
public void testLinkedListAddFirst() {
if (!isAddSupported()) return;
Object o = "hello";
resetEmpty();
getLinkedList().addFirst(o);
getConfirmedLinkedList().addFirst(o);
verify();
resetFull();
getLinkedList().addFirst(o);
getConfirmedLinkedList().addFirst(o);
verify();
}
/**
* Tests {@link LinkedList#addLast(Object)}.
*/
public void testLinkedListAddLast() {
if (!isAddSupported()) return;
Object o = "hello";
resetEmpty();
getLinkedList().addLast(o);
getConfirmedLinkedList().addLast(o);
verify();
resetFull();
getLinkedList().addLast(o);
getConfirmedLinkedList().addLast(o);
verify();
}
/**
* Tests {@link LinkedList#getFirst(Object)}.
*/
public void testLinkedListGetFirst() {
resetEmpty();
try {
getLinkedList().getFirst();
fail("getFirst() should throw a NoSuchElementException for an " +
"empty list.");
} catch (NoSuchElementException e) {
// This is correct
}
verify();
resetFull();
Object first = getLinkedList().getFirst();
Object confirmedFirst = getConfirmedLinkedList().getFirst();
assertEquals("Result returned by getFirst() was wrong.",
confirmedFirst, first);
verify();
}
/**
* Tests {@link LinkedList#getLast(Object)}.
*/
public void testLinkedListGetLast() {
resetEmpty();
try {
getLinkedList().getLast();
fail("getLast() should throw a NoSuchElementException for an " +
"empty list.");
} catch (NoSuchElementException e) {
// This is correct
}
verify();
resetFull();
Object last = getLinkedList().getLast();
Object confirmedLast = getConfirmedLinkedList().getLast();
assertEquals("Result returned by getLast() was wrong.",
confirmedLast, last);
verify();
}
/**
* Tests {@link LinkedList#removeFirst(Object)}.
*/
public void testLinkedListRemoveFirst() {
if (!isRemoveSupported()) return;
resetEmpty();
try {
getLinkedList().removeFirst();
fail("removeFirst() should throw a NoSuchElementException for " +
"an empty list.");
} catch (NoSuchElementException e) {
// This is correct
}
verify();
resetFull();
Object first = getLinkedList().removeFirst();
Object confirmedFirst = getConfirmedLinkedList().removeFirst();
assertEquals("Result returned by removeFirst() was wrong.",
confirmedFirst, first);
verify();
}
/**
* Tests {@link LinkedList#removeLast(Object)}.
*/
public void testLinkedListRemoveLast() {
if (!isRemoveSupported()) return;
resetEmpty();
try {
getLinkedList().removeLast();
fail("removeLast() should throw a NoSuchElementException for " +
"an empty list.");
} catch (NoSuchElementException e) {
// This is correct
}
verify();
resetFull();
Object last = getLinkedList().removeLast();
Object confirmedLast = getConfirmedLinkedList().removeLast();
assertEquals("Result returned by removeLast() was wrong.",
confirmedLast, last);
verify();
}
/**
* Returns an empty {@link ArrayList}.
*/
protected Collection makeConfirmedCollection() {
return new LinkedList();
}
/**
* Returns a full {@link ArrayList}.
*/
protected Collection makeConfirmedFullCollection() {
List list = new LinkedList();
list.addAll(Arrays.asList(getFullElements()));
return list;
}
}

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/TestMultiHashMap.java,v 1.9 2002/11/24 20:24:49 scolebourne Exp $ * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestMultiHashMap.java,v 1.10 2003/01/07 15:18:14 rwaldhoff Exp $
* $Revision: 1.9 $ * $Revision: 1.10 $
* $Date: 2002/11/24 20:24:49 $ * $Date: 2003/01/07 15:18:14 $
* *
* ==================================================================== * ====================================================================
* *
@ -92,8 +92,8 @@ public class TestMultiHashMap extends TestMap
} }
// MutltiHashMap was introduced in Collections 2.x // MutltiHashMap was introduced in Collections 2.x
public int getCompatibilityVersion() { public String getCompatibilityVersion() {
return 2; return "2";
} }
public Map makeEmptyMap() { public Map makeEmptyMap() {

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/TestNodeCachingLinkedList.java,v 1.1 2002/11/18 23:58:46 scolebourne Exp $ * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/Attic/TestNodeCachingLinkedList.java,v 1.2 2003/01/07 15:18:15 rwaldhoff Exp $
* $Revision: 1.1 $ * $Revision: 1.2 $
* $Date: 2002/11/18 23:58:46 $ * $Date: 2003/01/07 15:18:15 $
* *
* ==================================================================== * ====================================================================
* *
@ -68,14 +68,14 @@ import junit.framework.Test;
* *
* @author Jeff Varszegi * @author Jeff Varszegi
*/ */
public class TestNodeCachingLinkedList extends TestList { public class TestNodeCachingLinkedList extends TestLinkedList {
protected NodeCachingLinkedList list = null; protected NodeCachingLinkedList list = null;
public TestNodeCachingLinkedList(String _testName) { public TestNodeCachingLinkedList(String _testName) {
super(_testName); super(_testName);
} }
public List makeEmptyList() { public LinkedList makeEmptyLinkedList() {
return new NodeCachingLinkedList(); return new NodeCachingLinkedList();
} }
@ -87,6 +87,10 @@ public class TestNodeCachingLinkedList extends TestList {
return BulkTest.makeSuite(TestNodeCachingLinkedList.class); return BulkTest.makeSuite(TestNodeCachingLinkedList.class);
} }
public String getCompatibilityVersion() {
return "2.2";
}
public static void compareSpeed() { public static void compareSpeed() {
NodeCachingLinkedList ncll = new NodeCachingLinkedList(); NodeCachingLinkedList ncll = new NodeCachingLinkedList();
LinkedList ll = new LinkedList(); LinkedList ll = new LinkedList();
@ -157,8 +161,9 @@ public class TestNodeCachingLinkedList extends TestList {
public static void main(String args[]) { public static void main(String args[]) {
compareSpeed();
String[] testCaseName = { TestNodeCachingLinkedList.class.getName()}; String[] testCaseName = { TestNodeCachingLinkedList.class.getName()};
junit.textui.TestRunner.main(testCaseName); junit.textui.TestRunner.main(testCaseName);
} }
} }

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.15 2003/01/04 13:43:10 rwaldhoff Exp $ * $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.15 $ * $Revision: 1.16 $
* $Date: 2003/01/04 13:43:10 $ * $Date: 2003/01/07 15:18:15 $
* *
* ==================================================================== * ====================================================================
* *
@ -84,7 +84,7 @@ import java.io.Serializable;
* test case (method) your {@link Object} fails. * test case (method) your {@link Object} fails.
* *
* @author Rodney Waldhoff * @author Rodney Waldhoff
* @version $Id: TestObject.java,v 1.15 2003/01/04 13:43:10 rwaldhoff Exp $ * @version $Id: TestObject.java,v 1.16 2003/01/07 15:18:15 rwaldhoff Exp $
*/ */
public abstract class TestObject extends BulkTest { public abstract class TestObject extends BulkTest {
public TestObject(String testName) { public TestObject(String testName) {
@ -95,19 +95,23 @@ public abstract class TestObject extends BulkTest {
public static final int COLLECTIONS_MAJOR_VERSION = 2; public static final int COLLECTIONS_MAJOR_VERSION = 2;
/** /**
* Get the version of Collections that this object tries to
* maintain serialization compatibility with. Defaults to 1, the
* earliest Collections version. (Note: some collections did not
* even exist in this version).
*
* This constant makes it possible for TestMap (and other subclasses, * This constant makes it possible for TestMap (and other subclasses,
* if necessary) to automatically check CVS for a versionX copy of a * if necessary) to automatically check CVS for a versionX copy of a
* Serialized object, so we can make sure that compatibility is maintained. * Serialized object, so we can make sure that compatibility is maintained.
* See, for example, TestMap.getCanonicalFullMapName(Map map). * See, for example, TestMap.getCanonicalFullMapName(Map map).
* Subclasses can override this variable, indicating compatibility * Subclasses can override this variable, indicating compatibility
* with earlier Collections versions. * with earlier Collections versions.
* Defaults to 1, the earliest Collections version. (Note: some
* collections did not even exist in this version).
* *
* @return 1 * @return The version, or <code>null</code> if this object shouldn't be
* tested for compatibility with previous versions.
*/ */
public int getCompatibilityVersion() { public String getCompatibilityVersion() {
return 1; return "1";
} }
/** /**

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/TestReferenceMap.java,v 1.3 2002/11/07 21:43:36 bayard Exp $ * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestReferenceMap.java,v 1.4 2003/01/07 15:18:15 rwaldhoff Exp $
* $Revision: 1.3 $ * $Revision: 1.4 $
* $Date: 2002/11/07 21:43:36 $ * $Date: 2003/01/07 15:18:15 $
* *
* ==================================================================== * ====================================================================
* *
@ -72,7 +72,7 @@ import junit.framework.Test;
* Tests for ReferenceMap. * Tests for ReferenceMap.
* *
* @author Paul Jack * @author Paul Jack
* @version $Id: TestReferenceMap.java,v 1.3 2002/11/07 21:43:36 bayard Exp $ * @version $Id: TestReferenceMap.java,v 1.4 2003/01/07 15:18:15 rwaldhoff Exp $
*/ */
public class TestReferenceMap extends TestMap { public class TestReferenceMap extends TestMap {
@ -184,8 +184,8 @@ public class TestReferenceMap extends TestMap {
*/ */
public int getCompatibilityVersion() { public String getCompatibilityVersion() {
return 2; // actually 2.1, but can't represent that as an int return "2.1";
} }

View File

@ -89,8 +89,8 @@ public class TestSequencedHashMap extends TestMap {
// current versions of SequencedHashMap and subclasses are not // current versions of SequencedHashMap and subclasses are not
// compatible with Collections 1.x // compatible with Collections 1.x
public int getCompatibilityVersion() { public String getCompatibilityVersion() {
return 2; return "2";
} }
public static void main(String[] args) { public static void main(String[] args) {

View File

@ -28,8 +28,8 @@ public abstract class TestComparator extends TestObject {
* *
* @return 2 * @return 2
*/ */
public int getCompatibilityVersion() { public String getCompatibilityVersion() {
return 2; return "2";
} }
public void reverseObjects(List list) { public void reverseObjects(List list) {