diff --git a/src/main/java/org/apache/commons/collections4/multiset/AbstractMapMultiSet.java b/src/main/java/org/apache/commons/collections4/multiset/AbstractMapMultiSet.java index b752c23b2..f8282ac1f 100644 --- a/src/main/java/org/apache/commons/collections4/multiset/AbstractMapMultiSet.java +++ b/src/main/java/org/apache/commons/collections4/multiset/AbstractMapMultiSet.java @@ -267,14 +267,7 @@ public abstract class AbstractMapMultiSet extends AbstractCollection 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 diff --git a/src/test/java/org/apache/commons/collections4/multiset/AbstractMultiSetTest.java b/src/test/java/org/apache/commons/collections4/multiset/AbstractMultiSetTest.java index f825a85ff..67ef70049 100644 --- a/src/test/java/org/apache/commons/collections4/multiset/AbstractMultiSetTest.java +++ b/src/test/java/org/apache/commons/collections4/multiset/AbstractMultiSetTest.java @@ -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. *

- * 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. *

- * 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. *

* 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 extends AbstractCollectionTest { @@ -132,7 +132,7 @@ public abstract class AbstractMultiSetTest extends AbstractCollectionTest //----------------------------------------------------------------------- @SuppressWarnings("unchecked") - public void testBagAdd() { + public void testMultiSetAdd() { if (!isAddSupported()) { return; } @@ -150,7 +150,7 @@ public abstract class AbstractMultiSetTest extends AbstractCollectionTest } @SuppressWarnings("unchecked") - public void testBagEqualsSelf() { + public void testMultiSetEqualsSelf() { final MultiSet multiset = makeObject(); assertTrue(multiset.equals(multiset)); @@ -167,7 +167,7 @@ public abstract class AbstractMultiSetTest extends AbstractCollectionTest } @SuppressWarnings("unchecked") - public void testBagRemove() { + public void testMultiSetRemove() { if (!isRemoveSupported()) { return; } @@ -187,11 +187,11 @@ public abstract class AbstractMultiSetTest extends AbstractCollectionTest 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 extends AbstractCollectionTest 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 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 extends AbstractCollectionTest 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 extends AbstractCollectionTest 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 extends AbstractCollectionTest 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 extends AbstractCollectionTest 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 i = multiset.iterator(); boolean foundA = false; @@ -361,13 +361,13 @@ public abstract class AbstractMultiSetTest extends AbstractCollectionTest } } - 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 extends AbstractCollectionTest } @SuppressWarnings("unchecked") - public void testBagIteratorFailNoMore() { + public void testMultiSetIteratorFailNoMore() { if (!isAddSupported()) { return; } @@ -410,7 +410,7 @@ public abstract class AbstractMultiSetTest extends AbstractCollectionTest } @SuppressWarnings("unchecked") - public void testBagIteratorFailDoubleRemove() { + public void testMultiSetIteratorFailDoubleRemove() { if (!isAddSupported()) { return; } @@ -438,7 +438,7 @@ public abstract class AbstractMultiSetTest extends AbstractCollectionTest } @SuppressWarnings("unchecked") - public void testBagIteratorRemoveProtectsInvariants() { + public void testMultiSetIteratorRemoveProtectsInvariants() { if (!isAddSupported()) { return; } @@ -464,7 +464,7 @@ public abstract class AbstractMultiSetTest extends AbstractCollectionTest } @SuppressWarnings("unchecked") - public void testBagToArray() { + public void testMultiSetToArray() { if (!isAddSupported()) { return; } @@ -488,7 +488,7 @@ public abstract class AbstractMultiSetTest extends AbstractCollectionTest } @SuppressWarnings("unchecked") - public void testBagToArrayPopulate() { + public void testMultiSetToArrayPopulate() { if (!isAddSupported()) { return; } @@ -513,7 +513,7 @@ public abstract class AbstractMultiSetTest extends AbstractCollectionTest //----------------------------------------------------------------------- @SuppressWarnings("unchecked") - public void testBagEquals() { + public void testMultiSetEquals() { if (!isAddSupported()) { return; } @@ -537,7 +537,7 @@ public abstract class AbstractMultiSetTest extends AbstractCollectionTest } @SuppressWarnings("unchecked") - public void testBagEqualsHashBag() { + public void testMultiSetEqualsHashMultiSet() { if (!isAddSupported()) { return; } @@ -561,7 +561,7 @@ public abstract class AbstractMultiSetTest extends AbstractCollectionTest } @SuppressWarnings("unchecked") - public void testBagHashCode() { + public void testMultiSetHashCode() { if (!isAddSupported()) { return; } @@ -594,19 +594,19 @@ public abstract class AbstractMultiSetTest extends AbstractCollectionTest //----------------------------------------------------------------------- /** - * 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 { - public TestBagUniqueSet() { + public class TestMultiSetUniqueSet extends AbstractSetTest { + public TestMultiSetUniqueSet() { super(""); } @@ -642,7 +642,7 @@ public abstract class AbstractMultiSetTest extends AbstractCollectionTest @Override public boolean isRemoveSupported() { - return false; + return AbstractMultiSetTest.this.isRemoveSupported(); } @Override @@ -653,15 +653,15 @@ public abstract class AbstractMultiSetTest extends AbstractCollectionTest @Override public void resetEmpty() { AbstractMultiSetTest.this.resetEmpty(); - TestBagUniqueSet.this.setCollection(AbstractMultiSetTest.this.getCollection().uniqueSet()); - TestBagUniqueSet.this.setConfirmed(new HashSet(AbstractMultiSetTest.this.getConfirmed())); + TestMultiSetUniqueSet.this.setCollection(AbstractMultiSetTest.this.getCollection().uniqueSet()); + TestMultiSetUniqueSet.this.setConfirmed(new HashSet(AbstractMultiSetTest.this.getConfirmed())); } @Override public void resetFull() { AbstractMultiSetTest.this.resetFull(); - TestBagUniqueSet.this.setCollection(AbstractMultiSetTest.this.getCollection().uniqueSet()); - TestBagUniqueSet.this.setConfirmed(new HashSet(AbstractMultiSetTest.this.getConfirmed())); + TestMultiSetUniqueSet.this.setCollection(AbstractMultiSetTest.this.getCollection().uniqueSet()); + TestMultiSetUniqueSet.this.setConfirmed(new HashSet(AbstractMultiSetTest.this.getConfirmed())); } @Override @@ -673,29 +673,29 @@ public abstract class AbstractMultiSetTest extends AbstractCollectionTest //----------------------------------------------------------------------- /** - * 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 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 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); } } diff --git a/src/test/java/org/apache/commons/collections4/multiset/HashMultiSetTest.java b/src/test/java/org/apache/commons/collections4/multiset/HashMultiSetTest.java new file mode 100644 index 000000000..5e7a09479 --- /dev/null +++ b/src/test/java/org/apache/commons/collections4/multiset/HashMultiSetTest.java @@ -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 extends AbstractMultiSetTest { + + public HashMultiSetTest(final String testName) { + super(testName); + } + + public static Test suite() { + return BulkTest.makeSuite(HashMultiSetTest.class); + } + + //----------------------------------------------------------------------- + + @Override + public MultiSet makeObject() { + return new HashMultiSet(); + } + + @Override + public String getCompatibilityVersion() { + return "4.1"; + } + +// public void testCreate() throws Exception { +// MultiSet 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"); +// } + +} diff --git a/src/test/java/org/apache/commons/collections4/multiset/PredicatedMultiSetTest.java b/src/test/java/org/apache/commons/collections4/multiset/PredicatedMultiSetTest.java new file mode 100644 index 000000000..656b95941 --- /dev/null +++ b/src/test/java/org/apache/commons/collections4/multiset/PredicatedMultiSetTest.java @@ -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 extends AbstractMultiSetTest { + + public PredicatedMultiSetTest(final String testName) { + super(testName); + } + + public static Test suite() { + return BulkTest.makeSuite(PredicatedMultiSetTest.class); + } + + //-------------------------------------------------------------------------- + + protected Predicate stringPredicate() { + return new Predicate() { + @Override + public boolean evaluate(final T o) { + return o instanceof String; + } + }; + } + + protected Predicate truePredicate = TruePredicate.truePredicate(); + + protected MultiSet decorateMultiSet(final HashMultiSet multiset, final Predicate predicate) { + return PredicatedMultiSet.predicatedMultiSet(multiset, predicate); + } + + @Override + public MultiSet makeObject() { + return decorateMultiSet(new HashMultiSet(), truePredicate); + } + + protected MultiSet makeTestMultiSet() { + return decorateMultiSet(new HashMultiSet(), stringPredicate()); + } + + //-------------------------------------------------------------------------- + + @SuppressWarnings("unchecked") + public void testLegalAddRemove() { + final MultiSet 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 set = ((PredicatedMultiSet) multiset).uniqueSet(); + assertTrue("Unique set contains the first element",set.contains(els[0])); + assertEquals(true, multiset.remove(els[0])); + set = ((PredicatedMultiSet) multiset).uniqueSet(); + assertTrue("Unique set does not contain anymore the first element", + set.contains(els[0])); + } + + @SuppressWarnings("unchecked") + public void testIllegalAdd() { + final MultiSet 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 elements = new HashMultiSet(); + elements.add("one"); + elements.add("two"); + elements.add(Integer.valueOf(3)); + elements.add("four"); + try { + decorateMultiSet((HashMultiSet) elements, stringPredicate()); + fail("MultiSet contains an element that should fail the predicate."); + } catch (final IllegalArgumentException e) { + // expected + } + try { + decorateMultiSet(new HashMultiSet(), 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 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"); +// } + +} diff --git a/src/test/java/org/apache/commons/collections4/multiset/SynchronizedMultiSetTest.java b/src/test/java/org/apache/commons/collections4/multiset/SynchronizedMultiSetTest.java new file mode 100644 index 000000000..7133fa6dd --- /dev/null +++ b/src/test/java/org/apache/commons/collections4/multiset/SynchronizedMultiSetTest.java @@ -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 extends AbstractMultiSetTest { + + public SynchronizedMultiSetTest(final String testName) { + super(testName); + } + + public static Test suite() { + return BulkTest.makeSuite(SynchronizedMultiSetTest.class); + } + + //----------------------------------------------------------------------- + + @Override + public MultiSet makeObject() { + return SynchronizedMultiSet.synchronizedMultiSet(new HashMultiSet()); + } + + @Override + public String getCompatibilityVersion() { + return "4.1"; + } + +// public void testCreate() throws Exception { +// MultiSet 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"); +// } + +} diff --git a/src/test/java/org/apache/commons/collections4/multiset/UnmodifiableMultiSetTest.java b/src/test/java/org/apache/commons/collections4/multiset/UnmodifiableMultiSetTest.java new file mode 100644 index 000000000..79c870083 --- /dev/null +++ b/src/test/java/org/apache/commons/collections4/multiset/UnmodifiableMultiSetTest.java @@ -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 extends AbstractMultiSetTest { + + public UnmodifiableMultiSetTest(final String testName) { + super(testName); + } + + public static Test suite() { + return BulkTest.makeSuite(UnmodifiableMultiSetTest.class); + } + + //----------------------------------------------------------------------- + @Override + public MultiSet makeObject() { + return UnmodifiableMultiSet.unmodifiableMultiSet(new HashMultiSet()); + } + + @Override + public MultiSet makeFullCollection() { + final MultiSet multiset = new HashMultiSet(); + multiset.addAll(Arrays.asList(getFullElements())); + return UnmodifiableMultiSet.unmodifiableMultiSet(multiset); + } + + @Override + public MultiSet 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 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 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"); +// } + +} diff --git a/src/test/resources/data/test/HashMultiSet.emptyCollection.version4.1.obj b/src/test/resources/data/test/HashMultiSet.emptyCollection.version4.1.obj new file mode 100644 index 000000000..35a6014d0 Binary files /dev/null and b/src/test/resources/data/test/HashMultiSet.emptyCollection.version4.1.obj differ diff --git a/src/test/resources/data/test/HashMultiSet.fullCollection.version4.1.obj b/src/test/resources/data/test/HashMultiSet.fullCollection.version4.1.obj new file mode 100644 index 000000000..a17591ec9 Binary files /dev/null and b/src/test/resources/data/test/HashMultiSet.fullCollection.version4.1.obj differ diff --git a/src/test/resources/data/test/PredicatedMultiSet.emptyCollection.version4.1.obj b/src/test/resources/data/test/PredicatedMultiSet.emptyCollection.version4.1.obj new file mode 100644 index 000000000..6dce57c9c Binary files /dev/null and b/src/test/resources/data/test/PredicatedMultiSet.emptyCollection.version4.1.obj differ diff --git a/src/test/resources/data/test/PredicatedMultiSet.fullCollection.version4.1.obj b/src/test/resources/data/test/PredicatedMultiSet.fullCollection.version4.1.obj new file mode 100644 index 000000000..75fe110d9 Binary files /dev/null and b/src/test/resources/data/test/PredicatedMultiSet.fullCollection.version4.1.obj differ diff --git a/src/test/resources/data/test/SynchronizedMultiSet.emptyCollection.version4.1.obj b/src/test/resources/data/test/SynchronizedMultiSet.emptyCollection.version4.1.obj new file mode 100644 index 000000000..5d0ad8cac Binary files /dev/null and b/src/test/resources/data/test/SynchronizedMultiSet.emptyCollection.version4.1.obj differ diff --git a/src/test/resources/data/test/SynchronizedMultiSet.fullCollection.version4.1.obj b/src/test/resources/data/test/SynchronizedMultiSet.fullCollection.version4.1.obj new file mode 100644 index 000000000..250c47e86 Binary files /dev/null and b/src/test/resources/data/test/SynchronizedMultiSet.fullCollection.version4.1.obj differ diff --git a/src/test/resources/data/test/UnmodifiableMultiSet.emptyCollection.version4.1.obj b/src/test/resources/data/test/UnmodifiableMultiSet.emptyCollection.version4.1.obj new file mode 100644 index 000000000..e8c1ce641 Binary files /dev/null and b/src/test/resources/data/test/UnmodifiableMultiSet.emptyCollection.version4.1.obj differ diff --git a/src/test/resources/data/test/UnmodifiableMultiSet.fullCollection.version4.1.obj b/src/test/resources/data/test/UnmodifiableMultiSet.fullCollection.version4.1.obj new file mode 100644 index 000000000..d34db793d Binary files /dev/null and b/src/test/resources/data/test/UnmodifiableMultiSet.fullCollection.version4.1.obj differ