Author: sgoeschl
Date: Sun Oct 24 20:29:47 2010
New Revision: 1026881
URL: http://svn.apache.org/viewvc?rev=1026881&view=rev
Log:
[EXEC-34] Added a test case waiting for an asnchronous process using
DefaultExecuteResultHandler instead of "Watchdog.isWaiting".
Modified:
commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/DefaultExecutorTest.java
Modified:
commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/DefaultExecutorTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/DefaultExecutorTest.java?rev=1026881&r1=1026880&r2=1026881&view=diff
==============================================================================
---
commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/DefaultExecutorTest.java
(original)
+++
commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/DefaultExecutorTest.java
Sun Oct 24 20:29:47 2010
@@ -65,9 +65,9 @@ public class DefaultExecutorTest extends
// turn on debug mode and throw an exception for each encountered
problem
System.setProperty("org.apache.commons.exec.lenient", "false");
- System.setProperty("org.apache.commons.exec.debug", "true");
+ System.setProperty("org.apache.commons.exec.debug", "true");
}
-
+
protected void setUp() throws Exception {
System.out.println(">>> Executing " + getName() + " ...");
@@ -104,7 +104,7 @@ public class DefaultExecutorTest extends
int exitValue = exec.execute(cl);
assertEquals("FOO..", baos.toString().trim());
assertFalse(exec.isFailure(exitValue));
- assertEquals(new File("."), exec.getWorkingDirectory());
+ assertEquals(new File("."), exec.getWorkingDirectory());
}
public void testExecuteWithWorkingDirectory() throws Exception {
@@ -132,7 +132,7 @@ public class DefaultExecutorTest extends
public void testExecuteWithError() throws Exception {
CommandLine cl = new CommandLine(errorTestScript);
-
+
try{
exec.execute(cl);
fail("Must throw ExecuteException");
@@ -174,7 +174,7 @@ public class DefaultExecutorTest extends
*/
public void testExecuteAsync() throws Exception {
CommandLine cl = new CommandLine(testScript);
- DefaultExecuteResultHandler resultHandler = new
DefaultExecuteResultHandler();
+ DefaultExecuteResultHandler resultHandler = new
DefaultExecuteResultHandler();
exec.execute(cl, resultHandler);
resultHandler.waitFor(2000);
assertTrue(resultHandler.hasResult());
@@ -204,7 +204,7 @@ public class DefaultExecutorTest extends
* Start a asynchronous process and terminate it manually before the
* watchdog timeout occurs.
*
- * @throws Exception the test failed
+ * @throws Exception the test failed
*/
public void testExecuteAsyncWithTimelyUserTermination() throws Exception {
CommandLine cl = new CommandLine(foreverTestScript);
@@ -287,7 +287,7 @@ public class DefaultExecutorTest extends
return;
}
catch(Throwable t) {
- fail(t.getMessage());
+ fail(t.getMessage());
}
assertTrue("Killed process should be true",
executor.getWatchdog().killedProcess() );
@@ -398,10 +398,10 @@ public class DefaultExecutorTest extends
CommandLine cl = new CommandLine(testScript);
ShutdownHookProcessDestroyer processDestroyer = new
ShutdownHookProcessDestroyer();
exec.setProcessDestroyer(processDestroyer);
-
+
assertTrue(processDestroyer.size() == 0);
assertTrue(processDestroyer.isAddedAsShutdownHook() == false);
-
+
int exitValue = exec.execute(cl);
assertEquals("FOO..", baos.toString().trim());
@@ -409,7 +409,7 @@ public class DefaultExecutorTest extends
assertTrue(processDestroyer.size() == 0);
assertTrue(processDestroyer.isAddedAsShutdownHook() == false);
}
-
+
/**
* Test the proper handling of ProcessDestroyer for an asynchronous
process.
* Since we do not terminate the process it will be terminated in the
@@ -590,7 +590,7 @@ public class DefaultExecutorTest extends
}
/**
- * Call a script to dump the environment variables of the subprocess.
+ * Call a script to dump the environment variables of the subprocess.
*
* @throws Exception the test failed
*/
@@ -656,11 +656,11 @@ public class DefaultExecutorTest extends
*
* Race condition prevent watchdog working using ExecuteStreamHandler.
* The test fails because when watchdog.destroyProcess() is invoked the
- * external process is not bound to the watchdog yet
+ * external process is not bound to the watchdog yet.
*
* @throws Exception the test failed
*/
- public void testExec34() throws Exception {
+ public void testExec34_1() throws Exception {
CommandLine cmdLine = new CommandLine(pingScript);
cmdLine.addArgument("10"); // sleep 10 secs
@@ -679,6 +679,30 @@ public class DefaultExecutorTest extends
}
/**
+ * EXEC-34 https://issues.apache.org/jira/browse/EXEC-34
+ *
+ * Some user waited for an asynchronous process using
watchdog.isWatching() which
+ * is now properly implemented using
<code>DefaultExecuteResultHandler</code>.
+ *
+ * @throws Exception the test failed
+ */
+ public void testExec34_2() throws Exception {
+
+ CommandLine cmdLine = new CommandLine(pingScript);
+ cmdLine.addArgument("10"); // sleep 10 secs
+
+ ExecuteWatchdog watchdog = new ExecuteWatchdog(5000);
+ DefaultExecuteResultHandler handler = new
DefaultExecuteResultHandler();
+ exec.setWatchdog(watchdog);
+ exec.execute(cmdLine, handler);
+ handler.waitFor();
+ assertTrue("Process has exited", handler.hasResult());
+ assertNotNull("Process was aborted", handler.getException());
+ assertTrue("Watchdog should have killed the process",
watchdog.killedProcess());
+ assertFalse("Watchdog is no longer watching the process",
watchdog.isWatching());
+ }
+
+ /**
* Test EXEC-36 see https://issues.apache.org/jira/browse/EXEC-36
*
* Original example from Kai Hu which only can be tested on Unix
@@ -757,7 +781,7 @@ public class DefaultExecutorTest extends
System.err.println("The test 'testExec36_3' does not support the
following OS : " + System.getProperty("os.name"));
return;
}
-
+
CommandLine cmdl;
File file = new File("/Documents and Settings/myusername/Local
Settings/Temp/netfx.log");
Map map = new HashMap();
@@ -847,7 +871,7 @@ public class DefaultExecutorTest extends
* For this test we are using the batch file - under Windows the 'ping'
* process can't be killed (not supported by Win32) and will happily
* run the given time (e.g. 10 seconds) even hwen the batch file is already
- * killed.
+ * killed.
*
* @throws Exception the test failed
*/
@@ -860,7 +884,7 @@ public class DefaultExecutorTest extends
// create a custom "PumpStreamHandler" doing no pumping at all
PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(null,
null, null);
-
+
executor.setWatchdog(watchdog);
executor.setStreamHandler(pumpStreamHandler);