[COLLECTION-497,498,499] Refactored test framework for bag implementations, added missing tests, added CollectionSortedBag.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@1540833 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Thomas Neidhart 2013-11-11 20:28:22 +00:00
parent 528dbde3d2
commit 5a87d98aa1
32 changed files with 924 additions and 562 deletions

View File

@ -47,6 +47,13 @@ Major changes since 3.2.1
Changes since 4.0-alpha1
------------------------
o [COLLECTIONS-499] Refactored the test framework for Bag implementations to extend from
"AbstractCollectionTest" by decorating the concrete Bag instance with
a CollectionBag or CollectionSortedBag.
o [COLLECTIONS-498] "CollectionBag" will now also respect the contract of the decorated bag in case
a null argument is provided to either removeAll or retainAll.
o [COLLECTIONS-497] Added bag decorator "CollectionSortedBag" which decorates a SortedBag to make it
comply with the Collection contract.
o [COLLECTIONS-496] "UnmodifiableBoundedCollection" does now also implement the marker interface "Unmodifiable"
similar as all other unmodifiable decorators.
o [COLLECTIONS-495] "UnmodifiableTrie#unmodifiableTrie(Trie)" will not decorate again an already unmodifiable Trie.
@ -93,6 +100,7 @@ Removed classes
New classes
-----------
o [COLLECTIONS-497] CollectionSortedBag - decorates a SortedBag to make it comply with the Collection contract.
o [COLLECTIONS-468] CollectionBag - decorates another Bag to make it comply with the Collection contract.
o [COLLECTIONS-463] PushbackIterator - supports pushback of elements during iteration. Thanks to Andy Seaborne, Claude Warren.
o [COLLECTIONS-462] PeekingIterator - supports one-element lookahead during iteration. Thanks to Andy Seaborne, Claude Warren.
@ -163,6 +171,9 @@ New features
Changed classes / methods
-------------------------
o [COLLECTIONS-499] Refactored the test framework for Bag implementations to extend from
"AbstractCollectionTest" by decorating the concrete Bag instance with
a CollectionBag or CollectionSortedBag.
o [COLLECTIONS-496] "UnmodifiableBoundedCollection" does now also implement the marker interface "Unmodifiable"
similar as all other unmodifiable decorators.
o [COLLECTIONS-495] "UnmodifiableTrie#unmodifiableTrie(Trie)" will not decorate again an already unmodifiable Trie.
@ -231,6 +242,8 @@ Changed classes / methods
Fixed Bugs
----------
o [COLLECTIONS-498] "CollectionBag" will now also respect the contract of the decorated bag in case
a null argument is provided to either removeAll or retainAll.
o [COLLECTIONS-481] No collision detection/resolution was performed when calling "CompositeSet#addComposited(...)"
with more than one Set as argument. Additionally use varargs parameters instead of arrays
in CompositeSet and CompositeCollection constructor and addComposited method. Thanks to Hollis Waite.

View File

@ -38,6 +38,19 @@ Commons Collections is Java 5.
Users are encouraged to upgrade to this version as, in addition to new
features, this release includes numerous bug fixes.
">
<action issue="COLLECTIONS-499" dev="tn" type="update">
Refactored the test framework for Bag implementations to extend from
"AbstractCollectionTest" by decorating the concrete Bag instance with
a CollectionBag or CollectionSortedBag.
</action>
<action issue="COLLECTIONS-498" dev="tn" type="fix">
"CollectionBag" will now also respect the contract of the decorated bag in case
a null argument is provided to either removeAll or retainAll.
</action>
<action issue="COLLECTIONS-497" dev="tn" type="add">
Added bag decorator "CollectionSortedBag" which decorates a SortedBag to make it
comply with the Collection contract.
</action>
<action issue="COLLECTIONS-496" dev="tn" type="update">
"UnmodifiableBoundedCollection" does now also implement the marker interface "Unmodifiable"
similar as all other unmodifiable decorators.

View File

@ -124,20 +124,23 @@ public final class CollectionBag<E>
@Override
public boolean removeAll(final Collection<?> coll) {
boolean result = false;
if (coll != null) {
boolean result = false;
final Iterator<?> i = coll.iterator();
while (i.hasNext()) {
final Object obj = i.next();
final boolean changed = remove(obj, getCount(obj));
result = result || changed;
}
}
return result;
} else {
return decorated().removeAll(coll);
}
}
@Override
public boolean retainAll(final Collection<?> coll) {
if (coll != null) {
boolean modified = false;
final Iterator<E> e = iterator();
while (e.hasNext()) {
@ -147,6 +150,9 @@ public final class CollectionBag<E>
}
}
return modified;
} else {
return decorated().retainAll(coll);
}
}
//-----------------------------------------------------------------------

View File

@ -98,6 +98,7 @@ have changed.
<center><h3>New Classes</h3></center>
<ul>
<li>CollectionSortedBag - decorates a SortedBag to make it comply with the Collection contract.</li>
<li>CollectionBag - decorates another Bag to make it comply with the Collection contract.</li>
<li>PushbackIterator - supports pushback of elements during iteration. Thanks to Andy Seaborne, Claude Warren.</li>
<li>PeekingIterator - supports one-element lookahead during iteration. Thanks to Andy Seaborne, Claude Warren.</li>
@ -155,6 +156,8 @@ have changed.
<center><h3>Changed classes / methods</h3></center>
<ul>
<li>Refactored the test framework for Bag implementations to extend from "AbstractCollectionTest" by decorating
the concrete Bag instance with a CollectionBag or CollectionSortedBag.</li>
<li>"UnmodifiableBoundedCollection" does now also implement the marker interface "Unmodifiable"
similar as all other unmodifiable decorators.</li>
<li>"UnmodifiableTrie#unmodifiableTrie(Trie)" will not decorate again an already unmodifiable Trie.
@ -197,6 +200,7 @@ have changed.
<center><h3>Bugfixes</h3></center>
<ul>
<li>"CollectionBag" will now also respect the contract of the decorated bag in case a null argument is provided to either removeAll or retainAll.</li>
<li>Fixed collision detection/resolution when calling "CompositeSet#addComposited(...)" with more than one Set as argument.</li>
<li>Fixed conversion of timeout parameters in "PassiveExpiringMap".</li>
<li>ListOrderedMap#putAll(index, Object, Object) does not throw an exception anymore if the map contains null values. Additionally added javadoc clarification on the supported bounds for the index parameter. Thanks to Ning Chen.</li>

View File

