001: /*
002: * Copyright 2005-2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.kuali.core.util.properties;
018:
019: import java.lang.reflect.Method;
020: import java.util.Collection;
021: import java.util.Iterator;
022: import java.util.Map;
023: import java.util.Properties;
024: import java.util.Set;
025:
026: import org.kuali.kfs.context.KualiTestBase;
027: import org.springframework.beans.BeanUtils;
028:
029: /**
030: * This class tests the PropertyTreeTest methods.
031: */
032: public class PropertyTreeTest extends KualiTestBase {
033: private static final String KNOWN_SIMPLE_KEY = "simple";
034: private static final String KNOWN_SIMPLE_VALUE = "simple value";
035: private static final String KNOWN_COMPLEX_KEY = "known.complex.key";
036: private static final String KNOWN_COMPLEX_VALUE = "known complex value";
037: private static final int MIXED_COUNT = 13;
038:
039: PropertyTree tree;
040:
041: @Override
042: protected void setUp() throws Exception {
043: super .setUp();
044: tree = new PropertyTree();
045: }
046:
047: // entrySet
048: public final void testEntrySet_readwrite() {
049: Set entrySet = tree.entrySet();
050:
051: boolean failedAsExpected = false;
052:
053: try {
054: entrySet.clear();
055: } catch (UnsupportedOperationException e) {
056: failedAsExpected = true;
057: }
058:
059: assertTrue(failedAsExpected);
060: }
061:
062: public final void testEntrySet_emptyTree() {
063: Set entrySet = tree.entrySet();
064:
065: assertTrue(entrySet.isEmpty());
066: }
067:
068: public final void testEntrySet_oneSimpleKey() {
069: setOneSimpleKey();
070: Set entrySet = tree.entrySet();
071:
072: assertEquals(1, entrySet.size());
073:
074: boolean foundSimple = false;
075: for (Iterator i = entrySet.iterator(); i.hasNext();) {
076: Map.Entry e = (Map.Entry) i.next();
077: if (e.getKey().equals(KNOWN_SIMPLE_KEY)) {
078: foundSimple = true;
079: assertEquals(e.getValue(), KNOWN_SIMPLE_VALUE);
080: }
081: }
082: assertTrue(foundSimple);
083: }
084:
085: public final void testEntrySet_oneComplexKey() {
086: setOneComplexKey();
087: Set entrySet = tree.entrySet();
088:
089: assertEquals(1, entrySet.size());
090:
091: boolean foundComplex = false;
092: for (Iterator i = entrySet.iterator(); i.hasNext();) {
093: Map.Entry e = (Map.Entry) i.next();
094: if (e.getKey().equals(KNOWN_COMPLEX_KEY)) {
095: foundComplex = true;
096: assertEquals(e.getValue(), KNOWN_COMPLEX_VALUE);
097: }
098: }
099: assertTrue(foundComplex);
100: }
101:
102: public final void testEntrySet_manyMixedKeys() {
103: setManyMixedKeys();
104: Set entrySet = tree.entrySet();
105:
106: assertEquals(MIXED_COUNT, entrySet.size());
107:
108: boolean foundSimple = false;
109: boolean foundComplex = false;
110: for (Iterator i = entrySet.iterator(); i.hasNext();) {
111: Map.Entry e = (Map.Entry) i.next();
112: if (e.getKey().equals(KNOWN_SIMPLE_KEY)) {
113: foundSimple = true;
114: assertEquals(e.getValue(), KNOWN_SIMPLE_VALUE);
115: } else if (e.getKey().equals(KNOWN_COMPLEX_KEY)) {
116: foundComplex = true;
117: assertEquals(e.getValue(), KNOWN_COMPLEX_VALUE);
118: }
119: }
120: assertTrue(foundSimple);
121: assertTrue(foundComplex);
122: }
123:
124: // size()
125: public final void testSize_emptyTree() {
126: assertEquals(0, tree.size());
127: }
128:
129: public final void testSize_oneSimpleKey() {
130: setOneSimpleKey();
131:
132: assertEquals(1, tree.size());
133: }
134:
135: public final void testSize_oneComplexKey() {
136: setOneComplexKey();
137:
138: assertEquals(1, tree.size());
139: }
140:
141: public final void testSize_manyMixedKeys() {
142: setManyMixedKeys();
143:
144: assertEquals(MIXED_COUNT, tree.size());
145: }
146:
147: // isEmpty
148: public final void testIsEmpty_emptyTree() {
149: assertTrue(tree.isEmpty());
150: }
151:
152: public final void testIsEmpty_oneSimpleKey() {
153: setOneSimpleKey();
154:
155: assertFalse(tree.isEmpty());
156: }
157:
158: public final void testIsEmpty_oneComplexKey() {
159: setOneComplexKey();
160:
161: assertFalse(tree.isEmpty());
162: }
163:
164: public final void testIsEmpty_manyMixedKeys() {
165: setManyMixedKeys();
166:
167: assertFalse(tree.isEmpty());
168: }
169:
170: // values
171: public final void testValues_readwrite() {
172: Collection values = tree.values();
173:
174: boolean failedAsExpected = false;
175:
176: try {
177: values.clear();
178: } catch (UnsupportedOperationException e) {
179: failedAsExpected = true;
180: }
181:
182: assertTrue(failedAsExpected);
183: }
184:
185: public final void testValues_emptyTree() {
186: Collection values = tree.values();
187:
188: assertTrue(values.isEmpty());
189: }
190:
191: public final void testValues_oneSimpleKey() {
192: setOneSimpleKey();
193: Collection values = tree.values();
194:
195: assertEquals(1, values.size());
196: assertTrue(values.contains(KNOWN_SIMPLE_VALUE));
197: }
198:
199: public final void testValues_oneComplexKey() {
200: setOneComplexKey();
201: Collection values = tree.values();
202:
203: assertEquals(1, values.size());
204: assertTrue(values.contains(KNOWN_COMPLEX_VALUE));
205: }
206:
207: public final void testValues_manyMixedKeys() {
208: setManyMixedKeys();
209: Collection values = tree.values();
210:
211: assertEquals(MIXED_COUNT, values.size());
212: assertTrue(values.contains(KNOWN_SIMPLE_VALUE));
213: assertTrue(values.contains(KNOWN_COMPLEX_VALUE));
214: }
215:
216: // keySet
217: public final void testKeySet_readwrite() {
218: Set keys = tree.keySet();
219:
220: boolean failedAsExpected = false;
221:
222: try {
223: keys.clear();
224: } catch (UnsupportedOperationException e) {
225: failedAsExpected = true;
226: }
227:
228: assertTrue(failedAsExpected);
229: }
230:
231: public final void testKeySet_emptyTree() {
232: Set keys = tree.keySet();
233:
234: assertTrue(keys.isEmpty());
235: }
236:
237: public final void testKeySet_oneSimpleKey() {
238: setOneSimpleKey();
239: Set keys = tree.keySet();
240:
241: assertEquals(1, keys.size());
242: assertTrue(keys.contains(KNOWN_SIMPLE_KEY));
243: }
244:
245: public final void testKeySet_oneComplexKey() {
246: setOneComplexKey();
247: Set keys = tree.keySet();
248:
249: assertEquals(1, keys.size());
250: assertTrue(keys.contains(KNOWN_COMPLEX_KEY));
251: }
252:
253: public final void testKeySet_manyMixedKeys() {
254: setManyMixedKeys();
255: Set keys = tree.keySet();
256:
257: assertEquals(MIXED_COUNT, keys.size());
258: assertTrue(keys.contains(KNOWN_SIMPLE_KEY));
259: assertTrue(keys.contains(KNOWN_COMPLEX_KEY));
260: }
261:
262: // containsKey
263: public final void testContainsKey_invalidKey() {
264: boolean failedAsExpected = false;
265:
266: try {
267: assertFalse(tree.containsKey(null));
268: } catch (IllegalArgumentException e) {
269: failedAsExpected = true;
270: }
271:
272: assertTrue(failedAsExpected);
273: }
274:
275: public final void testContainsKey_emptyTree() {
276: assertFalse(tree.containsKey(KNOWN_SIMPLE_KEY));
277: }
278:
279: public final void testContainsKey_unknownKey() {
280: setManyMixedKeys();
281:
282: assertFalse(tree.containsKey("hopefully unknown key"));
283: }
284:
285: public final void testContainsKey_oneSimpleKey() {
286: setOneSimpleKey();
287:
288: assertTrue(tree.containsKey(KNOWN_SIMPLE_KEY));
289: }
290:
291: public final void testContainsKey_oneComplexKey() {
292: setOneComplexKey();
293:
294: assertTrue(tree.containsKey(KNOWN_COMPLEX_KEY));
295: }
296:
297: public final void testContainsKey_manyMixedKeys() {
298: setManyMixedKeys();
299:
300: assertTrue(tree.containsKey(KNOWN_SIMPLE_KEY));
301: assertTrue(tree.containsKey(KNOWN_COMPLEX_KEY));
302: }
303:
304: // containsValue
305: public final void testContainsValue_invalidValue() {
306: boolean failedAsExpected = false;
307:
308: try {
309: assertFalse(tree.containsValue(null));
310: } catch (IllegalArgumentException e) {
311: failedAsExpected = true;
312: }
313:
314: assertTrue(failedAsExpected);
315: }
316:
317: public final void testContainsValue_emptyTree() {
318: assertFalse(tree.containsValue(KNOWN_SIMPLE_VALUE));
319: }
320:
321: public final void testContainsValue_unknownValue() {
322: setManyMixedKeys();
323:
324: assertFalse(tree.containsValue("hopefully unknown value"));
325: }
326:
327: public final void testContainsValue_oneSimpleKey() {
328: setOneSimpleKey();
329:
330: assertTrue(tree.containsValue(KNOWN_SIMPLE_VALUE));
331: }
332:
333: public final void testContainsValue_oneComplexKey() {
334: setOneComplexKey();
335:
336: assertTrue(tree.containsValue(KNOWN_COMPLEX_VALUE));
337: }
338:
339: public final void testContainsValue_manyMixedKeys() {
340: setManyMixedKeys();
341:
342: assertTrue(tree.containsValue(KNOWN_SIMPLE_VALUE));
343: assertTrue(tree.containsValue(KNOWN_COMPLEX_VALUE));
344: }
345:
346: // get
347: public final void testGet_invalidKey() {
348: boolean failedAsExpected = false;
349:
350: try {
351: tree.get(null);
352: } catch (IllegalArgumentException e) {
353: failedAsExpected = true;
354: }
355:
356: assertTrue(failedAsExpected);
357: }
358:
359: public final void testGet_unknownKey() {
360: setManyMixedKeys();
361:
362: assertNull(tree.get("hopefully unknown key"));
363: }
364:
365: public final void testGet_oneSimpleKey() {
366: setOneSimpleKey();
367:
368: assertEquals(KNOWN_SIMPLE_VALUE, tree.get(KNOWN_SIMPLE_KEY)
369: .toString());
370: }
371:
372: public final void testGet_oneComplexKey() {
373: setOneComplexKey();
374:
375: assertEquals(KNOWN_COMPLEX_VALUE, tree.get(KNOWN_COMPLEX_KEY)
376: .toString());
377: }
378:
379: public final void testGet_manyMixedKeys() {
380: setManyMixedKeys();
381:
382: assertEquals(KNOWN_SIMPLE_VALUE, tree.get(KNOWN_SIMPLE_KEY)
383: .toString());
384: assertEquals(KNOWN_COMPLEX_VALUE, tree.get(KNOWN_COMPLEX_KEY)
385: .toString());
386: }
387:
388: public final void testGet_chainedGet() throws Exception {
389: setManyMixedKeys();
390:
391: String value = ((PropertyTree) ((PropertyTree) tree
392: .get("known")).get("complex")).get("key").toString();
393:
394: assertNotNull(value);
395: assertEquals(KNOWN_COMPLEX_VALUE, value);
396: }
397:
398: /*
399: * As close a simulation as possible of how the JSTL variable-reference will actually be implemented.
400: */
401: public final void testGet_jstlGet() throws Exception {
402: setManyMixedKeys();
403:
404: Class[] getParamTypes = { Object.class };
405:
406: Object level1 = tree.get("known");
407:
408: Method m1 = BeanUtils.findMethod(level1.getClass(), "get",
409: getParamTypes);
410: Object level2 = m1.invoke(level1, new Object[] { "complex" });
411:
412: Method m2 = BeanUtils.findMethod(level2.getClass(), "get",
413: getParamTypes);
414: Object level3 = m2.invoke(level2, new Object[] { "key" });
415:
416: String value = level3.toString();
417:
418: assertNotNull(value);
419: assertEquals(KNOWN_COMPLEX_VALUE, value);
420: }
421:
422: // unsupported operations
423: public final void testClear() {
424: setManyMixedKeys();
425:
426: boolean failedAsExpected = false;
427:
428: try {
429: tree.clear();
430: } catch (UnsupportedOperationException e) {
431: failedAsExpected = true;
432: }
433:
434: assertTrue(failedAsExpected);
435: }
436:
437: public final void testPut() {
438: setManyMixedKeys();
439:
440: boolean failedAsExpected = false;
441:
442: try {
443: tree.put("meaningless", "entry");
444: } catch (UnsupportedOperationException e) {
445: failedAsExpected = true;
446: }
447:
448: assertTrue(failedAsExpected);
449: }
450:
451: public final void testPutAll() {
452: setManyMixedKeys();
453:
454: Properties p = new Properties();
455: p.setProperty("meaningless", "value");
456:
457: boolean failedAsExpected = false;
458: try {
459: tree.putAll(p);
460: } catch (UnsupportedOperationException e) {
461: failedAsExpected = true;
462: }
463:
464: assertTrue(failedAsExpected);
465: }
466:
467: public final void testRemove() {
468: setManyMixedKeys();
469:
470: boolean failedAsExpected = false;
471:
472: try {
473: tree.remove(KNOWN_SIMPLE_KEY);
474: } catch (UnsupportedOperationException e) {
475: failedAsExpected = true;
476: }
477:
478: assertTrue(failedAsExpected);
479: }
480:
481: // support methods
482: private void setOneSimpleKey() {
483: Properties p = new Properties();
484: p.setProperty(KNOWN_SIMPLE_KEY, KNOWN_SIMPLE_VALUE);
485:
486: tree = new PropertyTree(p);
487: }
488:
489: private void setOneComplexKey() {
490: Properties p = new Properties();
491: p.setProperty(KNOWN_COMPLEX_KEY, KNOWN_COMPLEX_VALUE);
492:
493: tree = new PropertyTree(p);
494: }
495:
496: private void setManyMixedKeys() {
497: Properties p = new Properties();
498:
499: p.setProperty(KNOWN_SIMPLE_KEY, KNOWN_SIMPLE_VALUE);
500: p.setProperty(KNOWN_COMPLEX_KEY, KNOWN_COMPLEX_VALUE);
501: p.setProperty("a", "a");
502: p.setProperty("b.b", "bb");
503: p.setProperty("b.c", "cb");
504: p.setProperty("c.b.c", "cbc");
505: p.setProperty("c.b.d", "dbc");
506: p.setProperty("c.c.a", "acc");
507: p.setProperty("a.c.b", "bca");
508: p.setProperty("a.c.c", "cca");
509: p.setProperty("b.a", "ab");
510: p.setProperty("b", "b");
511: p.setProperty("d", "d");
512:
513: tree = new PropertyTree(p);
514: }
515: }
|