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
22 import org.apache.commons.collections4.Bag;
23 import org.apache.commons.collections4.SortedBag;
24 import org.junit.jupiter.api.Test;
25
26
27
28
29
30 public class TreeBagTest<T> extends AbstractSortedBagTest<T> {
31
32 public TreeBagTest() {
33 super(TreeBagTest.class.getSimpleName());
34 }
35
36 @Override
37 public String getCompatibilityVersion() {
38 return "4";
39 }
40
41 @Override
42 public SortedBag<T> makeObject() {
43 return new TreeBag<>();
44 }
45
46 @SuppressWarnings("unchecked")
47 public SortedBag<T> setupBag() {
48 final SortedBag<T> bag = makeObject();
49 bag.add((T) "C");
50 bag.add((T) "A");
51 bag.add((T) "B");
52 bag.add((T) "D");
53 return bag;
54 }
55
56 @Test
57 public void testCollections265() {
58 final Bag<Object> bag = new TreeBag<>();
59
60 assertThrows(IllegalArgumentException.class, () -> bag.add(new Object()));
61 }
62
63 @Test
64 public void testCollections555() {
65 final Bag<Object> bag = new TreeBag<>();
66
67 assertThrows(NullPointerException.class, () -> bag.add(null));
68
69 final Bag<String> bag2 = new TreeBag<>(String::compareTo);
70
71
72 bag2.add("a");
73
74 assertThrows(NullPointerException.class, () -> bag2.add(null));
75 }
76
77 @Test
78 public void testOrdering() {
79 final Bag<T> bag = setupBag();
80 assertEquals("A", bag.toArray()[0], "Should get elements in correct order");
81 assertEquals("B", bag.toArray()[1], "Should get elements in correct order");
82 assertEquals("C", bag.toArray()[2], "Should get elements in correct order");
83 assertEquals("A", ((SortedBag<T>) bag).first(), "Should get first key");
84 assertEquals("D", ((SortedBag<T>) bag).last(), "Should get last key");
85 }
86
87
88
89
90
91
92
93
94 }