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.easymock.EasyMock.replay;
20  import static org.easymock.EasyMock.verify;
21  
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  import org.apache.commons.collections4.Predicate;
26  import org.easymock.EasyMock;
27  import org.junit.jupiter.api.AfterEach;
28  import org.junit.jupiter.api.BeforeEach;
29  
30  /**
31   * Base class for tests of predicates which delegate to other predicates when evaluating an object.  This class
32   * provides methods to create and verify mock predicates to which to delegate.
33   */
34  public abstract class AbstractMockPredicateTest<T> {
35      /**
36       * Mock predicates created by a single test case which need to be verified after the test completes.
37       */
38      private List<Predicate<? super T>> mockPredicatesToVerify;
39  
40      /**
41       * The value to pass to mocks.
42       */
43      private final T testValue;
44  
45      /**
46       * Creates a new {@code PredicateTestBase}.
47       *
48       * @param testValue the value to pass to mock predicates.
49       */
50      protected AbstractMockPredicateTest(final T testValue) {
51          this.testValue = testValue;
52      }
53  
54      /**
55       * Creates a single mock predicate.
56       *
57       * @param returnValue the return value for the mock predicate, or null if the mock is not expected to be called.
58       * @return a single mock predicate.
59       */
60      @SuppressWarnings({"boxing"})
61      protected final Predicate<T> createMockPredicate(final Boolean returnValue) {
62          final Predicate<T> mockPredicate = EasyMock.createMock(Predicate.class);
63          if (returnValue != null) {
64              EasyMock.expect(mockPredicate.test(testValue)).andReturn(returnValue);
65          }
66          replay(mockPredicate);
67          mockPredicatesToVerify.add(mockPredicate);
68  
69          return mockPredicate;
70      }
71  
72      /**
73       * Creates the list of predicates to verify.
74       */
75      @BeforeEach
76      public final void createVerifyList() {
77          mockPredicatesToVerify = new ArrayList<>();
78      }
79  
80      /**
81       * Gets the value which will be passed to the mock predicates.
82       *
83       * @return the test value.
84       */
85      protected final T getTestValue() {
86          return testValue;
87      }
88  
89      /**
90       * Verifies all the mock predicates created for the test.
91       */
92      @AfterEach
93      public final void verifyPredicates() {
94          for (final Predicate<? super T> predicate : mockPredicatesToVerify) {
95              verify(predicate);
96          }
97      }
98  }