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.multiset;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import java.util.Set;
25  
26  import org.apache.commons.collections4.MultiSet;
27  import org.apache.commons.collections4.Predicate;
28  import org.apache.commons.collections4.functors.TruePredicate;
29  import org.junit.jupiter.api.Test;
30  
31  /**
32   * Extension of {@link AbstractMultiSetTest} for exercising the
33   * {@link PredicatedMultiSet} implementation.
34   */
35  public class PredicatedMultiSetTest<T> extends AbstractMultiSetTest<T> {
36  
37      protected Predicate<T> truePredicate = TruePredicate.<T>truePredicate();
38  
39      public PredicatedMultiSetTest() {
40          super(PredicatedMultiSetTest.class.getSimpleName());
41      }
42  
43      protected MultiSet<T> decorateMultiSet(final HashMultiSet<T> multiset, final Predicate<T> predicate) {
44          return PredicatedMultiSet.predicatedMultiSet(multiset, predicate);
45      }
46  
47      @Override
48      public String getCompatibilityVersion() {
49          return "4.1";
50      }
51  
52      @Override
53      protected int getIterationBehaviour() {
54          return UNORDERED;
55      }
56  
57      @Override
58      public MultiSet<T> makeObject() {
59          return decorateMultiSet(new HashMultiSet<>(), truePredicate);
60      }
61  
62      protected MultiSet<T> makeTestMultiSet() {
63          return decorateMultiSet(new HashMultiSet<>(), stringPredicate());
64      }
65  
66      protected Predicate<T> stringPredicate() {
67          return String.class::isInstance;
68      }
69  
70      @Test
71      @SuppressWarnings("unchecked")
72      public void testIllegalAdd() {
73          final MultiSet<T> multiset = makeTestMultiSet();
74          final Integer i = Integer.valueOf(3);
75          assertThrows(IllegalArgumentException.class, () -> multiset.add((T) i),
76                  "Integer should fail string predicate.");
77          assertFalse(multiset.contains(i), "Collection shouldn't contain illegal element");
78      }
79  
80      @Test
81      @SuppressWarnings("unchecked")
82      public void testIllegalDecorate() {
83          final HashMultiSet<Object> elements = new HashMultiSet<>();
84          elements.add("one");
85          elements.add("two");
86          elements.add(Integer.valueOf(3));
87          elements.add("four");
88          assertThrows(IllegalArgumentException.class, () -> decorateMultiSet((HashMultiSet<T>) elements, stringPredicate()),
89                  "MultiSet contains an element that should fail the predicate.");
90          assertThrows(NullPointerException.class, () -> decorateMultiSet(new HashMultiSet<>(), null),
91                  "Expecting NullPointerException for null predicate.");
92      }
93  
94      @Test
95      @SuppressWarnings("unchecked")
96      public void testLegalAddRemove() {
97          final MultiSet<T> multiset = makeTestMultiSet();
98          assertEquals(0, multiset.size());
99          final T[] els = (T[]) new Object[] { "1", "3", "5", "7", "2", "4", "1" };
100         for (int i = 0; i < els.length; i++) {
101             multiset.add(els[i]);
102             assertEquals(i + 1, multiset.size());
103             assertTrue(multiset.contains(els[i]));
104         }
105         Set<T> set = ((PredicatedMultiSet<T>) multiset).uniqueSet();
106         assertTrue(set.contains(els[0]), "Unique set contains the first element");
107         assertTrue(multiset.remove(els[0]));
108         set = ((PredicatedMultiSet<T>) multiset).uniqueSet();
109         assertTrue(set.contains(els[0]),
110             "Unique set does not contain anymore the first element");
111     }
112 
113 //    public void testCreate() throws Exception {
114 //        MultiSet<T> multiset = makeObject();
115 //        writeExternalFormToDisk((java.io.Serializable) multiset, "src/test/resources/data/test/PredicatedMultiSet.emptyCollection.version4.1.obj");
116 //        multiset = makeFullCollection();
117 //        writeExternalFormToDisk((java.io.Serializable) multiset, "src/test/resources/data/test/PredicatedMultiSet.fullCollection.version4.1.obj");
118 //    }
119 
120 }