1 4 package org.oddjob.framework; 5 6 import java.io.ByteArrayInputStream ; 7 8 import junit.framework.TestCase; 9 10 import org.apache.commons.beanutils.PropertyUtils; 11 import org.oddjob.Helper; 12 import org.oddjob.Oddjob; 13 import org.oddjob.Resetable; 14 import org.oddjob.Stoppable; 15 import org.oddjob.arooa.ArooaConstants; 16 import org.oddjob.arooa.ArooaContext; 17 import org.oddjob.arooa.ArooaRuntime; 18 import org.oddjob.arooa.registry.ComponentRegistry; 19 import org.oddjob.arooa.registry.Path; 20 import org.oddjob.state.JobState; 21 22 public class ServiceWrapperTest extends TestCase { 23 24 public static class MyS { 25 boolean started; 26 boolean stopped; 27 public void start() { 28 started = true; 29 } 30 public void stop() { 31 stopped = true; 32 } 33 public boolean isStarted() { 34 return started; 35 } 36 public boolean isStopped() { 37 return stopped; 38 } 39 } 40 41 public void testStartStop() throws Exception { 42 Service service = Service.serviceFor(new MyS()); 43 44 Runnable wrapper = ServiceWrapper.wrapperFor(service); 45 46 wrapper.run(); 47 48 assertEquals(JobState.EXECUTING, Helper.getJobState(wrapper)); 49 assertEquals(new Boolean (true), PropertyUtils.getProperty(wrapper, "started")); 50 51 ((Stoppable) wrapper).stop(); 52 53 assertEquals(JobState.COMPLETE, Helper.getJobState(wrapper)); 54 assertEquals(new Boolean (true), PropertyUtils.getProperty(wrapper, "stopped")); 55 56 ((Resetable) wrapper).hardReset(); 57 58 assertEquals(JobState.READY, Helper.getJobState(wrapper)); 59 } 60 61 public void testInOddjob() throws Exception { 62 String xml = "<oddjob><r class='" + MyS.class.getName() + "' id='s' /></oddjob>"; 63 Oddjob oj = new Oddjob(); 64 oj.setInput(new ByteArrayInputStream (xml.getBytes())); 65 66 oj.run(); 67 68 Object r = oj.lookup("s"); 69 assertEquals(JobState.EXECUTING, Helper.getJobState(r)); 70 assertEquals(new Boolean (true), PropertyUtils.getProperty(r, "started")); 71 72 oj.stop(); 73 74 assertEquals(JobState.COMPLETE, Helper.getJobState(r)); 75 assertEquals(new Boolean (true), PropertyUtils.getProperty(r, "stopped")); 76 } 77 78 public static class Bean { 79 String greeting; 80 public void setGreeting(String greeting) { 81 this.greeting = greeting; 82 } 83 public String getGreeting() { 84 return greeting; 85 } 86 } 87 88 public static class MyS2 { 89 public String result; 90 public void start() { 91 } 92 public void stop() { 93 } 94 public void setResult(String result) { 95 this.result = result; 96 } 97 public String getResult() { 98 return result; 99 } 100 } 101 102 public void testConfiguration() throws Exception { 103 ComponentRegistry cr = new ComponentRegistry(); 104 cr.register("b", new Bean()); 105 106 PropertyUtils.setProperty(cr.objectForPath(new Path("b")), 107 "greeting", "hello"); 108 109 ArooaContext ac = new ArooaContext(); 110 ac.set(ArooaConstants.COMPONENT_REGISTRY, cr); 111 112 MyS2 s2 = new MyS2(); 113 ArooaRuntime runtime = new ArooaRuntime(s2, "job", ac); 114 ac.set(ArooaConstants.CURRENTLY_CONFIGURING, runtime); 115 runtime.setAttribute("result", "${b.greeting}"); 116 117 Runnable wrapper = ServiceWrapper.wrapperFor( 118 Service.serviceFor(s2)); 119 ((ContextAware) wrapper).setContext(ac); 120 121 wrapper.run(); 122 123 assertEquals("hello", PropertyUtils.getProperty(wrapper, "result")); 124 125 PropertyUtils.setProperty(cr.objectForPath(new Path("b")), 126 "greeting", "goodbye"); 127 128 ((Stoppable) wrapper).stop(); 129 ((Resetable) wrapper).hardReset(); 130 wrapper.run(); 131 132 assertEquals("goodbye", PropertyUtils.getProperty(wrapper, "result")); 133 } 134 135 public void testInOddjob2() throws Exception { 136 String xml = "<oddjob><sequential>" + 137 "<variables id='v'><value name='x' value='hello'/></variables>" + 138 "<r class='" + MyS2.class.getName() + "' " + 139 "id='s' result='${v.x}' />" + 140 "</sequential></oddjob>"; 141 Oddjob oj = new Oddjob(); 142 oj.setInput(new ByteArrayInputStream (xml.getBytes())); 143 144 oj.run(); 145 146 Object r = oj.lookup("s"); 147 assertEquals("hello", PropertyUtils.getProperty(r, "result")); 148 149 oj.stop(); 150 } 151 } 152 | Popular Tags |