1 4 package org.oddjob.arooa.handlers; 5 6 import junit.framework.TestCase; 7 8 import org.oddjob.arooa.ArooaConstants; 9 import org.oddjob.arooa.ArooaContext; 10 import org.oddjob.arooa.ArooaHandler; 11 import org.oddjob.arooa.ArooaRuntime; 12 import org.oddjob.arooa.ComponentProxyResolver; 13 import org.oddjob.arooa.RuntimeConfiguration; 14 import org.oddjob.arooa.SimpleObjectFactory; 15 import org.oddjob.arooa.reflect.IntrospectionHelper; 16 import org.oddjob.arooa.registry.ComponentRegistry; 17 import org.oddjob.arooa.registry.Path; 18 import org.xml.sax.SAXException ; 19 import org.xml.sax.SAXParseException ; 20 import org.xml.sax.helpers.AttributesImpl ; 21 import org.xml.sax.helpers.LocatorImpl ; 22 23 27 public class DefaultComponentHandlerTest extends TestCase { 28 29 ArooaContext context1 = new ArooaContext(); 30 final SimpleObjectFactory factory = new SimpleObjectFactory(); 31 32 public void setUp() { 33 factory.set("dummy", DummyComponent.class.getName()); 34 context1 = new ArooaContext(); 35 context1.set(ArooaConstants.COMPONENT_REGISTRY, new ComponentRegistry()); 36 LocatorImpl loc = new LocatorImpl (); 37 loc.setLineNumber(0); 38 loc.setColumnNumber(0); 39 loc.setSystemId("test"); 40 context1.setLocator(loc); 41 context1.set(ArooaConstants.COMPONENT_FACTORY, factory); 42 context1.set(ArooaConstants.COMPONENT_HANDLER, new DefaultComponentHandler()); 43 context1 = new ArooaContext(context1); 44 45 } 46 47 public void testClassCreate() throws SAXException { 49 context1.set(ArooaConstants.COMPONENT_FACTORY, null); 50 ArooaContext context2 = new ArooaContext(context1); 51 52 AttributesImpl atts = new AttributesImpl (); 53 atts.addAttribute("", "class", "class", "java.lang.String", DummyComponent.class.getName()); 54 atts.addAttribute("", "foo", "foo", "java.lang.String", "ba"); 55 56 DefaultComponentHandler h = new DefaultComponentHandler(); 57 h.onStartElement("", "ignored", "ignored", atts, context2); 58 59 RuntimeConfiguration rtc = (RuntimeConfiguration) context2.get(ArooaConstants.CURRENTLY_CONFIGURING); 60 rtc.configure(); 61 DummyComponent result = (DummyComponent) rtc.getWrappedObject(); 62 assertEquals("ba", result.foo); 63 } 64 65 public void testFactoryCreate() throws SAXException { 67 ArooaContext context2 = new ArooaContext(context1); 68 69 AttributesImpl atts = new AttributesImpl (); 70 atts.addAttribute("", "foo", "foo", "java.lang.String", "ba"); 71 72 DefaultComponentHandler h = new DefaultComponentHandler(); 73 h.onStartElement("", "dummy", "dummy", atts, context2); 74 75 RuntimeConfiguration rtc = (RuntimeConfiguration) context2.get(ArooaConstants.CURRENTLY_CONFIGURING); 76 rtc.configure(); 77 DummyComponent result = (DummyComponent) rtc.getWrappedObject(); 78 assertEquals("ba", result.foo); 79 } 80 81 86 public void testAddComponent() 87 throws SAXParseException { 88 90 HasComponent test = new HasComponent(); 91 context1.set(ArooaConstants.CURRENTLY_CONFIGURING, 92 new ArooaRuntime(test, "test")); 93 94 IntrospectionHelper ih = IntrospectionHelper.getHelper(test.getClass()); 95 ArooaHandler handler = ih.provideHandler(test, "dummy", context1); 96 97 assertEquals("", context1.getLocal(ArooaConstants.ELEMENT_NAME)); 98 99 ArooaContext context2 = new ArooaContext(context1); 100 AttributesImpl atts = new AttributesImpl (); 101 atts.addAttribute("", "foo", "foo", "java.lang.String", "Hello World"); 102 103 handler.onStartElement("", "dummy", "dummy", atts, context2); 105 assertTrue(context2.getLocal(ArooaConstants.CURRENTLY_CONFIGURING) instanceof RuntimeConfiguration); 106 107 handler.onEndElement("", "dummy", context2); 109 110 assertNotNull(test.dummy); 111 test.dummy.getRuntimeConfigurable().configure(); 112 assertTrue("Component not equal to that saved", test.dummy.getFoo().equals("Hello World")); 113 } 114 115 public static class HasComponent { 116 DummyComponent dummy; 117 public void addComponent(DummyComponent dummy) { 118 this.dummy = dummy; 119 } 120 } 121 122 127 public void testAddComponentChild() 134 throws SAXParseException { 135 137 HasComponentChild test = new HasComponentChild(); 138 IntrospectionHelper ih = IntrospectionHelper.getHelper(test.getClass()); 139 context1.set(ArooaConstants.CURRENTLY_CONFIGURING, 140 new ArooaRuntime(test, "test")); 141 142 ArooaHandler handler = ih.provideHandler(test, "child", context1); 143 assertEquals("child", context1.get(ArooaConstants.ELEMENT_NAME)); 144 assertTrue(handler instanceof SkipLevelHandler); 145 146 ArooaContext context2 = new ArooaContext(context1); 147 AttributesImpl atts = new AttributesImpl (); 148 handler.onStartElement("", "child", "child", atts, context2); 150 151 152 AttributesImpl innerAtts = new AttributesImpl (); 153 innerAtts.addAttribute("", "foo", "foo", "java.lang.String", "Hello World"); 154 155 ArooaHandler innerHandler = handler.onStartChild( 156 "", "dummy", "dummy", innerAtts, context2); 157 assertTrue(innerHandler instanceof DefaultComponentHandler); 158 159 ArooaContext context3 = new ArooaContext(context2); 160 innerHandler.onStartElement("", "dummy", "dummy", innerAtts, context3); 162 assertTrue(context3.getLocal(ArooaConstants.CURRENTLY_CONFIGURING) instanceof RuntimeConfiguration); 163 164 innerHandler.onEndElement("", "dummy", context3); 165 assertEquals(context2, context3.getParent()); 166 167 handler.onEndElement("", "child", context2); 169 170 assertNotNull(test.dummy); 171 test.dummy.getRuntimeConfigurable().configure(); 172 assertTrue("Component not equal to that saved", test.dummy.getFoo().equals("Hello World")); 173 } 174 175 public static class HasComponentChild { 176 DummyComponent dummy; 177 178 public void addComponentChild(DummyComponent dummy) { 179 this.dummy = dummy; 180 } 181 } 182 183 public static class Component { 184 185 } 186 187 public static class Proxy { 188 189 } 190 191 public static class Resolver implements ComponentProxyResolver { 192 public Object proxyFor(Object component) { 193 assertTrue(component instanceof Component); 194 return new Proxy(); 195 } 196 } 197 198 public void testComponentProxyReolver() throws Exception { 199 ComponentRegistry cr = new ComponentRegistry(); 200 201 ArooaContext ac = new ArooaContext(); 202 ac.set(ArooaConstants.COMPONENT_PROXY_RESOLVER, new Resolver()); 203 ac.set(ArooaConstants.COMPONENT_REGISTRY, cr); 204 205 AttributesImpl attrs = new AttributesImpl (); 206 attrs.addAttribute("", "class", "class", "java.lang.String", Component.class.getName()); 207 attrs.addAttribute("", "id", "id", "java.lang.String", "x"); 208 209 DefaultComponentHandler h = new DefaultComponentHandler(); 210 h.onStartElement("", "x", "x", attrs, ac); 211 212 assertEquals(Proxy.class, 213 ac.get(DefaultComponentHandler.CURRENT_COMPONENT).getClass()); 214 215 assertEquals(Proxy.class, 216 cr.objectForPath(new Path("x")).getClass()); 217 } 218 219 } 220 221 | Popular Tags |