1 package org.oddjob.arooa; 2 3 import java.util.HashMap ; 4 import java.util.Map ; 5 6 import junit.framework.TestCase; 7 8 import org.oddjob.arooa.reflect.DefaultRegistryLookup; 9 import org.oddjob.arooa.reflect.RegistryLookup; 10 import org.oddjob.arooa.registry.ComponentRegistry; 11 import org.oddjob.jobs.job.ResetJob; 12 import org.oddjob.jobs.job.StopJob; 13 14 18 public class ArooaRuntimeTest extends TestCase { 19 20 ComponentRegistry cr; 21 RegistryLookup props; 22 Parent parent; 23 ArooaRuntime root; 24 Nested nested; 25 ArooaRuntime child; 26 27 public void setUp() { 28 cr = new ComponentRegistry(); 29 props = new DefaultRegistryLookup(cr); 30 parent = new Parent(); 31 root = new ArooaRuntime(parent, "root"); 32 root.setAttribute("foo", "${foo.value}"); 33 nested = new Nested(); 34 child = new ArooaRuntime(nested, "whatever"); 35 child.setAttribute("a", "${greeting.value}"); 36 } 37 38 public static class Component { 39 private Object value; 40 public Object getValue() { 41 return value; 42 } 43 public void setValue(Object value) { 44 this.value = value; 45 } 46 } 47 48 52 public void testAttributeSetter1() { 53 Component c1 = new Component(); 54 c1.setValue("Hello"); 55 56 cr.register("x", c1); 57 58 ArooaContext ac = new ArooaContext(); 59 ac.set(ArooaConstants.COMPONENT_REGISTRY, cr); 60 61 Component c2 = new Component(); 62 ArooaRuntime test = new ArooaRuntime(c2, "comp", ac); 63 64 test.setAttribute("value", "${x.value}"); 65 test.configure(); 66 67 assertEquals("Hello", c2.getValue()); 68 69 c1.setValue("Goodbye"); 70 71 test.configure(); 72 73 assertEquals("Goodbye", c2.getValue()); 74 } 75 76 80 public void testAttributeSetter2() { 81 Component c = new Component(); 82 cr.register("foo", c); 83 c.setValue("Hello World"); 84 root.configure(props, false); 85 assertEquals("Hello World", parent.getFoo()); 86 87 c.setValue("Goodbye"); 88 root.configure(props, false); 89 assertEquals("Goodbye", parent.getFoo()); 90 } 91 92 public void testProxyTypeAttributeSetter() { 93 Component c = new Component(); 94 cr.register("foo", c); 95 96 c.setValue(new ProxyType()); 97 root.configure(props, false); 98 assertEquals("apple", parent.getFoo()); 99 } 100 101 public void testMixedProxyTypeAttributeSetter() { 102 root.setAttribute("foo", "${foo}s and pairs"); 103 cr.register("foo", new ProxyType()); 104 root.configure(props, false); 105 assertEquals("apples and pairs", parent.getFoo()); 106 } 107 108 public void testAddNestedElement() { 109 Component c = new Component(); 110 cr.register("greeting", c); 111 cr.register("foo", c); 112 113 root.addChild(child); 114 root.configure(props, false); 115 assertNotNull("parent.n", parent.n); 116 } 117 118 public void testAttributesOnNestedElement() { 119 Component c = new Component(); 120 cr.register("greeting", c); 121 cr.register("foo", c); 122 123 root.addChild(child); 124 125 c.setValue("Hello"); 126 root.configure(props, false); 127 assertEquals("parent.n.a", "Hello", parent.n.a); 128 } 129 130 public void testAttributeSetterTwice() { 131 Component c = new Component(); 132 cr.register("foo", c); 133 134 c.value = "Hello World"; 135 root.configure(props, false); 136 c.value = "Goodbye World"; 137 root.configure(props, false); 138 139 assertEquals("Goodbye World", parent.getFoo()); 140 } 141 142 public void testAttributesOnNestedElementTwice() { 143 Component c = new Component(); 144 cr.register("greeting", c); 145 cr.register("foo", c); 146 147 root.addChild(child); 148 149 c.setValue("Hello"); 150 root.configure(props, false); 151 152 c.setValue("Goodbye"); 153 root.configure(props, false); 154 155 assertEquals("parent.n.a", "Goodbye", parent.n.a); 156 } 157 158 public void testNullStrict() { 160 ArooaContext c = new ArooaContext(); 161 c.set(ArooaConstants.SUBSTITUTION_POLICY, ArooaConstants.SUBPOLICY_STRICT); 162 root = new ArooaRuntime(parent, null, c); 163 root.setAttribute("foo", "${dontexist}"); 164 try { 165 root.configure(); 166 fail("Exception should be thrown."); 167 } catch (NullPointerException e) { 168 } 170 } 171 172 public void testNullNotStrict() { 174 root.setAttribute("foo", "${dontexist}"); 175 root.configure(new DefaultRegistryLookup( 176 new ComponentRegistry()), false); 177 assertNull(parent.getFoo()); 178 } 179 180 public void testRealJob() { 181 ArooaContext c = new ArooaContext(); 182 ComponentRegistry cr = new ComponentRegistry(); 183 c.set(ArooaConstants.COMPONENT_REGISTRY, cr); 184 cr.register("foo", new StopJob()); 185 ArooaRuntime rtc = new ArooaRuntime(new ResetJob(), "whatever", c); 186 rtc.setAttribute("job", "${foo}"); 187 rtc.configure(); 188 assertNotNull(rtc.getWrappedObject()); 189 } 190 191 public void testMappedConstant() { 192 Component c = new Component(); 193 cr.register("foo", c); 194 195 RuntimeConfiguration rtc = new ArooaRuntime(new String ("hello"), "string"); 196 root.setMappedProperty("fred", "greeting", rtc); 197 root.configure(new DefaultRegistryLookup( 198 cr), false); 199 assertEquals("hello", parent.map.get("greeting")); 200 } 201 202 public void testMappedMap() { 203 Component c = new Component(); 204 cr.register("foo", c); 205 206 Map m = new HashMap (); 207 m.put("greeting", "hello"); 208 m.put("food", "chips"); 209 210 RuntimeConfiguration rtc = new ArooaRuntime(m, "string"); 211 root.setMappedProperty("fred", null, rtc); 212 root.configure(new DefaultRegistryLookup( 213 cr), false); 214 assertEquals("hello", parent.map.get("greeting")); 215 assertEquals("chips", parent.map.get("food")); 216 } 217 218 public static class Parent { 219 String foo; 220 Nested n; 221 Map map = new HashMap (); 222 223 public void setFoo(String foo) { 224 this.foo = foo; 225 } 226 public String getFoo() { 227 return foo; 228 } 229 public void addConfiguredWhatever(Nested n) { 230 if (this.n != null) { 231 throw new IllegalStateException ("Shouldn't happen"); 232 } 233 this.n = n; 234 } 235 236 public void setFred(String name, String greeting) { 237 map.put(name, greeting); 238 } 239 } 240 241 public static class Nested { 242 String a; 243 public void setA(String a) { 244 this.a = a; 245 } 246 } 247 248 public static class ProxyType { 249 public Object valueFor(Class required) { 250 return "apple"; 251 } 252 } 253 } 254 | Popular Tags |