[COLLECTIONS-567] Add missing unit tests, fix problems with remove method.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@1714462 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Thomas Neidhart 2015-11-15 15:04:19 +00:00
parent e58963b51e
commit 59c6e94d52
14 changed files with 449 additions and 90 deletions

View File

@ -267,14 +267,7 @@ public abstract class AbstractMapMultiSet<E> extends AbstractCollection<E> imple
@Override @Override
public boolean remove(final Object object) { public boolean remove(final Object object) {
final MutableInteger mut = map.get(object); return remove(object, 1) != 0;
if (mut == null) {
return false;
}
modCount++;
map.remove(object);
size -= mut.value;
return true;
} }
@Override @Override

View File

@ -28,7 +28,6 @@ import java.util.List;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Set; import java.util.Set;
import org.apache.commons.collections4.Bag;
import org.apache.commons.collections4.BulkTest; import org.apache.commons.collections4.BulkTest;
import org.apache.commons.collections4.MultiSet; import org.apache.commons.collections4.MultiSet;
import org.apache.commons.collections4.collection.AbstractCollectionTest; import org.apache.commons.collections4.collection.AbstractCollectionTest;
@ -41,19 +40,20 @@ import org.apache.commons.collections4.set.AbstractSetTest;
* To use, simply extend this class, and implement * To use, simply extend this class, and implement
* the {@link #makeObject} method. * the {@link #makeObject} method.
* <p> * <p>
* If your bag fails one of these tests by design, * If your multiset fails one of these tests by design,
* you may still use this base set of cases. Simply override the * you may still use this base set of cases. Simply override the
* test case (method) your bag fails. * test case (method) your multiset fails.
* <p> * <p>
* This abstract test class does wrap the concrete bag implementation * This abstract test class does wrap the concrete multiset implementation
* with such a decorator, see the overridden {@link #resetEmpty()} and * with such a decorator, see the overridden {@link #resetEmpty()} and
* {@link #resetFull()} methods. * {@link #resetFull()} methods.
* <p> * <p>
* In addition to the generic collection tests (prefix testCollection) inherited * In addition to the generic collection tests (prefix testCollection) inherited
* from AbstractCollectionTest, there are test methods that test the "normal" Bag * from AbstractCollectionTest, there are test methods that test the "normal" MultiSet
* interface (prefix testBag). For Bag specific tests use the {@link #makeObject()} and * interface (prefix testMultiSet). For MultiSet specific tests use the {@link #makeObject()} and
* {@link #makeFullCollection()} methods instead of {@link #resetEmpty()} and resetFull(). * {@link #makeFullCollection()} methods instead of {@link #resetEmpty()} and resetFull().
* *
* @since 4.1
* @version $Id$ * @version $Id$
*/ */
public abstract class AbstractMultiSetTest<T> extends AbstractCollectionTest<T> { public abstract class AbstractMultiSetTest<T> extends AbstractCollectionTest<T> {
@ -132,7 +132,7 @@ public abstract class AbstractMultiSetTest<T> extends AbstractCollectionTest<T>
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void testBagAdd() { public void testMultiSetAdd() {
if (!isAddSupported()) { if (!isAddSupported()) {
return; return;
} }
@ -150,7 +150,7 @@ public abstract class AbstractMultiSetTest<T> extends AbstractCollectionTest<T>
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void testBagEqualsSelf() { public void testMultiSetEqualsSelf() {
final MultiSet<T> multiset = makeObject(); final MultiSet<T> multiset = makeObject();
assertTrue(multiset.equals(multiset)); assertTrue(multiset.equals(multiset));
@ -167,7 +167,7 @@ public abstract class AbstractMultiSetTest<T> extends AbstractCollectionTest<T>
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void testBagRemove() { public void testMultiSetRemove() {
if (!isRemoveSupported()) { if (!isRemoveSupported()) {
return; return;
} }
@ -187,11 +187,11 @@ public abstract class AbstractMultiSetTest<T> extends AbstractCollectionTest<T>
multiset.remove("A", 2); multiset.remove("A", 2);
assertEquals("Should have count of 2", 2, multiset.getCount("A")); assertEquals("Should have count of 2", 2, multiset.getCount("A"));
multiset.remove("A"); multiset.remove("A");
assertEquals("Should have count of 0", 0, multiset.getCount("A")); assertEquals("Should have count of 1", 1, multiset.getCount("A"));
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void testBagRemoveAll() { public void testMultiSetRemoveAll() {
if (!isRemoveSupported()) { if (!isRemoveSupported()) {
return; return;
} }
@ -206,38 +206,38 @@ public abstract class AbstractMultiSetTest<T> extends AbstractCollectionTest<T>
delete.add("A"); delete.add("A");
delete.add("B"); delete.add("B");
multiset.removeAll(delete); multiset.removeAll(delete);
assertEquals("Should have count of 1", 1, multiset.getCount("A")); assertEquals("Should have count of 0", 0, multiset.getCount("A"));
assertEquals("Should have count of 0", 0, multiset.getCount("B")); assertEquals("Should have count of 0", 0, multiset.getCount("B"));
assertEquals("Should have count of 1", 1, multiset.getCount("C")); assertEquals("Should have count of 1", 1, multiset.getCount("C"));
assertEquals("Should have count of 2", 2, multiset.size()); assertEquals("Should have count of 1", 1, multiset.size());
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void testBagContains() { public void testMultiSetContains() {
if (!isAddSupported()) { if (!isAddSupported()) {
return; return;
} }
final MultiSet<T> multiset = makeObject(); final MultiSet<T> multiset = makeObject();
assertEquals("Bag does not have at least 1 'A'", false, multiset.contains("A")); assertEquals("MultiSet does not have at least 1 'A'", false, multiset.contains("A"));
assertEquals("Bag does not have at least 1 'B'", false, multiset.contains("B")); assertEquals("MultiSet does not have at least 1 'B'", false, multiset.contains("B"));
multiset.add((T) "A"); // multiset 1A multiset.add((T) "A"); // multiset 1A
assertEquals("Bag has at least 1 'A'", true, multiset.contains("A")); assertEquals("MultiSet has at least 1 'A'", true, multiset.contains("A"));
assertEquals("Bag does not have at least 1 'B'", false, multiset.contains("B")); assertEquals("MultiSet does not have at least 1 'B'", false, multiset.contains("B"));
multiset.add((T) "A"); // multiset 2A multiset.add((T) "A"); // multiset 2A
assertEquals("Bag has at least 1 'A'", true, multiset.contains("A")); assertEquals("MultiSet has at least 1 'A'", true, multiset.contains("A"));
assertEquals("Bag does not have at least 1 'B'", false, multiset.contains("B")); assertEquals("MultiSet does not have at least 1 'B'", false, multiset.contains("B"));
multiset.add((T) "B"); // multiset 2A,1B multiset.add((T) "B"); // multiset 2A,1B
assertEquals("Bag has at least 1 'A'", true, multiset.contains("A")); assertEquals("MultiSet has at least 1 'A'", true, multiset.contains("A"));
assertEquals("Bag has at least 1 'B'", true, multiset.contains("B")); assertEquals("MultiSet has at least 1 'B'", true, multiset.contains("B"));
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void testBagContainsAll() { public void testMultiSetContainsAll() {
if (!isAddSupported()) { if (!isAddSupported()) {
return; return;
} }
@ -255,43 +255,43 @@ public abstract class AbstractMultiSetTest<T> extends AbstractCollectionTest<T>
known1A1B.add("A"); known1A1B.add("A");
known1A1B.add("B"); known1A1B.add("B");
assertEquals("Bag containsAll of empty", true, multiset.containsAll(known)); assertEquals("MultiSet containsAll of empty", true, multiset.containsAll(known));
assertEquals("Bag does not containsAll of 1 'A'", false, multiset.containsAll(known1A)); assertEquals("MultiSet does not containsAll of 1 'A'", false, multiset.containsAll(known1A));
assertEquals("Bag does not containsAll of 2 'A'", false, multiset.containsAll(known2A)); assertEquals("MultiSet does not containsAll of 2 'A'", false, multiset.containsAll(known2A));
assertEquals("Bag does not containsAll of 1 'B'", false, multiset.containsAll(known1B)); assertEquals("MultiSet does not containsAll of 1 'B'", false, multiset.containsAll(known1B));
assertEquals("Bag does not containsAll of 1 'A' 1 'B'", false, multiset.containsAll(known1A1B)); assertEquals("MultiSet does not containsAll of 1 'A' 1 'B'", false, multiset.containsAll(known1A1B));
multiset.add((T) "A"); // multiset 1A multiset.add((T) "A"); // multiset 1A
assertEquals("Bag containsAll of empty", true, multiset.containsAll(known)); assertEquals("MultiSet containsAll of empty", true, multiset.containsAll(known));
assertEquals("Bag containsAll of 1 'A'", true, multiset.containsAll(known1A)); assertEquals("MultiSet containsAll of 1 'A'", true, multiset.containsAll(known1A));
assertEquals("Bag does not containsAll of 2 'A'", false, multiset.containsAll(known2A)); assertEquals("MultiSet does not containsAll 'A'", true, multiset.containsAll(known2A));
assertEquals("Bag does not containsAll of 1 'B'", false, multiset.containsAll(known1B)); assertEquals("MultiSet does not containsAll of 1 'B'", false, multiset.containsAll(known1B));
assertEquals("Bag does not containsAll of 1 'A' 1 'B'", false, multiset.containsAll(known1A1B)); assertEquals("MultiSet does not containsAll of 1 'A' 1 'B'", false, multiset.containsAll(known1A1B));
multiset.add((T) "A"); // multiset 2A multiset.add((T) "A"); // multiset 2A
assertEquals("Bag containsAll of empty", true, multiset.containsAll(known)); assertEquals("MultiSet containsAll of empty", true, multiset.containsAll(known));
assertEquals("Bag containsAll of 1 'A'", true, multiset.containsAll(known1A)); assertEquals("MultiSet containsAll of 1 'A'", true, multiset.containsAll(known1A));
assertEquals("Bag containsAll of 2 'A'", true, multiset.containsAll(known2A)); assertEquals("MultiSet containsAll of 2 'A'", true, multiset.containsAll(known2A));
assertEquals("Bag does not containsAll of 1 'B'", false, multiset.containsAll(known1B)); assertEquals("MultiSet does not containsAll of 1 'B'", false, multiset.containsAll(known1B));
assertEquals("Bag does not containsAll of 1 'A' 1 'B'", false, multiset.containsAll(known1A1B)); assertEquals("MultiSet does not containsAll of 1 'A' 1 'B'", false, multiset.containsAll(known1A1B));
multiset.add((T) "A"); // multiset 3A multiset.add((T) "A"); // multiset 3A
assertEquals("Bag containsAll of empty", true, multiset.containsAll(known)); assertEquals("MultiSet containsAll of empty", true, multiset.containsAll(known));
assertEquals("Bag containsAll of 1 'A'", true, multiset.containsAll(known1A)); assertEquals("MultiSet containsAll of 1 'A'", true, multiset.containsAll(known1A));
assertEquals("Bag containsAll of 2 'A'", true, multiset.containsAll(known2A)); assertEquals("MultiSet containsAll of 2 'A'", true, multiset.containsAll(known2A));
assertEquals("Bag does not containsAll of 1 'B'", false, multiset.containsAll(known1B)); assertEquals("MultiSet does not containsAll of 1 'B'", false, multiset.containsAll(known1B));
assertEquals("Bag does not containsAll of 1 'A' 1 'B'", false, multiset.containsAll(known1A1B)); assertEquals("MultiSet does not containsAll of 1 'A' 1 'B'", false, multiset.containsAll(known1A1B));
multiset.add((T) "B"); // multiset 3A1B multiset.add((T) "B"); // multiset 3A1B
assertEquals("Bag containsAll of empty", true, multiset.containsAll(known)); assertEquals("MultiSet containsAll of empty", true, multiset.containsAll(known));
assertEquals("Bag containsAll of 1 'A'", true, multiset.containsAll(known1A)); assertEquals("MultiSet containsAll of 1 'A'", true, multiset.containsAll(known1A));
assertEquals("Bag containsAll of 2 'A'", true, multiset.containsAll(known2A)); assertEquals("MultiSet containsAll of 2 'A'", true, multiset.containsAll(known2A));
assertEquals("Bag containsAll of 1 'B'", true, multiset.containsAll(known1B)); assertEquals("MultiSet containsAll of 1 'B'", true, multiset.containsAll(known1B));
assertEquals("Bag containsAll of 1 'A' 1 'B'", true, multiset.containsAll(known1A1B)); assertEquals("MultiSet containsAll of 1 'A' 1 'B'", true, multiset.containsAll(known1A1B));
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void testBagSize() { public void testMultiSetSize() {
if (!isAddSupported()) { if (!isAddSupported()) {
return; return;
} }
@ -312,11 +312,11 @@ public abstract class AbstractMultiSetTest<T> extends AbstractCollectionTest<T>
assertEquals("Should have 1 'A'", 1, multiset.getCount("A")); assertEquals("Should have 1 'A'", 1, multiset.getCount("A"));
assertEquals("Should have 3 total items", 3, multiset.size()); assertEquals("Should have 3 total items", 3, multiset.size());
multiset.remove("B"); multiset.remove("B");
assertEquals("Should have 1 total item", 1, multiset.size()); assertEquals("Should have 2 total item", 2, multiset.size());
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void testBagRetainAll() { public void testMultiSetRetainAll() {
if (!isAddSupported()) { if (!isAddSupported()) {
return; return;
} }
@ -332,11 +332,11 @@ public abstract class AbstractMultiSetTest<T> extends AbstractCollectionTest<T>
retains.add("B"); retains.add("B");
retains.add("C"); retains.add("C");
multiset.retainAll(retains); multiset.retainAll(retains);
assertEquals("Should have 2 total items", 2, multiset.size()); assertEquals("Should have 3 total items", 3, multiset.size());
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void testBagIterator() { public void testMultiSetIterator() {
if (!isAddSupported()) { if (!isAddSupported()) {
return; return;
} }
@ -345,7 +345,7 @@ public abstract class AbstractMultiSetTest<T> extends AbstractCollectionTest<T>
multiset.add((T) "A"); multiset.add((T) "A");
multiset.add((T) "A"); multiset.add((T) "A");
multiset.add((T) "B"); multiset.add((T) "B");
assertEquals("Bag should have 3 items", 3, multiset.size()); assertEquals("MultiSet should have 3 items", 3, multiset.size());
final Iterator<T> i = multiset.iterator(); final Iterator<T> i = multiset.iterator();
boolean foundA = false; boolean foundA = false;
@ -361,13 +361,13 @@ public abstract class AbstractMultiSetTest<T> extends AbstractCollectionTest<T>
} }
} }
assertTrue("Bag should still contain 'A'", multiset.contains("A")); assertTrue("MultiSet should still contain 'A'", multiset.contains("A"));
assertEquals("Bag should have 2 items", 2, multiset.size()); assertEquals("MultiSet should have 2 items", 2, multiset.size());
assertEquals("Bag should have 1 'A'", 1, multiset.getCount("A")); assertEquals("MultiSet should have 1 'A'", 1, multiset.getCount("A"));
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void testBagIteratorFail() { public void testMultiSetIteratorFail() {
if (!isAddSupported()) { if (!isAddSupported()) {
return; return;
} }
@ -388,7 +388,7 @@ public abstract class AbstractMultiSetTest<T> extends AbstractCollectionTest<T>
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void testBagIteratorFailNoMore() { public void testMultiSetIteratorFailNoMore() {
if (!isAddSupported()) { if (!isAddSupported()) {
return; return;
} }
@ -410,7 +410,7 @@ public abstract class AbstractMultiSetTest<T> extends AbstractCollectionTest<T>
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void testBagIteratorFailDoubleRemove() { public void testMultiSetIteratorFailDoubleRemove() {
if (!isAddSupported()) { if (!isAddSupported()) {
return; return;
} }
@ -438,7 +438,7 @@ public abstract class AbstractMultiSetTest<T> extends AbstractCollectionTest<T>
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void testBagIteratorRemoveProtectsInvariants() { public void testMultiSetIteratorRemoveProtectsInvariants() {
if (!isAddSupported()) { if (!isAddSupported()) {
return; return;
} }
@ -464,7 +464,7 @@ public abstract class AbstractMultiSetTest<T> extends AbstractCollectionTest<T>
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void testBagToArray() { public void testMultiSetToArray() {
if (!isAddSupported()) { if (!isAddSupported()) {
return; return;
} }
@ -488,7 +488,7 @@ public abstract class AbstractMultiSetTest<T> extends AbstractCollectionTest<T>
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void testBagToArrayPopulate() { public void testMultiSetToArrayPopulate() {
if (!isAddSupported()) { if (!isAddSupported()) {
return; return;
} }
@ -513,7 +513,7 @@ public abstract class AbstractMultiSetTest<T> extends AbstractCollectionTest<T>
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void testBagEquals() { public void testMultiSetEquals() {
if (!isAddSupported()) { if (!isAddSupported()) {
return; return;
} }
@ -537,7 +537,7 @@ public abstract class AbstractMultiSetTest<T> extends AbstractCollectionTest<T>
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void testBagEqualsHashBag() { public void testMultiSetEqualsHashMultiSet() {
if (!isAddSupported()) { if (!isAddSupported()) {
return; return;
} }
@ -561,7 +561,7 @@ public abstract class AbstractMultiSetTest<T> extends AbstractCollectionTest<T>
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void testBagHashCode() { public void testMultiSetHashCode() {
if (!isAddSupported()) { if (!isAddSupported()) {
return; return;
} }
@ -594,19 +594,19 @@ public abstract class AbstractMultiSetTest<T> extends AbstractCollectionTest<T>
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
/** /**
* Bulk test {@link Bag#uniqueSet()}. This method runs through all of * Bulk test {@link MultiSet#uniqueSet()}. This method runs through all of
* the tests in {@link AbstractSetTest}. * the tests in {@link AbstractSetTest}.
* After modification operations, {@link #verify()} is invoked to ensure * After modification operations, {@link #verify()} is invoked to ensure
* that the multiset and the other collection views are still valid. * that the multiset and the other collection views are still valid.
* *
* @return a {@link AbstractSetTest} instance for testing the multiset's unique set * @return a {@link AbstractSetTest} instance for testing the multiset's unique set
*/ */
public BulkTest bulkTestBagUniqueSet() { public BulkTest bulkTestMultiSetUniqueSet() {
return new TestBagUniqueSet(); return new TestMultiSetUniqueSet();
} }
public class TestBagUniqueSet extends AbstractSetTest<T> { public class TestMultiSetUniqueSet extends AbstractSetTest<T> {
public TestBagUniqueSet() { public TestMultiSetUniqueSet() {
super(""); super("");
} }
@ -642,7 +642,7 @@ public abstract class AbstractMultiSetTest<T> extends AbstractCollectionTest<T>
@Override @Override
public boolean isRemoveSupported() { public boolean isRemoveSupported() {
return false; return AbstractMultiSetTest.this.isRemoveSupported();
} }
@Override @Override
@ -653,15 +653,15 @@ public abstract class AbstractMultiSetTest<T> extends AbstractCollectionTest<T>
@Override @Override
public void resetEmpty() { public void resetEmpty() {
AbstractMultiSetTest.this.resetEmpty(); AbstractMultiSetTest.this.resetEmpty();
TestBagUniqueSet.this.setCollection(AbstractMultiSetTest.this.getCollection().uniqueSet()); TestMultiSetUniqueSet.this.setCollection(AbstractMultiSetTest.this.getCollection().uniqueSet());
TestBagUniqueSet.this.setConfirmed(new HashSet<T>(AbstractMultiSetTest.this.getConfirmed())); TestMultiSetUniqueSet.this.setConfirmed(new HashSet<T>(AbstractMultiSetTest.this.getConfirmed()));
} }
@Override @Override
public void resetFull() { public void resetFull() {
AbstractMultiSetTest.this.resetFull(); AbstractMultiSetTest.this.resetFull();
TestBagUniqueSet.this.setCollection(AbstractMultiSetTest.this.getCollection().uniqueSet()); TestMultiSetUniqueSet.this.setCollection(AbstractMultiSetTest.this.getCollection().uniqueSet());
TestBagUniqueSet.this.setConfirmed(new HashSet<T>(AbstractMultiSetTest.this.getConfirmed())); TestMultiSetUniqueSet.this.setConfirmed(new HashSet<T>(AbstractMultiSetTest.this.getConfirmed()));
} }
@Override @Override
@ -673,29 +673,29 @@ public abstract class AbstractMultiSetTest<T> extends AbstractCollectionTest<T>
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
/** /**
* Compare the current serialized form of the Bag * Compare the current serialized form of the MultiSet
* against the canonical version in SVN. * against the canonical version in SVN.
*/ */
public void testEmptyBagCompatibility() throws IOException, ClassNotFoundException { public void testEmptyMultiSetCompatibility() throws IOException, ClassNotFoundException {
// test to make sure the canonical form has been preserved // test to make sure the canonical form has been preserved
final MultiSet<T> multiset = makeObject(); final MultiSet<T> multiset = makeObject();
if (multiset instanceof Serializable && !skipSerializedCanonicalTests() && isTestSerialization()) { if (multiset instanceof Serializable && !skipSerializedCanonicalTests() && isTestSerialization()) {
final MultiSet<?> multiset2 = (MultiSet<?>) readExternalFormFromDisk(getCanonicalEmptyCollectionName(multiset)); final MultiSet<?> multiset2 = (MultiSet<?>) readExternalFormFromDisk(getCanonicalEmptyCollectionName(multiset));
assertTrue("Bag is empty",multiset2.size() == 0); assertTrue("MultiSet is empty",multiset2.size() == 0);
assertEquals(multiset, multiset2); assertEquals(multiset, multiset2);
} }
} }
/** /**
* Compare the current serialized form of the Bag * Compare the current serialized form of the MultiSet
* against the canonical version in SVN. * against the canonical version in SVN.
*/ */
public void testFullBagCompatibility() throws IOException, ClassNotFoundException { public void testFullMultiSetCompatibility() throws IOException, ClassNotFoundException {
// test to make sure the canonical form has been preserved // test to make sure the canonical form has been preserved
final MultiSet<T> multiset = makeFullCollection(); final MultiSet<T> multiset = makeFullCollection();
if (multiset instanceof Serializable && !skipSerializedCanonicalTests() && isTestSerialization()) { if (multiset instanceof Serializable && !skipSerializedCanonicalTests() && isTestSerialization()) {
final MultiSet<?> multiset2 = (MultiSet<?>) readExternalFormFromDisk(getCanonicalFullCollectionName(multiset)); final MultiSet<?> multiset2 = (MultiSet<?>) readExternalFormFromDisk(getCanonicalFullCollectionName(multiset));
assertEquals("Bag is the right size",multiset.size(), multiset2.size()); assertEquals("MultiSet is the right size",multiset.size(), multiset2.size());
assertEquals(multiset, multiset2); assertEquals(multiset, multiset2);
} }
} }

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.multiset;
import junit.framework.Test;
import org.apache.commons.collections4.BulkTest;
import org.apache.commons.collections4.MultiSet;
/**
* Extension of {@link AbstractMultiSetTest} for exercising the
* {@link HashMultiSet} implementation.
*
* @since 4.1
* @version $Id$
*/
public class HashMultiSetTest<T> extends AbstractMultiSetTest<T> {
public HashMultiSetTest(final String testName) {
super(testName);
}
public static Test suite() {
return BulkTest.makeSuite(HashMultiSetTest.class);
}
//-----------------------------------------------------------------------
@Override
public MultiSet<T> makeObject() {
return new HashMultiSet<T>();
}
@Override
public String getCompatibilityVersion() {
return "4.1";
}
// public void testCreate() throws Exception {
// MultiSet<T> multiset = makeObject();
// writeExternalFormToDisk((java.io.Serializable) multiset, "src/test/resources/data/test/HashMultiSet.emptyCollection.version4.1.obj");
// multiset = makeFullCollection();
// writeExternalFormToDisk((java.io.Serializable) multiset, "src/test/resources/data/test/HashMultiSet.fullCollection.version4.1.obj");
// }
}

View File

@ -0,0 +1,138 @@
/*
* 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.multiset;
import java.util.Set;
import junit.framework.Test;
import org.apache.commons.collections4.BulkTest;
import org.apache.commons.collections4.MultiSet;
import org.apache.commons.collections4.Predicate;
import org.apache.commons.collections4.functors.TruePredicate;
/**
* Extension of {@link AbstractMultiSetTest} for exercising the
* {@link PredicatedMultiSet} implementation.
*
* @since 4.1
* @version $Id$
*/
public class PredicatedMultiSetTest<T> extends AbstractMultiSetTest<T> {
public PredicatedMultiSetTest(final String testName) {
super(testName);
}
public static Test suite() {
return BulkTest.makeSuite(PredicatedMultiSetTest.class);
}
//--------------------------------------------------------------------------
protected Predicate<T> stringPredicate() {
return new Predicate<T>() {
@Override
public boolean evaluate(final T o) {
return o instanceof String;
}
};
}
protected Predicate<T> truePredicate = TruePredicate.<T>truePredicate();
protected MultiSet<T> decorateMultiSet(final HashMultiSet<T> multiset, final Predicate<T> predicate) {
return PredicatedMultiSet.predicatedMultiSet(multiset, predicate);
}
@Override
public MultiSet<T> makeObject() {
return decorateMultiSet(new HashMultiSet<T>(), truePredicate);
}
protected MultiSet<T> makeTestMultiSet() {
return decorateMultiSet(new HashMultiSet<T>(), stringPredicate());
}
//--------------------------------------------------------------------------
@SuppressWarnings("unchecked")
public void testLegalAddRemove() {
final MultiSet<T> multiset = makeTestMultiSet();
assertEquals(0, multiset.size());
final T[] els = (T[]) new Object[] { "1", "3", "5", "7", "2", "4", "1" };
for (int i = 0; i < els.length; i++) {
multiset.add(els[i]);
assertEquals(i + 1, multiset.size());
assertEquals(true, multiset.contains(els[i]));
}
Set<T> set = ((PredicatedMultiSet<T>) multiset).uniqueSet();
assertTrue("Unique set contains the first element",set.contains(els[0]));
assertEquals(true, multiset.remove(els[0]));
set = ((PredicatedMultiSet<T>) multiset).uniqueSet();
assertTrue("Unique set does not contain anymore the first element",
set.contains(els[0]));
}
@SuppressWarnings("unchecked")
public void testIllegalAdd() {
final MultiSet<T> multiset = makeTestMultiSet();
final Integer i = Integer.valueOf(3);
try {
multiset.add((T) i);
fail("Integer should fail string predicate.");
} catch (final IllegalArgumentException e) {
// expected
}
assertTrue("Collection shouldn't contain illegal element",
!multiset.contains(i));
}
@SuppressWarnings("unchecked")
public void testIllegalDecorate() {
final HashMultiSet<Object> elements = new HashMultiSet<Object>();
elements.add("one");
elements.add("two");
elements.add(Integer.valueOf(3));
elements.add("four");
try {
decorateMultiSet((HashMultiSet<T>) elements, stringPredicate());
fail("MultiSet contains an element that should fail the predicate.");
} catch (final IllegalArgumentException e) {
// expected
}
try {
decorateMultiSet(new HashMultiSet<T>(), null);
fail("Expecting NullPointerException for null predicate.");
} catch (final NullPointerException e) {
// expected
}
}
@Override
public String getCompatibilityVersion() {
return "4.1";
}
// public void testCreate() throws Exception {
// MultiSet<T> multiset = makeObject();
// writeExternalFormToDisk((java.io.Serializable) multiset, "src/test/resources/data/test/PredicatedMultiSet.emptyCollection.version4.1.obj");
// multiset = makeFullCollection();
// writeExternalFormToDisk((java.io.Serializable) multiset, "src/test/resources/data/test/PredicatedMultiSet.fullCollection.version4.1.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.multiset;
import junit.framework.Test;
import org.apache.commons.collections4.BulkTest;
import org.apache.commons.collections4.MultiSet;
/**
* Extension of {@link AbstractMultiSetTest} for exercising the
* {@link SynchronizedMultiSet} implementation.
*
* @since 4.1
* @version $Id$
*/
public class SynchronizedMultiSetTest<T> extends AbstractMultiSetTest<T> {
public SynchronizedMultiSetTest(final String testName) {
super(testName);
}
public static Test suite() {
return BulkTest.makeSuite(SynchronizedMultiSetTest.class);
}
//-----------------------------------------------------------------------
@Override
public MultiSet<T> makeObject() {
return SynchronizedMultiSet.synchronizedMultiSet(new HashMultiSet<T>());
}
@Override
public String getCompatibilityVersion() {
return "4.1";
}
// public void testCreate() throws Exception {
// MultiSet<T> multiset = makeObject();
// writeExternalFormToDisk((java.io.Serializable) multiset, "src/test/resources/data/test/SynchronizedMultiSet.emptyCollection.version4.1.obj");
// multiset = makeFullCollection();
// writeExternalFormToDisk((java.io.Serializable) multiset, "src/test/resources/data/test/SynchronizedMultiSet.fullCollection.version4.1.obj");
// }
}

View File

@ -0,0 +1,108 @@
/*
* 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.multiset;
import java.util.Arrays;
import junit.framework.Test;
import org.apache.commons.collections4.BulkTest;
import org.apache.commons.collections4.MultiSet;
import org.apache.commons.collections4.Unmodifiable;
/**
* Extension of {@link AbstractMultiSetTest} for exercising the
* {@link UnmodifiableMultiSet} implementation.
*
* @since 4.1
* @version $Id$
*/
public class UnmodifiableMultiSetTest<E> extends AbstractMultiSetTest<E> {
public UnmodifiableMultiSetTest(final String testName) {
super(testName);
}
public static Test suite() {
return BulkTest.makeSuite(UnmodifiableMultiSetTest.class);
}
//-----------------------------------------------------------------------
@Override
public MultiSet<E> makeObject() {
return UnmodifiableMultiSet.unmodifiableMultiSet(new HashMultiSet<E>());
}
@Override
public MultiSet<E> makeFullCollection() {
final MultiSet<E> multiset = new HashMultiSet<E>();
multiset.addAll(Arrays.asList(getFullElements()));
return UnmodifiableMultiSet.unmodifiableMultiSet(multiset);
}
@Override
public MultiSet<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 MultiSet<E> multiset = makeFullCollection();
assertSame(multiset, UnmodifiableMultiSet.unmodifiableMultiSet(multiset));
try {
UnmodifiableMultiSet.unmodifiableMultiSet(null);
fail();
} catch (final NullPointerException ex) {}
}
//-----------------------------------------------------------------------
@Override
public String getCompatibilityVersion() {
return "4.1";
}
// public void testCreate() throws Exception {
// MultiSet<E> multiset = makeObject();
// writeExternalFormToDisk((java.io.Serializable) multiset, "src/test/resources/data/test/UnmodifiableMultiSet.emptyCollection.version4.1.obj");
// multiset = makeFullCollection();
// writeExternalFormToDisk((java.io.Serializable) multiset, "src/test/resources/data/test/UnmodifiableMultiSet.fullCollection.version4.1.obj");
// }
}