1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.collections4;
18
19 import static org.junit.jupiter.api.Assertions.assertAll;
20 import static org.junit.jupiter.api.Assertions.assertSame;
21 import static org.junit.jupiter.api.Assertions.assertThrows;
22 import static org.junit.jupiter.api.Assertions.assertTrue;
23
24 import org.apache.commons.collections4.bag.HashBag;
25 import org.apache.commons.collections4.bag.PredicatedBag;
26 import org.apache.commons.collections4.bag.PredicatedSortedBag;
27 import org.apache.commons.collections4.bag.SynchronizedBag;
28 import org.apache.commons.collections4.bag.SynchronizedSortedBag;
29 import org.apache.commons.collections4.bag.TransformedBag;
30 import org.apache.commons.collections4.bag.TransformedSortedBag;
31 import org.apache.commons.collections4.bag.TreeBag;
32 import org.apache.commons.collections4.bag.UnmodifiableBag;
33 import org.apache.commons.collections4.bag.UnmodifiableSortedBag;
34 import org.apache.commons.collections4.functors.TruePredicate;
35 import org.junit.jupiter.api.Test;
36
37
38
39
40 public class BagUtilsTest {
41
42 protected Predicate<Object> truePredicate = TruePredicate.truePredicate();
43 protected Transformer<Object, Object> nopTransformer = TransformerUtils.nopTransformer();
44
45 @Test
46 public void testPredicatedBag() {
47 final Bag<Object> bag = BagUtils.predicatedBag(new HashBag<>(), truePredicate);
48 assertTrue(bag instanceof PredicatedBag, "Returned object should be a PredicatedBag.");
49 assertAll(
50 () -> assertThrows(NullPointerException.class, () -> BagUtils.predicatedBag(null, truePredicate),
51 "Expecting NullPointerException for null bag."),
52 () -> assertThrows(NullPointerException.class, () -> BagUtils.predicatedBag(new HashBag<>(), null),
53 "Expecting NullPointerException for null predicate.")
54 );
55 }
56
57 @Test
58 public void testPredicatedSortedBag() {
59 final Bag<Object> bag = BagUtils.predicatedSortedBag(new TreeBag<>(), truePredicate);
60 assertTrue(bag instanceof PredicatedSortedBag, "Returned object should be a PredicatedSortedBag.");
61 assertAll(
62 () -> assertThrows(NullPointerException.class, () -> BagUtils.predicatedSortedBag(null, truePredicate),
63 "Expecting NullPointerException for null bag."),
64 () -> assertThrows(NullPointerException.class, () -> BagUtils.predicatedSortedBag(new TreeBag<>(), null),
65 "Expecting NullPointerException for null predicate.")
66 );
67 }
68
69 @Test
70 public void testSynchronizedBag() {
71 final Bag<Object> bag = BagUtils.synchronizedBag(new HashBag<>());
72 assertTrue(bag instanceof SynchronizedBag, "Returned object should be a SynchronizedBag.");
73 assertThrows(NullPointerException.class, () -> BagUtils.synchronizedBag(null),
74 "Expecting NullPointerException for null bag.");
75 }
76
77 @Test
78 public void testSynchronizedSortedBag() {
79 final Bag<Object> bag = BagUtils.synchronizedSortedBag(new TreeBag<>());
80 assertTrue(bag instanceof SynchronizedSortedBag, "Returned object should be a SynchronizedSortedBag.");
81 assertThrows(NullPointerException.class, () -> BagUtils.synchronizedSortedBag(null),
82 "Expecting NullPointerException for null bag.");
83 }
84
85 @Test
86 public void testTransformedBag() {
87 final Bag<Object> bag = BagUtils.transformingBag(new HashBag<>(), nopTransformer);
88 assertTrue(bag instanceof TransformedBag, "Returned object should be an TransformedBag.");
89 assertAll(
90 () -> assertThrows(NullPointerException.class, () -> BagUtils.transformingBag(null, nopTransformer),
91 "Expecting NullPointerException for null bag."),
92 () -> assertThrows(NullPointerException.class, () -> BagUtils.transformingBag(new HashBag<>(), null),
93 "Expecting NullPointerException for null transformer.")
94 );
95 }
96
97 @Test
98 public void testTransformedSortedBag() {
99 final Bag<Object> bag = BagUtils.transformingSortedBag(new TreeBag<>(), nopTransformer);
100 assertTrue(bag instanceof TransformedSortedBag, "Returned object should be an TransformedSortedBag");
101 assertAll(
102 () -> assertThrows(NullPointerException.class, () -> BagUtils.transformingSortedBag(null, nopTransformer),
103 "Expecting NullPointerException for null bag."),
104 () -> assertThrows(NullPointerException.class, () -> BagUtils.transformingSortedBag(new TreeBag<>(), null),
105 "Expecting NullPointerException for null transformer.")
106 );
107 }
108
109 @Test
110 public void testUnmodifiableBag() {
111 final Bag<Object> bag = BagUtils.unmodifiableBag(new HashBag<>());
112 assertTrue(bag instanceof UnmodifiableBag, "Returned object should be an UnmodifiableBag.");
113 assertThrows(NullPointerException.class, () -> BagUtils.unmodifiableBag(null),
114 "Expecting NullPointerException for null bag.");
115 assertSame(bag, BagUtils.unmodifiableBag(bag), "UnmodifiableBag shall not be decorated");
116 }
117
118 @Test
119 public void testUnmodifiableSortedBag() {
120 final SortedBag<Object> bag = BagUtils.unmodifiableSortedBag(new TreeBag<>());
121 assertTrue(bag instanceof UnmodifiableSortedBag, "Returned object should be an UnmodifiableSortedBag.");
122 assertThrows(NullPointerException.class, () -> BagUtils.unmodifiableSortedBag(null),
123 "Expecting NullPointerException for null bag.");
124 assertSame(bag, BagUtils.unmodifiableSortedBag(bag), "UnmodifiableSortedBag shall not be decorated");
125 }
126
127 }