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 java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.Collection;
22 import java.util.List;
23
24 import org.apache.commons.collections4.Predicate;
25 import org.apache.commons.collections4.functors.TruePredicate;
26
27
28
29
30
31
32
33 public class PredicatedCollectionTest<E> extends AbstractCollectionTest<E> {
34
35 public PredicatedCollectionTest(final String name) {
36 super(name);
37 }
38
39
40 protected Predicate<E> truePredicate = TruePredicate.<E>truePredicate();
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 Collection<E> makeObject() {
49 return decorateCollection(new ArrayList<E>(), truePredicate);
50 }
51
52 @Override
53 public Collection<E> makeConfirmedCollection() {
54 return new ArrayList<>();
55 }
56
57 @Override
58 @SuppressWarnings("unchecked")
59 public E[] getFullElements() {
60 return (E[]) new Object[] { "1", "3", "5", "7", "2", "4", "6" };
61 }
62
63 @Override
64 public Collection<E> makeFullCollection() {
65 final List<E> list = new ArrayList<>();
66 list.addAll(Arrays.asList(getFullElements()));
67 return decorateCollection(list, truePredicate);
68 }
69
70 @Override
71 public Collection<E> makeConfirmedFullCollection() {
72 final List<E> list = new ArrayList<>();
73 list.addAll(Arrays.asList(getFullElements()));
74 return list;
75 }
76
77
78 protected Predicate<E> testPredicate =
79 new Predicate<E>() {
80 @Override
81 public boolean evaluate(final E o) {
82 return o instanceof String;
83 }
84 };
85
86 public Collection<E> makeTestCollection() {
87 return decorateCollection(new ArrayList<E>(), testPredicate);
88 }
89
90 @SuppressWarnings("unchecked")
91 public void testIllegalAdd() {
92 final Collection<E> c = makeTestCollection();
93 final Integer i = Integer.valueOf(3);
94 try {
95 c.add((E) i);
96 fail("Integer should fail string predicate.");
97 } catch (final IllegalArgumentException e) {
98
99 }
100 assertTrue("Collection shouldn't contain illegal element",
101 !c.contains(i));
102 }
103
104 @SuppressWarnings("unchecked")
105 public void testIllegalAddAll() {
106 final Collection<E> c = makeTestCollection();
107 final List<E> elements = new ArrayList<>();
108 elements.add((E) "one");
109 elements.add((E) "two");
110 elements.add((E) Integer.valueOf(3));
111 elements.add((E) "four");
112 try {
113 c.addAll(elements);
114 fail("Integer should fail string predicate.");
115 } catch (final IllegalArgumentException e) {
116
117 }
118 assertTrue("Collection shouldn't contain illegal element", !c.contains("one"));
119 assertTrue("Collection shouldn't contain illegal element", !c.contains("two"));
120 assertTrue("Collection shouldn't contain illegal element", !c.contains(Integer.valueOf(3)));
121 assertTrue("Collection shouldn't contain illegal element", !c.contains("four"));
122 }
123
124 @Override
125 public String getCompatibilityVersion() {
126 return "4";
127 }
128
129
130
131
132
133
134
135
136 }