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