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.bag;
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.Bag;
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 AbstractBagTest} for exercising the {@link PredicatedBag}
33   * implementation.
34   */
35  public class PredicatedBagTest<T> extends AbstractBagTest<T> {
36  
37      protected Predicate<T> truePredicate = TruePredicate.<T>truePredicate();
38  
39      public PredicatedBagTest() {
40          super(PredicatedBagTest.class.getSimpleName());
41      }
42  
43      protected Bag<T> decorateBag(final HashBag<T> bag, final Predicate<T> predicate) {
44          return PredicatedBag.predicatedBag(bag, predicate);
45      }
46  
47      @Override
48      public String getCompatibilityVersion() {
49          return "4";
50      }
51  
52      @Override
53      protected int getIterationBehaviour() {
54          return UNORDERED;
55      }
56  
57      @Override
58      public Bag<T> makeObject() {
59          return decorateBag(new HashBag<>(), truePredicate);
60      }
61  
62      protected Bag<T> makeTestBag() {
63          return decorateBag(new HashBag<>(), 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 Bag<T> bag = makeTestBag();
74          final Integer i = 3;
75  
76          assertThrows(IllegalArgumentException.class, () -> bag.add((T) i));
77  
78          assertFalse(bag.contains(i), "Collection shouldn't contain illegal element");
79      }
80  
81      @Test
82      @SuppressWarnings("unchecked")
83      public void testIllegalDecorate() {
84          final HashBag<Object> elements = new HashBag<>();
85          elements.add("one");
86          elements.add("two");
87          elements.add(3);
88          elements.add("four");
89  
90          assertThrows(IllegalArgumentException.class, () -> decorateBag((HashBag<T>) elements, stringPredicate()));
91  
92          assertThrows(NullPointerException.class, () -> decorateBag(new HashBag<>(), null));
93      }
94  
95      @Test
96      @SuppressWarnings("unchecked")
97      public void testLegalAddRemove() {
98          final Bag<T> bag = makeTestBag();
99          assertEquals(0, bag.size());
100         final T[] els = (T[]) new Object[] { "1", "3", "5", "7", "2", "4", "1" };
101         for (int i = 0; i < els.length; i++) {
102             bag.add(els[i]);
103             assertEquals(i + 1, bag.size());
104             assertTrue(bag.contains(els[i]));
105         }
106         Set<T> set = bag.uniqueSet();
107         assertTrue(set.contains(els[0]), "Unique set contains the first element");
108         assertTrue(bag.remove(els[0]));
109         set = bag.uniqueSet();
110         assertFalse(set.contains(els[0]), "Unique set now does not contain the first element");
111     }
112 
113 //    public void testCreate() throws Exception {
114 //        Bag<T> bag = makeObject();
115 //        writeExternalFormToDisk((java.io.Serializable) bag, "src/test/resources/data/test/PredicatedBag.emptyCollection.version4.obj");
116 //        bag = makeFullCollection();
117 //        writeExternalFormToDisk((java.io.Serializable) bag, "src/test/resources/data/test/PredicatedBag.fullCollection.version4.obj");
118 //    }
119 
120 }