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 java.util.ArrayList;
20  import java.util.Arrays;
21  import java.util.Collection;
22  import java.util.List;
23  
24  import org.apache.commons.collections4.Predicate;
25  import org.apache.commons.collections4.functors.TruePredicate;
26  
27  /**
28   * Extension of {@link AbstractCollectionTest} for exercising the
29   * {@link PredicatedCollection} implementation.
30   *
31   * @since 3.0
32   */
33  public class PredicatedCollectionTest<E> extends AbstractCollectionTest<E> {
34  
35      public PredicatedCollectionTest(final String name) {
36          super(name);
37      }
38  
39     //------------------------------------------------------------------------
40      protected Predicate<E> truePredicate = TruePredicate.<E>truePredicate();
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 Collection<E> makeObject() {
49          return decorateCollection(new ArrayList<E>(), truePredicate);
50      }
51  
52      @Override
53      public Collection<E> makeConfirmedCollection() {
54          return new ArrayList<>();
55      }
56  
57      @Override
58      @SuppressWarnings("unchecked")
59      public E[] getFullElements() {
60          return (E[]) new Object[] { "1", "3", "5", "7", "2", "4", "6" };
61      }
62  
63      @Override
64      public Collection<E> makeFullCollection() {
65          final List<E> list = new ArrayList<>();
66          list.addAll(Arrays.asList(getFullElements()));
67          return decorateCollection(list, truePredicate);
68      }
69  
70      @Override
71      public Collection<E> makeConfirmedFullCollection() {
72          final List<E> list = new ArrayList<>();
73          list.addAll(Arrays.asList(getFullElements()));
74          return list;
75      }
76  
77      //-----------------------------------------------------------------------
78      protected Predicate<E> testPredicate =
79          new Predicate<E>() {
80              @Override
81              public boolean evaluate(final E o) {
82                  return o instanceof String;
83              }
84          };
85  
86      public Collection<E> makeTestCollection() {
87          return decorateCollection(new ArrayList<E>(), testPredicate);
88      }
89  
90      @SuppressWarnings("unchecked")
91      public void testIllegalAdd() {
92          final Collection<E> c = makeTestCollection();
93          final Integer i = Integer.valueOf(3);
94          try {
95              c.add((E) i);
96              fail("Integer should fail string predicate.");
97          } catch (final IllegalArgumentException e) {
98              // expected
99          }
100         assertTrue("Collection shouldn't contain illegal element",
101          !c.contains(i));
102     }
103 
104     @SuppressWarnings("unchecked")
105     public void testIllegalAddAll() {
106         final Collection<E> c = makeTestCollection();
107         final List<E> elements = new ArrayList<>();
108         elements.add((E) "one");
109         elements.add((E) "two");
110         elements.add((E) Integer.valueOf(3));
111         elements.add((E) "four");
112         try {
113             c.addAll(elements);
114             fail("Integer should fail string predicate.");
115         } catch (final IllegalArgumentException e) {
116             // expected
117         }
118         assertTrue("Collection shouldn't contain illegal element", !c.contains("one"));
119         assertTrue("Collection shouldn't contain illegal element", !c.contains("two"));
120         assertTrue("Collection shouldn't contain illegal element", !c.contains(Integer.valueOf(3)));
121         assertTrue("Collection shouldn't contain illegal element", !c.contains("four"));
122     }
123 
124     @Override
125     public String getCompatibilityVersion() {
126         return "4";
127     }
128 
129 //    public void testCreate() throws Exception {
130 //        resetEmpty();
131 //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/PredicatedCollection.emptyCollection.version4.obj");
132 //        resetFull();
133 //        writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/PredicatedCollection.fullCollection.version4.obj");
134 //    }
135 
136 }