1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.collections4;
19
20 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
21 import static org.junit.jupiter.api.Assertions.assertSame;
22 import static org.junit.jupiter.api.Assertions.assertThrows;
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 assertInstanceOf(PredicatedBag.class, bag, "Returned object should be a PredicatedBag.");
49 assertThrows(NullPointerException.class, () -> BagUtils.predicatedBag(null, truePredicate), "Expecting NullPointerException for null bag.");
50 assertThrows(NullPointerException.class, () -> BagUtils.predicatedBag(new HashBag<>(), null), "Expecting NullPointerException for null predicate.");
51 }
52
53 @Test
54 public void testPredicatedSortedBag() {
55 final Bag<Object> bag = BagUtils.predicatedSortedBag(new TreeBag<>(), truePredicate);
56 assertInstanceOf(PredicatedSortedBag.class, bag, "Returned object should be a PredicatedSortedBag.");
57 assertThrows(NullPointerException.class, () -> BagUtils.predicatedSortedBag(null, truePredicate), "Expecting NullPointerException for null bag.");
58 assertThrows(NullPointerException.class, () -> BagUtils.predicatedSortedBag(new TreeBag<>(), null),
59 "Expecting NullPointerException for null predicate.");
60 }
61
62 @Test
63 public void testSynchronizedBag() {
64 final Bag<Object> bag = BagUtils.synchronizedBag(new HashBag<>());
65 assertInstanceOf(SynchronizedBag.class, bag, "Returned object should be a SynchronizedBag.");
66 assertThrows(NullPointerException.class, () -> BagUtils.synchronizedBag(null), "Expecting NullPointerException for null bag.");
67 }
68
69 @Test
70 public void testSynchronizedSortedBag() {
71 final Bag<Object> bag = BagUtils.synchronizedSortedBag(new TreeBag<>());
72 assertInstanceOf(SynchronizedSortedBag.class, bag, "Returned object should be a SynchronizedSortedBag.");
73 assertThrows(NullPointerException.class, () -> BagUtils.synchronizedSortedBag(null), "Expecting NullPointerException for null bag.");
74 }
75
76 @Test
77 public void testTransformedBag() {
78 final Bag<Object> bag = BagUtils.transformingBag(new HashBag<>(), nopTransformer);
79 assertInstanceOf(TransformedBag.class, bag, "Returned object should be an TransformedBag.");
80 assertThrows(NullPointerException.class, () -> BagUtils.transformingBag(null, nopTransformer), "Expecting NullPointerException for null bag.");
81 assertThrows(NullPointerException.class, () -> BagUtils.transformingBag(new HashBag<>(), null), "Expecting NullPointerException for null transformer.");
82 }
83
84 @Test
85 public void testTransformedSortedBag() {
86 final Bag<Object> bag = BagUtils.transformingSortedBag(new TreeBag<>(), nopTransformer);
87 assertInstanceOf(TransformedSortedBag.class, bag, "Returned object should be an TransformedSortedBag");
88 assertThrows(NullPointerException.class, () -> BagUtils.transformingSortedBag(null, nopTransformer), "Expecting NullPointerException for null bag.");
89 assertThrows(NullPointerException.class, () -> BagUtils.transformingSortedBag(new TreeBag<>(), null),
90 "Expecting NullPointerException for null transformer.");
91 }
92
93 @Test
94 public void testUnmodifiableBag() {
95 final Bag<Object> bag = BagUtils.unmodifiableBag(new HashBag<>());
96 assertInstanceOf(UnmodifiableBag.class, bag, "Returned object should be an UnmodifiableBag.");
97 assertThrows(NullPointerException.class, () -> BagUtils.unmodifiableBag(null), "Expecting NullPointerException for null bag.");
98 assertSame(bag, BagUtils.unmodifiableBag(bag), "UnmodifiableBag shall not be decorated");
99 }
100
101 @Test
102 public void testUnmodifiableSortedBag() {
103 final SortedBag<Object> bag = BagUtils.unmodifiableSortedBag(new TreeBag<>());
104 assertInstanceOf(UnmodifiableSortedBag.class, bag, "Returned object should be an UnmodifiableSortedBag.");
105 assertThrows(NullPointerException.class, () -> BagUtils.unmodifiableSortedBag(null), "Expecting NullPointerException for null bag.");
106 assertSame(bag, BagUtils.unmodifiableSortedBag(bag), "UnmodifiableSortedBag shall not be decorated");
107 }
108 }