001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: TestHierarchicalProperties.java 3643 2007-01-12 15:29:45Z gbevin $
007: */
008: package com.uwyn.rife.ioc;
009:
010: import com.uwyn.rife.ioc.exceptions.IncompatiblePropertyValueTypeException;
011: import com.uwyn.rife.ioc.exceptions.ParticipantUnknownException;
012: import com.uwyn.rife.ioc.exceptions.PropertyValueException;
013: import java.math.BigDecimal;
014: import java.util.Date;
015: import java.util.Iterator;
016: import java.util.LinkedHashMap;
017: import java.util.Map;
018: import junit.framework.TestCase;
019:
020: public class TestHierarchicalProperties extends TestCase {
021: public TestHierarchicalProperties(String name) {
022: super (name);
023: }
024:
025: public void testInstantiation() {
026: HierarchicalProperties properties = new HierarchicalProperties();
027: assertNotNull(properties);
028: assertEquals(0, properties.size());
029: assertNotNull(properties.getNames());
030: assertEquals(0, properties.getNames().size());
031: assertNotNull(properties.getInjectableNames());
032: assertEquals(0, properties.getInjectableNames().size());
033: assertNotNull(properties.getLocalMap());
034: assertEquals(0, properties.getLocalMap().size());
035: }
036:
037: public void testSingleInstance() {
038: Iterator names_it;
039:
040: PropertyValue property1 = new PropertyValueObject("value1");
041: PropertyValue property2 = new PropertyValueObject("value2");
042: PropertyValue property3 = new PropertyValueObject("value3");
043: PropertyValue property4 = new PropertyValueObject("value4");
044: PropertyValue property5 = new PropertyValueObject("value5");
045:
046: HierarchicalProperties properties = new HierarchicalProperties();
047: assertSame(properties, properties.put("name1", property1));
048: assertSame(properties, properties.put("name2", property2));
049: assertSame(properties, properties.put("non.identifier.name3",
050: property3));
051: assertEquals(3, properties.size());
052: assertNotNull(properties.getLocalMap());
053: assertEquals(3, properties.getLocalMap().size());
054:
055: assertTrue(properties.contains("name1"));
056: assertTrue(properties.contains("name2"));
057: assertTrue(properties.contains("non.identifier.name3"));
058:
059: assertSame(property1, properties.get("name1"));
060: assertSame(property2, properties.get("name2"));
061: assertSame(property3, properties.get("non.identifier.name3"));
062: assertEquals("value1", properties.getValue("name1"));
063: assertEquals("value2", properties.getValue("name2"));
064: assertEquals("value3", properties
065: .getValue("non.identifier.name3"));
066: assertEquals("value1", properties.getValueString("name1"));
067: assertEquals("value2", properties.getValueString("name2"));
068: assertEquals("value3", properties
069: .getValueString("non.identifier.name3"));
070:
071: names_it = properties.getNames().iterator();
072: assertEquals("name1", names_it.next());
073: assertEquals("name2", names_it.next());
074: assertEquals("non.identifier.name3", names_it.next());
075: assertFalse(names_it.hasNext());
076:
077: names_it = properties.getInjectableNames().iterator();
078: assertEquals("name1", names_it.next());
079: assertEquals("name2", names_it.next());
080: assertFalse(names_it.hasNext());
081:
082: assertSame(property2, properties.remove("name2"));
083: assertEquals(2, properties.size());
084:
085: assertTrue(properties.contains("name1"));
086: assertFalse(properties.contains("name2"));
087: assertTrue(properties.contains("non.identifier.name3"));
088:
089: names_it = properties.getNames().iterator();
090: assertEquals("name1", names_it.next());
091: assertEquals("non.identifier.name3", names_it.next());
092: assertFalse(names_it.hasNext());
093:
094: names_it = properties.getInjectableNames().iterator();
095: assertEquals("name1", names_it.next());
096: assertFalse(names_it.hasNext());
097:
098: HierarchicalProperties properties_alternative = new HierarchicalProperties();
099: assertSame(properties_alternative, properties_alternative.put(
100: "name4", property4));
101: assertSame(properties_alternative, properties_alternative.put(
102: "non.identifier.name5", property5));
103: assertEquals(2, properties_alternative.size());
104:
105: assertSame(properties, properties
106: .putAll(properties_alternative));
107:
108: assertEquals(4, properties.size());
109:
110: assertTrue(properties.contains("name1"));
111: assertTrue(properties.contains("non.identifier.name3"));
112: assertTrue(properties.contains("name4"));
113: assertTrue(properties.contains("non.identifier.name5"));
114:
115: assertSame(property1, properties.get("name1"));
116: assertSame(property3, properties.get("non.identifier.name3"));
117: assertSame(property4, properties.get("name4"));
118: assertSame(property5, properties.get("non.identifier.name5"));
119: assertEquals("value1", properties.getValue("name1"));
120: assertNull(properties.getValue("name2"));
121: Integer default_value = new Integer(34);
122: assertSame(default_value, properties.getValue("name2",
123: default_value));
124: assertEquals("value3", properties
125: .getValue("non.identifier.name3"));
126: assertEquals("value4", properties.getValue("name4"));
127: assertEquals("value5", properties
128: .getValue("non.identifier.name5"));
129: assertEquals("value1", properties.getValueString("name1"));
130: assertNull(properties.getValueString("name2"));
131: assertEquals("somevalue", properties.getValueString("name2",
132: "somevalue"));
133: assertEquals("value3", properties
134: .getValueString("non.identifier.name3"));
135: assertEquals("value4", properties.getValueString("name4"));
136: assertEquals("value5", properties
137: .getValueString("non.identifier.name5"));
138:
139: names_it = properties.getNames().iterator();
140: assertEquals("name1", names_it.next());
141: assertEquals("non.identifier.name3", names_it.next());
142: assertEquals("name4", names_it.next());
143: assertEquals("non.identifier.name5", names_it.next());
144: assertFalse(names_it.hasNext());
145:
146: names_it = properties.getInjectableNames().iterator();
147: assertEquals("name1", names_it.next());
148: assertEquals("name4", names_it.next());
149: assertFalse(names_it.hasNext());
150: }
151:
152: public void testHierarchy() {
153: /*
154: * This is the hierarchy that's being built.
155: *
156: * grandparent
157: * -----------
158: * name1 (property1d)
159: * name2 (property2d)
160: * name2_grandparent (property2d)
161: * non.identifier.name3 (property3d)
162: * |
163: * ______________|___________________________
164: * / \
165: * / \
166: * parent1 parent2
167: * ------- -------
168: * name1 (property1c) name2 (property2c)
169: * name2_parent (property2c) non.identifier.name3_parent (property3c)
170: * | |
171: * ___________|___________________ |
172: * / \ |
173: * / \ |
174: * child1 child2 child3
175: * ------ ------ ------
176: * name1 (property1) name1 (property1b) non.identifier.name3 (property3b)
177: * name2 (property2) name2 (property2b)
178: * non.identifier.name3 (property3)
179: */
180: Iterator names_it;
181:
182: PropertyValue property1 = new PropertyValueObject("value1");
183: PropertyValue property2 = new PropertyValueObject("value2");
184: PropertyValue property3 = new PropertyValueObject("value3");
185: PropertyValue property1b = new PropertyValueObject("value1b");
186: PropertyValue property2b = new PropertyValueObject("value2b");
187: PropertyValue property3b = new PropertyValueObject("value3b");
188: PropertyValue property1c = new PropertyValueObject("value1c");
189: PropertyValue property2c = new PropertyValueObject("value2c");
190: PropertyValue property3c = new PropertyValueObject("value3c");
191: PropertyValue property1d = new PropertyValueObject("value1d");
192: PropertyValue property2d = new PropertyValueObject("value2d");
193: PropertyValue property3d = new PropertyValueObject("value3d");
194:
195: HierarchicalProperties properties_child1 = new HierarchicalProperties();
196: assertSame(properties_child1, properties_child1.put("name1",
197: property1));
198: assertSame(properties_child1, properties_child1.put("name2",
199: property2));
200: assertSame(properties_child1, properties_child1.put(
201: "non.identifier.name3", property3));
202: assertEquals(3, properties_child1.size());
203: assertSame(property1, properties_child1.get("name1"));
204: assertSame(property2, properties_child1.get("name2"));
205: assertSame(property3, properties_child1
206: .get("non.identifier.name3"));
207: names_it = properties_child1.getNames().iterator();
208: assertEquals("name1", names_it.next());
209: assertEquals("name2", names_it.next());
210: assertEquals("non.identifier.name3", names_it.next());
211: assertFalse(names_it.hasNext());
212: names_it = properties_child1.getInjectableNames().iterator();
213: assertEquals("name1", names_it.next());
214: assertEquals("name2", names_it.next());
215: assertFalse(names_it.hasNext());
216:
217: HierarchicalProperties properties_child2 = new HierarchicalProperties();
218: assertSame(properties_child2, properties_child2.put("name1",
219: property1b));
220: assertSame(properties_child2, properties_child2.put("name2",
221: property2b));
222: assertEquals(2, properties_child2.size());
223: assertSame(property1b, properties_child2.get("name1"));
224: assertSame(property2b, properties_child2.get("name2"));
225: names_it = properties_child2.getNames().iterator();
226: assertEquals("name1", names_it.next());
227: assertEquals("name2", names_it.next());
228: assertFalse(names_it.hasNext());
229: names_it = properties_child2.getInjectableNames().iterator();
230: assertEquals("name1", names_it.next());
231: assertEquals("name2", names_it.next());
232: assertFalse(names_it.hasNext());
233:
234: HierarchicalProperties properties_child3 = new HierarchicalProperties();
235: assertSame(properties_child3, properties_child3.put(
236: "non.identifier.name3", property3b));
237: assertEquals(1, properties_child3.size());
238: assertSame(property3b, properties_child3
239: .get("non.identifier.name3"));
240: names_it = properties_child3.getNames().iterator();
241: assertEquals("non.identifier.name3", names_it.next());
242: assertFalse(names_it.hasNext());
243: names_it = properties_child3.getInjectableNames().iterator();
244: assertFalse(names_it.hasNext());
245:
246: HierarchicalProperties properties_parent1 = new HierarchicalProperties();
247: assertSame(properties_parent1, properties_parent1.put("name1",
248: property1c));
249: assertSame(properties_parent1, properties_parent1.put(
250: "name2_parent", property2c));
251: assertEquals(2, properties_parent1.size());
252: assertSame(property1c, properties_parent1.get("name1"));
253: assertSame(property2c, properties_parent1.get("name2_parent"));
254: names_it = properties_parent1.getNames().iterator();
255: assertEquals("name1", names_it.next());
256: assertEquals("name2_parent", names_it.next());
257: assertFalse(names_it.hasNext());
258: names_it = properties_parent1.getInjectableNames().iterator();
259: assertEquals("name1", names_it.next());
260: assertEquals("name2_parent", names_it.next());
261: assertFalse(names_it.hasNext());
262:
263: HierarchicalProperties properties_parent2 = new HierarchicalProperties();
264: assertSame(properties_parent2, properties_parent2.put("name2",
265: property2c));
266: assertSame(properties_parent2, properties_parent2.put(
267: "non.identifier.name3_parent", property3c));
268: assertEquals(2, properties_parent2.size());
269: assertSame(property2c, properties_parent2.get("name2"));
270: assertSame(property3c, properties_parent2
271: .get("non.identifier.name3_parent"));
272: names_it = properties_parent2.getNames().iterator();
273: assertEquals("name2", names_it.next());
274: assertEquals("non.identifier.name3_parent", names_it.next());
275: assertFalse(names_it.hasNext());
276: names_it = properties_parent2.getInjectableNames().iterator();
277: assertEquals("name2", names_it.next());
278: assertFalse(names_it.hasNext());
279:
280: HierarchicalProperties properties_grandparent = new HierarchicalProperties();
281: assertSame(properties_grandparent, properties_grandparent.put(
282: "name1", property1d));
283: assertSame(properties_grandparent, properties_grandparent.put(
284: "name2", property2d));
285: assertSame(properties_grandparent, properties_grandparent.put(
286: "name2_grandparent", property2d));
287: assertSame(properties_grandparent, properties_grandparent.put(
288: "non.identifier.name3", property3d));
289: assertEquals(4, properties_grandparent.size());
290: assertSame(property1d, properties_grandparent.get("name1"));
291: assertSame(property2d, properties_grandparent.get("name2"));
292: assertSame(property2d, properties_grandparent
293: .get("name2_grandparent"));
294: assertSame(property3d, properties_grandparent
295: .get("non.identifier.name3"));
296: names_it = properties_grandparent.getNames().iterator();
297: assertEquals("name1", names_it.next());
298: assertEquals("name2", names_it.next());
299: assertEquals("name2_grandparent", names_it.next());
300: assertEquals("non.identifier.name3", names_it.next());
301: assertFalse(names_it.hasNext());
302: names_it = properties_grandparent.getInjectableNames()
303: .iterator();
304: assertEquals("name1", names_it.next());
305: assertEquals("name2", names_it.next());
306: assertEquals("name2_grandparent", names_it.next());
307: assertFalse(names_it.hasNext());
308:
309: // set the first level parents
310: properties_child1.parent(properties_parent1);
311: assertSame(properties_parent1, properties_child1.getRoot());
312: assertEquals(4, properties_child1.size());
313: assertSame(property1, properties_child1.get("name1"));
314: assertSame(property2, properties_child1.get("name2"));
315: assertSame(property2c, properties_child1.get("name2_parent"));
316: assertSame(property3, properties_child1
317: .get("non.identifier.name3"));
318: names_it = properties_child1.getNames().iterator();
319: assertEquals("name1", names_it.next());
320: assertEquals("name2", names_it.next());
321: assertEquals("non.identifier.name3", names_it.next());
322: assertEquals("name2_parent", names_it.next());
323: assertFalse(names_it.hasNext());
324: names_it = properties_child1.getInjectableNames().iterator();
325: assertEquals("name1", names_it.next());
326: assertEquals("name2", names_it.next());
327: assertEquals("name2_parent", names_it.next());
328: assertFalse(names_it.hasNext());
329:
330: properties_child2.parent(properties_parent1);
331: assertSame(properties_parent1, properties_child2.getRoot());
332: assertEquals(3, properties_child2.size());
333: assertSame(property1b, properties_child2.get("name1"));
334: assertSame(property2b, properties_child2.get("name2"));
335: names_it = properties_child2.getNames().iterator();
336: assertEquals("name1", names_it.next());
337: assertEquals("name2", names_it.next());
338: assertEquals("name2_parent", names_it.next());
339: assertFalse(names_it.hasNext());
340: names_it = properties_child2.getInjectableNames().iterator();
341: assertEquals("name1", names_it.next());
342: assertEquals("name2", names_it.next());
343: assertEquals("name2_parent", names_it.next());
344: assertFalse(names_it.hasNext());
345:
346: properties_child3.parent(properties_parent2);
347: assertSame(properties_parent2, properties_child3.getRoot());
348: assertEquals(3, properties_child3.size());
349: assertSame(property3b, properties_child3
350: .get("non.identifier.name3"));
351: assertSame(property2c, properties_child3.get("name2"));
352: assertSame(property3c, properties_child3
353: .get("non.identifier.name3_parent"));
354: names_it = properties_child3.getNames().iterator();
355: assertEquals("non.identifier.name3", names_it.next());
356: assertEquals("name2", names_it.next());
357: assertEquals("non.identifier.name3_parent", names_it.next());
358: assertFalse(names_it.hasNext());
359: names_it = properties_child3.getInjectableNames().iterator();
360: assertEquals("name2", names_it.next());
361: assertFalse(names_it.hasNext());
362:
363: // set the second level parents
364: properties_parent1.parent(properties_grandparent);
365: assertEquals(5, properties_child1.size());
366: assertSame(property1, properties_child1.get("name1"));
367: assertSame(property2, properties_child1.get("name2"));
368: assertSame(property2c, properties_child1.get("name2_parent"));
369: assertSame(property3, properties_child1
370: .get("non.identifier.name3"));
371: assertSame(property2d, properties_child1
372: .get("name2_grandparent"));
373: names_it = properties_child1.getNames().iterator();
374: assertEquals("name1", names_it.next());
375: assertEquals("name2", names_it.next());
376: assertEquals("non.identifier.name3", names_it.next());
377: assertEquals("name2_parent", names_it.next());
378: assertEquals("name2_grandparent", names_it.next());
379: assertFalse(names_it.hasNext());
380: names_it = properties_child1.getInjectableNames().iterator();
381: assertEquals("name1", names_it.next());
382: assertEquals("name2", names_it.next());
383: assertEquals("name2_parent", names_it.next());
384: assertEquals("name2_grandparent", names_it.next());
385: assertFalse(names_it.hasNext());
386:
387: properties_parent2.parent(properties_grandparent);
388: assertEquals(5, properties_child3.size());
389: assertSame(property3b, properties_child3
390: .get("non.identifier.name3"));
391: assertSame(property2c, properties_child3.get("name2"));
392: assertSame(property3c, properties_child3
393: .get("non.identifier.name3_parent"));
394: assertSame(property1d, properties_child3.get("name1"));
395: assertSame(property2d, properties_child3
396: .get("name2_grandparent"));
397: names_it = properties_child3.getNames().iterator();
398: assertEquals("non.identifier.name3", names_it.next());
399: assertEquals("name2", names_it.next());
400: assertEquals("non.identifier.name3_parent", names_it.next());
401: assertEquals("name1", names_it.next());
402: assertEquals("name2_grandparent", names_it.next());
403: assertFalse(names_it.hasNext());
404: names_it = properties_child3.getInjectableNames().iterator();
405: assertEquals("name2", names_it.next());
406: assertEquals("name1", names_it.next());
407: assertEquals("name2_grandparent", names_it.next());
408: assertFalse(names_it.hasNext());
409:
410: assertSame(properties_grandparent, properties_child1.getRoot());
411: assertSame(properties_grandparent, properties_child2.getRoot());
412: assertSame(properties_grandparent, properties_child3.getRoot());
413:
414: // manipulate the hierarchy
415: assertSame(property1, properties_child1.remove("name1"));
416: assertNull(properties_child1.remove("name1"));
417: assertSame(property1c, properties_child1.get("name1"));
418:
419: assertSame(property2c, properties_parent1
420: .remove("name2_parent"));
421: assertNull(properties_parent1.remove("name2_parent"));
422: assertNull(properties_parent1.get("name2_parent"));
423:
424: assertSame(property1c, properties_parent1.remove("name1"));
425: assertNull(properties_child1.remove("name1"));
426: assertSame(property1d, properties_child1.get("name1"));
427: assertSame(property1d, properties_parent1.get("name1"));
428:
429: assertSame(property3b, properties_child3
430: .remove("non.identifier.name3"));
431: assertNull(properties_child3.remove("non.identifier.name3"));
432: assertSame(property3d, properties_child3
433: .get("non.identifier.name3"));
434:
435: assertSame(property2c, properties_parent2.remove("name2"));
436: assertNull(properties_parent2.remove("name2"));
437: assertSame(property2d, properties_child3.get("name2"));
438:
439: assertSame(property2d, properties_grandparent.remove("name2"));
440: assertNull(properties_grandparent.remove("name2"));
441: assertNull(properties_child3.get("name2"));
442:
443: names_it = properties_child1.getNames().iterator();
444: assertEquals("name2", names_it.next());
445: assertEquals("non.identifier.name3", names_it.next());
446: assertEquals("name1", names_it.next());
447: assertEquals("name2_grandparent", names_it.next());
448: assertFalse(names_it.hasNext());
449: names_it = properties_child1.getInjectableNames().iterator();
450: assertEquals("name2", names_it.next());
451: assertEquals("name1", names_it.next());
452: assertEquals("name2_grandparent", names_it.next());
453: assertFalse(names_it.hasNext());
454:
455: names_it = properties_child2.getNames().iterator();
456: assertEquals("name1", names_it.next());
457: assertEquals("name2", names_it.next());
458: assertEquals("name2_grandparent", names_it.next());
459: assertEquals("non.identifier.name3", names_it.next());
460: assertFalse(names_it.hasNext());
461: names_it = properties_child2.getInjectableNames().iterator();
462: assertEquals("name1", names_it.next());
463: assertEquals("name2", names_it.next());
464: assertEquals("name2_grandparent", names_it.next());
465: assertFalse(names_it.hasNext());
466:
467: names_it = properties_child3.getNames().iterator();
468: assertEquals("non.identifier.name3_parent", names_it.next());
469: assertEquals("name1", names_it.next());
470: assertEquals("name2_grandparent", names_it.next());
471: assertEquals("non.identifier.name3", names_it.next());
472: assertFalse(names_it.hasNext());
473: names_it = properties_child3.getInjectableNames().iterator();
474: assertEquals("name1", names_it.next());
475: assertEquals("name2_grandparent", names_it.next());
476: assertFalse(names_it.hasNext());
477:
478: PropertyValue property2e = new PropertyValueObject("value1e");
479: assertSame(properties_parent1, properties_parent1.put(
480: "new_name2", property2e));
481: PropertyValue property3e = new PropertyValueObject("value3e");
482: assertSame(properties_grandparent, properties_grandparent.put(
483: "new_non.identifier.name3", property3e));
484:
485: names_it = properties_child1.getNames().iterator();
486: assertEquals("name2", names_it.next());
487: assertEquals("non.identifier.name3", names_it.next());
488: assertEquals("new_name2", names_it.next());
489: assertEquals("name1", names_it.next());
490: assertEquals("name2_grandparent", names_it.next());
491: assertEquals("new_non.identifier.name3", names_it.next());
492: assertFalse(names_it.hasNext());
493: names_it = properties_child1.getInjectableNames().iterator();
494: assertEquals("name2", names_it.next());
495: assertEquals("new_name2", names_it.next());
496: assertEquals("name1", names_it.next());
497: assertEquals("name2_grandparent", names_it.next());
498: assertFalse(names_it.hasNext());
499:
500: names_it = properties_child2.getNames().iterator();
501: assertEquals("name1", names_it.next());
502: assertEquals("name2", names_it.next());
503: assertEquals("new_name2", names_it.next());
504: assertEquals("name2_grandparent", names_it.next());
505: assertEquals("non.identifier.name3", names_it.next());
506: assertEquals("new_non.identifier.name3", names_it.next());
507: assertFalse(names_it.hasNext());
508: names_it = properties_child2.getInjectableNames().iterator();
509: assertEquals("name1", names_it.next());
510: assertEquals("name2", names_it.next());
511: assertEquals("new_name2", names_it.next());
512: assertEquals("name2_grandparent", names_it.next());
513: assertFalse(names_it.hasNext());
514:
515: names_it = properties_child3.getNames().iterator();
516: assertEquals("non.identifier.name3_parent", names_it.next());
517: assertEquals("name1", names_it.next());
518: assertEquals("name2_grandparent", names_it.next());
519: assertEquals("non.identifier.name3", names_it.next());
520: assertEquals("new_non.identifier.name3", names_it.next());
521: assertFalse(names_it.hasNext());
522: names_it = properties_child3.getInjectableNames().iterator();
523: assertEquals("name1", names_it.next());
524: assertEquals("name2_grandparent", names_it.next());
525: assertFalse(names_it.hasNext());
526: }
527:
528: public void testPutAllMap() {
529: HierarchicalProperties properties = new HierarchicalProperties();
530: assertEquals(0, properties.size());
531:
532: Map map = new LinkedHashMap();
533: Object value1 = new StringBuffer("test");
534: Object value2 = new Date(106, 7, 8);
535: map.put(24, value1);
536: map.put("test", value2);
537: properties.putAll(map);
538:
539: assertEquals(2, properties.size());
540: Iterator names_it = properties.getNames().iterator();
541: assertEquals("24", names_it.next());
542: assertEquals("test", names_it.next());
543: assertSame(value1, properties.get("24").getValue());
544: assertSame(value2, properties.get("test").getValue());
545: }
546:
547: public void testGetValueString() {
548: HierarchicalProperties properties = new HierarchicalProperties();
549: Object value1 = new StringBuffer("test");
550: Object value2 = new BigDecimal("12682861E+10");
551: properties.put("value1", value1);
552: properties.put("value2", value2);
553: properties.put("value3", null);
554: properties.put("value4", "");
555: properties.put("value5", new PropertyValueParticipant(
556: "inexistent_participant",
557: new PropertyValueObject("key")));
558:
559: assertEquals("test", properties.getValueString("value1"));
560: assertEquals("1.2682861E+17", properties
561: .getValueString("value2"));
562: assertNull(properties.getValueString("value3"));
563: assertNull(properties.getValueString("value4"));
564: assertNull(properties.getValueString("inexistent"));
565: try {
566: properties.getValueString("value5");
567: fail("Expected exception");
568: } catch (PropertyValueException e) {
569: assertTrue(e instanceof ParticipantUnknownException);
570: assertEquals("inexistent_participant",
571: ((ParticipantUnknownException) e).getName());
572: }
573:
574: assertEquals("test", properties.getValueString("value1",
575: "default1"));
576: assertEquals("1.2682861E+17", properties.getValueString(
577: "value2", "default2"));
578: assertEquals("default3", properties.getValueString("value3",
579: "default3"));
580: assertEquals("default4", properties.getValueString("value4",
581: "default4"));
582: assertEquals("default5", properties.getValueString(
583: "inexistent", "default5"));
584: try {
585: properties.getValueString("value5", "default5");
586: fail("Expected exception");
587: } catch (PropertyValueException e) {
588: assertTrue(e instanceof ParticipantUnknownException);
589: assertEquals("inexistent_participant",
590: ((ParticipantUnknownException) e).getName());
591: }
592: }
593:
594: public void testGetValueTyped() {
595: HierarchicalProperties properties = new HierarchicalProperties();
596: Object value1 = new StringBuffer("test");
597: Object value2 = new BigDecimal("12682861E+10");
598: Object value4 = "";
599: properties.put("value1", value1);
600: properties.put("value2", value2);
601: properties.put("value3", null);
602: properties.put("value4", value4);
603: properties.put("value5", new PropertyValueParticipant(
604: "inexistent_participant",
605: new PropertyValueObject("key")));
606:
607: assertSame(value1, properties.getValueTyped("value1",
608: StringBuffer.class));
609: assertSame(value2, properties.getValueTyped("value2",
610: BigDecimal.class));
611: assertNull(properties.getValueTyped("value3", String.class));
612: assertSame(value4, properties.getValueTyped("value4",
613: String.class));
614: assertNull(properties.getValueTyped("inexistent", String.class));
615: try {
616: properties.getValueTyped("value5", String.class);
617: fail("Expected exception");
618: } catch (PropertyValueException e) {
619: assertTrue(e instanceof ParticipantUnknownException);
620: assertEquals("inexistent_participant",
621: ((ParticipantUnknownException) e).getName());
622: }
623:
624: BigDecimal default3 = new BigDecimal("5718620E+6");
625: Integer default5 = 97586;
626: assertSame(value1, properties.getValueTyped("value1",
627: StringBuffer.class, new StringBuffer("default1")));
628: assertSame(value2, properties.getValueTyped("value2",
629: BigDecimal.class, new BigDecimal(1268)));
630: assertSame(default3, properties.getValueTyped("value3",
631: BigDecimal.class, default3));
632: assertSame(value4, properties.getValueTyped("value4",
633: String.class, "default4"));
634: assertSame(default5, properties.getValueTyped("inexistent",
635: Integer.class, default5));
636: try {
637: properties
638: .getValueTyped("value5", String.class, "default5");
639: fail("Expected exception");
640: } catch (PropertyValueException e) {
641: assertTrue(e instanceof ParticipantUnknownException);
642: assertEquals("inexistent_participant",
643: ((ParticipantUnknownException) e).getName());
644: }
645:
646: try {
647: properties.getValueTyped("value2", Date.class);
648: fail("Expected exception");
649: } catch (PropertyValueException e) {
650: assertTrue(e instanceof IncompatiblePropertyValueTypeException);
651: assertEquals("value2",
652: ((IncompatiblePropertyValueTypeException) e)
653: .getPropertyName());
654: assertSame(Date.class,
655: ((IncompatiblePropertyValueTypeException) e)
656: .getExpectedType());
657: assertSame(BigDecimal.class,
658: ((IncompatiblePropertyValueTypeException) e)
659: .getActualType());
660: }
661: }
662: }
|