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.list;
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.assertNotNull;
22  import static org.junit.jupiter.api.Assertions.assertThrows;
23  import static org.junit.jupiter.api.Assertions.assertTrue;
24  
25  import java.util.ArrayList;
26  import java.util.List;
27  
28  import org.apache.commons.collections4.Predicate;
29  import org.apache.commons.collections4.functors.TruePredicate;
30  import org.junit.jupiter.api.Test;
31  
32  /**
33   * Extension of {@link AbstractListTest} for exercising the
34   * {@link PredicatedList} implementation.
35   */
36  public class PredicatedListTest<E> extends AbstractListTest<E> {
37  
38      protected Predicate<E> truePredicate = TruePredicate.<E>truePredicate();
39  
40      protected Predicate<E> testPredicate =
41          String.class::isInstance;
42  
43      protected List<E> decorateList(final List<E> list, final Predicate<E> predicate) {
44          return PredicatedList.predicatedList(list, 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 List<E> makeObject() {
60          return decorateList(new ArrayList<>(), truePredicate);
61      }
62  
63      public List<E> makeTestList() {
64          return decorateList(new ArrayList<>(), testPredicate);
65      }
66  
67      @Test
68      @SuppressWarnings("unchecked")
69      public void testIllegalAdd() {
70          final List<E> list = makeTestList();
71          final Integer i = Integer.valueOf(3);
72  
73          assertThrows(IllegalArgumentException.class, () -> list.add((E) i),
74                  "Integer should fail string predicate.");
75  
76          assertFalse(list.contains(i), "Collection shouldn't contain illegal element");
77      }
78  
79      @Test
80      @SuppressWarnings("unchecked")
81      public void testIllegalAddAll() {
82          final List<E> list = makeTestList();
83          final List<E> elements = new ArrayList<>();
84          elements.add((E) "one");
85          elements.add((E) "two");
86          elements.add((E) Integer.valueOf(3));
87          elements.add((E) "four");
88  
89          assertThrows(IllegalArgumentException.class, () -> list.addAll(0, elements),
90                  "Integer should fail string predicate.");
91  
92          assertFalse(list.contains("one"), "List shouldn't contain illegal element");
93          assertFalse(list.contains("two"), "List shouldn't contain illegal element");
94          assertFalse(list.contains(Integer.valueOf(3)), "List shouldn't contain illegal element");
95          assertFalse(list.contains("four"), "List shouldn't contain illegal element");
96      }
97  
98      @Test
99      @SuppressWarnings("unchecked")
100     public void testIllegalSet() {
101         final List<E> list = makeTestList();
102         assertThrows(IllegalArgumentException.class, () -> list.set(0, (E) Integer.valueOf(3)),
103                 "Integer should fail string predicate.");
104     }
105 
106     @Test
107     @SuppressWarnings("unchecked")
108     public void testLegalAddAll() {
109         final List<E> list = makeTestList();
110         list.add((E) "zero");
111         final List<E> elements = new ArrayList<>();
112         elements.add((E) "one");
113         elements.add((E) "two");
114         elements.add((E) "three");
115         list.addAll(1, elements);
116         assertTrue(list.contains("zero"), "List should contain legal element");
117         assertTrue(list.contains("one"), "List should contain legal element");
118         assertTrue(list.contains("two"), "List should contain legal element");
119         assertTrue(list.contains("three"), "List should contain legal element");
120     }
121 
122     @Test
123     public void testSubList() {
124         final List<E> list = makeTestList();
125         list.add((E) "zero");
126         //subList without any element of list
127         List<E> subList = list.subList(0, 0);
128         assertNotNull(subList);
129         assertEquals(0, subList.size());
130 
131         //subList with one element oif list
132         subList = list.subList(0, 1);
133         assertEquals(1, subList.size());
134 
135         final List<E> elements = new ArrayList<>();
136         elements.add((E) "one");
137         elements.add((E) "two");
138         elements.add((E) "three");
139         list.addAll(1, elements);
140         //subList with all elements of list
141         subList = list.subList(0, list.size());
142         assertEquals(list.size(), subList.size());
143     }
144 
145 //    public void testCreate() throws Exception {
146 //        resetEmpty();
147 //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/PredicatedList.emptyCollection.version4.obj");
148 //        resetFull();
149 //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/PredicatedList.fullCollection.version4.obj");
150 //    }
151 
152 }