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.assertThrows;
21 import static org.junit.jupiter.api.Assertions.assertTrue;
22
23 import java.io.IOException;
24 import java.io.Serializable;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.Collection;
28 import java.util.Comparator;
29
30 import org.apache.commons.collections4.Bag;
31 import org.apache.commons.collections4.Predicate;
32 import org.apache.commons.collections4.collection.AbstractCollectionTest;
33 import org.apache.commons.collections4.functors.NonePredicate;
34 import org.junit.jupiter.api.Test;
35
36
37
38
39
40
41
42 public class CollectionBagTest<T> extends AbstractCollectionTest<T> {
43
44 @Override
45 public String getCompatibilityVersion() {
46 return "4";
47 }
48
49 @Override
50 protected int getIterationBehaviour() {
51 return UNORDERED;
52 }
53
54
55
56
57
58
59 @Override
60 public Collection<T> makeConfirmedCollection() {
61 return new ArrayList<>();
62 }
63
64
65
66
67
68
69 @Override
70 public Collection<T> makeConfirmedFullCollection() {
71 final Collection<T> set = makeConfirmedCollection();
72 set.addAll(Arrays.asList(getFullElements()));
73 return set;
74 }
75
76 @Override
77 public Bag<T> makeObject() {
78 return CollectionBag.collectionBag(new HashBag<>());
79 }
80
81 @Test
82 public void testAdd_Predicate_ComparatorCustom() throws Throwable {
83 final TreeBag<Predicate<Object>> treeBagOfPredicateOfObject = new TreeBag<>(Comparator.comparing(Predicate::toString));
84 final CollectionBag<Predicate<Object>> collectionBagOfPredicateOfObject = new CollectionBag<>(treeBagOfPredicateOfObject);
85 collectionBagOfPredicateOfObject.add(NonePredicate.nonePredicate(collectionBagOfPredicateOfObject), 24);
86 }
87
88 @Test
89 public void testAdd_Predicate_ComparatorDefault() throws Throwable {
90 final TreeBag<Predicate<Object>> treeBagOfPredicateOfObject = new TreeBag<>();
91 final CollectionBag<Predicate<Object>> collectionBagOfPredicateOfObject = new CollectionBag<>(treeBagOfPredicateOfObject);
92 assertThrows(ClassCastException.class, () -> collectionBagOfPredicateOfObject.add(NonePredicate.nonePredicate(collectionBagOfPredicateOfObject), 24));
93 }
94
95
96
97
98
99
100
101
102
103
104
105
106 @Test
107 public void testEmptyBagCompatibility() throws IOException, ClassNotFoundException {
108
109 final Bag<T> bag = makeObject();
110 if (bag instanceof Serializable && !skipSerializedCanonicalTests() && isTestSerialization()) {
111 final Bag<?> bag2 = (Bag<?>) readExternalFormFromDisk(getCanonicalEmptyCollectionName(bag));
112 assertTrue(bag2.isEmpty(), "Bag is empty");
113 assertEquals(bag, bag2);
114 }
115 }
116
117
118
119
120
121 @Test
122 public void testFullBagCompatibility() throws IOException, ClassNotFoundException {
123
124 final Bag<T> bag = (Bag<T>) makeFullCollection();
125 if (bag instanceof Serializable && !skipSerializedCanonicalTests() && isTestSerialization()) {
126 final Bag<?> bag2 = (Bag<?>) readExternalFormFromDisk(getCanonicalFullCollectionName(bag));
127 assertEquals(bag.size(), bag2.size(), "Bag is the right size");
128 assertEquals(bag, bag2);
129 }
130 }
131 }