KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > arooa > PropertyExpansionTest


1 package org.oddjob.arooa;
2
3 import junit.framework.TestCase;
4
5 import org.oddjob.arooa.reflect.DefaultRegistryLookup;
6 import org.oddjob.arooa.reflect.RegistryLookup;
7 import org.oddjob.arooa.registry.ComponentRegistry;
8
9 /**
10  * class to look at how we expand properties
11  */

12 public class PropertyExpansionTest extends TestCase {
13
14     ComponentRegistry cr = new ComponentRegistry();
15     RegistryLookup props = new DefaultRegistryLookup(cr);
16
17     /**
18      * run through the test cases of expansion
19      */

20     public void testPropertyExpansion() {
21         assertExpandsTo("","");
22         assertExpandsTo("$","$");
23         assertExpandsTo("$$-","$-");
24         assertExpandsTo("$$","$");
25         cr.register("expanded","EXPANDED");
26         assertExpandsTo("a${expanded}b","aEXPANDEDb");
27         assertExpandsTo("${expanded}${expanded}","EXPANDEDEXPANDED");
28         assertExpandsTo("$$$","$$");
29         assertExpandsTo("$$$$-","$$-");
30         assertExpandsTo("","");
31         assertExpandsTo("Class$$subclass","Class$subclass");
32     }
33     
34     /**
35      * new things we want
36      */

37     public void testDollarPassthru() {
38         assertExpandsTo("$-","$-");
39         assertExpandsTo("Class$subclass","Class$subclass");
40         assertExpandsTo("$$$-","$$-");
41         assertExpandsTo("$$$$$","$$$");
42         assertExpandsTo("a${unassigned-property}","a");
43         assertExpandsTo("a$b","a$b");
44         assertExpandsTo("$}}","$}}");
45     }
46
47     /**
48      * Object replacement
49      */

50     public void testObjectReplacement() {
51         cr.register("an-int",new Integer JavaDoc(2));
52         assertExpandsTo("${an-int}", new Integer JavaDoc(2));
53         assertExpandsTo("${unassigned}", null);
54         assertExpandsTo("${unassigned.property}", null);
55     }
56     
57     public void testProxyExpansion() {
58         cr.register("fruit", new ProxyType());
59         assertExpandsTo("${fruit}s and pairs", "apples and pairs");
60     }
61     
62     /**
63      * old things we dont want; not a test no more
64      */

65     public void oldtestQuirkyLegacyBehavior() {
66         assertExpandsTo("Class$subclass","Classsubclass");
67         assertExpandsTo("$$$-","$-");
68         assertExpandsTo("a$b","ab");
69         assertExpandsTo("$}}","}}");
70     }
71
72     /**
73      * Test isConstant method.
74      *
75      */

76     public void testisConstant() {
77         
78         PropertyHelper ph;
79         ph = new PropertyHelper("abc");
80         assertTrue(ph.isConstant());
81         ph = new PropertyHelper("${abc}");
82         assertFalse(ph.isConstant());
83     }
84
85     /**
86      * Test nested exapansion
87      *
88      */

89     public void testNestedExpansion() {
90         
91         NestedProp foo = new NestedProp();
92         cr.register("foo", foo);
93         assertExpandsTo("${foo.ba}", "Ba");
94         try {
95             assertExpandsTo("${foo.Ba}", null);
96             fail("Ba doesn't exist");
97         } catch (ArooaException expected) { }
98         assertExpandsTo("${foo.ba2.ba}", "Ba");
99     }
100     
101     /**
102      * little helper method to validate stuff
103      */

104     private void assertExpandsTo(String JavaDoc source, Object JavaDoc expected) {
105         PropertyHelper ph = new PropertyHelper(source);
106         Object JavaDoc actual=ph.replaceProperties(props, false);
107         assertEquals(source,expected,actual);
108     }
109
110     public class NestedProp {
111
112         public String JavaDoc getBa() {
113             return "Ba";
114         }
115         public NestedProp getBa2() {
116             return new NestedProp();
117         }
118     }
119     
120     public static class ProxyType {
121         public Object JavaDoc valueFor(Class JavaDoc requested) {
122             return "apple";
123         }
124     }
125     
126 //end class
127
}
128
Popular Tags