1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
32
33 public abstract class AbstractCompositePredicateTest<T> extends AbstractMockPredicateTest<T> {
34
35
36
37
38
39
40 protected AbstractCompositePredicateTest(final T testValue) {
41 super(testValue);
42 }
43
44
45
46
47
48
49
50
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
62
63
64
65
66 protected abstract Predicate<T> getPredicateInstance(Collection<Predicate<T>> predicates);
67
68
69
70
71
72
73
74 protected abstract Predicate<T> getPredicateInstance(Predicate<? super T>... predicates);
75
76
77
78
79 @Test
80 public final void nullArrayToGetInstance() {
81 assertThrows(NullPointerException.class, () -> getPredicateInstance((Predicate<T>[]) null));
82 }
83
84
85
86
87 @Test
88 public final void nullCollectionToGetInstance() {
89 assertThrows(NullPointerException.class, () -> getPredicateInstance((Collection<Predicate<T>>) null));
90 }
91
92
93
94
95 @SuppressWarnings({"unchecked"})
96 @Test
97 public final void nullElementInArrayToGetInstance() {
98 assertThrows(NullPointerException.class, () -> getPredicateInstance(new Predicate[] { null }));
99 }
100
101
102
103
104 @SuppressWarnings({"unchecked"})
105 @Test
106 public final void nullElementsInArrayToGetInstance() {
107 assertThrows(NullPointerException.class, () -> getPredicateInstance(new Predicate[] { null, null }));
108 }
109
110
111
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
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
133
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 }