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      public PredicatedSetTest() {
42          super(PredicatedSetTest.class.getSimpleName());
43      }
44  
45      protected PredicatedSet<E> decorateSet(final Set<E> set, final Predicate<? super E> predicate) {
46          return PredicatedSet.predicatedSet(set, predicate);
47      }
48  
49      @Override
50      public String getCompatibilityVersion() {
51          return "4";
52      }
53  
54      @Override
55      @SuppressWarnings("unchecked")
56      public E[] getFullElements() {
57          return (E[]) new Object[] {"1", "3", "5", "7", "2", "4", "6"};
58      }
59  
60      @Override
61      protected int getIterationBehaviour() {
62          return UNORDERED;
63      }
64  
65      @Override
66      public PredicatedSet<E> makeObject() {
67          return decorateSet(new HashSet<>(), truePredicate);
68      }
69  
70      protected PredicatedSet<E> makeTestSet() {
71          return decorateSet(new HashSet<>(), testPredicate);
72      }
73  
74      @Test
75      public void testGetSet() {
76          final PredicatedSet<E> set = makeTestSet();
77          assertNotNull(set.decorated(), "returned set should not be null");
78      }
79  
80      @Test
81      @SuppressWarnings("unchecked")
82      public void testIllegalAdd() {
83          final Set<E> set = makeTestSet();
84          final Integer i = Integer.valueOf(3);
85          assertThrows(IllegalArgumentException.class, () -> set.add((E) i),
86                  "Integer should fail string predicate.");
87          assertFalse(set.contains(i), "Collection shouldn't contain illegal element");
88      }
89  
90      @Test
91      @SuppressWarnings("unchecked")
92      public void testIllegalAddAll() {
93          final Set<E> set = makeTestSet();
94          final Set<E> elements = new HashSet<>();
95          elements.add((E) "one");
96          elements.add((E) "two");
97          elements.add((E) Integer.valueOf(3));
98          elements.add((E) "four");
99          assertThrows(IllegalArgumentException.class, () -> set.addAll(elements),
100                 "Integer should fail string predicate.");
101         assertFalse(set.contains("one"), "Set shouldn't contain illegal element");
102         assertFalse(set.contains("two"), "Set shouldn't contain illegal element");
103         assertFalse(set.contains(Integer.valueOf(3)), "Set shouldn't contain illegal element");
104         assertFalse(set.contains("four"), "Set shouldn't contain illegal element");
105     }
106 
107 //    public void testCreate() throws Exception {
108 //        resetEmpty();
109 //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/PredicatedSet.emptyCollection.version4.obj");
110 //        resetFull();
111 //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/PredicatedSet.fullCollection.version4.obj");
112 //    }
113 
114 }