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