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 public PredicatedSortedBagTest() {
41 super(PredicatedSortedBagTest.class.getSimpleName());
42 }
43
44 protected SortedBag<T> decorateBag(final SortedBag<T> bag, final Predicate<T> predicate) {
45 return PredicatedSortedBag.predicatedSortedBag(bag, predicate);
46 }
47
48 @Override
49 public String getCompatibilityVersion() {
50 return "4";
51 }
52
53 @Override
54 public SortedBag<T> makeObject() {
55 return decorateBag(new TreeBag<>(), truePredicate);
56 }
57
58 protected SortedBag<T> makeTestBag() {
59 return decorateBag(new TreeBag<>(), stringPredicate());
60 }
61
62 protected Predicate<T> stringPredicate() {
63 return String.class::isInstance;
64 }
65
66 @Test
67 public void testDecorate() {
68 final SortedBag<T> bag = decorateBag(new TreeBag<>(), stringPredicate());
69 ((PredicatedSortedBag<T>) bag).decorated();
70
71 assertThrows(NullPointerException.class, () -> decorateBag(new TreeBag<>(), null));
72
73 assertThrows(NullPointerException.class, () -> decorateBag(nullBag, stringPredicate()));
74 }
75
76 @Test
77 @SuppressWarnings("unchecked")
78 public void testSortOrder() {
79 final SortedBag<T> bag = decorateBag(new TreeBag<>(), stringPredicate());
80 final String one = "one";
81 final String two = "two";
82 final String three = "three";
83 bag.add((T) one);
84 bag.add((T) two);
85 bag.add((T) three);
86 assertEquals(bag.first(), one, "first element");
87 assertEquals(bag.last(), two, "last element");
88 final Comparator<? super T> c = bag.comparator();
89 assertNull(c, "natural order, so comparator should be null");
90 }
91
92
93
94
95
96
97
98
99 }