1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.collections4.collection;
18
19 import static java.util.Arrays.asList;
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21 import static org.junit.jupiter.api.Assertions.assertNull;
22 import static org.junit.jupiter.api.Assertions.assertThrows;
23
24 import java.io.Serializable;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.Collection;
28
29 import org.apache.commons.collections4.Transformer;
30 import org.junit.jupiter.api.Test;
31
32
33
34
35
36 @SuppressWarnings("boxing")
37 public class IndexedCollectionTest extends AbstractCollectionTest<String> {
38
39 private static final class IntegerTransformer implements Transformer<String, Integer>, Serializable {
40 private static final long serialVersionUID = 809439581555072949L;
41
42 @Override
43 public Integer transform(final String input) {
44 return Integer.valueOf(input);
45 }
46 }
47
48 public IndexedCollectionTest() {
49 super(IndexedCollectionTest.class.getSimpleName());
50 }
51
52 protected Collection<String> decorateCollection(final Collection<String> collection) {
53 return IndexedCollection.nonUniqueIndexedCollection(collection, new IntegerTransformer());
54 }
55
56 protected IndexedCollection<Integer, String> decorateUniqueCollection(final Collection<String> collection) {
57 return IndexedCollection.uniqueIndexedCollection(collection, new IntegerTransformer());
58 }
59
60 @Override
61 public String[] getFullElements() {
62 return new String[] { "1", "3", "5", "7", "2", "4", "6" };
63 }
64
65 @Override
66 public String[] getOtherElements() {
67 return new String[] {"9", "88", "678", "87", "98", "78", "99"};
68 }
69
70 @Override
71 public Collection<String> makeConfirmedCollection() {
72 return new ArrayList<>();
73 }
74
75 @Override
76 public Collection<String> makeConfirmedFullCollection() {
77 return new ArrayList<>(Arrays.asList(getFullElements()));
78 }
79
80 @Override
81 public Collection<String> makeFullCollection() {
82 return decorateCollection(new ArrayList<>(Arrays.asList(getFullElements())));
83 }
84
85 @Override
86 public Collection<String> makeObject() {
87 return decorateCollection(new ArrayList<>());
88 }
89
90 public Collection<String> makeTestCollection() {
91 return decorateCollection(new ArrayList<>());
92 }
93
94 public Collection<String> makeUniqueTestCollection() {
95 return decorateUniqueCollection(new ArrayList<>());
96 }
97
98 @Override
99 protected boolean skipSerializedCanonicalTests() {
100
101 return true;
102 }
103
104 @Test
105 public void testAddedObjectsCanBeRetrievedByKey() throws Exception {
106 final Collection<String> coll = makeTestCollection();
107 coll.add("12");
108 coll.add("16");
109 coll.add("1");
110 coll.addAll(asList("2", "3", "4"));
111
112 @SuppressWarnings("unchecked")
113 final IndexedCollection<Integer, String> indexed = (IndexedCollection<Integer, String>) coll;
114 assertEquals("12", indexed.get(12));
115 assertEquals("16", indexed.get(16));
116 assertEquals("1", indexed.get(1));
117 assertEquals("2", indexed.get(2));
118 assertEquals("3", indexed.get(3));
119 assertEquals("4", indexed.get(4));
120 }
121
122 @Test
123 public void testDecoratedCollectionIsIndexedOnCreation() throws Exception {
124 final Collection<String> original = makeFullCollection();
125 final IndexedCollection<Integer, String> indexed = decorateUniqueCollection(original);
126
127 assertEquals("1", indexed.get(1));
128 assertEquals("2", indexed.get(2));
129 assertEquals("3", indexed.get(3));
130 }
131
132 @Test
133 public void testEnsureDuplicateObjectsCauseException() throws Exception {
134 final Collection<String> coll = makeUniqueTestCollection();
135
136 coll.add("1");
137
138 assertThrows(IllegalArgumentException.class, () -> coll.add("1"));
139 }
140
141 @Test
142 public void testReindexUpdatesIndexWhenDecoratedCollectionIsModifiedSeparately() throws Exception {
143 final Collection<String> original = new ArrayList<>();
144 final IndexedCollection<Integer, String> indexed = decorateUniqueCollection(original);
145
146 original.add("1");
147 original.add("2");
148 original.add("3");
149
150 assertNull(indexed.get(1));
151 assertNull(indexed.get(2));
152 assertNull(indexed.get(3));
153
154 indexed.reindex();
155
156 assertEquals("1", indexed.get(1));
157 assertEquals("2", indexed.get(2));
158 assertEquals("3", indexed.get(3));
159 }
160
161 }