From 59c6e94d52eae65060b30fe9210da71bcd14665e Mon Sep 17 00:00:00 2001 From: Thomas Neidhart Date: Sun, 15 Nov 2015 15:04:19 +0000 Subject: [PATCH] [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 --- .../multiset/AbstractMapMultiSet.java | 9 +- .../multiset/AbstractMultiSetTest.java | 164 +++++++++--------- .../multiset/HashMultiSetTest.java | 60 +++++++ .../multiset/PredicatedMultiSetTest.java | 138 +++++++++++++++ .../multiset/SynchronizedMultiSetTest.java | 60 +++++++ .../multiset/UnmodifiableMultiSetTest.java | 108 ++++++++++++ ...ashMultiSet.emptyCollection.version4.1.obj | Bin 0 -> 81 bytes ...HashMultiSet.fullCollection.version4.1.obj | Bin 0 -> 590 bytes ...tedMultiSet.emptyCollection.version4.1.obj | Bin 0 -> 487 bytes ...atedMultiSet.fullCollection.version4.1.obj | Bin 0 -> 996 bytes ...zedMultiSet.emptyCollection.version4.1.obj | Bin 0 -> 310 bytes ...izedMultiSet.fullCollection.version4.1.obj | Bin 0 -> 819 bytes ...bleMultiSet.emptyCollection.version4.1.obj | Bin 0 -> 369 bytes ...ableMultiSet.fullCollection.version4.1.obj | Bin 0 -> 871 bytes 14 files changed, 449 insertions(+), 90 deletions(-) create mode 100644 src/test/java/org/apache/commons/collections4/multiset/HashMultiSetTest.java create mode 100644 src/test/java/org/apache/commons/collections4/multiset/PredicatedMultiSetTest.java create mode 100644 src/test/java/org/apache/commons/collections4/multiset/SynchronizedMultiSetTest.java create mode 100644 src/test/java/org/apache/commons/collections4/multiset/UnmodifiableMultiSetTest.java create mode 100644 src/test/resources/data/test/HashMultiSet.emptyCollection.version4.1.obj create mode 100644 src/test/resources/data/test/HashMultiSet.fullCollection.version4.1.obj create mode 100644 src/test/resources/data/test/PredicatedMultiSet.emptyCollection.version4.1.obj create mode 100644 src/test/resources/data/test/PredicatedMultiSet.fullCollection.version4.1.obj create mode 100644 src/test/resources/data/test/SynchronizedMultiSet.emptyCollection.version4.1.obj create mode 100644 src/test/resources/data/test/SynchronizedMultiSet.fullCollection.version4.1.obj create mode 100644 src/test/resources/data/test/UnmodifiableMultiSet.emptyCollection.version4.1.obj create mode 100644 src/test/resources/data/test/UnmodifiableMultiSet.fullCollection.version4.1.obj 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 0000000000000000000000000000000000000000..35a6014d057b37a8e1acf9a243b4576dce81fcfd GIT binary patch literal 81 zcmZ4UmVvdnh`}_!C|xhHATc>3RWCU|H#a}87)a;jq$ZbS0@)^dxurQJnZ>CkdLD_z b8NMJ+aB2wy5HK271~D@*R1}o6fVdR^vkMw0 literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..a17591ec9c769e9024245d0c92e086dad67d1415 GIT binary patch literal 590 zcmZ4UmVvdnh`}_!C|xhHATc>3RWCU|H#a}87)a;jq$ZbS0@)^dxurQJnZ>CkdLD_z z8NMJ+aB2wy5HK271~D@*R1}o60C_?{F))J>!e<1UEtr*9mZ+DLn3t~SnOBmUo?0aI zXu*>2jqMgp42+%(tYwKgrKuG~3<3ysex{yrQ2?f&#V<1t>WLBtW`8J~2^iE!9&*dcPb|6h_T-}5HXq&q z&2mFA%i#blo=O-vLNYRoN>X6~2s8#MCS3AMlX6lUHXriR%>K;+G{*(i91*ZDp>bXT E060jSxBvhE literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..6dce57c9c2263e36aad4a3f1b1b24435e6ef4869 GIT binary patch literal 487 zcmZ4UmVvdnh`~C)C|xhHATc>3RWCU|H#a}87)a;jq$ZbS0@)^dxurQJnZ>CkdI3eL zDVfQMC8;UCAfe#Y5(Xe(G_Fi#VqmB!Vz4J<62e#{vz*~}ZD(G% zm~ABt+CD&&^&xK5hq@6+BiyJDcbYZO9d0DJ!!fD2q$n}D1mOyo)a3l4#FG4?uss68 zyO%9A0Xv5aVOm`_$>S)zVvNoI~dLZfv>0WdgC30V#gPLIUm477j@Vg?3eK{*R3 mTq}TvnGrH9tu!wg=+t7pkfPF5cp&IqopSSFpqdUSS_%M?l&_Qk literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..75fe110d9c0545380a301b5c1c0e3ef4b0649874 GIT binary patch literal 996 zcmZ4UmVvdnh`~C)C|xhHATc>3RWCU|H#a}87)a;jq$ZbS0@)^dxurQJnZ>CkdI3eL zDVfQMC8;UCAfe#Y5(Xe(G_Fi#VqmB!Vz4J<62e#{vz*~}ZD(G% zm~ABt+CD&&^&xK5hq@6+BiyJDcbYZO9d0DJ!!fD2q$n}D1mOyo)a3l4#FG4?uss68 zyO%9A0Xv5aVOm`_$>S)zVvNoI~dLZfv>0WdgC30V#gPLIUm477j@Vg?3eK{*RB zScQOr3uZ7v_>4fa1wq#8v`stq^7492|Ze{sLtih>esd+FSlc6b^LN1sT zYe+^>YARH=h=CtrV{k@(QAvicxv)vXtstPgf?@6gS;ob{4cEhm(BqSzmu}ttWWt$# z<@-P_UZ`4t4g>-onAJ>%CNL(eYi4>z2{sd*Doax5_$9Qw3-~GnG|>sgL;;w77Qf6q zs3%GonEjz_CSXt_dB`m%Ke6Q2+mnlK+kAKfG|LUeEQbTIcq(Dw2+7DSDoKR}AkY}3 zm~hE2P0C4a*nG%KGy69S&>R<3b40+tgvNOVP_G%Gl$2JQmkdm&#d;w{rK#{7pm%l3 M&4YnzI^ehl0QhS$UH||9 literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..5d0ad8cac76e1d1b88b82470a0b655c79707362c GIT binary patch literal 310 zcmZ4UmVvdnh`~0$C|xhHATc>3RWCU|H#a}87)a;jq$ZbS0@)^dxurQJnZ>Ckdcl=> z$r(lYd6`wIDZU`V;M5WZAYe4EOl4wVs3>A^BxDxCU=-7w;r1!kFc$6iK$N2s^Pf#$CuW}v?c%2`0(tSDrtV_*RQ)>mn) literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..250c47e860d6c9789f363c2889e0150f47706b77 GIT binary patch literal 819 zcmZ4UmVvdnh`~0$C|xhHATc>3RWCU|H#a}87)a;jq$ZbS0@)^dxurQJnZ>Ckdcl=> z$r(lYd6`wIDZU`V;M5WZAYe4EOl4wVs3>A^BxDxCU=-7w;r1!kFc$6iK$N2s^Pf#$CuW}v?c%2|NE6axAc%wUA@8G&XCf}ErWvQp18uOu}+wMgjEf+gP@+bx(F z7(E$S%Mx=+Q-R?kfKcaGnwykb)V5}-EccWL-JoDA028;V&D2Vn73 y!oU%dky%ud3JXA>F-S4tl3$vXliINPke6omZx*0AE~w^+fPD##^9oR4u>b%>nd@Kx literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..e8c1ce641976dfc4e69c10f75723f759b751bd8a GIT binary patch literal 369 zcmZ4UmVvdnh`~0$C|xhHATc>3RWCU|H#a}87)a;jq$ZbS0@)^dxurQJnZ>CkdZBr_ z`6-!cnTbg`slFh=;M5WZAYe4E3}$9vs3>A^BHAp+q~emI#N-l~IWDQm`9+B(`9)9z zgP6bux)CxE;Zi-Ak3RWCU|H#a}87)a;jq$ZbS0@)^dxurQJnZ>CkdZBr_ z`6-!cnTbg`slFh=;M5WZAYe4E3}$9vs3>A^BHAp+q~emI#N-l~IWDQm`9+B(`9)9z zgP6bux)CxE;Zi-AknC+ zED(MP18Z< zfh^->;D+nrL+J6z&r7%Nelp=qzw&*c7B5sSpip68;DK4qWM~3ovbtubXOv(w(W$Z| zb&g*`%e#QDGC&iZP)rnn>1XlF%!7KOgn`)~%4PxvHIj$ia`F>PZoNIZ=(f#=H$b!8 zP|R{T0E?g!29A)7%%YN1SO5ZzL5c~N{8C`rZ`gduOEdd73(y=FRC7eYzJx}01t_rC FDgf%=0L=gZ literal 0 HcmV?d00001