1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.collections4.properties;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertFalse;
21
22 import java.io.FileNotFoundException;
23 import java.io.FileReader;
24 import java.io.IOException;
25 import java.util.Collections;
26 import java.util.Enumeration;
27 import java.util.Iterator;
28 import java.util.Map;
29 import java.util.Map.Entry;
30 import java.util.concurrent.atomic.AtomicInteger;
31
32 import org.junit.jupiter.api.Test;
33
34
35
36
37 public class OrderedPropertiesTest {
38
39 private void assertAscendingOrder(final OrderedProperties orderedProperties) {
40 final int first = 1;
41 final int last = 11;
42 final Enumeration<Object> enumObjects = orderedProperties.keys();
43 for (int i = first; i <= last; i++) {
44 assertEquals("key" + i, enumObjects.nextElement());
45 }
46 final Iterator<Object> iterSet = orderedProperties.keySet().iterator();
47 for (int i = first; i <= last; i++) {
48 assertEquals("key" + i, iterSet.next());
49 }
50 final Iterator<Entry<Object, Object>> iterEntrySet = orderedProperties.entrySet().iterator();
51 for (int i = first; i <= last; i++) {
52 final Entry<Object, Object> next = iterEntrySet.next();
53 assertEquals("key" + i, next.getKey());
54 assertEquals("value" + i, next.getValue());
55 }
56 final Enumeration<?> propertyNames = orderedProperties.propertyNames();
57 for (int i = first; i <= last; i++) {
58 assertEquals("key" + i, propertyNames.nextElement());
59 }
60 }
61
62 private OrderedProperties assertDescendingOrder(final OrderedProperties orderedProperties) {
63 final int first = 11;
64 final int last = 1;
65 final Enumeration<Object> enumObjects = orderedProperties.keys();
66 for (int i = first; i <= last; i--) {
67 assertEquals("key" + i, enumObjects.nextElement());
68 }
69 final Iterator<Object> iterSet = orderedProperties.keySet().iterator();
70 for (int i = first; i <= last; i--) {
71 assertEquals("key" + i, iterSet.next());
72 }
73 final Iterator<Entry<Object, Object>> iterEntrySet = orderedProperties.entrySet().iterator();
74 for (int i = first; i <= last; i--) {
75 final Entry<Object, Object> next = iterEntrySet.next();
76 assertEquals("key" + i, next.getKey());
77 assertEquals("value" + i, next.getValue());
78 }
79 final Enumeration<?> propertyNames = orderedProperties.propertyNames();
80 for (int i = first; i <= last; i--) {
81 assertEquals("key" + i, propertyNames.nextElement());
82 }
83 return orderedProperties;
84 }
85
86 private OrderedProperties loadOrderedKeysReverse() throws FileNotFoundException, IOException {
87 final OrderedProperties orderedProperties = new OrderedProperties();
88 try (FileReader reader = new FileReader("src/test/resources/org/apache/commons/collections4/properties/test-reverse.properties")) {
89 orderedProperties.load(reader);
90 }
91 return assertDescendingOrder(orderedProperties);
92 }
93
94 @Test
95 public void testCompute() {
96 final OrderedProperties orderedProperties = new OrderedProperties();
97 int first = 1;
98 int last = 11;
99 for (int i = first; i <= last; i++) {
100 final AtomicInteger aInt = new AtomicInteger(i);
101 orderedProperties.compute("key" + i, (k, v) -> "value" + aInt.get());
102 }
103 assertAscendingOrder(orderedProperties);
104 orderedProperties.clear();
105 first = 11;
106 last = 1;
107 for (int i = first; i >= last; i--) {
108 final AtomicInteger aInt = new AtomicInteger(i);
109 orderedProperties.compute("key" + i, (k, v) -> "value" + aInt.get());
110 }
111 assertDescendingOrder(orderedProperties);
112 }
113
114 @Test
115 public void testComputeIfAbsent() {
116 final OrderedProperties orderedProperties = new OrderedProperties();
117 int first = 1;
118 int last = 11;
119 for (int i = first; i <= last; i++) {
120 final AtomicInteger aInt = new AtomicInteger(i);
121 orderedProperties.computeIfAbsent("key" + i, k -> "value" + aInt.get());
122 }
123 assertAscendingOrder(orderedProperties);
124 orderedProperties.clear();
125 first = 11;
126 last = 1;
127 for (int i = first; i >= last; i--) {
128 final AtomicInteger aInt = new AtomicInteger(i);
129 orderedProperties.computeIfAbsent("key" + i, k -> "value" + aInt.get());
130 }
131 assertDescendingOrder(orderedProperties);
132 }
133
134 @Test
135 public void testEntrySet() {
136 final OrderedProperties orderedProperties = new OrderedProperties();
137 final char first = 'Z';
138 final char last = 'A';
139 for (char ch = first; ch >= last; ch--) {
140 orderedProperties.put(String.valueOf(ch), "Value" + ch);
141 }
142 final Iterator<Map.Entry<Object, Object>> entries = orderedProperties.entrySet().iterator();
143 for (char ch = first; ch <= last; ch++) {
144 final Map.Entry<Object, Object> entry = entries.next();
145 assertEquals(String.valueOf(ch), entry.getKey());
146 assertEquals("Value" + ch, entry.getValue());
147 }
148 }
149
150 @Test
151 public void testForEach() {
152 final OrderedProperties orderedProperties = new OrderedProperties();
153 final char first = 'Z';
154 final char last = 'A';
155 for (char ch = first; ch >= last; ch--) {
156 orderedProperties.put(String.valueOf(ch), "Value" + ch);
157 }
158 final AtomicInteger aCh = new AtomicInteger(first);
159 orderedProperties.forEach((k, v) -> {
160 final char ch = (char) aCh.getAndDecrement();
161 assertEquals(String.valueOf(ch), k);
162 assertEquals("Value" + ch, v);
163 });
164 }
165
166 @Test
167 public void testKeys() {
168 final OrderedProperties orderedProperties = new OrderedProperties();
169 final char first = 'Z';
170 final char last = 'A';
171 for (char ch = first; ch >= last; ch--) {
172 orderedProperties.put(String.valueOf(ch), "Value" + ch);
173 }
174 final Enumeration<Object> keys = orderedProperties.keys();
175 for (char ch = first; ch <= last; ch++) {
176 assertEquals(String.valueOf(ch), keys.nextElement());
177 }
178 }
179
180 @Test
181 public void testLoadOrderedKeys() throws IOException {
182 final OrderedProperties orderedProperties = new OrderedProperties();
183 try (FileReader reader = new FileReader("src/test/resources/org/apache/commons/collections4/properties/test.properties")) {
184 orderedProperties.load(reader);
185 }
186 assertAscendingOrder(orderedProperties);
187 }
188
189 @Test
190 public void testLoadOrderedKeysReverse() throws IOException {
191 loadOrderedKeysReverse();
192 }
193
194 @Test
195 public void testMerge() {
196 final OrderedProperties orderedProperties = new OrderedProperties();
197 int first = 1;
198 int last = 11;
199 for (int i = first; i <= last; i++) {
200 orderedProperties.merge("key" + i, "value" + i, (k, v) -> v);
201 }
202 assertAscendingOrder(orderedProperties);
203 orderedProperties.clear();
204 first = 11;
205 last = 1;
206 for (int i = first; i >= last; i--) {
207 orderedProperties.merge("key" + i, "value" + i, (k, v) -> v);
208 }
209 assertDescendingOrder(orderedProperties);
210 }
211
212 @Test
213 public void testPut() {
214 final OrderedProperties orderedProperties = new OrderedProperties();
215 int first = 1;
216 int last = 11;
217 for (int i = first; i <= last; i++) {
218 orderedProperties.put("key" + i, "value" + i);
219 }
220 assertAscendingOrder(orderedProperties);
221 orderedProperties.clear();
222 first = 11;
223 last = 1;
224 for (int i = first; i >= last; i--) {
225 orderedProperties.put("key" + i, "value" + i);
226 }
227 assertDescendingOrder(orderedProperties);
228 }
229
230 @Test
231 public void testPutAll() {
232 final OrderedProperties sourceProperties = new OrderedProperties();
233 int first = 1;
234 int last = 11;
235 for (int i = first; i <= last; i++) {
236 sourceProperties.put("key" + i, "value" + i);
237 }
238 final OrderedProperties orderedProperties = new OrderedProperties();
239 orderedProperties.putAll(sourceProperties);
240 assertAscendingOrder(orderedProperties);
241 orderedProperties.clear();
242 first = 11;
243 last = 1;
244 for (int i = first; i >= last; i--) {
245 orderedProperties.put("key" + i, "value" + i);
246 }
247 assertDescendingOrder(orderedProperties);
248 }
249
250 @Test
251 public void testPutIfAbsent() {
252 final OrderedProperties orderedProperties = new OrderedProperties();
253 int first = 1;
254 int last = 11;
255 for (int i = first; i <= last; i++) {
256 orderedProperties.putIfAbsent("key" + i, "value" + i);
257 }
258 assertAscendingOrder(orderedProperties);
259 orderedProperties.clear();
260 first = 11;
261 last = 1;
262 for (int i = first; i >= last; i--) {
263 orderedProperties.putIfAbsent("key" + i, "value" + i);
264 }
265 assertDescendingOrder(orderedProperties);
266 }
267
268 @Test
269 public void testRemoveKey() throws FileNotFoundException, IOException {
270 final OrderedProperties props = loadOrderedKeysReverse();
271 final String k = "key1";
272 props.remove(k);
273 assertFalse(props.contains(k));
274 assertFalse(props.containsKey(k));
275 assertFalse(Collections.list(props.keys()).contains(k));
276 assertFalse(Collections.list(props.propertyNames()).contains(k));
277 }
278
279 @Test
280 public void testRemoveKeyValue() throws FileNotFoundException, IOException {
281 final OrderedProperties props = loadOrderedKeysReverse();
282 final String k = "key1";
283 props.remove(k, "value1");
284 assertFalse(props.contains(k));
285 assertFalse(props.containsKey(k));
286 assertFalse(Collections.list(props.keys()).contains(k));
287 assertFalse(Collections.list(props.propertyNames()).contains(k));
288 }
289
290 @Test
291 public void testToString() {
292 final OrderedProperties orderedProperties = new OrderedProperties();
293 final char first = 'Z';
294 final char last = 'A';
295 for (char ch = first; ch >= last; ch--) {
296 orderedProperties.put(String.valueOf(ch), "Value" + ch);
297 }
298 assertEquals(
299 "{Z=ValueZ, Y=ValueY, X=ValueX, W=ValueW, V=ValueV, U=ValueU, T=ValueT, S=ValueS, R=ValueR, Q=ValueQ, P=ValueP, O=ValueO, N=ValueN, M=ValueM, L=ValueL, K=ValueK, J=ValueJ, I=ValueI, H=ValueH, G=ValueG, F=ValueF, E=ValueE, D=ValueD, C=ValueC, B=ValueB, A=ValueA}",
300 orderedProperties.toString());
301 }
302 }