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      public PredicatedListTest() {
44          super(PredicatedListTest.class.getSimpleName());
45      }
46  
47      protected List<E> decorateList(final List<E> list, final Predicate<E> predicate) {
48          return PredicatedList.predicatedList(list, predicate);
49      }
50  
51      @Override
52      public String getCompatibilityVersion() {
53          return "4";
54      }
55  
56      @Override
57      @SuppressWarnings("unchecked")
58      public E[] getFullElements() {
59          return (E[]) new Object[] { "1", "3", "5", "7", "2", "4", "6" };
60      }
61  
62      @Override
63      public List<E> makeObject() {
64          return decorateList(new ArrayList<>(), truePredicate);
65      }
66  
67      public List<E> makeTestList() {
68          return decorateList(new ArrayList<>(), testPredicate);
69      }
70  
71      @Test
72      @SuppressWarnings("unchecked")
73      public void testIllegalAdd() {
74          final List<E> list = makeTestList();
75          final Integer i = Integer.valueOf(3);
76  
77          assertThrows(IllegalArgumentException.class, () -> list.add((E) i),
78                  "Integer should fail string predicate.");
79  
80          assertFalse(list.contains(i), "Collection shouldn't contain illegal element");
81      }
82  
83      @Test
84      @SuppressWarnings("unchecked")
85      public void testIllegalAddAll() {
86          final List<E> list = makeTestList();
87          final List<E> elements = new ArrayList<>();
88          elements.add((E) "one");
89          elements.add((E) "two");
90          elements.add((E) Integer.valueOf(3));
91          elements.add((E) "four");
92  
93          assertThrows(IllegalArgumentException.class, () -> list.addAll(0, elements),
94                  "Integer should fail string predicate.");
95  
96          assertFalse(list.contains("one"), "List shouldn't contain illegal element");
97          assertFalse(list.contains("two"), "List shouldn't contain illegal element");
98          assertFalse(list.contains(Integer.valueOf(3)), "List shouldn't contain illegal element");
99          assertFalse(list.contains("four"), "List shouldn't contain illegal element");
100     }
101 
102     @Test
103     @SuppressWarnings("unchecked")
104     public void testIllegalSet() {
105         final List<E> list = makeTestList();
106         assertThrows(IllegalArgumentException.class, () -> list.set(0, (E) Integer.valueOf(3)),
107                 "Integer should fail string predicate.");
108     }
109 
110     @Test
111     @SuppressWarnings("unchecked")
112     public void testLegalAddAll() {
113         final List<E> list = makeTestList();
114         list.add((E) "zero");
115         final List<E> elements = new ArrayList<>();
116         elements.add((E) "one");
117         elements.add((E) "two");
118         elements.add((E) "three");
119         list.addAll(1, elements);
120         assertTrue(list.contains("zero"), "List should contain legal element");
121         assertTrue(list.contains("one"), "List should contain legal element");
122         assertTrue(list.contains("two"), "List should contain legal element");
123         assertTrue(list.contains("three"), "List should contain legal element");
124     }
125 
126     @Test
127     public void testSubList() {
128         final List<E> list = makeTestList();
129         list.add((E) "zero");
130         //subList without any element of list
131         List<E> subList = list.subList(0, 0);
132         assertNotNull(subList);
133         assertEquals(0, subList.size());
134 
135         //subList with one element oif list
136         subList = list.subList(0, 1);
137         assertEquals(1, subList.size());
138 
139         final List<E> elements = new ArrayList<>();
140         elements.add((E) "one");
141         elements.add((E) "two");
142         elements.add((E) "three");
143         list.addAll(1, elements);
144         //subList with all elements of list
145         subList = list.subList(0, list.size());
146         assertEquals(list.size(), subList.size());
147     }
148 
149 //    public void testCreate() throws Exception {
150 //        resetEmpty();
151 //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/PredicatedList.emptyCollection.version4.obj");
152 //        resetFull();
153 //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/PredicatedList.fullCollection.version4.obj");
154 //    }
155 
156 }