@ -19,13 +19,19 @@ package org.apache.commons.collections4.bag;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.ConcurrentModificationException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;
import org.apache.commons.collections4.AbstractObjectTest;
import org.apache.commons.collections4.Bag;
import org.apache.commons.collections4.BulkTest;
import org.apache.commons.collections4.collection.AbstractCollectionTest;
import org.apache.commons.collections4.set.AbstractSetTest;
/**
* Abstract test class for {@link org.apache.commons.collections4.Bag Bag} methods and contracts.
@ -36,14 +42,25 @@ import org.apache.commons.collections4.Bag;
* If your bag fails one of these tests by design,
* you may still use this base set of cases. Simply override the
* test case (method) your bag fails.
* <p>
* <b>Note:</b> The Bag interface does not conform to the Collection interface
* so the generic collection tests from AbstractCollectionTest would normally fail.
* As a work-around since 4.0, a CollectionBag decorator can be used
* to make any Bag implementation comply to the Collection contract.
* <p>
* This abstract test class does wrap the concrete bag implementation
* with such a decorator, see the overridden {@link #resetEmpty()} and
* {@link #resetFull()} methods.
* <p>
* In addition to the generic collection tests (prefix testCollection) inherited
* from AbstractCollectionTest, there are test methods that test the "normal" Bag
* interface (prefix testBag). For Bag specific tests use the {@link #makeObject()} and
* {@link #makeFullCollection()} methods instead of {@link #resetEmpty()} and resetFull(),
* otherwise the collection will be wrapped by a {@link CollectionBag} decorator.
*
* @version $Id$
*/
public abstract class AbstractBagTest<T> extends AbstractObjectTest {
// TODO: this class should really extend from AbstractCollectionTest, but the bag
// implementations currently do not conform to the Collection interface. Once
// those are fixed or at least a strategy is made for resolving the issue, this
// can be changed back to extend TestCollection instead.
public abstract class AbstractBagTest<T> extends AbstractCollectionTest<T> {
/**
* JUnit constructor.
@ -55,6 +72,25 @@ public abstract class AbstractBagTest<T> extends AbstractObjectTest {
}
//-----------------------------------------------------------------------
/**
* Returns an empty {@link ArrayList}.
*/
@Override
public Collection<T> makeConfirmedCollection() {
final ArrayList<T> list = new ArrayList<T>();
return list;
}
/**
* Returns a full collection.
*/
@Override
public Collection<T> makeConfirmedFullCollection() {
final Collection<T> coll = makeConfirmedCollection();
coll.addAll(Arrays.asList(getFullElements()));
return coll;
}
/**
* Return a new, empty bag to used for testing.
*
@ -63,9 +99,48 @@ public abstract class AbstractBagTest<T> extends AbstractObjectTest {
@Override
public abstract Bag<T> makeObject();
/**
* {@inheritDoc}
*/
@Override
public Bag<T> makeFullCollection() {
final Bag<T> bag = makeObject();
bag.addAll(Arrays.asList(getFullElements()));
return bag;
}
//-----------------------------------------------------------------------
@Override
public void resetEmpty() {
this.setCollection(CollectionBag.collectionBag(makeObject()));
this.setConfirmed(makeConfirmedCollection());
}
@Override
public void resetFull() {
this.setCollection(CollectionBag.collectionBag(makeFullCollection()));
this.setConfirmed(makeConfirmedFullCollection());
}
//-----------------------------------------------------------------------
/**
* Returns the {@link #collection} field cast to a {@link Bag}.
*
* @return the collection field as a Bag
*/
@Override
public Bag<T> getCollection() {
return (Bag<T>) super.getCollection();
}
//-----------------------------------------------------------------------
@SuppressWarnings("unchecked")
public void testBagAdd() {
if (!isAddSupported()) {
return;
}
final Bag<T> bag = makeObject();
bag.add((T) "A");
assertTrue("Should contain 'A'", bag.contains("A"));
@ -82,6 +157,11 @@ public abstract class AbstractBagTest<T> extends AbstractObjectTest {
public void testBagEqualsSelf() {
final Bag<T> bag = makeObject();
assertTrue(bag.equals(bag));
if (!isAddSupported()) {
return;
}
bag.add((T) "elt");
assertTrue(bag.equals(bag));
bag.add((T) "elt"); // again
@ -91,7 +171,11 @@ public abstract class AbstractBagTest<T> extends AbstractObjectTest {
}
@SuppressWarnings("unchecked")
public void testRemove() {
public void testBagRemove() {
if (!isRemoveSupported()) {
return;
}
final Bag<T> bag = makeObject();
bag.add((T) "A");
assertEquals("Should have count of 1", 1, bag.getCount("A"));
@ -111,7 +195,11 @@ public abstract class AbstractBagTest<T> extends AbstractObjectTest {
}
@SuppressWarnings("unchecked")
public void testRemoveAll() {
public void testBagRemoveAll() {
if (!isRemoveSupported()) {
return;
}
final Bag<T> bag = makeObject();
bag.add((T) "A", 2);
assertEquals("Should have count of 2", 2, bag.getCount("A"));
@ -129,7 +217,11 @@ public abstract class AbstractBagTest<T> extends AbstractObjectTest {
}
@SuppressWarnings("unchecked")
public void testContains() {
public void testBagContains() {
if (!isAddSupported()) {
return;
}
final Bag<T> bag = makeObject();
assertEquals("Bag does not have at least 1 'A'", false, bag.contains("A"));
@ -149,7 +241,11 @@ public abstract class AbstractBagTest<T> extends AbstractObjectTest {
}
@SuppressWarnings("unchecked")
public void testContainsAll() {
public void testBagContainsAll() {
if (!isAddSupported()) {
return;
}
final Bag<T> bag = makeObject();
final List<String> known = new ArrayList<String>();
final List<String> known1A = new ArrayList<String>();
@ -199,7 +295,11 @@ public abstract class AbstractBagTest<T> extends AbstractObjectTest {
}
@SuppressWarnings("unchecked")
public void testSize() {
public void testBagSize() {
if (!isAddSupported()) {
return;
}
final Bag<T> bag = makeObject();
assertEquals("Should have 0 total items", 0, bag.size());
bag.add((T) "A");
@ -220,7 +320,11 @@ public abstract class AbstractBagTest<T> extends AbstractObjectTest {
}
@SuppressWarnings("unchecked")
public void testRetainAll() {
public void testBagRetainAll() {
if (!isAddSupported()) {
return;
}
final Bag<T> bag = makeObject();
bag.add((T) "A");
bag.add((T) "A");
@ -236,7 +340,11 @@ public abstract class AbstractBagTest<T> extends AbstractObjectTest {
}
@SuppressWarnings("unchecked")
public void testIterator() {
public void testBagIterator() {
if (!isAddSupported()) {
return;
}
final Bag<T> bag = makeObject();
bag.add((T) "A");
bag.add((T) "A");
@ -263,7 +371,11 @@ public abstract class AbstractBagTest<T> extends AbstractObjectTest {
}
@SuppressWarnings("unchecked")
public void testIteratorFail() {
public void testBagIteratorFail() {
if (!isAddSupported()) {
return;
}
final Bag<T> bag = makeObject();
bag.add((T) "A");
bag.add((T) "A");
@ -280,7 +392,11 @@ public abstract class AbstractBagTest<T> extends AbstractObjectTest {
}
@SuppressWarnings("unchecked")
public void testIteratorFailNoMore() {
public void testBagIteratorFailNoMore() {
if (!isAddSupported()) {
return;
}
final Bag<T> bag = makeObject();
bag.add((T) "A");
bag.add((T) "A");
@ -298,7 +414,11 @@ public abstract class AbstractBagTest<T> extends AbstractObjectTest {
}
@SuppressWarnings("unchecked")
public void testIteratorFailDoubleRemove() {
public void testBagIteratorFailDoubleRemove() {
if (!isAddSupported()) {
return;
}
final Bag<T> bag = makeObject();
bag.add((T) "A");
bag.add((T) "A");
@ -322,7 +442,11 @@ public abstract class AbstractBagTest<T> extends AbstractObjectTest {
}
@SuppressWarnings("unchecked")
public void testIteratorRemoveProtectsInvariants() {
public void testBagIteratorRemoveProtectsInvariants() {
if (!isAddSupported()) {
return;
}
final Bag<T> bag = makeObject();
bag.add((T) "A");
bag.add((T) "A");
@ -344,7 +468,11 @@ public abstract class AbstractBagTest<T> extends AbstractObjectTest {
}
@SuppressWarnings("unchecked")
public void testToArray() {
public void testBagToArray() {
if (!isAddSupported()) {
return;
}
final Bag<T> bag = makeObject();
bag.add((T) "A");
bag.add((T) "A");
@ -364,7 +492,11 @@ public abstract class AbstractBagTest<T> extends AbstractObjectTest {
}
@SuppressWarnings("unchecked")
public void testToArrayPopulate() {
public void testBagToArrayPopulate() {
if (!isAddSupported()) {
return;
}
final Bag<T> bag = makeObject();
bag.add((T) "A");
bag.add((T) "A");
@ -385,7 +517,11 @@ public abstract class AbstractBagTest<T> extends AbstractObjectTest {
//-----------------------------------------------------------------------
@SuppressWarnings("unchecked")
public void testEquals() {
public void testBagEquals() {
if (!isAddSupported()) {
return;
}
final Bag<T> bag = makeObject();
final Bag<T> bag2 = makeObject();
assertEquals(true, bag.equals(bag2));
@ -405,7 +541,11 @@ public abstract class AbstractBagTest<T> extends AbstractObjectTest {
}
@SuppressWarnings("unchecked")
public void testEqualsHashBag() {
public void testBagEqualsHashBag() {
if (!isAddSupported()) {
return;
}
final Bag<T> bag = makeObject();
final Bag<T> bag2 = new HashBag<T>();
assertEquals(true, bag.equals(bag2));
@ -425,7 +565,11 @@ public abstract class AbstractBagTest<T> extends AbstractObjectTest {
}
@SuppressWarnings("unchecked")
public void testHashCode() {
public void testBagHashCode() {
if (!isAddSupported()) {
return;
}
final Bag<T> bag = makeObject();
final Bag<T> bag2 = makeObject();
assertEquals(0, bag.hashCode());
@ -452,39 +596,86 @@ public abstract class AbstractBagTest<T> extends AbstractObjectTest {
}
//-----------------------------------------------------------------------
public void testEmptyBagSerialization() throws IOException, ClassNotFoundException {
final Bag<T> bag = makeObject();
if (!(bag instanceof Serializable && isTestSerialization())) {
return;
/**
* Bulk test {@link Bag#uniqueSet()}. This method runs through all of
* the tests in {@link AbstractSetTest}.
* After modification operations, {@link #verify()} is invoked to ensure
* that the bag and the other collection views are still valid.
*
* @return a {@link AbstractSetTest} instance for testing the bag's unique set
*/
public BulkTest bulkTestBagUniqueSet() {
return new TestBagUniqueSet();
}
final byte[] objekt = writeExternalFormToBytes((Serializable) bag);
final Bag<?> bag2 = (Bag<?>) readExternalFormFromBytes(objekt);
assertEquals("Bag should be empty",0, bag.size());
assertEquals("Bag should be empty",0, bag2.size());
public class TestBagUniqueSet extends AbstractSetTest<T> {
public TestBagUniqueSet() {
super("");
}
@SuppressWarnings("unchecked")
public void testFullBagSerialization() throws IOException, ClassNotFoundException {
final Bag<T> bag = makeObject();
bag.add((T) "A");
bag.add((T) "A");
bag.add((T) "B");
bag.add((T) "B");
bag.add((T) "C");
final int size = bag.size();
if (!(bag instanceof Serializable && isTestSerialization())) {
return;
@Override
public T[] getFullElements() {
return AbstractBagTest.this.getFullElements();
}
final byte[] objekt = writeExternalFormToBytes((Serializable) bag);
final Bag<?> bag2 = (Bag<?>) readExternalFormFromBytes(objekt);
assertEquals("Bag should be same size", size, bag.size());
assertEquals("Bag should be same size", size, bag2.size());
@Override
public T[] getOtherElements() {
return AbstractBagTest.this.getOtherElements();
}
@Override
public Set<T> makeObject() {
return AbstractBagTest.this.makeObject().uniqueSet();
}
@Override
public Set<T> makeFullCollection() {
return AbstractBagTest.this.makeFullCollection().uniqueSet();
}
@Override
public boolean isNullSupported() {
return AbstractBagTest.this.isNullSupported();
}
@Override
public boolean isAddSupported() {
return false;
}
@Override
public boolean isRemoveSupported() {
return false;
}
@Override
public boolean isTestSerialization() {
return false;
}
@Override
public void resetEmpty() {
AbstractBagTest.this.resetEmpty();
TestBagUniqueSet.this.setCollection(AbstractBagTest.this.getCollection().uniqueSet());
TestBagUniqueSet.this.setConfirmed(new HashSet<T>(AbstractBagTest.this.getConfirmed()));
}
@Override
public void resetFull() {
AbstractBagTest.this.resetFull();
TestBagUniqueSet.this.setCollection(AbstractBagTest.this.getCollection().uniqueSet());
TestBagUniqueSet.this.setConfirmed(new HashSet<T>(AbstractBagTest.this.getConfirmed()));
}
@Override
public void verify() {
super.verify();
}
}
//-----------------------------------------------------------------------
/**
* Compare the current serialized form of the Bag
* against the canonical version in SVN.
@ -503,15 +694,9 @@ public abstract class AbstractBagTest<T> extends AbstractObjectTest {
* Compare the current serialized form of the Bag
* against the canonical version in SVN.
*/
@SuppressWarnings("unchecked")
public void testFullBagCompatibility() throws IOException, ClassNotFoundException {
// test to make sure the canonical form has been preserved
final Bag<T> bag = makeObject();
bag.add((T) "A");
bag.add((T) "A");
bag.add((T) "B");
bag.add((T) "B");
bag.add((T) "C");
final Bag<T> bag = makeFullCollection();
if (bag instanceof Serializable && !skipSerializedCanonicalTests() && isTestSerialization()) {
final Bag<?> bag2 = (Bag<?>) readExternalFormFromDisk(getCanonicalFullCollectionName(bag));
assertEquals("Bag is the right size",bag.size(), bag2.size());

View File

@ -16,6 +16,8 @@
*/
package org.apache.commons.collections4.bag;
import java.util.Iterator;
import org.apache.commons.collections4.SortedBag;
/**
@ -32,11 +34,126 @@ public abstract class AbstractSortedBagTest<T> extends AbstractBagTest<T> {
super(testName);
}
//-----------------------------------------------------------------------
/**
* Verification extension, will check the order of elements,
* the sets should already be verified equal.
*/
@Override
public void verify() {
super.verify();
// Check that iterator returns elements in order and first() and last()
// are consistent
final Iterator<T> colliter = getCollection().iterator();
final Iterator<T> confiter = getConfirmed().iterator();
T first = null;
T last = null;
while (colliter.hasNext()) {
if (first == null) {
first = colliter.next();
last = first;
} else {
last = colliter.next();
}
assertEquals("Element appears to be out of order.", last, confiter.next());
}
if (getCollection().size() > 0) {
assertEquals("Incorrect element returned by first().", first,
getCollection().first());
assertEquals("Incorrect element returned by last().", last,
getCollection().last());
}
}
//-----------------------------------------------------------------------
/**
* Overridden because SortedBags don't allow null elements (normally).
* @return false
*/
@Override
public boolean isNullSupported() {
return false;
}
/**
* {@inheritDoc}
*/
@Override
public abstract SortedBag<T> makeObject();
/**
* {@inheritDoc}
*/
@Override
public SortedBag<T> makeFullCollection() {
return (SortedBag<T>) super.makeFullCollection();
}
/**
* Returns an empty {@link TreeBag} for use in modification testing.
*
* @return a confirmed empty collection
*/
@Override
public SortedBag<T> makeConfirmedCollection() {
return new TreeBag<T>();
}
//-----------------------------------------------------------------------
@Override
public void resetEmpty() {
this.setCollection(CollectionSortedBag.collectionSortedBag(makeObject()));
this.setConfirmed(makeConfirmedCollection());
}
@Override
public void resetFull() {
this.setCollection(CollectionSortedBag.collectionSortedBag(makeFullCollection()));
this.setConfirmed(makeConfirmedFullCollection());
}
//-----------------------------------------------------------------------
/**
* Override to return comparable objects.
*/
@Override
@SuppressWarnings("unchecked")
public T[] getFullNonNullElements() {
final Object[] elements = new Object[30];
for (int i = 0; i < 30; i++) {
elements[i] = Integer.valueOf(i + i + 1);
}
return (T[]) elements;
}
/**
* Override to return comparable objects.
*/
@Override
@SuppressWarnings("unchecked")
public T[] getOtherNonNullElements() {
final Object[] elements = new Object[30];
for (int i = 0; i < 30; i++) {
elements[i] = Integer.valueOf(i + i + 2);
}
return (T[]) elements;
}
//-----------------------------------------------------------------------
/**
* Returns the {@link #collection} field cast to a {@link SortedBag}.
*
* @return the collection field as a SortedBag
*/
@Override
public SortedBag<T> getCollection() {
return (SortedBag<T>) super.getCollection();
}
//-----------------------------------------------------------------------
// TODO: Add the SortedBag tests!
}

View File

@ -21,10 +21,6 @@ import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import org.apache.commons.collections4.Bag;
import org.apache.commons.collections4.bag.HashBag;
@ -32,6 +28,9 @@ import org.apache.commons.collections4.collection.AbstractCollectionTest;
/**
* Test class for {@link CollectionBag}.
* <p>
* Note: This test is mainly for serialization support, the CollectionBag decorator
* is extensively used and tested in AbstractBagTest.
*
* @version $Id$
* @since 4.0
@ -82,439 +81,13 @@ public class CollectionBagTest<T> extends AbstractCollectionTest<T> {
}
// public void testCreate() throws Exception {
// Bag bag = makeObject();
// writeExternalFormToDisk((java.io.Serializable) bag, "src/test/resources/data/test/CollectionBag.emptyCollection.version4.obj");
// bag = makeObject();
// bag.add("A");
// bag.add("A");
// bag.add("B");
// bag.add("B");
// bag.add("C");
// writeExternalFormToDisk((java.io.Serializable) bag, "src/test/resources/data/test/CollectionBag.fullCollection.version4.obj");
// resetEmpty();
// writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/CollectionBag.emptyCollection.version4.obj");
// resetFull();
// writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/CollectionBag.fullCollection.version4.obj");
// }
//-----------------------------------------------------------------------
@SuppressWarnings("unchecked")
public void testBagAdd() {
final Bag<T> bag = makeObject();
bag.add((T) "A");
assertTrue("Should contain 'A'", bag.contains("A"));
assertEquals("Should have count of 1", 1, bag.getCount("A"));
bag.add((T) "A");
assertTrue("Should contain 'A'", bag.contains("A"));
assertEquals("Should have count of 2", 2, bag.getCount("A"));
bag.add((T) "B");
assertTrue(bag.contains("A"));
assertTrue(bag.contains("B"));
}
@SuppressWarnings("unchecked")
public void testBagEqualsSelf() {
final Bag<T> bag = makeObject();
assertTrue(bag.equals(bag));
bag.add((T) "elt");
assertTrue(bag.equals(bag));
bag.add((T) "elt"); // again
assertTrue(bag.equals(bag));
bag.add((T) "elt2");
assertTrue(bag.equals(bag));
}
@SuppressWarnings("unchecked")
public void testRemove() {
final Bag<T> bag = makeObject();
bag.add((T) "A");
assertEquals("Should have count of 1", 1, bag.getCount("A"));
bag.remove("A");
assertEquals("Should have count of 0", 0, bag.getCount("A"));
bag.add((T) "A");
bag.add((T) "A");
bag.add((T) "A");
bag.add((T) "A");
assertEquals("Should have count of 4", 4, bag.getCount("A"));
bag.remove("A", 0);
assertEquals("Should have count of 4", 4, bag.getCount("A"));
bag.remove("A", 2);
assertEquals("Should have count of 2", 2, bag.getCount("A"));
bag.remove("A");
assertEquals("Should have count of 1", 1, bag.getCount("A"));
}
@SuppressWarnings("unchecked")
public void testRemoveAll() {
final Bag<T> bag = makeObject();
bag.add((T) "A", 2);
assertEquals("Should have count of 2", 2, bag.getCount("A"));
bag.add((T) "B");
bag.add((T) "C");
assertEquals("Should have count of 4", 4, bag.size());
final List<String> delete = new ArrayList<String>();
delete.add("A");
delete.add("B");
bag.removeAll(delete);
assertEquals("Should have count of 0", 0, bag.getCount("A"));
assertEquals("Should have count of 0", 0, bag.getCount("B"));
assertEquals("Should have count of 1", 1, bag.getCount("C"));
assertEquals("Should have count of 1", 1, bag.size());
}
@SuppressWarnings("unchecked")
public void testContains() {
final Bag<T> bag = makeObject();
assertEquals("Bag does not have at least 1 'A'", false, bag.contains("A"));
assertEquals("Bag does not have at least 1 'B'", false, bag.contains("B"));
bag.add((T) "A"); // bag 1A
assertEquals("Bag has at least 1 'A'", true, bag.contains("A"));
assertEquals("Bag does not have at least 1 'B'", false, bag.contains("B"));
bag.add((T) "A"); // bag 2A
assertEquals("Bag has at least 1 'A'", true, bag.contains("A"));
assertEquals("Bag does not have at least 1 'B'", false, bag.contains("B"));
bag.add((T) "B"); // bag 2A,1B
assertEquals("Bag has at least 1 'A'", true, bag.contains("A"));
assertEquals("Bag has at least 1 'B'", true, bag.contains("B"));
}
@SuppressWarnings("unchecked")
public void testContainsAll() {
final Bag<T> bag = makeObject();
final List<String> known = new ArrayList<String>();
final List<String> known1A = new ArrayList<String>();
known1A.add("A");
final List<String> known2A = new ArrayList<String>();
known2A.add("A");
known2A.add("A");
final List<String> known1B = new ArrayList<String>();
known1B.add("B");
final List<String> known1A1B = new ArrayList<String>();
known1A1B.add("A");
known1A1B.add("B");
assertEquals("Bag containsAll of empty", true, bag.containsAll(known));
assertEquals("Bag does not containsAll of 1 'A'", false, bag.containsAll(known1A));
assertEquals("Bag does not containsAll of 2 'A'", false, bag.containsAll(known2A));
assertEquals("Bag does not containsAll of 1 'B'", false, bag.containsAll(known1B));
assertEquals("Bag does not containsAll of 1 'A' 1 'B'", false, bag.containsAll(known1A1B));
bag.add((T) "A"); // bag 1A
assertEquals("Bag containsAll of empty", true, bag.containsAll(known));
assertEquals("Bag containsAll of 1 'A'", true, bag.containsAll(known1A));
assertEquals("Bag does containsAll of 2 'A'", true, bag.containsAll(known2A));
assertEquals("Bag does not containsAll of 1 'B'", false, bag.containsAll(known1B));
assertEquals("Bag does not containsAll of 1 'A' 1 'B'", false, bag.containsAll(known1A1B));
bag.add((T) "A"); // bag 2A
assertEquals("Bag containsAll of empty", true, bag.containsAll(known));
assertEquals("Bag containsAll of 1 'A'", true, bag.containsAll(known1A));
assertEquals("Bag containsAll of 2 'A'", true, bag.containsAll(known2A));
assertEquals("Bag does not containsAll of 1 'B'", false, bag.containsAll(known1B));
assertEquals("Bag does not containsAll of 1 'A' 1 'B'", false, bag.containsAll(known1A1B));
bag.add((T) "A"); // bag 3A
assertEquals("Bag containsAll of empty", true, bag.containsAll(known));
assertEquals("Bag containsAll of 1 'A'", true, bag.containsAll(known1A));
assertEquals("Bag containsAll of 2 'A'", true, bag.containsAll(known2A));
assertEquals("Bag does not containsAll of 1 'B'", false, bag.containsAll(known1B));
assertEquals("Bag does not containsAll of 1 'A' 1 'B'", false, bag.containsAll(known1A1B));
bag.add((T) "B"); // bag 3A1B
assertEquals("Bag containsAll of empty", true, bag.containsAll(known));
assertEquals("Bag containsAll of 1 'A'", true, bag.containsAll(known1A));
assertEquals("Bag containsAll of 2 'A'", true, bag.containsAll(known2A));
assertEquals("Bag containsAll of 1 'B'", true, bag.containsAll(known1B));
assertEquals("Bag containsAll of 1 'A' 1 'B'", true, bag.containsAll(known1A1B));
}
@SuppressWarnings("unchecked")
public void testSize() {
final Bag<T> bag = makeObject();
assertEquals("Should have 0 total items", 0, bag.size());
bag.add((T) "A");
assertEquals("Should have 1 total items", 1, bag.size());
bag.add((T) "A");
assertEquals("Should have 2 total items", 2, bag.size());
bag.add((T) "A");
assertEquals("Should have 3 total items", 3, bag.size());
bag.add((T) "B");
assertEquals("Should have 4 total items", 4, bag.size());
bag.add((T) "B");
assertEquals("Should have 5 total items", 5, bag.size());
bag.remove("A", 2);
assertEquals("Should have 1 'A'", 1, bag.getCount("A"));
assertEquals("Should have 3 total items", 3, bag.size());
// this should only remove 1 occurrence of B
bag.remove("B");
assertEquals("Should have 2 total item", 2, bag.size());
}
@SuppressWarnings("unchecked")
public void testRetainAll() {
final Bag<T> bag = makeObject();
bag.add((T) "A");
bag.add((T) "A");
bag.add((T) "A");
bag.add((T) "B");
bag.add((T) "B");
bag.add((T) "C");
final List<String> retains = new ArrayList<String>();
retains.add("B");
retains.add("C");
bag.retainAll(retains);
assertEquals("Should have 3 total items", 3, bag.size());
}
@SuppressWarnings("unchecked")
public void testIterator() {
final Bag<T> bag = makeObject();
bag.add((T) "A");
bag.add((T) "A");
bag.add((T) "B");
assertEquals("Bag should have 3 items", 3, bag.size());
final Iterator<T> i = bag.iterator();
boolean foundA = false;
while (i.hasNext()) {
final String element = (String) i.next();
// ignore the first A, remove the second via Iterator.remove()
if (element.equals("A")) {
if (foundA == false) {
foundA = true;
} else {
i.remove();
}
}
}
assertTrue("Bag should still contain 'A'", bag.contains("A"));
assertEquals("Bag should have 2 items", 2, bag.size());
assertEquals("Bag should have 1 'A'", 1, bag.getCount("A"));
}
@SuppressWarnings("unchecked")
public void testIteratorFail() {
final Bag<T> bag = makeObject();
bag.add((T) "A");
bag.add((T) "A");
bag.add((T) "B");
final Iterator<T> it = bag.iterator();
it.next();
bag.remove("A");
try {
it.next();
fail("Should throw ConcurrentModificationException");
} catch (final ConcurrentModificationException e) {
// expected
}
}
@SuppressWarnings("unchecked")
public void testIteratorFailNoMore() {
final Bag<T> bag = makeObject();
bag.add((T) "A");
bag.add((T) "A");
bag.add((T) "B");
final Iterator<T> it = bag.iterator();
it.next();
it.next();
it.next();
try {
it.next();
fail("Should throw NoSuchElementException");
} catch (final NoSuchElementException ex) {
// expected
}
}
@SuppressWarnings("unchecked")
public void testIteratorFailDoubleRemove() {
final Bag<T> bag = makeObject();
bag.add((T) "A");
bag.add((T) "A");
bag.add((T) "B");
final Iterator<T> it = bag.iterator();
it.next();
it.next();
assertEquals(3, bag.size());
it.remove();
assertEquals(2, bag.size());
try {
it.remove();
fail("Should throw IllegalStateException");
} catch (final IllegalStateException ex) {
// expected
}
assertEquals(2, bag.size());
it.next();
it.remove();
assertEquals(1, bag.size());
}
@SuppressWarnings("unchecked")
public void testIteratorRemoveProtectsInvariants() {
final Bag<T> bag = makeObject();
bag.add((T) "A");
bag.add((T) "A");
assertEquals(2, bag.size());
final Iterator<T> it = bag.iterator();
assertEquals("A", it.next());
assertEquals(true, it.hasNext());
it.remove();
assertEquals(1, bag.size());
assertEquals(true, it.hasNext());
assertEquals("A", it.next());
assertEquals(false, it.hasNext());
it.remove();
assertEquals(0, bag.size());
assertEquals(false, it.hasNext());
final Iterator<T> it2 = bag.iterator();
assertEquals(false, it2.hasNext());
}
@SuppressWarnings("unchecked")
public void testToArray() {
final Bag<T> bag = makeObject();
bag.add((T) "A");
bag.add((T) "A");
bag.add((T) "B");
bag.add((T) "B");
bag.add((T) "C");
final Object[] array = bag.toArray();
int a = 0, b = 0, c = 0;
for (final Object element : array) {
a += element.equals("A") ? 1 : 0;
b += element.equals("B") ? 1 : 0;
c += element.equals("C") ? 1 : 0;
}
assertEquals(2, a);
assertEquals(2, b);
assertEquals(1, c);
}
@SuppressWarnings("unchecked")
public void testToArrayPopulate() {
final Bag<T> bag = makeObject();
bag.add((T) "A");
bag.add((T) "A");
bag.add((T) "B");
bag.add((T) "B");
bag.add((T) "C");
final String[] array = bag.toArray(new String[0]);
int a = 0, b = 0, c = 0;
for (final String element : array) {
a += element.equals("A") ? 1 : 0;
b += element.equals("B") ? 1 : 0;
c += element.equals("C") ? 1 : 0;
}
assertEquals(2, a);
assertEquals(2, b);
assertEquals(1, c);
}
//-----------------------------------------------------------------------
@SuppressWarnings("unchecked")
public void testEquals() {
final Bag<T> bag = makeObject();
final Bag<T> bag2 = makeObject();
assertEquals(true, bag.equals(bag2));
bag.add((T) "A");
assertEquals(false, bag.equals(bag2));
bag2.add((T) "A");
assertEquals(true, bag.equals(bag2));
bag.add((T) "A");
bag.add((T) "B");
bag.add((T) "B");
bag.add((T) "C");
bag2.add((T) "A");
bag2.add((T) "B");
bag2.add((T) "B");
bag2.add((T) "C");
assertEquals(true, bag.equals(bag2));
}
@SuppressWarnings("unchecked")
public void testEqualsHashBag() {
final Bag<T> bag = makeObject();
final Bag<T> bag2 = new HashBag<T>();
assertEquals(true, bag.equals(bag2));
bag.add((T) "A");
assertEquals(false, bag.equals(bag2));
bag2.add((T) "A");
assertEquals(true, bag.equals(bag2));
bag.add((T) "A");
bag.add((T) "B");
bag.add((T) "B");
bag.add((T) "C");
bag2.add((T) "A");
bag2.add((T) "B");
bag2.add((T) "B");
bag2.add((T) "C");
assertEquals(true, bag.equals(bag2));
}
@SuppressWarnings("unchecked")
public void testHashCode() {
final Bag<T> bag = makeObject();
final Bag<T> bag2 = makeObject();
assertEquals(0, bag.hashCode());
assertEquals(0, bag2.hashCode());
assertEquals(bag.hashCode(), bag2.hashCode());
bag.add((T) "A");
bag.add((T) "A");
bag.add((T) "B");
bag.add((T) "B");
bag.add((T) "C");
bag2.add((T) "A");
bag2.add((T) "A");
bag2.add((T) "B");
bag2.add((T) "B");
bag2.add((T) "C");
assertEquals(bag.hashCode(), bag2.hashCode());
int total = 0;
total += "A".hashCode() ^ 2;
total += "B".hashCode() ^ 2;
total += "C".hashCode() ^ 1;
assertEquals(total, bag.hashCode());
assertEquals(total, bag2.hashCode());
}
//-----------------------------------------------------------------------
public void testEmptyBagSerialization() throws IOException, ClassNotFoundException {
final Bag<T> bag = makeObject();
if (!(bag instanceof Serializable && isTestSerialization())) {
return;
}
final byte[] objekt = writeExternalFormToBytes((Serializable) bag);
final Bag<?> bag2 = (Bag<?>) readExternalFormFromBytes(objekt);
assertEquals("Bag should be empty",0, bag.size());
assertEquals("Bag should be empty",0, bag2.size());
}
@SuppressWarnings("unchecked")
public void testFullBagSerialization() throws IOException, ClassNotFoundException {
final Bag<T> bag = makeObject();
bag.add((T) "A");
bag.add((T) "A");
bag.add((T) "B");
bag.add((T) "B");
bag.add((T) "C");
final int size = bag.size();
if (!(bag instanceof Serializable && isTestSerialization())) {
return;
}
final byte[] objekt = writeExternalFormToBytes((Serializable) bag);
final Bag<?> bag2 = (Bag<?>) readExternalFormFromBytes(objekt);
assertEquals("Bag should be same size", size, bag.size());
assertEquals("Bag should be same size", size, bag2.size());
}
/**
* Compare the current serialized form of the Bag
@ -534,15 +107,9 @@ public class CollectionBagTest<T> extends AbstractCollectionTest<T> {
* Compare the current serialized form of the Bag
* against the canonical version in SVN.
*/
@SuppressWarnings("unchecked")
public void testFullBagCompatibility() throws IOException, ClassNotFoundException {
// test to make sure the canonical form has been preserved
final Bag<T> bag = makeObject();
bag.add((T) "A");
bag.add((T) "A");
bag.add((T) "B");
bag.add((T) "B");
bag.add((T) "C");
final Bag<T> bag = (Bag<T>) makeFullCollection();
if (bag instanceof Serializable && !skipSerializedCanonicalTests() && isTestSerialization()) {
final Bag<?> bag2 = (Bag<?>) readExternalFormFromDisk(getCanonicalFullCollectionName(bag));
assertEquals("Bag is the right size",bag.size(), bag2.size());

View File

@ -0,0 +1,159 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.collections4.bag;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import org.apache.commons.collections4.Bag;
import org.apache.commons.collections4.SortedBag;
import org.apache.commons.collections4.collection.AbstractCollectionTest;
/**
* Test class for {@link CollectionSortedBag}.
* <p>
* Note: This test is mainly for serialization support, the CollectionSortedBag decorator
* is extensively used and tested in AbstractSortedBagTest.
*
* @version $Id$
* @since 4.0
*/
public class CollectionSortedBagTest<T> extends AbstractCollectionTest<T> {
/**
* JUnit constructor.
*
* @param testName the test class name
*/
public CollectionSortedBagTest(final String testName) {
super(testName);
}
//-----------------------------------------------------------------------
/**
* Overridden because SortedBags don't allow null elements (normally).
* @return false
*/
@Override
public boolean isNullSupported() {
return false;
}
@Override
public Bag<T> makeObject() {
return CollectionSortedBag.collectionSortedBag(new TreeBag<T>());
}
/**
* Returns an empty List for use in modification testing.
*
* @return a confirmed empty collection
*/
@Override
public Collection<T> makeConfirmedCollection() {
return new ArrayList<T>();
}
/**
* Returns a full Set for use in modification testing.
*
* @return a confirmed full collection
*/
@Override
public Collection<T> makeConfirmedFullCollection() {
final Collection<T> set = makeConfirmedCollection();
set.addAll(Arrays.asList(getFullElements()));
return set;
}
//-----------------------------------------------------------------------
/**
* Override to return comparable objects.
*/
@Override
@SuppressWarnings("unchecked")
public T[] getFullNonNullElements() {
final Object[] elements = new Object[30];
for (int i = 0; i < 30; i++) {
elements[i] = Integer.valueOf(i + i + 1);
}
return (T[]) elements;
}
/**
* Override to return comparable objects.
*/
@Override
@SuppressWarnings("unchecked")
public T[] getOtherNonNullElements() {
final Object[] elements = new Object[30];
for (int i = 0; i < 30; i++) {
elements[i] = Integer.valueOf(i + i + 2);
}
return (T[]) elements;
}
//-----------------------------------------------------------------------
@Override
public String getCompatibilityVersion() {
return "4";
}
// public void testCreate() throws Exception {
// resetEmpty();
// writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/CollectionSortedBag.emptyCollection.version4.obj");
// resetFull();
// writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/CollectionSortedBag.fullCollection.version4.obj");
// }
//-----------------------------------------------------------------------
/**
* Compare the current serialized form of the Bag
* against the canonical version in SVN.
*/
public void testEmptyBagCompatibility() throws IOException, ClassNotFoundException {
// test to make sure the canonical form has been preserved
final Bag<T> bag = makeObject();
if (bag instanceof Serializable && !skipSerializedCanonicalTests() && isTestSerialization()) {
final Bag<?> bag2 = (Bag<?>) readExternalFormFromDisk(getCanonicalEmptyCollectionName(bag));
assertTrue("Bag is empty",bag2.size() == 0);
assertEquals(bag, bag2);
}
}
/**
* Compare the current serialized form of the Bag
* against the canonical version in SVN.
*/
public void testFullBagCompatibility() throws IOException, ClassNotFoundException {
// test to make sure the canonical form has been preserved
final SortedBag<T> bag = (SortedBag<T>) makeFullCollection();
if (bag instanceof Serializable && !skipSerializedCanonicalTests() && isTestSerialization()) {
final SortedBag<?> bag2 = (SortedBag<?>) readExternalFormFromDisk(getCanonicalFullCollectionName(bag));
assertEquals("Bag is the right size", bag.size(), bag2.size());
assertEquals(bag, bag2);
}
}
}

View File

@ -16,7 +16,10 @@
*/
package org.apache.commons.collections4.bag;
import junit.framework.Test;
import org.apache.commons.collections4.Bag;
import org.apache.commons.collections4.BulkTest;
/**
* Extension of {@link AbstractBagTest} for exercising the {@link HashBag}
@ -30,6 +33,12 @@ public class HashBagTest<T> extends AbstractBagTest<T> {
super(testName);
}
public static Test suite() {
return BulkTest.makeSuite(HashBagTest.class);
}
//-----------------------------------------------------------------------
@Override
public Bag<T> makeObject() {
return new HashBag<T>();
@ -41,14 +50,9 @@ public class HashBagTest<T> extends AbstractBagTest<T> {
}
// public void testCreate() throws Exception {
// Bag bag = makeObject();
// Bag<T> bag = makeObject();
// writeExternalFormToDisk((java.io.Serializable) bag, "src/test/resources/data/test/HashBag.emptyCollection.version4.obj");
// bag = makeObject();
// bag.add("A");
// bag.add("A");
// bag.add("B");
// bag.add("B");
// bag.add("C");
// bag = makeFullCollection();
// writeExternalFormToDisk((java.io.Serializable) bag, "src/test/resources/data/test/HashBag.fullCollection.version4.obj");
// }
}

View File

@ -18,7 +18,10 @@ package org.apache.commons.collections4.bag;
import java.util.Set;
import junit.framework.Test;
import org.apache.commons.collections4.Bag;
import org.apache.commons.collections4.BulkTest;
import org.apache.commons.collections4.Predicate;
import org.apache.commons.collections4.functors.TruePredicate;
@ -35,6 +38,10 @@ public class PredicatedBagTest<T> extends AbstractBagTest<T> {
super(testName);
}
public static Test suite() {
return BulkTest.makeSuite(PredicatedBagTest.class);
}
//--------------------------------------------------------------------------
protected Predicate<T> stringPredicate() {
@ -121,14 +128,9 @@ public class PredicatedBagTest<T> extends AbstractBagTest<T> {
}
// public void testCreate() throws Exception {
// Bag bag = makeObject();
// Bag<T> bag = makeObject();
// writeExternalFormToDisk((java.io.Serializable) bag, "src/test/resources/data/test/PredicatedBag.emptyCollection.version4.obj");
// bag = makeObject();
// bag.add("A");
// bag.add("A");
// bag.add("B");
// bag.add("B");
// bag.add("C");
// bag = makeFullCollection();
// writeExternalFormToDisk((java.io.Serializable) bag, "src/test/resources/data/test/PredicatedBag.fullCollection.version4.obj");
// }

View File

@ -18,6 +18,9 @@ package org.apache.commons.collections4.bag;
import java.util.Comparator;
import junit.framework.Test;
import org.apache.commons.collections4.BulkTest;
import org.apache.commons.collections4.Predicate;
import org.apache.commons.collections4.SortedBag;
import org.apache.commons.collections4.functors.TruePredicate;
@ -37,6 +40,10 @@ public class PredicatedSortedBagTest<T> extends AbstractSortedBagTest<T> {
super(testName);
}
public static Test suite() {
return BulkTest.makeSuite(PredicatedSortedBagTest.class);
}
//--------------------------------------------------------------------------
protected Predicate<T> stringPredicate() {
@ -98,14 +105,9 @@ public class PredicatedSortedBagTest<T> extends AbstractSortedBagTest<T> {
}
// public void testCreate() throws Exception {
// org.apache.commons.collections4.Bag bag = makeObject();
// Bag<T> bag = makeObject();
// writeExternalFormToDisk((java.io.Serializable) bag, "src/test/resources/data/test/PredicatedSortedBag.emptyCollection.version4.obj");
// bag = makeObject();
// bag.add("A");
// bag.add("A");
// bag.add("B");
// bag.add("B");
// bag.add("C");
// bag = makeFullCollection();
// writeExternalFormToDisk((java.io.Serializable) bag, "src/test/resources/data/test/PredicatedSortedBag.fullCollection.version4.obj");
// }

View File

@ -0,0 +1,60 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.collections4.bag;
import junit.framework.Test;
import org.apache.commons.collections4.Bag;
import org.apache.commons.collections4.BulkTest;
/**
* Extension of {@link AbstractBagTest} for exercising the {@link SynchronizedBag}
* implementation.
*
* @since 4.0
* @version $Id$
*/
public class SynchronizedBagTest<T> extends AbstractBagTest<T> {
public SynchronizedBagTest(final String testName) {
super(testName);
}
public static Test suite() {
return BulkTest.makeSuite(SynchronizedBagTest.class);
}
//-----------------------------------------------------------------------
@Override
public Bag<T> makeObject() {
return SynchronizedBag.synchronizedBag(new HashBag<T>());
}
@Override
public String getCompatibilityVersion() {
return "4";
}
// public void testCreate() throws Exception {
// Bag<T> bag = makeObject();
// writeExternalFormToDisk((java.io.Serializable) bag, "src/test/resources/data/test/SynchronizedBag.emptyCollection.version4.obj");
// bag = makeFullCollection();
// writeExternalFormToDisk((java.io.Serializable) bag, "src/test/resources/data/test/SynchronizedBag.fullCollection.version4.obj");
// }
}

View File

@ -16,7 +16,10 @@
*/
package org.apache.commons.collections4.bag;
import junit.framework.Test;
import org.apache.commons.collections4.Bag;
import org.apache.commons.collections4.BulkTest;
import org.apache.commons.collections4.Transformer;
import org.apache.commons.collections4.collection.TransformedCollectionTest;
@ -33,6 +36,12 @@ public class TransformedBagTest<T> extends AbstractBagTest<T> {
super(testName);
}
public static Test suite() {
return BulkTest.makeSuite(TransformedBagTest.class);
}
//-----------------------------------------------------------------------
@Override
@SuppressWarnings("unchecked")
public Bag<T> makeObject() {
@ -83,14 +92,9 @@ public class TransformedBagTest<T> extends AbstractBagTest<T> {
}
// public void testCreate() throws Exception {
// Bag bag = makeObject();
// Bag<T> bag = makeObject();
// writeExternalFormToDisk((java.io.Serializable) bag, "src/test/resources/data/test/TransformedBag.emptyCollection.version4.obj");
// bag = makeObject();
// bag.add("A");
// bag.add("A");
// bag.add("B");
// bag.add("B");
// bag.add("C");
// bag = makeFullCollection();
// writeExternalFormToDisk((java.io.Serializable) bag, "src/test/resources/data/test/TransformedBag.fullCollection.version4.obj");
// }

View File

@ -16,7 +16,10 @@
*/
package org.apache.commons.collections4.bag;
import junit.framework.Test;
import org.apache.commons.collections4.Bag;
import org.apache.commons.collections4.BulkTest;
import org.apache.commons.collections4.SortedBag;
import org.apache.commons.collections4.Transformer;
import org.apache.commons.collections4.collection.TransformedCollectionTest;
@ -34,6 +37,12 @@ public class TransformedSortedBagTest<T> extends AbstractSortedBagTest<T> {
super(testName);
}
public static Test suite() {
return BulkTest.makeSuite(TransformedSortedBagTest.class);
}
//-----------------------------------------------------------------------
@Override
@SuppressWarnings("unchecked")
public SortedBag<T> makeObject() {
@ -76,14 +85,9 @@ public class TransformedSortedBagTest<T> extends AbstractSortedBagTest<T> {
}
// public void testCreate() throws Exception {
// Bag bag = makeObject();
// Bag<T> bag = makeObject();
// writeExternalFormToDisk((java.io.Serializable) bag, "src/test/resources/data/test/TransformedSortedBag.emptyCollection.version4.obj");
// bag = makeObject();
// bag.add("A");
// bag.add("A");
// bag.add("B");
// bag.add("B");
// bag.add("C");
// bag = makeFullCollection();
// writeExternalFormToDisk((java.io.Serializable) bag, "src/test/resources/data/test/TransformedSortedBag.fullCollection.version4.obj");
// }

View File

@ -16,7 +16,10 @@
*/
package org.apache.commons.collections4.bag;
import junit.framework.Test;
import org.apache.commons.collections4.Bag;
import org.apache.commons.collections4.BulkTest;
import org.apache.commons.collections4.SortedBag;
/**
@ -31,6 +34,12 @@ public class TreeBagTest<T> extends AbstractSortedBagTest<T> {
super(testName);
}
public static Test suite() {
return BulkTest.makeSuite(TreeBagTest.class);
}
//-----------------------------------------------------------------------
@Override
public SortedBag<T> makeObject() {
return new TreeBag<T>();
@ -71,14 +80,9 @@ public class TreeBagTest<T> extends AbstractSortedBagTest<T> {
}
// public void testCreate() throws Exception {
// Bag bag = makeObject();
// Bag<T> bag = makeObject();
// writeExternalFormToDisk((java.io.Serializable) bag, "src/test/resources/data/test/TreeBag.emptyCollection.version4.obj");
// bag = makeObject();
// bag.add("A");
// bag.add("A");
// bag.add("B");
// bag.add("B");
// bag.add("C");
// bag = makeFullCollection();
// writeExternalFormToDisk((java.io.Serializable) bag, "src/test/resources/data/test/TreeBag.fullCollection.version4.obj");
// }
}

View File

@ -0,0 +1,109 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.collections4.bag;
import java.util.Arrays;
import junit.framework.Test;
import org.apache.commons.collections4.Bag;
import org.apache.commons.collections4.BulkTest;
import org.apache.commons.collections4.Unmodifiable;
import org.apache.commons.collections4.collection.AbstractCollectionTest;
/**
* Extension of {@link AbstractCollectionTest} for exercising the
* {@link UnmodifiableBag} implementation.
*
* @since 4.0
* @version $Id$
*/
public class UnmodifiableBagTest<E> extends AbstractBagTest<E> {
public UnmodifiableBagTest(final String testName) {
super(testName);
}
public static Test suite() {
return BulkTest.makeSuite(UnmodifiableBagTest.class);
}
//-----------------------------------------------------------------------
@Override
public Bag<E> makeObject() {
return UnmodifiableBag.unmodifiableBag(new HashBag<E>());
}
@Override
public Bag<E> makeFullCollection() {
final Bag<E> bag = new HashBag<E>();
bag.addAll(Arrays.asList(getFullElements()));
return UnmodifiableBag.unmodifiableBag(bag);
}
@Override
public Bag<E> getCollection() {
return super.getCollection();
}
@Override
public boolean isAddSupported() {
return false;
}
@Override
public boolean isRemoveSupported() {
return false;
}
@Override
public boolean isNullSupported() {
return false;
}
//-----------------------------------------------------------------------
public void testUnmodifiable() {
assertTrue(makeObject() instanceof Unmodifiable);
assertTrue(makeFullCollection() instanceof Unmodifiable);
}
public void testDecorateFactory() {
final Bag<E> queue = makeFullCollection();
assertSame(queue, UnmodifiableBag.unmodifiableBag(queue));
try {
UnmodifiableBag.unmodifiableBag(null);
fail();
} catch (final IllegalArgumentException ex) {}
}
//-----------------------------------------------------------------------
@Override
public String getCompatibilityVersion() {
return "4";
}
// public void testCreate() throws Exception {
// Bag<E> bag = makeObject();
// writeExternalFormToDisk((java.io.Serializable) bag, "src/test/resources/data/test/UnmodifiableBag.emptyCollection.version4.obj");
// bag = makeFullCollection();
// writeExternalFormToDisk((java.io.Serializable) bag, "src/test/resources/data/test/UnmodifiableBag.fullCollection.version4.obj");
// }
}

View File

@ -0,0 +1,109 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.collections4.bag;
import java.util.Arrays;
import junit.framework.Test;
import org.apache.commons.collections4.BulkTest;
import org.apache.commons.collections4.SortedBag;
import org.apache.commons.collections4.Unmodifiable;
import org.apache.commons.collections4.collection.AbstractCollectionTest;
/**
* Extension of {@link AbstractCollectionTest} for exercising the
* {@link UnmodifiableSortedBag} implementation.
*
* @since 4.0
* @version $Id$
*/
public class UnmodifiableSortedBagTest<E> extends AbstractSortedBagTest<E> {
public UnmodifiableSortedBagTest(final String testName) {
super(testName);
}
public static Test suite() {
return BulkTest.makeSuite(UnmodifiableSortedBagTest.class);
}
//-----------------------------------------------------------------------
@Override
public SortedBag<E> makeObject() {
return UnmodifiableSortedBag.unmodifiableSortedBag(new TreeBag<E>());
}
@Override
public SortedBag<E> makeFullCollection() {
final SortedBag<E> bag = new TreeBag<E>();
bag.addAll(Arrays.asList(getFullElements()));
return UnmodifiableSortedBag.unmodifiableSortedBag(bag);
}
@Override
public SortedBag<E> getCollection() {
return super.getCollection();
}
@Override
public boolean isAddSupported() {
return false;
}
@Override
public boolean isRemoveSupported() {
return false;
}
@Override
public boolean isNullSupported() {
return false;
}
//-----------------------------------------------------------------------
public void testUnmodifiable() {
assertTrue(makeObject() instanceof Unmodifiable);
assertTrue(makeFullCollection() instanceof Unmodifiable);
}
public void testDecorateFactory() {
final SortedBag<E> queue = makeFullCollection();
assertSame(queue, UnmodifiableSortedBag.unmodifiableSortedBag(queue));
try {
UnmodifiableSortedBag.unmodifiableSortedBag(null);
fail();
} catch (final IllegalArgumentException ex) {}
}
//-----------------------------------------------------------------------
@Override
public String getCompatibilityVersion() {
return "4";
}
// public void testCreate() throws Exception {
// SortedBag<E> bag = makeObject();
// writeExternalFormToDisk((java.io.Serializable) bag, "src/test/resources/data/test/UnmodifiableSortedBag.emptyCollection.version4.obj");
// bag = makeFullCollection();
// writeExternalFormToDisk((java.io.Serializable) bag, "src/test/resources/data/test/UnmodifiableSortedBag.fullCollection.version4.obj");
// }
}