1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.kuali.rice.krad.util;
18
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.springframework.beans.BeanUtils;
22
23 import java.lang.reflect.Method;
24 import java.util.Collection;
25 import java.util.Iterator;
26 import java.util.Map;
27 import java.util.Properties;
28 import java.util.Set;
29
30 import static org.junit.Assert.*;
31
32
33
34
35 public class PropertyTreeTest {
36 private static final String KNOWN_SIMPLE_KEY = "simple";
37 private static final String KNOWN_SIMPLE_VALUE = "simple value";
38 private static final String KNOWN_COMPLEX_KEY = "known.complex.key";
39 private static final String KNOWN_COMPLEX_VALUE = "known complex value";
40 private static final int MIXED_COUNT = 13;
41
42 JstlPropertyHolder.PropertyTree tree;
43
44 @Before
45 public void setUp() throws Exception {
46 tree = new JstlPropertyHolder.PropertyTree();
47 }
48
49
50 @Test public void testEntrySet_readwrite() {
51 Set entrySet = tree.entrySet();
52
53 boolean failedAsExpected = false;
54
55 try {
56 entrySet.clear();
57 }
58 catch (UnsupportedOperationException e) {
59 failedAsExpected = true;
60 }
61
62 assertTrue(failedAsExpected);
63 }
64
65 @Test public void testEntrySet_emptyTree() {
66 Set entrySet = tree.entrySet();
67
68 assertTrue(entrySet.isEmpty());
69 }
70
71 @Test public void testEntrySet_oneSimpleKey() {
72 setOneSimpleKey();
73 Set entrySet = tree.entrySet();
74
75 assertEquals(1, entrySet.size());
76
77 boolean foundSimple = false;
78 for (Iterator i = entrySet.iterator(); i.hasNext();) {
79 Map.Entry e = (Map.Entry) i.next();
80 if (e.getKey().equals(KNOWN_SIMPLE_KEY)) {
81 foundSimple = true;
82 assertEquals(e.getValue(), KNOWN_SIMPLE_VALUE);
83 }
84 }
85 assertTrue(foundSimple);
86 }
87
88 @Test public void testEntrySet_oneComplexKey() {
89 setOneComplexKey();
90 Set entrySet = tree.entrySet();
91
92 assertEquals(1, entrySet.size());
93
94 boolean foundComplex = false;
95 for (Iterator i = entrySet.iterator(); i.hasNext();) {
96 Map.Entry e = (Map.Entry) i.next();
97 if (e.getKey().equals(KNOWN_COMPLEX_KEY)) {
98 foundComplex = true;
99 assertEquals(e.getValue(), KNOWN_COMPLEX_VALUE);
100 }
101 }
102 assertTrue(foundComplex);
103 }
104
105 @Test public void testEntrySet_manyMixedKeys() {
106 setManyMixedKeys();
107 Set entrySet = tree.entrySet();
108
109 assertEquals(MIXED_COUNT, entrySet.size());
110
111 boolean foundSimple = false;
112 boolean foundComplex = false;
113 for (Iterator i = entrySet.iterator(); i.hasNext();) {
114 Map.Entry e = (Map.Entry) i.next();
115 if (e.getKey().equals(KNOWN_SIMPLE_KEY)) {
116 foundSimple = true;
117 assertEquals(e.getValue(), KNOWN_SIMPLE_VALUE);
118 }
119 else if (e.getKey().equals(KNOWN_COMPLEX_KEY)) {
120 foundComplex = true;
121 assertEquals(e.getValue(), KNOWN_COMPLEX_VALUE);
122 }
123 }
124 assertTrue(foundSimple);
125 assertTrue(foundComplex);
126 }
127
128
129 @Test public void testSize_emptyTree() {
130 assertEquals(0, tree.size());
131 }
132
133 @Test public void testSize_oneSimpleKey() {
134 setOneSimpleKey();
135
136 assertEquals(1, tree.size());
137 }
138
139 @Test public void testSize_oneComplexKey() {
140 setOneComplexKey();
141
142 assertEquals(1, tree.size());
143 }
144
145 @Test public void testSize_manyMixedKeys() {
146 setManyMixedKeys();
147
148 assertEquals(MIXED_COUNT, tree.size());
149 }
150
151
152 @Test public void testIsEmpty_emptyTree() {
153 assertTrue(tree.isEmpty());
154 }
155
156 @Test public void testIsEmpty_oneSimpleKey() {
157 setOneSimpleKey();
158
159 assertFalse(tree.isEmpty());
160 }
161
162 @Test public void testIsEmpty_oneComplexKey() {
163 setOneComplexKey();
164
165 assertFalse(tree.isEmpty());
166 }
167
168 @Test public void testIsEmpty_manyMixedKeys() {
169 setManyMixedKeys();
170
171 assertFalse(tree.isEmpty());
172 }
173
174
175 @Test public void testValues_readwrite() {
176 Collection values = tree.values();
177
178 boolean failedAsExpected = false;
179
180 try {
181 values.clear();
182 }
183 catch (UnsupportedOperationException e) {
184 failedAsExpected = true;
185 }
186
187 assertTrue(failedAsExpected);
188 }
189
190 @Test public void testValues_emptyTree() {
191 Collection values = tree.values();
192
193 assertTrue(values.isEmpty());
194 }
195
196 @Test public void testValues_oneSimpleKey() {
197 setOneSimpleKey();
198 Collection values = tree.values();
199
200 assertEquals(1, values.size());
201 assertTrue(values.contains(KNOWN_SIMPLE_VALUE));
202 }
203
204 @Test public void testValues_oneComplexKey() {
205 setOneComplexKey();
206 Collection values = tree.values();
207
208 assertEquals(1, values.size());
209 assertTrue(values.contains(KNOWN_COMPLEX_VALUE));
210 }
211
212 @Test public void testValues_manyMixedKeys() {
213 setManyMixedKeys();
214 Collection values = tree.values();
215
216 assertEquals(MIXED_COUNT, values.size());
217 assertTrue(values.contains(KNOWN_SIMPLE_VALUE));
218 assertTrue(values.contains(KNOWN_COMPLEX_VALUE));
219 }
220
221
222 @Test public void testKeySet_readwrite() {
223 Set keys = tree.keySet();
224
225 boolean failedAsExpected = false;
226
227 try {
228 keys.clear();
229 }
230 catch (UnsupportedOperationException e) {
231 failedAsExpected = true;
232 }
233
234 assertTrue(failedAsExpected);
235 }
236
237 @Test public void testKeySet_emptyTree() {
238 Set keys = tree.keySet();
239
240 assertTrue(keys.isEmpty());
241 }
242
243 @Test public void testKeySet_oneSimpleKey() {
244 setOneSimpleKey();
245 Set keys = tree.keySet();
246
247 assertEquals(1, keys.size());
248 assertTrue(keys.contains(KNOWN_SIMPLE_KEY));
249 }
250
251 @Test public void testKeySet_oneComplexKey() {
252 setOneComplexKey();
253 Set keys = tree.keySet();
254
255 assertEquals(1, keys.size());
256 assertTrue(keys.contains(KNOWN_COMPLEX_KEY));
257 }
258
259 @Test public void testKeySet_manyMixedKeys() {
260 setManyMixedKeys();
261 Set keys = tree.keySet();
262
263 assertEquals(MIXED_COUNT, keys.size());
264 assertTrue(keys.contains(KNOWN_SIMPLE_KEY));
265 assertTrue(keys.contains(KNOWN_COMPLEX_KEY));
266 }
267
268
269
270 @Test public void testContainsKey_invalidKey() {
271 boolean failedAsExpected = false;
272
273 try {
274 assertFalse(tree.containsKey(null));
275 }
276 catch (IllegalArgumentException e) {
277 failedAsExpected = true;
278 }
279
280 assertTrue(failedAsExpected);
281 }
282
283 @Test public void testContainsKey_emptyTree() {
284 assertFalse(tree.containsKey(KNOWN_SIMPLE_KEY));
285 }
286
287 @Test public void testContainsKey_unknownKey() {
288 setManyMixedKeys();
289
290 assertFalse(tree.containsKey("hopefully unknown key"));
291 }
292
293 @Test public void testContainsKey_oneSimpleKey() {
294 setOneSimpleKey();
295
296 assertTrue(tree.containsKey(KNOWN_SIMPLE_KEY));
297 }
298
299 @Test public void testContainsKey_oneComplexKey() {
300 setOneComplexKey();
301
302 assertTrue(tree.containsKey(KNOWN_COMPLEX_KEY));
303 }
304
305 @Test public void testContainsKey_manyMixedKeys() {
306 setManyMixedKeys();
307
308 assertTrue(tree.containsKey(KNOWN_SIMPLE_KEY));
309 assertTrue(tree.containsKey(KNOWN_COMPLEX_KEY));
310 }
311
312
313
314 @Test public void testContainsValue_invalidValue() {
315 boolean failedAsExpected = false;
316
317 try {
318 assertFalse(tree.containsValue(null));
319 }
320 catch (IllegalArgumentException e) {
321 failedAsExpected = true;
322 }
323
324 assertTrue(failedAsExpected);
325 }
326
327 @Test public void testContainsValue_emptyTree() {
328 assertFalse(tree.containsValue(KNOWN_SIMPLE_VALUE));
329 }
330
331 @Test public void testContainsValue_unknownValue() {
332 setManyMixedKeys();
333
334 assertFalse(tree.containsValue("hopefully unknown value"));
335 }
336
337 @Test public void testContainsValue_oneSimpleKey() {
338 setOneSimpleKey();
339
340 assertTrue(tree.containsValue(KNOWN_SIMPLE_VALUE));
341 }
342
343 @Test public void testContainsValue_oneComplexKey() {
344 setOneComplexKey();
345
346 assertTrue(tree.containsValue(KNOWN_COMPLEX_VALUE));
347 }
348
349 @Test public void testContainsValue_manyMixedKeys() {
350 setManyMixedKeys();
351
352 assertTrue(tree.containsValue(KNOWN_SIMPLE_VALUE));
353 assertTrue(tree.containsValue(KNOWN_COMPLEX_VALUE));
354 }
355
356
357
358 @Test public void testGet_invalidKey() {
359 boolean failedAsExpected = false;
360
361 try {
362 tree.get(null);
363 }
364 catch (IllegalArgumentException e) {
365 failedAsExpected = true;
366 }
367
368 assertTrue(failedAsExpected);
369 }
370
371 @Test public void testGet_unknownKey() {
372 setManyMixedKeys();
373
374 assertNull(tree.get("hopefully unknown key"));
375 }
376
377 @Test public void testGet_oneSimpleKey() {
378 setOneSimpleKey();
379
380 assertEquals(KNOWN_SIMPLE_VALUE, tree.get(KNOWN_SIMPLE_KEY).toString());
381 }
382
383 @Test public void testGet_oneComplexKey() {
384 setOneComplexKey();
385
386 assertEquals(KNOWN_COMPLEX_VALUE, tree.get(KNOWN_COMPLEX_KEY).toString());
387 }
388
389 @Test public void testGet_manyMixedKeys() {
390 setManyMixedKeys();
391
392 assertEquals(KNOWN_SIMPLE_VALUE, tree.get(KNOWN_SIMPLE_KEY).toString());
393 assertEquals(KNOWN_COMPLEX_VALUE, tree.get(KNOWN_COMPLEX_KEY).toString());
394 }
395
396 @Test public void testGet_chainedGet() throws Exception {
397 setManyMixedKeys();
398
399 String value = ((JstlPropertyHolder.PropertyTree) ((JstlPropertyHolder.PropertyTree) tree.get("known")).get("complex")).get("key").toString();
400
401 assertNotNull(value);
402 assertEquals(KNOWN_COMPLEX_VALUE, value);
403 }
404
405
406
407
408 @Test public void testGet_jstlGet() throws Exception {
409 setManyMixedKeys();
410
411 Class[] getParamTypes = { Object.class };
412
413 Object level1 = tree.get("known");
414
415 Method m1 = BeanUtils.findMethod(level1.getClass(), "get", getParamTypes);
416 Object level2 = m1.invoke(level1, new Object[] { "complex" });
417
418 Method m2 = BeanUtils.findMethod(level2.getClass(), "get", getParamTypes);
419 Object level3 = m2.invoke(level2, new Object[] { "key" });
420
421 String value = level3.toString();
422
423 assertNotNull(value);
424 assertEquals(KNOWN_COMPLEX_VALUE, value);
425 }
426
427
428 @Test public void testClear() {
429 setManyMixedKeys();
430
431 boolean failedAsExpected = false;
432
433 try {
434 tree.clear();
435 }
436 catch (UnsupportedOperationException e) {
437 failedAsExpected = true;
438 }
439
440 assertTrue(failedAsExpected);
441 }
442
443 @Test public void testPut() {
444 setManyMixedKeys();
445
446 boolean failedAsExpected = false;
447
448 try {
449 tree.put("meaningless", "entry");
450 }
451 catch (UnsupportedOperationException e) {
452 failedAsExpected = true;
453 }
454
455 assertTrue(failedAsExpected);
456 }
457
458 @Test public void testPutAll() {
459 setManyMixedKeys();
460
461 Properties p = new Properties();
462 p.setProperty("meaningless", "value");
463
464 boolean failedAsExpected = false;
465 try {
466 tree.putAll(p);
467 }
468 catch (UnsupportedOperationException e) {
469 failedAsExpected = true;
470 }
471
472 assertTrue(failedAsExpected);
473 }
474
475 @Test public void testRemove() {
476 setManyMixedKeys();
477
478 boolean failedAsExpected = false;
479
480 try {
481 tree.remove(KNOWN_SIMPLE_KEY);
482 }
483 catch (UnsupportedOperationException e) {
484 failedAsExpected = true;
485 }
486
487 assertTrue(failedAsExpected);
488 }
489
490
491
492 private void setOneSimpleKey() {
493 Properties p = new Properties();
494 p.setProperty(KNOWN_SIMPLE_KEY, KNOWN_SIMPLE_VALUE);
495
496 tree = new JstlPropertyHolder.PropertyTree(p);
497 }
498
499 private void setOneComplexKey() {
500 Properties p = new Properties();
501 p.setProperty(KNOWN_COMPLEX_KEY, KNOWN_COMPLEX_VALUE);
502
503 tree = new JstlPropertyHolder.PropertyTree(p);
504 }
505
506 private void setManyMixedKeys() {
507 Properties p = new Properties();
508
509 p.setProperty(KNOWN_SIMPLE_KEY, KNOWN_SIMPLE_VALUE);
510 p.setProperty(KNOWN_COMPLEX_KEY, KNOWN_COMPLEX_VALUE);
511 p.setProperty("a", "a");
512 p.setProperty("b.b", "bb");
513 p.setProperty("b.c", "cb");
514 p.setProperty("c.b.c", "cbc");
515 p.setProperty("c.b.d", "dbc");
516 p.setProperty("c.c.a", "acc");
517 p.setProperty("a.c.b", "bca");
518 p.setProperty("a.c.c", "cca");
519 p.setProperty("b.a", "ab");
520 p.setProperty("b", "b");
521 p.setProperty("d", "d");
522
523 tree = new JstlPropertyHolder.PropertyTree(p);
524 }
525 }