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.set;
18  
19  import static org.junit.jupiter.api.Assertions.assertFalse;
20  import static org.junit.jupiter.api.Assertions.assertNotNull;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  
23  import java.util.HashSet;
24  import java.util.Set;
25  
26  import org.apache.commons.collections4.Predicate;
27  import org.apache.commons.collections4.functors.TruePredicate;
28  import org.junit.jupiter.api.Test;
29  
30  /**
31   * Extension of {@link AbstractSetTest} for exercising the
32   * {@link PredicatedSet} implementation.
33   */
34  public class PredicatedSetTest<E> extends AbstractSetTest<E> {
35  
36      protected Predicate<E> truePredicate = TruePredicate.<E>truePredicate();
37  
38      protected Predicate<E> testPredicate =
39          String.class::isInstance;
40  
41      protected PredicatedSet<E> decorateSet(final Set<E> set, final Predicate<? super E> predicate) {
42          return PredicatedSet.predicatedSet(set, predicate);
43      }
44  
45      @Override
46      public String getCompatibilityVersion() {
47          return "4";
48      }
49  
50      @Override
51      @SuppressWarnings("unchecked")
52      public E[] getFullElements() {
53          return (E[]) new Object[] {"1", "3", "5", "7", "2", "4", "6"};
54      }
55  
56      @Override
57      protected int getIterationBehaviour() {
58          return UNORDERED;
59      }
60  
61      @Override
62      public PredicatedSet<E> makeObject() {
63          return decorateSet(new HashSet<>(), truePredicate);
64      }
65  
66      protected PredicatedSet<E> makeTestSet() {
67          return decorateSet(new HashSet<>(), testPredicate);
68      }
69  
70      @Test
71      public void testGetSet() {
72          final PredicatedSet<E> set = makeTestSet();
73          assertNotNull(set.decorated(), "returned set should not be null");
74      }
75  
76      @Test
77      @SuppressWarnings("unchecked")
78      public void testIllegalAdd() {
79          final Set<E> set = makeTestSet();
80          final Integer i = Integer.valueOf(3);
81          assertThrows(IllegalArgumentException.class, () -> set.add((E) i),
82                  "Integer should fail string predicate.");
83          assertFalse(set.contains(i), "Collection shouldn't contain illegal element");
84      }
85  
86      @Test
87      @SuppressWarnings("unchecked")
88      public void testIllegalAddAll() {
89          final Set<E> set = makeTestSet();
90          final Set<E> elements = new HashSet<>();
91          elements.add((E) "one");
92          elements.add((E) "two");
93          elements.add((E) Integer.valueOf(3));
94          elements.add((E) "four");
95          assertThrows(IllegalArgumentException.class, () -> set.addAll(elements),
96                  "Integer should fail string predicate.");
97          assertFalse(set.contains("one"), "Set shouldn't contain illegal element");
98          assertFalse(set.contains("two"), "Set shouldn't contain illegal element");
99          assertFalse(set.contains(Integer.valueOf(3)), "Set shouldn't contain illegal element");
100         assertFalse(set.contains("four"), "Set shouldn't contain illegal element");
101     }
102 
103 //    public void testCreate() throws Exception {
104 //        resetEmpty();
105 //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/PredicatedSet.emptyCollection.version4.obj");
106 //        resetFull();
107 //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/PredicatedSet.fullCollection.version4.obj");
108 //    }
109 
110 }