1 4 package org.oddjob.values; 5 6 import java.io.ByteArrayInputStream ; 7 import java.util.Date ; 8 9 import junit.framework.TestCase; 10 11 import org.apache.commons.beanutils.BeanUtils; 12 import org.apache.commons.beanutils.DynaBean; 13 import org.apache.commons.beanutils.PropertyUtils; 14 import org.apache.log4j.Logger; 15 import org.oddjob.Helper; 16 import org.oddjob.Oddjob; 17 import org.oddjob.arooa.ArooaConstants; 18 import org.oddjob.arooa.ArooaContext; 19 import org.oddjob.arooa.ArooaHandler; 20 import org.oddjob.arooa.ArooaRuntime; 21 import org.oddjob.arooa.handlers.MappedPropertyHandler; 22 import org.oddjob.arooa.reflect.BeanUtilsHelper; 23 import org.oddjob.arooa.reflect.DefaultRegistryLookup; 24 import org.oddjob.arooa.reflect.IntrospectionHelper; 25 import org.oddjob.arooa.reflect.RegistryLookup; 26 import org.oddjob.arooa.registry.ComponentRegistry; 27 import org.oddjob.jobs.EchoJob; 28 import org.oddjob.state.JobState; 29 import org.oddjob.values.types.PropertiesType; 30 import org.oddjob.values.types.PropertyType; 31 32 36 public class VariablesJobTest extends TestCase { 37 private static final Logger logger = Logger.getLogger(VariablesJobTest.class); 38 protected void setUp() { 39 logger.debug("-------------- " + getName() + " -------------"); 40 } 41 42 public void testSimple() throws Exception { 44 VariablesJob j = new VariablesJob(); 45 j.setValue("test", "fred"); 46 Object result = PropertyUtils.getProperty(j, "test"); 47 assertNotNull(result); 48 assertTrue(result instanceof PropertyType); 49 } 50 51 public void testHandlerProvided() { 52 VariablesJob j = new VariablesJob(); 53 IntrospectionHelper ih = IntrospectionHelper.getHelper(j.getClass()); 54 ArooaContext c = new ArooaContext(); 55 ArooaHandler h = ih.provideHandler(j, "whatever", c); 56 57 assertEquals("value", c.get(ArooaConstants.ELEMENT_NAME)); 58 assertTrue(h instanceof MappedPropertyHandler); 59 } 60 61 public void testTypesForSetting() { 63 VariablesJob vj = new VariablesJob(); 64 vj.set("fruit", "apple"); 65 vj.set("fruit.citrus", "orange"); 66 67 assertEquals(PropertyType.class, 68 BeanUtilsHelper.getPropertyType(vj, "fruit")); 69 assertEquals(PropertyType.class, 70 BeanUtilsHelper.getPropertyType(vj, "fruit.citrus")); 71 assertEquals(Object .class, 72 BeanUtilsHelper.getPropertyType(vj, "vegtables")); 73 74 } 75 76 public void testGetValues() throws Exception { 79 ComponentRegistry cr = new ComponentRegistry(); 80 RegistryLookup lookup = new DefaultRegistryLookup(cr); 81 VariablesJob j = new VariablesJob(); 82 cr.register("myj", j); 83 84 BeanUtilsHelper.setMappedProperty(j, "value", "test", new Boolean (true)); 85 Class type = BeanUtilsHelper.getPropertyType(j, "test"); 86 assertEquals(PropertyType.class, type); 87 88 BeanUtilsHelper.setMappedProperty(j, "value", "next", new Short ((short) 1234)); 89 type = BeanUtilsHelper.getPropertyType(j, "next"); 90 assertEquals(PropertyType.class, type); 91 92 Object o1 = lookup.getProperty("myj.test"); 93 Object r1 = IntrospectionHelper.valueFor(o1, Boolean.TYPE); 94 assertEquals(Boolean .class, r1.getClass()); 95 assertEquals(new Boolean (true), r1); 96 97 Object o2 = lookup.getProperty("myj.next"); 98 Object r2 = IntrospectionHelper.valueFor(o2, Short.TYPE); 99 assertEquals(Short .class, r2.getClass()); 100 assertEquals(new Short ((short) 1234), r2); 101 } 102 103 public void testGetValues2() throws Exception { 105 VariablesJob j = new VariablesJob(); 106 107 ComponentRegistry cr = new ComponentRegistry(); 108 RegistryLookup lookup = new DefaultRegistryLookup(cr); 109 ArooaContext c = new ArooaContext(); 110 c.set(ArooaConstants.RTC_LEAVE_PROXY, new Boolean (true)); 111 c.set(ArooaConstants.COMPONENT_REGISTRY, cr); 112 ArooaRuntime rtc1 = new ArooaRuntime(j, "ignored", c); 113 114 cr.register("myj", j); 115 116 ArooaRuntime rtc2 = new ArooaRuntime(new Boolean (true), "boolean", c); 117 rtc1.setMappedProperty("value", "test", rtc2); 118 119 ArooaRuntime rtc3 = new ArooaRuntime(new Short ((short) 1234), "short", c); 120 rtc1.setMappedProperty("value", "next", rtc3); 121 122 rtc1.configure(); 123 124 Object o1 = lookup.getProperty("myj.test"); 125 Object r1 = IntrospectionHelper.valueFor(o1, Boolean.TYPE); 126 assertEquals(new Boolean (true), r1); 127 128 Object o2 = lookup.getProperty("myj.next"); 129 Object r2 = IntrospectionHelper.valueFor(o2, Short.TYPE); 130 System.out.println(r2.getClass()); 131 assertEquals(new Short ((short) 1234), r2); 132 } 133 134 public void testMap() throws Exception { 136 VariablesJob j = new VariablesJob(); 137 ArooaContext c = new ArooaContext(); 138 c.set(ArooaConstants.RTC_LEAVE_PROXY, new Boolean (true)); 139 ArooaRuntime rtc1 = new ArooaRuntime(j, "ignored", c); 140 PropertiesType p = new PropertiesType(); 141 p.setInput(getClass().getResourceAsStream("/org/oddjob/values/proxies.properties")); 142 ArooaRuntime rtc2 = new ArooaRuntime(p, "ignored", c); 143 rtc1.setMappedProperty("value", null, rtc2); 144 rtc1.configure(); 145 146 assertEquals("org.oddjob.values.types.DateType", BeanUtils.getProperty(j, "java.util.Date")); 147 } 148 149 public void testNullValue() { 150 String xml= 151 "<oddjob><sequential><variables id='v'>" + 152 "<value name='rubbish' value='${nosuchid}'/>" + 153 "</variables>" + 154 "<echo text='${v.rubbish}'/>" + 155 "</sequential></oddjob>"; 156 157 Oddjob oj = new Oddjob(); 158 oj.setInput(new ByteArrayInputStream (xml.getBytes())); 159 oj.run(); 160 DynaBean b = (DynaBean) oj.lookup("v"); 161 String result = (String ) IntrospectionHelper.valueFor( 162 BeanUtilsHelper.getProperty(b, "JAVA_HOME")); 163 assertNull(result); 164 } 165 166 public void testSelfUse() { 167 168 String xml = "<oddjob>" + 169 "<variables id='v'>" + 170 " <date name='today' date='25-dec-05' timeZone='GMT'/>" + 171 " <format name='yyyymmdd_today' date='${v.today}' format='yyyyMMdd'/>" + 172 "</variables>" + 173 "</oddjob>"; 174 175 Oddjob oj = new Oddjob(); 176 oj.setInput(new ByteArrayInputStream (xml.getBytes())); 177 oj.run(); 178 179 assertEquals(JobState.COMPLETE, oj.lastJobStateEvent().getJobState()); 180 VariablesJob v= (VariablesJob) oj.lookup("v"); 181 182 Object result = IntrospectionHelper.valueFor(v.get("today")); 183 184 assertEquals(Date .class, result.getClass()); 185 186 System.out.println(v.get("yyyymmdd_today")); 187 188 } 189 190 210 public void testInOddjob() { 211 Oddjob oj = new Oddjob(); 212 oj.setInput(getClass().getResourceAsStream("variables-test.xml")); 213 oj.run(); 214 215 CheckBasicSetters check = (CheckBasicSetters)oj.lookup("check"); 216 assertEquals("Job state", JobState.COMPLETE, Helper.getJobState(check)); 217 } 218 219 } 220 | Popular Tags |