1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.collections4.bag;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertNull;
21 import static org.junit.jupiter.api.Assertions.assertThrows;
22
23 import java.util.Comparator;
24
25 import org.apache.commons.collections4.Predicate;
26 import org.apache.commons.collections4.SortedBag;
27 import org.apache.commons.collections4.functors.TruePredicate;
28 import org.junit.jupiter.api.Test;
29
30
31
32
33
34 public class PredicatedSortedBagTest<T> extends AbstractSortedBagTest<T> {
35
36 private final SortedBag<T> nullBag = null;
37
38 protected Predicate<T> truePredicate = TruePredicate.<T>truePredicate();
39
40 protected SortedBag<T> decorateBag(final SortedBag<T> bag, final Predicate<T> predicate) {
41 return PredicatedSortedBag.predicatedSortedBag(bag, predicate);
42 }
43
44 @Override
45 public String getCompatibilityVersion() {
46 return "4";
47 }
48
49 @Override
50 public SortedBag<T> makeObject() {
51 return decorateBag(new TreeBag<>(), truePredicate);
52 }
53
54 protected SortedBag<T> makeTestBag() {
55 return decorateBag(new TreeBag<>(), stringPredicate());
56 }
57
58 protected Predicate<T> stringPredicate() {
59 return String.class::isInstance;
60 }
61
62 @Test
63 public void testDecorate() {
64 final SortedBag<T> bag = decorateBag(new TreeBag<>(), stringPredicate());
65 ((PredicatedSortedBag<T>) bag).decorated();
66
67 assertThrows(NullPointerException.class, () -> decorateBag(new TreeBag<>(), null));
68
69 assertThrows(NullPointerException.class, () -> decorateBag(nullBag, stringPredicate()));
70 }
71
72 @Test
73 @SuppressWarnings("unchecked")
74 public void testSortOrder() {
75 final SortedBag<T> bag = decorateBag(new TreeBag<>(), stringPredicate());
76 final String one = "one";
77 final String two = "two";
78 final String three = "three";
79 bag.add((T) one);
80 bag.add((T) two);
81 bag.add((T) three);
82 assertEquals(bag.first(), one, "first element");
83 assertEquals(bag.last(), two, "last element");
84 final Comparator<? super T> c = bag.comparator();
85 assertNull(c, "natural order, so comparator should be null");
86 }
87
88
89
90
91
92
93
94
95 }