1 4 package org.oddjob; 5 6 import java.io.ByteArrayInputStream ; 7 import java.io.File ; 8 9 import junit.framework.TestCase; 10 11 import org.apache.commons.beanutils.DynaBean; 12 import org.oddjob.arooa.ArooaConstants; 13 import org.oddjob.arooa.ArooaContext; 14 import org.oddjob.arooa.reflect.DefaultRegistryLookup; 15 import org.oddjob.arooa.reflect.IntrospectionHelper; 16 import org.oddjob.arooa.reflect.RegistryLookup; 17 import org.oddjob.arooa.registry.ComponentRegistry; 18 import org.oddjob.io.DeleteJob; 19 import org.oddjob.persist.SerializePersister; 20 import org.oddjob.state.JobState; 21 import org.oddjob.structural.StructuralEvent; 22 import org.oddjob.structural.StructuralListener; 23 import org.oddjob.values.VariablesJob; 24 import org.oddjob.values.types.PropertyType; 25 import org.xml.sax.helpers.LocatorImpl ; 26 27 31 public class OddjobTest extends TestCase { 32 33 37 public void testReset() { 38 class MyL implements StructuralListener { 39 int count; 40 public void childAdded(StructuralEvent event) { 41 count++; 42 } 43 public void childRemoved(StructuralEvent event) { 44 count--; 45 } 46 } 47 Oddjob oj = new Oddjob(); 48 oj.setFile(new File ("test/conf/oddjob-test.xml")); 49 50 MyL l = new MyL(); 51 oj.addStructuralListener(l); 52 53 assertEquals(0, l.count); 54 55 oj.run(); 56 57 assertEquals(1, l.count); 58 59 oj.hardReset(); 60 61 assertEquals(0, l.count); 62 63 oj.run(); 64 65 assertEquals(1, l.count); 66 } 67 68 72 public void testLoadNoChild() { 73 String config = "<oddjob id='this'/>"; 74 ByteArrayInputStream is = new ByteArrayInputStream (config.getBytes()); 75 76 Oddjob oj = new Oddjob(); 77 oj.setInput(is); 78 oj.load(); 79 Object test = oj.lookup("this"); 80 assertTrue("Oddjob configured", oj == test); 81 } 82 83 public void testLoadNestedOddjob() { 84 String config = "<oddjob><oddjob id='nested'/></oddjob>"; 85 ByteArrayInputStream is = new ByteArrayInputStream (config.getBytes()); 86 87 Oddjob oj = new Oddjob(); 88 oj.setInput(is); 89 oj.load(); 90 Object test = oj.lookup("nested"); 91 assertNotNull("Nested oddjob", test); 92 } 93 94 public void testLoadOddjobClassloader() { 95 String config = "<oddjob><oddjob id='nested'>" 96 + "<classpath><files dir='build' pattern='*.jar'/></classpath>" 97 + "</oddjob></oddjob>"; 98 ByteArrayInputStream is = new ByteArrayInputStream (config.getBytes()); 99 100 Oddjob oj = new Oddjob(); 101 oj.setInput(is); 102 oj.setLoadOnly(true); 103 oj.run(); 104 105 String nestedConf = "<oddjob/>"; 106 ByteArrayInputStream nestedIs = new ByteArrayInputStream (nestedConf.getBytes()); 107 Oddjob test = (Oddjob) oj.lookup("nested"); 108 test.setInput(nestedIs); 109 test.run(); 110 111 assertNotNull("Nested classloader", 112 test.getClassLoader()); 113 } 114 115 public void testLookup() { 116 ComponentRegistry cr = new ComponentRegistry(); 117 RegistryLookup lookup = new DefaultRegistryLookup(cr); 118 String config = "<oddjob id='this'><variables id='fruits'>" + 119 "<value name='fruit' value='apple'/></variables></oddjob>"; 120 ByteArrayInputStream is = new ByteArrayInputStream (config.getBytes()); 121 ArooaContext ac = new ArooaContext(); 122 ac.set(ArooaConstants.COMPONENT_REGISTRY, cr); 123 124 Oddjob oj = new Oddjob(); 125 cr.register("nested", oj); 126 oj.setContext(ac); 127 oj.setInput(is); 128 oj.run(); 129 String fruit = (String ) IntrospectionHelper.valueFor( 130 lookup.getProperty("nested/fruits.fruit")); 131 assertEquals("apple", fruit); 132 } 133 134 public void testArgs() { 135 String config = "<oddjob id='this'><variables id='fruits'>" + 136 "<value name='fruit' value='${this.args[0]}'/></variables></oddjob>"; 137 ByteArrayInputStream is = new ByteArrayInputStream (config.getBytes()); 138 Oddjob oj = new Oddjob(); 139 oj.setInput(is); 140 oj.setArgs(new String [] { "apple" }); 141 oj.run(); 142 String fruit = (String ) IntrospectionHelper.valueFor( 143 ((DynaBean) oj.lookup("fruits")).get("fruit")); 144 assertEquals("apple", fruit); 145 } 146 147 public void testSetArgs() { 148 ComponentRegistry cr = new ComponentRegistry(); 149 RegistryLookup lookup = new DefaultRegistryLookup(cr); 150 151 String config = "<oddjob id='this'/>"; 152 ByteArrayInputStream is = new ByteArrayInputStream (config.getBytes()); 153 Oddjob oj = new Oddjob(); 154 cr.register("nested", oj); 155 oj.setInput(is); 156 oj.run(); 157 lookup.setProperty("nested.args[0]", "apple"); 158 assertEquals("apple", oj.getArgs()[0]); 159 } 160 161 public void testSetLookup() { 162 ComponentRegistry cr = new ComponentRegistry(); 163 RegistryLookup lookup = new DefaultRegistryLookup(cr); 164 ArooaContext ac = new ArooaContext(); 165 ac.set(ArooaConstants.COMPONENT_REGISTRY, cr); 166 167 String config = "<oddjob id='this'>" + 168 "<variables id='vars'/>" + 169 "</oddjob>"; 170 ByteArrayInputStream is = new ByteArrayInputStream (config.getBytes()); 171 Oddjob oj = new Oddjob(); 172 cr.register("nested", oj); 173 oj.setInput(is); 174 oj.setContext(ac); 175 oj.run(); 176 lookup.setProperty("nested/vars.fruit", "apple"); 177 Object result = ((VariablesJob) oj.lookup("vars")).get("fruit"); 178 assertEquals(PropertyType.class, result.getClass()); 179 assertEquals("apple", IntrospectionHelper.valueFor(result, Object .class)); 180 } 181 182 186 public void testDir() { 187 Oddjob oj = new Oddjob(); 188 oj.setFile(new File ("xyz.xml")); 189 assertEquals(new File ("whatever").getAbsoluteFile().getParentFile(), oj.getDir()); 190 } 191 192 197 public void testRegistryManagement() { 198 ComponentRegistry cr = new ComponentRegistry(); 199 ArooaContext ac = new ArooaContext(); 200 ac.set(ArooaConstants.COMPONENT_REGISTRY, cr); 201 ac.setLocator(new LocatorImpl ()); 203 204 205 Oddjob oj = new Oddjob(); 206 oj.setContext(ac); 207 208 String xml = "<oddjob><echo id='echo' text='Hello'/></oddjob>"; 209 210 oj.setInput(new ByteArrayInputStream (xml.getBytes())); 211 212 assertEquals(0, cr.childCount()); 213 214 oj.run(); 215 216 assertEquals(1, cr.childCount()); 217 assertNotNull(oj.lookup("echo")); 218 219 oj.hardReset(); 220 221 assertEquals(0, cr.childCount()); 222 223 oj.setInput(new ByteArrayInputStream (xml.getBytes())); 224 oj.run(); 225 226 assertEquals(1, cr.childCount()); 227 assertNotNull(oj.lookup("echo")); 228 } 229 230 public void testPersist() { 231 DeleteJob dj = new DeleteJob(); 232 dj.setFiles(new File [] { new File (new File ("test/persist"), "e.ser") } ); 233 dj.run(); 234 235 String xml = "<oddjob><echo id='e' text='Hello'/></oddjob>"; 236 237 Oddjob oj = new Oddjob(); 238 239 SerializePersister persister = new SerializePersister(); 240 persister.setDir(new File ("test/persist")); 241 242 oj.setPersister(persister); 243 oj.setInput(new ByteArrayInputStream (xml.getBytes())); 244 245 oj.run(); 246 persister.close(); 247 248 Oddjob next = new Oddjob(); 249 next.setPersister(persister); 250 next.setInput(new ByteArrayInputStream (xml.getBytes())); 251 next.setLoadOnly(true); 252 253 next.run(); 254 255 assertEquals(JobState.COMPLETE, Helper.getJobState( 256 next.lookup("e"))); 257 258 } 259 } 260 261 | Popular Tags |