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.functors;
18  
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.Collections;
22  import java.util.List;
23  
24  import org.apache.commons.collections4.Predicate;
25  import org.junit.Assert;
26  import org.junit.Test;
27  
28  /**
29   * Base class for tests of composite predicates.
30   *
31   * @since 3.0
32   */
33  public abstract class AbstractCompositePredicateTest<T> extends AbstractMockPredicateTest<T> {
34  
35      /**
36       * Creates a new <code>TestCompositePredicate</code>.
37       *
38       * @param testValue the value which the mock predicates should expect to see (may be null).
39       */
40      protected AbstractCompositePredicateTest(final T testValue) {
41          super(testValue);
42      }
43  
44      /**
45       * Creates an instance of the predicate to test.
46       *
47       * @param predicates the arguments to <code>getInstance</code>.
48       *
49       * @return a predicate to test.
50       */
51      protected abstract Predicate<T> getPredicateInstance(final Predicate<? super T> ... predicates);
52  
53      /**
54       * Creates an instance of the predicate to test.
55       *
56       * @param predicates the argument to <code>getInstance</code>.
57       *
58       * @return a predicate to test.
59       */
60      protected abstract Predicate<T> getPredicateInstance(final Collection<Predicate<T>> predicates);
61  
62      /**
63       * Creates an instance of the predicate to test.
64       *
65       * @param mockReturnValues the return values for the mock predicates, or null if that mock is not expected
66       *                         to be called
67       *
68       * @return a predicate to test.
69       */
70      protected final Predicate<T> getPredicateInstance(final Boolean... mockReturnValues) {
71          final List<Predicate<T>> predicates = new ArrayList<>();
72          for (final Boolean returnValue : mockReturnValues) {
73              predicates.add(createMockPredicate(returnValue));
74          }
75          return getPredicateInstance(predicates);
76      }
77  
78      /**
79       * Tests whether <code>getInstance</code> with a one element array returns the first element in the array.
80       */
81      @SuppressWarnings("unchecked")
82      public void singleElementArrayToGetInstance() {
83          final Predicate<T> predicate = createMockPredicate(null);
84          final Predicate<T> allPredicate = getPredicateInstance(predicate);
85          Assert.assertSame("expected argument to be returned by getInstance()", predicate, allPredicate);
86      }
87  
88      /**
89       * Tests that passing a singleton collection to <code>getInstance</code> returns the single element in the
90       * collection.
91       */
92      public void singletonCollectionToGetInstance() {
93          final Predicate<T> predicate = createMockPredicate(null);
94          final Predicate<T> allPredicate = getPredicateInstance(
95                  Collections.<Predicate<T>>singleton(predicate));
96          Assert.assertSame("expected argument to be returned by getInstance()", predicate, allPredicate);
97      }
98  
99      /**
100      * Tests <code>getInstance</code> with a null predicate array.
101      */
102     @Test(expected = NullPointerException.class)
103     public final void nullArrayToGetInstance() {
104         getPredicateInstance((Predicate<T>[]) null);
105     }
106 
107     /**
108      * Tests <code>getInstance</code> with a single null element in the predicate array.
109      */
110     @SuppressWarnings({"unchecked"})
111     @Test(expected = NullPointerException.class)
112     public final void nullElementInArrayToGetInstance() {
113         getPredicateInstance(new Predicate[] { null });
114     }
115 
116     /**
117      * Tests <code>getInstance</code> with two null elements in the predicate array.
118      */
119     @SuppressWarnings({"unchecked"})
120     @Test(expected = NullPointerException.class)
121     public final void nullElementsInArrayToGetInstance() {
122         getPredicateInstance(new Predicate[] { null, null });
123     }
124 
125 
126     /**
127      * Tests <code>getInstance</code> with a null predicate collection
128      */
129     @Test(expected = NullPointerException.class)
130     public final void nullCollectionToGetInstance() {
131         getPredicateInstance((Collection<Predicate<T>>) null);
132     }
133 
134     /**
135      * Tests <code>getInstance</code> with a predicate collection that contains null elements
136      */
137     @Test(expected = NullPointerException.class)
138     public final void nullElementsInCollectionToGetInstance() {
139         final Collection<Predicate<T>> coll = new ArrayList<>();
140         coll.add(null);
141         coll.add(null);
142         getPredicateInstance(coll);
143     }
144 }