View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.collections4;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertThrows;
21  
22  import java.util.Arrays;
23  
24  import org.apache.commons.collections4.multiset.HashMultiSet;
25  import org.junit.jupiter.api.BeforeEach;
26  import org.junit.jupiter.api.Test;
27  
28  /**
29   * Tests for MultiSetUtils.
30   */
31  public class MultiSetUtilsTest {
32  
33      private String[] fullArray;
34      private MultiSet<String> multiSet;
35  
36      @BeforeEach
37      public void setUp() {
38          fullArray = new String[]{
39              "a", "a", "b", "c", "d", "d", "d"
40          };
41          multiSet = new HashMultiSet<>(Arrays.asList(fullArray));
42      }
43  
44      /**
45       * Tests {@link MultiSetUtils#emptyMultiSet()}.
46       */
47      @Test
48      public void testEmptyMultiSet() {
49          final MultiSet<Integer> empty = MultiSetUtils.emptyMultiSet();
50          assertEquals(0, empty.size());
51  
52          assertThrows(UnsupportedOperationException.class, () -> empty.add(55),
53                  "Empty multi set must be read-only");
54      }
55  
56      /**
57       * Tests {@link MultiSetUtils#predicatedMultiSet(org.apache.commons.collections4.MultiSet, org.apache.commons.collections4.Predicate)}.
58       */
59      @Test
60      public void testPredicatedMultiSet() {
61          final Predicate<String> predicate = object -> object.length() == 1;
62          final MultiSet<String> predicated = MultiSetUtils.predicatedMultiSet(multiSet, predicate);
63          assertEquals(multiSet.size(), predicated.size());
64          assertEquals(multiSet.getCount("a"), predicated.getCount("a"));
65  
66          assertThrows(NullPointerException.class, () -> MultiSetUtils.predicatedMultiSet(null, predicate),
67                  "Expecting NPE");
68  
69          assertThrows(NullPointerException.class, () -> MultiSetUtils.predicatedMultiSet(multiSet, null),
70                  "Expecting NPE");
71  
72          assertThrows(IllegalArgumentException.class, () -> MultiSetUtils.predicatedMultiSet(multiSet, object -> object.equals("a")),
73                  "Predicate is violated for all elements not being 'a'");
74      }
75  
76      /**
77       * Tests {@link MultiSetUtils#unmodifiableMultiSet(org.apache.commons.collections4.MultiSet) ()}.
78       */
79      @Test
80      public void testSynchronizedMultiSet() {
81          final MultiSet<String> synced = MultiSetUtils.synchronizedMultiSet(multiSet);
82          assertEquals(multiSet, synced);
83          synced.add("a"); // ensure adding works
84      }
85  
86      /**
87       * Tests {@link MultiSetUtils#unmodifiableMultiSet(org.apache.commons.collections4.MultiSet) ()}.
88       */
89      @Test
90      public void testUnmodifiableMultiSet() {
91          final MultiSet<String> unmodifiable = MultiSetUtils.unmodifiableMultiSet(multiSet);
92          assertEquals(multiSet, unmodifiable);
93  
94          assertThrows(UnsupportedOperationException.class, () -> unmodifiable.add("a"),
95                  "Empty multi set must be read-only");
96  
97          assertThrows(NullPointerException.class, () -> MultiSetUtils.unmodifiableMultiSet(null),
98                  "Expecting NPE");
99      }
100 
101 }