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