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