KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > arooa > handlers > DefaultComponentHandlerTest


1 /*
2  * Copyright (c) 2004, Rob Gordon.
3  */

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 JavaDoc;
19 import org.xml.sax.SAXParseException JavaDoc;
20 import org.xml.sax.helpers.AttributesImpl JavaDoc;
21 import org.xml.sax.helpers.LocatorImpl JavaDoc;
22
23 /**
24  *
25  * @author Rob Gordon.
26  */

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 JavaDoc loc = new LocatorImpl JavaDoc();
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     // test component created from class attribute.
48
public void testClassCreate() throws SAXException JavaDoc {
49         context1.set(ArooaConstants.COMPONENT_FACTORY, null);
50         ArooaContext context2 = new ArooaContext(context1);
51         
52         AttributesImpl JavaDoc atts = new AttributesImpl JavaDoc();
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     // test component crated from factory.
66
public void testFactoryCreate() throws SAXException JavaDoc {
67         ArooaContext context2 = new ArooaContext(context1);
68         
69         AttributesImpl JavaDoc atts = new AttributesImpl JavaDoc();
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     /**
82      * Test the handler creates a component by
83      * calling the
84      * handler methods with appropriate values.
85      */

86     public void testAddComponent()
87     throws SAXParseException JavaDoc {
88         // create a handler
89

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 JavaDoc atts = new AttributesImpl JavaDoc();
101         atts.addAttribute("", "foo", "foo", "java.lang.String", "Hello World");
102         
103         // start
104
handler.onStartElement("", "dummy", "dummy", atts, context2);
105         assertTrue(context2.getLocal(ArooaConstants.CURRENTLY_CONFIGURING) instanceof RuntimeConfiguration);
106         
107         // and end
108
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     /**
123      * Test the handler creates a component by
124      * calling the
125      * handler methods with appropriate values.
126      */

127     // this simulates
128
// <hasComponentChild>
129
// <child>
130
// <dummy foo="Hello World"/>
131
// </child>
132
// </hasComponentChild>
133
public void testAddComponentChild()
134     throws SAXParseException JavaDoc {
135         // create a handler
136

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 JavaDoc atts = new AttributesImpl JavaDoc();
148         // <child>
149
handler.onStartElement("", "child", "child", atts, context2);
150                 
151
152         AttributesImpl JavaDoc innerAtts = new AttributesImpl JavaDoc();
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         // <dummy>
161
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         // end
168
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 JavaDoc proxyFor(Object JavaDoc component) {
193             assertTrue(component instanceof Component);
194             return new Proxy();
195         }
196     }
197     
198     public void testComponentProxyReolver() throws Exception JavaDoc {
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 JavaDoc attrs = new AttributesImpl JavaDoc();
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