1 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 ; 12 import org.xml.sax.helpers.AttributesImpl ; 13 14 import junit.framework.TestCase; 15 16 19 public class DesignComponentHandlerTest extends TestCase { 20 21 class MockOF implements ObjectFactory { 22 public Object createObject(String name) throws ArooaException { 23 if ("test".equals(name)) { 24 return new String ("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 u; 35 public void unknown(String u) { 36 this.u = u; 37 } 38 public String toString() { 39 return "OTHER"; 40 } 41 public void setTag(String tag) {} 42 public void setName(String name) {} 43 } 44 45 public static class Test1 { 46 Object t2; 47 public void addComponentTest(Object t2) { 48 this.t2 = t2; 49 } 50 } 51 52 public static class Test2 { 53 String what; 54 public void setWhat(String what) { 55 this.what = what; 56 } 57 } 58 59 public void testCreateComponet() throws SAXParseException { 60 DesignComponentHandler h = new DesignComponentHandler(); 61 AttributesImpl attrs = new AttributesImpl (); 62 ArooaContext context = new ArooaContext(); 63 context.set(ArooaConstants.COMPONENT_FACTORY, new MockOF()); 64 65 h.onStartElement("", "test", "test", attrs, context); 66 67 Object 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 { 73 DesignComponentHandler h = new DesignComponentHandler(); 74 AttributesImpl attrs = new AttributesImpl (); 75 ArooaContext context = new ArooaContext(); 76 context.set(ArooaConstants.COMPONENT_FACTORY, new MockOF()); 77 78 h.onStartElement("", "whatever", "whatever", attrs, context); 79 80 Object component = context.get(ArooaConstants.CURRENTLY_CONFIGURING); 81 assertEquals("component", "OTHER", component.toString()); 82 } 84 85 public void testStartChild() throws SAXParseException { 86 DesignComponentHandler h = new DesignComponentHandler(); 87 AttributesImpl attrs = new AttributesImpl (); 88 ArooaContext context = new ArooaContext(); 89 context.set(ArooaConstants.COMPONENT_FACTORY, new MockOF()); 90 context.set(ArooaConstants.CURRENTLY_CONFIGURING, new Object ()); 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 { 98 DesignComponentHandler h = new DesignComponentHandler(); 99 AttributesImpl attrs = new AttributesImpl (); 100 ArooaContext context = new ArooaContext(); 101 context.set(ArooaConstants.COMPONENT_FACTORY, new MockOF()); 102 context.set(ArooaConstants.CURRENTLY_CONFIGURING, new Object ()); 103 104 DualHandler dh = (DualHandler) h.onStartChild("", "test", "test", attrs, context); 105 assertNull("unknown", context.get("unknown")); 106 107 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 { 119 DesignComponentHandler h = new DesignComponentHandler(); 120 AttributesImpl attrs = new AttributesImpl (); 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 |