1 4 package org.oddjob.designer.components; 5 6 import java.io.ByteArrayOutputStream ; 7 8 import junit.framework.TestCase; 9 10 import org.apache.commons.beanutils.BeanUtils; 11 import org.oddjob.arooa.ArooaHandler; 12 import org.oddjob.arooa.ArooaRuntime; 13 import org.oddjob.arooa.ArooaContext; 14 import org.oddjob.arooa.ArooaConstants; 15 import org.oddjob.arooa.PropertyProxyResolver; 16 import org.oddjob.arooa.SimpleObjectFactory; 17 import org.oddjob.arooa.handlers.DefaultComponentHandler; 18 import org.oddjob.arooa.handlers.MainHandler; 19 import org.oddjob.arooa.handlers.MappedPropertyHandler; 20 import org.oddjob.arooa.handlers.NestedElementHandler; 21 import org.oddjob.arooa.handlers.NestedPropertyHandler; 22 import org.oddjob.arooa.handlers.XmlHandler; 23 import org.oddjob.arooa.reflect.IntrospectionHelper; 24 import org.oddjob.designer.DesignerXMLParser; 25 import org.oddjob.designer.arooa.DesignParser; 26 import org.oddjob.designer.elements.ScheduleDE; 27 import org.oddjob.designer.elements.date.DayAttribute; 28 import org.oddjob.designer.elements.date.TimeAttribute; 29 import org.oddjob.designer.elements.scheduling.OJScheduleDE; 30 import org.oddjob.designer.elements.scheduling.ScheduleListDE; 31 import org.oddjob.designer.model.DesignElementType; 32 33 36 public class ScheduleDCTest extends TestCase { 37 38 public class DummyRoot { 39 ScheduleDC sdc; 40 UnknownDC uk; 41 Object other; 42 public void addComponent(Object o) { 43 if (o instanceof ScheduleDC) { 44 sdc = (ScheduleDC) o; 45 } else if (o instanceof UnknownDC) { 46 uk = (UnknownDC) uk; 47 } else { 48 other = o; 49 } 50 51 } 52 } 53 54 public void testCreate() { 55 56 String xml = "<schedule name=\"A Schedule\">" + 57 "<schedules>" + 58 "<ojschedule>" + 59 "<schedule>" + 60 "<dayofweek on=\"SUN\">" + 61 "<time from=\"12:00\" to=\"16:00\"/>" + 62 "</dayofweek>" + 63 "<dayofweek on=\"WED\">" + 64 "<time from=\"12:00\" to=\"16:00\"/>" + 65 "</dayofweek>" + 66 "</schedule></ojschedule>" + 67 "</schedules></schedule>"; 68 69 Object dc = DesignerXMLParser.buildComponent(xml); 70 71 assertNotNull("created", dc); 72 } 74 75 public void testParse() throws Exception { 76 TimeAttribute t1 = new TimeAttribute(); 77 t1.attribute("12:00"); 78 TimeAttribute t2 = new TimeAttribute(); 79 t1.attribute("13:00"); 80 81 DayAttribute d1= new DayAttribute(); 82 d1.attribute("MON"); 83 DayAttribute d2= new DayAttribute(); 84 d1.attribute("FRI"); 85 86 ScheduleDE sde = new ScheduleDE(); 87 sde.type("schedule"); 88 89 DesignElementType tm = sde.createType("time"); 90 IntrospectionHelper ih = IntrospectionHelper.getHelper(tm.getClass()); 91 BeanUtils.setProperty(tm, "from", t1); 92 BeanUtils.setProperty(tm, "to", t2); 93 94 DesignElementType dow1 = sde.createType("dayofweek"); 95 IntrospectionHelper ih1 = IntrospectionHelper.getHelper(dow1.getClass()); 96 BeanUtils.setProperty(dow1, "on", d1); 97 98 DesignElementType dow2 = sde.createType("dayofweek"); 99 IntrospectionHelper ih2 = IntrospectionHelper.getHelper(dow2.getClass()); 100 BeanUtils.setProperty(dow2, "on", d2); 101 102 dow1.addChild(tm); 103 dow2.addChild(tm); 104 sde.addChild(dow1); 105 sde.addChild(dow2); 106 107 108 OJScheduleDE ojs = new OJScheduleDE(); 109 ojs.setSchedule(sde); 110 ojs.type("ojschedule"); 111 112 ScheduleListDE sl = new ScheduleListDE(); 113 sl.addChild(ojs); 114 115 ScheduleDC sdc = new ScheduleDC(); 116 sdc.tag("schedule"); 117 sdc.setSchedules(sl); 118 119 SimpleObjectFactory componentFactory = new SimpleObjectFactory(); 120 componentFactory.addResource("/org/oddjob/oddjob.properties"); 121 SimpleObjectFactory valueFactory = new SimpleObjectFactory(); 122 valueFactory.addResource("/org/oddjob/values/types.properties"); 123 PropertyProxyResolver ppr = new PropertyProxyResolver(); 124 ppr.addResource("/org/oddjob/values/proxies.properties"); 125 ArooaHandler componentHandler = new DefaultComponentHandler(); 126 127 ArooaContext context = new ArooaContext(); 128 context.set(ArooaConstants.COMPONENT_FACTORY, componentFactory); 129 context.set(ArooaConstants.VALUE_FACTORY, valueFactory); 130 context.set(ArooaConstants.PROPERTY_PROXY_RESOLVER, ppr); 131 context.set(ArooaConstants.COMPONENT_HANDLER, componentHandler); 132 context.set(ArooaConstants.MAPPED_HANDLER, new MappedPropertyHandler()); 133 context.set(ArooaConstants.PROPERTY_HANDLER, new NestedPropertyHandler()); 134 context.set(ArooaConstants.ELEMENT_HANDLER, new NestedElementHandler()); 135 136 DummyRoot dr = new DummyRoot(); 137 context.set(ArooaConstants.CURRENTLY_CONFIGURING, new ArooaRuntime(dr, "whatever", context)); 138 context = new ArooaContext(context); 139 140 ByteArrayOutputStream out = new ByteArrayOutputStream (); 141 XmlHandler xmlHandler = new XmlHandler(out); 142 143 DesignParser dp = new DesignParser(context); 144 dp.parse("schedule", sdc, new MainHandler(xmlHandler)); 145 System.out.println(out.toString()); 146 147 dp.parse("schedule", sdc, new MainHandler(componentHandler)); 148 149 150 } 151 } 152 | Popular Tags |