1 4 package org.oddjob.jobs.structural; 5 6 import java.io.File ; 7 import java.text.ParseException ; 8 import java.util.LinkedList ; 9 10 import junit.framework.TestCase; 11 12 import org.apache.log4j.Logger; 13 import org.oddjob.Helper; 14 import org.oddjob.Oddjob; 15 import org.oddjob.arooa.ArooaConstants; 16 import org.oddjob.arooa.ArooaContext; 17 import org.oddjob.arooa.ArooaHandler; 18 import org.oddjob.arooa.ArooaRuntime; 19 import org.oddjob.arooa.SimpleObjectFactory; 20 import org.oddjob.arooa.registry.ComponentRegistry; 21 import org.oddjob.arooa.registry.Path; 22 import org.oddjob.framework.SimpleJob; 23 import org.oddjob.jobs.DummyStateJob; 24 import org.oddjob.state.JobState; 25 import org.oddjob.structural.StructuralEvent; 26 import org.oddjob.structural.StructuralListener; 27 import org.oddjob.util.DateHelper; 28 import org.xml.sax.SAXParseException ; 29 import org.xml.sax.helpers.AttributesImpl ; 30 31 35 public class ForEachJobTest extends TestCase { 36 private static final Logger logger = Logger.getLogger(ForEachJobTest.class); 37 38 static RegistryCheck instance; 39 public static class RegistryCheck extends SimpleJob { 40 ArooaContext ac; 41 public boolean setContext(ArooaContext ac) { 42 this.ac = ac; 43 return super.setContext(ac); 44 } 45 protected int execute() throws Throwable { 46 instance = this; 47 return 0; 48 } 49 } 50 51 public void testPsudoRegistry() throws SAXParseException { 52 ForEachJob test = new ForEachJob(); 53 test.setId("test"); 54 test.setValues(new String [] { "one" }); 55 ComponentRegistry cr = new ComponentRegistry(); 56 cr.register("test", test); 57 58 SimpleObjectFactory cf = new SimpleObjectFactory(); 59 cf.set("check", RegistryCheck.class.getName()); 60 61 ArooaContext ac = new ArooaContext(); 62 ac.set(ArooaConstants.COMPONENT_REGISTRY, cr); 63 ac.set(ArooaConstants.COMPONENT_FACTORY, cf); 64 ac.set(ArooaConstants.CURRENTLY_CONFIGURING, 65 new ArooaRuntime(test, "test", ac)); 66 ArooaHandler ah = test.handlerForChild(ac); 67 68 ah.onStartElement("", "child", "child", new AttributesImpl (), ac); 69 ArooaHandler ah2 = ah.onStartChild("", "check", "check", new AttributesImpl (), ac); 70 ah2.onStartElement("", "check", "check", new AttributesImpl (), ac); 71 ah2.onEndElement("", "check", ac); 72 ah.onEndChild("", "check", "check", ac); 73 ah.onEndElement("", "child", ac); 74 75 test.run(); 76 77 assertNotNull(instance); 78 79 ComponentRegistry crRecovered = (ComponentRegistry) 80 instance.ac.get(ArooaConstants.COMPONENT_REGISTRY); 81 assertEquals(ForEachJob.PsudoRegistry.class, 82 crRecovered.getClass()); 83 84 Object bean = crRecovered.objectForPath(new Path("test")); 85 assertNotNull(bean); 86 assertEquals(ForEachJob.LocalBean.class, bean.getClass()); 87 88 ForEachJob.LocalBean lb = (ForEachJob.LocalBean) bean; 89 90 int index = lb.getIndex(); 91 assertEquals(0, index); 92 93 String current = (String ) lb.getCurrent(); 94 assertEquals("one", current); 95 } 96 97 98 99 public void testBasic() throws ParseException { 100 checks = new Object [] { 101 new String ("hello"), 102 DateHelper.parseDate("25-dec-05"), 103 null, 104 new File ("file.txt") 105 }; 106 executed = 0; 107 108 Oddjob oj = new Oddjob(); 109 oj.setInput(getClass().getResourceAsStream("foreach-test.xml")); 110 oj.run(); 111 112 Check check = (Check)oj.lookup("check"); 114 assertNull(check); 115 116 assertEquals(4, executed); 117 } 118 119 static Object [] checks; 120 static int executed; 121 122 public static class Check extends SimpleJob { 123 124 Object o; 125 int i; 126 127 public void setObject(Object o) { 128 this.o = o; 129 } 130 public void setIndex(int i) { 131 this.i = i; 132 } 133 protected int execute() { 134 executed++; 135 logger.debug("Executing with object [" + o + "]"); 136 assertEquals(checks[i], o); 137 return 0; 138 } 139 } 140 141 public void testReset() { 142 class Childs implements StructuralListener { 143 LinkedList children = new LinkedList (); 144 public void childAdded(StructuralEvent event) { 145 children.add(event.getIndex(), event.getChild()); 146 } 147 public void childRemoved(StructuralEvent event) { 148 children.remove(event.getIndex()); 149 } 150 } 151 Childs childs = new Childs(); 152 153 String xml = 154 "<foreach id='e'>" + 155 " <values>" + 156 " <value value='complete'/>" + 157 " <value value='not complete'/>" + 158 " </values>" + 159 " <child>" + 160 " <class class='" + DummyStateJob.class.getName() + "' desired='${e.current}'/>" + 161 " </child>" + 162 "</foreach>"; 163 164 ForEachJob test = (ForEachJob) Helper.createComponentFromXml(xml); 165 test.addStructuralListener(childs); 166 167 test.run(); 168 169 assertEquals(JobState.COMPLETE, Helper.getJobState( 170 childs.children.get(0))); 171 assertEquals(JobState.NOT_COMPLETE, Helper.getJobState( 172 childs.children.get(1))); 173 assertEquals(JobState.NOT_COMPLETE, Helper.getJobState( 174 test)); 175 176 test.softReset(); 177 178 assertEquals(JobState.COMPLETE, Helper.getJobState( 179 childs.children.get(0))); 180 assertEquals(JobState.READY, Helper.getJobState( 181 childs.children.get(1))); 182 assertEquals(JobState.READY, Helper.getJobState( 183 test)); 184 185 test.run(); 186 187 assertEquals(JobState.COMPLETE, Helper.getJobState( 188 childs.children.get(0))); 189 assertEquals(JobState.NOT_COMPLETE, Helper.getJobState( 190 childs.children.get(1))); 191 assertEquals(JobState.NOT_COMPLETE, Helper.getJobState( 192 test)); 193 194 test.hardReset(); 195 196 assertEquals(0, childs.children.size()); 197 198 test.run(); 199 200 assertEquals(JobState.COMPLETE, Helper.getJobState( 201 childs.children.get(0))); 202 assertEquals(JobState.NOT_COMPLETE, Helper.getJobState( 203 childs.children.get(1))); 204 assertEquals(JobState.NOT_COMPLETE, Helper.getJobState( 205 test)); 206 } 207 208 } 209 | Popular Tags |