[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
public boolean remove(final Object object) {
final MutableInteger mut = map.get(object);
if (mut == null) {
return false;
}
modCount++;
map.remove(object);
size -= mut.value;
return true;
return remove(object, 1) != 0;
}
@Override

View File

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

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");
// }
}