Repository: commons-collections Updated Branches: refs/heads/master 13ba1cc91 -> 75e87fc76
COLLECTIONS-681: New unit test for MultiSetUtils Project: http://git-wip-us.apache.org/repos/asf/commons-collections/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-collections/commit/13467cbc Tree: http://git-wip-us.apache.org/repos/asf/commons-collections/tree/13467cbc Diff: http://git-wip-us.apache.org/repos/asf/commons-collections/diff/13467cbc Branch: refs/heads/master Commit: 13467cbc7f319f7f7e9e9185a673714736e77b83 Parents: 13ba1cc Author: Stephan Fuhrmann <s...@sfuhrm.de> Authored: Sat Jun 9 20:11:59 2018 +0200 Committer: Bruno P. Kinoshita <ki...@apache.org> Committed: Sun Jun 10 11:34:43 2018 +1200 ---------------------------------------------------------------------- .../commons/collections4/MultiSetUtilsTest.java | 127 +++++++++++++++++++ 1 file changed, 127 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-collections/blob/13467cbc/src/test/java/org/apache/commons/collections4/MultiSetUtilsTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/collections4/MultiSetUtilsTest.java b/src/test/java/org/apache/commons/collections4/MultiSetUtilsTest.java new file mode 100644 index 0000000..b695c6c --- /dev/null +++ b/src/test/java/org/apache/commons/collections4/MultiSetUtilsTest.java @@ -0,0 +1,127 @@ +/* + * 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; + +import static org.junit.Assert.*; + +import java.util.Arrays; +import org.apache.commons.collections4.multiset.HashMultiSet; +import org.junit.Before; +import org.junit.Test; + +/** + * Tests for MultiSetUtils. + * @since 4.2 + */ +public class MultiSetUtilsTest { + + private String[] fullArray; + private MultiSet<String> multiSet; + + @Before + public void setUp() { + fullArray = new String[]{ + "a", "a", "b", "c", "d", "d", "d" + }; + multiSet = new HashMultiSet<>(Arrays.asList(fullArray)); + } + + /** + * Tests {@link MultiSetUtils#emptyMultiSet()}. + */ + @Test + public void testEmptyMultiSet() { + MultiSet<Integer> empty = MultiSetUtils.emptyMultiSet(); + assertEquals(0, empty.size()); + try { + empty.add(55); + fail("Empty multi set must be read-only"); + } catch (UnsupportedOperationException e) { + } + } + + /** + * Tests {@link MultiSetUtils#unmodifiableMultiSet(org.apache.commons.collections4.MultiSet) ()}. + */ + @Test + public void testUnmodifiableMultiSet() { + MultiSet<String> unmodifiable = MultiSetUtils.unmodifiableMultiSet(multiSet); + assertEquals(multiSet, unmodifiable); + + try { + unmodifiable.add("a"); + fail("Empty multi set must be read-only"); + } catch (UnsupportedOperationException e) { + } + + try { + MultiSetUtils.unmodifiableMultiSet(null); + fail("Expecting NPE"); + } catch (NullPointerException e) { + } + } + + /** + * Tests {@link MultiSetUtils#unmodifiableMultiSet(org.apache.commons.collections4.MultiSet) ()}. + */ + @Test + public void testSynchronizedMultiSet() { + MultiSet<String> synced = MultiSetUtils.synchronizedMultiSet(multiSet); + assertEquals(multiSet, synced); + synced.add("a"); // ensure adding works + } + + /** + * Tests {@link MultiSetUtils#predicatedMultiSet(org.apache.commons.collections4.MultiSet, org.apache.commons.collections4.Predicate)}. + */ + @Test + public void testPredicatedMultiSet() { + Predicate<String> predicate = new Predicate<String>() { + @Override + public boolean evaluate(String object) { + return object.length() == 1; + }; + }; + MultiSet<String> predicated = MultiSetUtils.predicatedMultiSet(multiSet, predicate); + assertEquals(multiSet.size(), predicated.size()); + assertEquals(multiSet.getCount("a"), predicated.getCount("a")); + + try { + MultiSetUtils.predicatedMultiSet(null, predicate); + fail("Expecting NPE"); + } catch (NullPointerException e) { + } + + try { + MultiSetUtils.predicatedMultiSet(multiSet, null); + fail("Expecting NPE"); + } catch (NullPointerException e) { + } + + try { + MultiSetUtils.predicatedMultiSet(multiSet, new Predicate<String>() { + @Override + public boolean evaluate(String object) { + return object.equals("a"); + }; + }); + fail("Predicate is violated for all elements not being 'a'"); + } + catch (IllegalArgumentException iae) { + } + } +}