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 protected Collection<String> decorateCollection(final Collection<String> collection) {
49 return IndexedCollection.nonUniqueIndexedCollection(collection, new IntegerTransformer());
50 }
51
52 protected IndexedCollection<Integer, String> decorateUniqueCollection(final Collection<String> collection) {
53 return IndexedCollection.uniqueIndexedCollection(collection, new IntegerTransformer());
54 }
55
56 @Override
57 public String[] getFullElements() {
58 return new String[] { "1", "3", "5", "7", "2", "4", "6" };
59 }
60
61 @Override
62 public String[] getOtherElements() {
63 return new String[] {"9", "88", "678", "87", "98", "78", "99"};
64 }
65
66 @Override
67 public Collection<String> makeConfirmedCollection() {
68 return new ArrayList<>();
69 }
70
71 @Override
72 public Collection<String> makeConfirmedFullCollection() {
73 return new ArrayList<>(Arrays.asList(getFullElements()));
74 }
75
76 @Override
77 public Collection<String> makeFullCollection() {
78 return decorateCollection(new ArrayList<>(Arrays.asList(getFullElements())));
79 }
80
81 @Override
82 public Collection<String> makeObject() {
83 return decorateCollection(new ArrayList<>());
84 }
85
86 public Collection<String> makeTestCollection() {
87 return decorateCollection(new ArrayList<>());
88 }
89
90 public Collection<String> makeUniqueTestCollection() {
91 return decorateUniqueCollection(new ArrayList<>());
92 }
93
94 @Override
95 protected boolean skipSerializedCanonicalTests() {
96
97 return true;
98 }
99
100 @Test
101 public void testAddedObjectsCanBeRetrievedByKey() throws Exception {
102 final Collection<String> coll = makeTestCollection();
103 coll.add("12");
104 coll.add("16");
105 coll.add("1");
106 coll.addAll(asList("2", "3", "4"));
107
108 @SuppressWarnings("unchecked")
109 final IndexedCollection<Integer, String> indexed = (IndexedCollection<Integer, String>) coll;
110 assertEquals("12", indexed.get(12));
111 assertEquals("16", indexed.get(16));
112 assertEquals("1", indexed.get(1));
113 assertEquals("2", indexed.get(2));
114 assertEquals("3", indexed.get(3));
115 assertEquals("4", indexed.get(4));
116 }
117
118 @Test
119 public void testDecoratedCollectionIsIndexedOnCreation() throws Exception {
120 final Collection<String> original = makeFullCollection();
121 final IndexedCollection<Integer, String> indexed = decorateUniqueCollection(original);
122
123 assertEquals("1", indexed.get(1));
124 assertEquals("2", indexed.get(2));
125 assertEquals("3", indexed.get(3));
126 }
127
128 @Test
129 public void testEnsureDuplicateObjectsCauseException() throws Exception {
130 final Collection<String> coll = makeUniqueTestCollection();
131
132 coll.add("1");
133
134 assertThrows(IllegalArgumentException.class, () -> coll.add("1"));
135 }
136
137 @Test
138 public void testReindexUpdatesIndexWhenDecoratedCollectionIsModifiedSeparately() throws Exception {
139 final Collection<String> original = new ArrayList<>();
140 final IndexedCollection<Integer, String> indexed = decorateUniqueCollection(original);
141
142 original.add("1");
143 original.add("2");
144 original.add("3");
145
146 assertNull(indexed.get(1));
147 assertNull(indexed.get(2));
148 assertNull(indexed.get(3));
149
150 indexed.reindex();
151
152 assertEquals("1", indexed.get(1));
153 assertEquals("2", indexed.get(2));
154 assertEquals("3", indexed.get(3));
155 }
156
157 }