1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.collections4.collection;
18
19 import static org.junit.jupiter.api.Assertions.assertFalse;
20 import static org.junit.jupiter.api.Assertions.assertThrows;
21
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.Collection;
25 import java.util.List;
26
27 import org.apache.commons.collections4.Predicate;
28 import org.apache.commons.collections4.functors.TruePredicate;
29 import org.junit.jupiter.api.Test;
30
31
32
33
34
35 public class PredicatedCollectionTest<E> extends AbstractCollectionTest<E> {
36
37 protected Predicate<E> truePredicate = TruePredicate.<E>truePredicate();
38
39 protected Predicate<E> testPredicate =
40 String.class::isInstance;
41
42 protected Collection<E> decorateCollection(
43 final Collection<E> collection, final Predicate<E> predicate) {
44 return PredicatedCollection.predicatedCollection(collection, predicate);
45 }
46
47 @Override
48 public String getCompatibilityVersion() {
49 return "4";
50 }
51
52 @Override
53 @SuppressWarnings("unchecked")
54 public E[] getFullElements() {
55 return (E[]) new Object[] { "1", "3", "5", "7", "2", "4", "6" };
56 }
57
58 @Override
59 public Collection<E> makeConfirmedCollection() {
60 return new ArrayList<>();
61 }
62
63 @Override
64 public Collection<E> makeConfirmedFullCollection() {
65 return new ArrayList<>(Arrays.asList(getFullElements()));
66 }
67
68 @Override
69 public Collection<E> makeFullCollection() {
70 final List<E> list = new ArrayList<>(Arrays.asList(getFullElements()));
71 return decorateCollection(list, truePredicate);
72 }
73
74 @Override
75 public Collection<E> makeObject() {
76 return decorateCollection(new ArrayList<>(), truePredicate);
77 }
78
79 public Collection<E> makeTestCollection() {
80 return decorateCollection(new ArrayList<>(), testPredicate);
81 }
82
83 @Test
84 @SuppressWarnings("unchecked")
85 public void testIllegalAdd() {
86 final Collection<E> c = makeTestCollection();
87 final Integer i = 3;
88
89 assertThrows(IllegalArgumentException.class, () -> c.add((E) i), "Integer should fail string predicate.");
90
91 assertFalse(c.contains(i), "Collection shouldn't contain illegal element");
92 }
93
94 @Test
95 @SuppressWarnings("unchecked")
96 public void testIllegalAddAll() {
97 final Collection<E> c = makeTestCollection();
98 final List<E> elements = new ArrayList<>();
99 elements.add((E) "one");
100 elements.add((E) "two");
101 elements.add((E) Integer.valueOf(3));
102 elements.add((E) "four");
103
104 assertThrows(IllegalArgumentException.class, () -> c.addAll(elements), "Integer should fail string predicate.");
105
106 assertFalse(c.contains("one"), "Collection shouldn't contain illegal element");
107 assertFalse(c.contains("two"), "Collection shouldn't contain illegal element");
108 assertFalse(c.contains(3), "Collection shouldn't contain illegal element");
109 assertFalse(c.contains("four"), "Collection shouldn't contain illegal element");
110 }
111
112
113
114
115
116
117
118
119 }