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.apache.commons.collections4.functors.AllPredicate.allPredicate;
20  import static org.junit.jupiter.api.Assertions.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertTrue;
22  
23  import java.util.Collection;
24  import java.util.Collections;
25  
26  import org.apache.commons.collections4.Predicate;
27  import org.junit.jupiter.api.Test;
28  
29  /**
30   * Tests the org.apache.commons.collections.functors.AllPredicate class.
31   */
32  @SuppressWarnings("boxing")
33  public class AllPredicateTest extends AbstractAnyAllOnePredicateTest<Integer> {
34  
35      /**
36       * Creates a new {@code TestAllPredicate}.
37       */
38      public AllPredicateTest() {
39          super(42);
40      }
41  
42      /**
43       * {@inheritDoc}
44       */
45      @Override
46      protected final Predicate<Integer> getPredicateInstance(final Collection<Predicate<Integer>> predicates) {
47          return AllPredicate.allPredicate(predicates);
48      }
49  
50      /**
51       * {@inheritDoc}
52       */
53      @Override
54      protected final Predicate<Integer> getPredicateInstance(final Predicate<? super Integer>... predicates) {
55          return AllPredicate.allPredicate(predicates);
56      }
57  
58      /**
59       * Tests whether multiple true predicates evaluates to true.
60       */
61      @Test
62      public void testAllTrue() {
63          assertTrue(getPredicateInstance(true, true).evaluate(getTestValue()),
64                  "multiple true predicates evaluated to false");
65          assertTrue(getPredicateInstance(true, true, true).evaluate(getTestValue()),
66                  "multiple true predicates evaluated to false");
67      }
68  
69      /**
70       * Verifies that providing an empty predicate array evaluates to true.
71       */
72      @SuppressWarnings({"unchecked"})
73      @Test
74      public void testEmptyArrayToGetInstance() {
75          assertTrue(getPredicateInstance(new Predicate[] {}).evaluate(null), "empty array not true");
76      }
77  
78      /**
79       * Verifies that providing an empty predicate collection evaluates to true.
80       */
81      @Test
82      public void testEmptyCollectionToGetInstance() {
83          final Predicate<Integer> allPredicate = getPredicateInstance(
84                  Collections.<Predicate<Integer>>emptyList());
85          assertTrue(allPredicate.evaluate(getTestValue()), "empty collection not true");
86      }
87  
88      /**
89       * Tests whether a single false predicate evaluates to true.
90       */
91      @SuppressWarnings("unchecked")
92      @Test
93      public void testOneFalsePredicate() {
94          // use the constructor directly, as getInstance() returns the original predicate when passed
95          // an array of size one.
96          final Predicate<Integer> predicate = createMockPredicate(false);
97          assertFalse(allPredicate(predicate).evaluate(getTestValue()),
98                  "single false predicate evaluated to true");
99      }
100 
101     /**
102      * Tests whether a single true predicate evaluates to true.
103      */
104     @SuppressWarnings("unchecked")
105     @Test
106     public void testOneTruePredicate() {
107         // use the constructor directly, as getInstance() returns the original predicate when passed
108         // an array of size one.
109         final Predicate<Integer> predicate = createMockPredicate(true);
110 
111         assertTrue(allPredicate(predicate).evaluate(getTestValue()), "single true predicate evaluated to false");
112     }
113 
114     /**
115      * Tests whether combining some true and one false evaluates to false.  Also verifies that only the first
116      * false predicate is actually evaluated
117      */
118     @Test
119     public void testTrueAndFalseCombined() {
120         assertFalse(getPredicateInstance(false, null).evaluate(getTestValue()),
121                 "false predicate evaluated to true");
122         assertFalse(getPredicateInstance(false, null, null).evaluate(getTestValue()),
123                 "false predicate evaluated to true");
124         assertFalse(getPredicateInstance(true, false, null).evaluate(getTestValue()),
125                 "false predicate evaluated to true");
126         assertFalse(getPredicateInstance(true, true, false).evaluate(getTestValue()),
127                 "false predicate evaluated to true");
128         assertFalse(getPredicateInstance(true, true, false, null).evaluate(getTestValue()),
129                 "false predicate evaluated to true");
130     }
131 }