KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > designer > arooa > DesignComponentHandlerTest


1 /*
2  * (c) Rob Gordon 2005.
3  */

4 package org.oddjob.designer.arooa;
5
6 import org.oddjob.arooa.ArooaException;
7 import org.oddjob.arooa.ArooaHandler;
8 import org.oddjob.arooa.ArooaContext;
9 import org.oddjob.arooa.ArooaConstants;
10 import org.oddjob.arooa.ObjectFactory;
11 import org.xml.sax.SAXParseException JavaDoc;
12 import org.xml.sax.helpers.AttributesImpl JavaDoc;
13
14 import junit.framework.TestCase;
15
16 /**
17  *
18  */

19 public class DesignComponentHandlerTest extends TestCase {
20
21     class MockOF implements ObjectFactory {
22         public Object JavaDoc createObject(String JavaDoc name) throws ArooaException {
23             if ("test".equals(name)) {
24                 return new String JavaDoc("TEST");
25             }
26             else if ("other".equals(name)) {
27                 return new Unknown();
28             }
29             else return null;
30         }
31     }
32     
33     public static class Unknown {
34         String JavaDoc u;
35         public void unknown(String JavaDoc u) {
36             this.u = u;
37         }
38         public String JavaDoc toString() {
39             return "OTHER";
40         }
41         public void setTag(String JavaDoc tag) {}
42         public void setName(String JavaDoc name) {}
43     }
44
45     public static class Test1 {
46         Object JavaDoc t2;
47         public void addComponentTest(Object JavaDoc t2) {
48             this.t2 = t2;
49         }
50     }
51     
52     public static class Test2 {
53         String JavaDoc what;
54         public void setWhat(String JavaDoc what) {
55             this.what = what;
56         }
57     }
58     
59     public void testCreateComponet() throws SAXParseException JavaDoc {
60         DesignComponentHandler h = new DesignComponentHandler();
61         AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
62         ArooaContext context = new ArooaContext();
63         context.set(ArooaConstants.COMPONENT_FACTORY, new MockOF());
64
65         h.onStartElement("", "test", "test", attrs, context);
66
67         Object JavaDoc component = context.get(ArooaConstants.CURRENTLY_CONFIGURING);
68         assertEquals("component", "TEST", component.toString());
69         assertNull("unknown", context.get("unknown"));
70     }
71
72     public void testCreateOther() throws SAXParseException JavaDoc {
73         DesignComponentHandler h = new DesignComponentHandler();
74         AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
75         ArooaContext context = new ArooaContext();
76         context.set(ArooaConstants.COMPONENT_FACTORY, new MockOF());
77         
78         h.onStartElement("", "whatever", "whatever", attrs, context);
79
80         Object JavaDoc component = context.get(ArooaConstants.CURRENTLY_CONFIGURING);
81         assertEquals("component", "OTHER", component.toString());
82 // assertNotNull("unknown", context.get("unknown"));
83
}
84     
85     public void testStartChild() throws SAXParseException JavaDoc {
86         DesignComponentHandler h = new DesignComponentHandler();
87         AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
88         ArooaContext context = new ArooaContext();
89         context.set(ArooaConstants.COMPONENT_FACTORY, new MockOF());
90         context.set(ArooaConstants.CURRENTLY_CONFIGURING, new Object JavaDoc());
91         ArooaHandler ah = h.onStartChild("", "whatever", "whatever", attrs, context);
92
93         assertTrue("DualHandler", DualHandler.class.equals(ah.getClass()));
94         assertNull("unknown", context.get("unknown"));
95     }
96     
97     public void testUnknownEndChild() throws SAXParseException JavaDoc {
98         DesignComponentHandler h = new DesignComponentHandler();
99         AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
100         ArooaContext context = new ArooaContext();
101         context.set(ArooaConstants.COMPONENT_FACTORY, new MockOF());
102         context.set(ArooaConstants.CURRENTLY_CONFIGURING, new Object JavaDoc());
103         
104         DualHandler dh = (DualHandler) h.onStartChild("", "test", "test", attrs, context);
105         assertNull("unknown", context.get("unknown"));
106
107         // force handler to fail
108
dh.onStartElement("", "rubish", "rubish", attrs, new ArooaContext(context));
109
110         assertTrue("xml hanlder", dh.isHandler1OK());
111         assertFalse("other handler", dh.isHandler2OK());
112
113         h.onEndChild("", "whatever", "whatever", context);
114         
115         assertNotNull("unknown", context.get("unknown"));
116     }
117
118     public void testUnknownEndElement() throws SAXParseException JavaDoc {
119         DesignComponentHandler h = new DesignComponentHandler();
120         AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
121         ArooaContext context1 = new ArooaContext();
122         Test1 t1 = new Test1();
123         context1.set(ArooaConstants.COMPONENT_FACTORY, new MockOF());
124         context1.set(ArooaConstants.CURRENTLY_CONFIGURING, t1);
125         context1.set(ArooaConstants.ELEMENT_NAME, "test");
126         
127         ArooaContext context2 = new ArooaContext(context1);
128         context2.set(ArooaConstants.CURRENTLY_CONFIGURING, new Test2());
129         context2.set("unknown", "true");
130
131         h.onEndElement("", "test", context2);
132         
133         assertTrue("unknow comp", t1.t2 instanceof Unknown);
134         assertNotNull("unknown", context2.get("unknown"));
135     }
136 }
137
Popular Tags