1 4 package org.oddjob.jobs; 5 6 import java.io.ByteArrayInputStream ; 7 import java.io.ByteArrayOutputStream ; 8 import java.io.File ; 9 import java.io.IOException ; 10 import java.io.OutputStream ; 11 import java.util.ArrayList ; 12 import java.util.List ; 13 14 import junit.framework.TestCase; 15 16 import org.apache.log4j.Logger; 17 import org.oddjob.Helper; 18 import org.oddjob.Oddjob; 19 import org.oddjob.Stateful; 20 import org.oddjob.io.BufferType; 21 import org.oddjob.logging.ConsoleArchive; 22 import org.oddjob.logging.LogEvent; 23 import org.oddjob.logging.LogLevel; 24 import org.oddjob.logging.LogListener; 25 import org.oddjob.state.JobState; 26 import org.oddjob.structural.ChildHelper; 27 28 31 public class ExecJobTest extends TestCase { 32 private static final Logger logger = Logger.getLogger(ExecJobTest.class); 33 34 protected void setUp() { 35 logger.debug("================== " + getName() + " ==================="); 36 } 37 38 public void testCreate() { 39 40 String xml = "<oddjob><exec name=\"A Test Exec\">" + 41 "<args>" + 42 "<value value='java'/>" + 43 "<value value='-version'/>" + 44 "</args>" + 45 "</exec></oddjob>"; 46 47 Oddjob oj = new Oddjob(); 48 oj.setInput(new ByteArrayInputStream (xml.getBytes())); 49 oj.run(); 50 51 Object [] children = ChildHelper.getChildren(oj); 52 assertNotNull("created", children[0]); 53 assertEquals("state", JobState.COMPLETE, Helper.getJobState( 54 (Stateful) children[0])); 55 } 56 57 public void testCommand() { 58 ExecJob job = new ExecJob(); 59 job.setCommand("java -version"); 60 61 job.run(); 62 assertEquals("Success", JobState.COMPLETE, job.lastJobStateEvent().getJobState()); 63 64 } 65 66 public void testStop() throws InterruptedException { 67 BufferType buf = new BufferType(); 68 69 final ExecJob job = new ExecJob(); 70 job.setCommand("java -cp build/classes;lib/core/log4j-1.2.8.jar;lib/core/commons-beanutils.jar;lib/core/commons-logging.jar org.oddjob.Main -f test/conf/wait.xml"); 71 job.setStdout((OutputStream ) buf.valueFor(OutputStream .class)); 72 job.setStderr((OutputStream ) buf.valueFor(OutputStream .class)); 73 74 Thread t = new Thread (new Runnable () { 75 public void run() { 76 try { 77 job.run(); 78 } catch (Throwable t) { 79 t.printStackTrace(); 80 fail(); 81 } 82 } 83 }); 84 t.start(); 85 Thread.sleep(2000); 86 job.stop(); 87 t.join(); 88 89 logger.debug("" + buf.valueFor(String .class)); 90 assertEquals(JobState.NOT_COMPLETE, job.lastJobStateEvent().getJobState()); 91 92 } 93 94 public void testFailure() { 95 ExecJob job = new ExecJob(); 96 job.setCommand("java rubbish"); 97 98 job.run(); 99 assertEquals(JobState.NOT_COMPLETE, job.lastJobStateEvent().getJobState()); 100 } 101 102 public void testOutput() throws IOException { 103 104 ExecJob ej = new ExecJob(); 105 ej.setCommand("cmd /c echo hello"); 106 ByteArrayOutputStream os = new ByteArrayOutputStream (); 107 ej.setStdout(os); 108 ej.run(); 109 byte[] bytes = os.toByteArray(); 110 assertEquals("hello" + System.getProperty("line.separator"), new String (bytes)); 111 } 112 113 public void testConsole1() throws IOException { 114 115 ExecJob ej = new ExecJob(); 116 ej.setCommand("cmd /c echo hello"); 117 ej.run(); 118 119 LL ll = new LL(); 120 ej.consoleLog().addListener(ll, LogLevel.DEBUG, -1, 1000); 121 String result = ll.getLines()[0]; 122 logger.debug(result); 123 assertEquals("hello" + System.getProperty("line.separator"), result); 124 } 125 126 144 public void testChained() throws IOException { 145 146 String xml = 147 "<oddjob>" + 148 " <sequential>" + 149 " <exec name='One' id='one' command='cmd /c echo hello'>" + 150 " <stdout>" + 151 " <buffer/>" + 152 " </stdout>" + 153 " </exec>" + 154 " <exec name='Two' id='two' command='c:/cygwin/bin/cat'" + 155 " stdin='${one.stdout}'/>" + 156 " </sequential>" + 157 "</oddjob>"; 158 159 Oddjob oj = new Oddjob(); 160 oj.setInput(new ByteArrayInputStream (xml.getBytes())); 161 oj.run(); 162 163 assertEquals(JobState.COMPLETE, oj.lastJobStateEvent().getJobState()); 164 165 ConsoleArchive ca = (ConsoleArchive) oj.lookup("two"); 167 168 LL ll = new LL(); 169 ca.consoleLog().addListener(ll, LogLevel.DEBUG, -1, 1000); 170 String result = ll.getLines()[0]; 171 System.out.println(result); 172 assertEquals("hello" + System.getProperty("line.separator"), result); 173 174 } 175 176 class LL implements LogListener { 177 List list = new ArrayList (); 178 public void logEvent(LogEvent logEvent) { 179 list.add(logEvent.getMessage()); 180 } 181 String [] getLines() { 182 return (String []) list.toArray(new String [0]); 183 } 184 } 185 186 } 187 | Popular Tags |