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.collection;
18  
19  import static org.junit.jupiter.api.Assertions.assertFalse;
20  import static org.junit.jupiter.api.Assertions.assertThrows;
21  
22  import java.util.ArrayList;
23  import java.util.Arrays;
24  import java.util.Collection;
25  import java.util.List;
26  
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 AbstractCollectionTest} for exercising the
33   * {@link PredicatedCollection} implementation.
34   */
35  public class PredicatedCollectionTest<E> extends AbstractCollectionTest<E> {
36  
37      protected Predicate<E> truePredicate = TruePredicate.<E>truePredicate();
38  
39      protected Predicate<E> testPredicate =
40          String.class::isInstance;
41  
42      protected Collection<E> decorateCollection(
43                  final Collection<E> collection, final Predicate<E> predicate) {
44          return PredicatedCollection.predicatedCollection(collection, predicate);
45      }
46  
47      @Override
48      public String getCompatibilityVersion() {
49          return "4";
50      }
51  
52      @Override
53      @SuppressWarnings("unchecked")
54      public E[] getFullElements() {
55          return (E[]) new Object[] { "1", "3", "5", "7", "2", "4", "6" };
56      }
57  
58      @Override
59      public Collection<E> makeConfirmedCollection() {
60          return new ArrayList<>();
61      }
62  
63      @Override
64      public Collection<E> makeConfirmedFullCollection() {
65          return new ArrayList<>(Arrays.asList(getFullElements()));
66      }
67  
68      @Override
69      public Collection<E> makeFullCollection() {
70          final List<E> list = new ArrayList<>(Arrays.asList(getFullElements()));
71          return decorateCollection(list, truePredicate);
72      }
73  
74      @Override
75      public Collection<E> makeObject() {
76          return decorateCollection(new ArrayList<>(), truePredicate);
77      }
78  
79      public Collection<E> makeTestCollection() {
80          return decorateCollection(new ArrayList<>(), testPredicate);
81      }
82  
83      @Test
84      @SuppressWarnings("unchecked")
85      public void testIllegalAdd() {
86          final Collection<E> c = makeTestCollection();
87          final Integer i = 3;
88  
89          assertThrows(IllegalArgumentException.class, () -> c.add((E) i), "Integer should fail string predicate.");
90  
91          assertFalse(c.contains(i), "Collection shouldn't contain illegal element");
92      }
93  
94      @Test
95      @SuppressWarnings("unchecked")
96      public void testIllegalAddAll() {
97          final Collection<E> c = makeTestCollection();
98          final List<E> elements = new ArrayList<>();
99          elements.add((E) "one");
100         elements.add((E) "two");
101         elements.add((E) Integer.valueOf(3));
102         elements.add((E) "four");
103 
104         assertThrows(IllegalArgumentException.class, () -> c.addAll(elements), "Integer should fail string predicate.");
105 
106         assertFalse(c.contains("one"), "Collection shouldn't contain illegal element");
107         assertFalse(c.contains("two"), "Collection shouldn't contain illegal element");
108         assertFalse(c.contains(3), "Collection shouldn't contain illegal element");
109         assertFalse(c.contains("four"), "Collection shouldn't contain illegal element");
110     }
111 
112 //    public void testCreate() throws Exception {
113 //        resetEmpty();
114 //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/PredicatedCollection.emptyCollection.version4.obj");
115 //        resetFull();
116 //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/PredicatedCollection.fullCollection.version4.obj");
117 //    }
118 
119 }