You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by sg...@apache.org on 2010/08/13 17:14:12 UTC
svn commit: r985229 - in
/commons/proper/exec/trunk/src/test/java/org/apache/commons/exec:
CommandLineTest.java DefaultExecutorTest.java
Author: sgoeschl
Date: Fri Aug 13 15:14:09 2010
New Revision: 985229
URL: http://svn.apache.org/viewvc?rev=985229&view=rev
Log:
Adding more command line parsing tests
Modified:
commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/CommandLineTest.java
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/CommandLineTest.java
URL: http://svn.apache.org/viewvc/commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/CommandLineTest.java?rev=985229&r1=985228&r2=985229&view=diff
==============================================================================
--- commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/CommandLineTest.java (original)
+++ commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/CommandLineTest.java Fri Aug 13 15:14:09 2010
@@ -428,20 +428,49 @@ public class CommandLineTest extends Tes
}
/**
- public void testComplexAddArguments3() {
+ * Some complex real-life command line from
+ * http://blogs.msdn.com/b/astebner/archive/2005/12/13/503471.aspx
+ */
+ public void _testExec36_1() throws Exception {
CommandLine cmdl;
- String result = "dotnetfx.exe /q:a /c:\"install.exe /l \"\"c:\\Documents and Settings\\myusername\\Local Settings\\Temp\\netfx.log\"\" /q\"";
- // using command line parser
- cmdl = CommandLine.parse("dotnetfx.exe /q:a /c:'\"install.exe /l \"\"c:\\Documents and Settings\\myusername\\Local Settings\\Temp\\netfx.log\"\" /q\"\"'");
- // assertEquals(cmdl.toString(), result);
-
- // building the command line incrementally
- cmdl = new CommandLine("dotnetfx.exe");
- cmdl.addArgument("/q:a", false);
- cmdl.addArgument("/c:\"install.exe /l \"\"c:\\Documents and Settings\\myusername\\Local Settings\\Temp\\netfx.log\"\" /q\"", false);
- assertEquals(cmdl.toStrings(), );
+ String line = "./script/jrake "
+ + "cruise:publish_installers "
+ + "INSTALLER_VERSION=unstable_2_1 "
+ + "INSTALLER_PATH=\"/var/lib/cruise-agent/installers\" "
+ + "INSTALLER_DOWNLOAD_SERVER='something'"
+ + "WITHOUT_HELP_DOC=true";
+
+ cmdl = CommandLine.parse(line);
+ String[] args = cmdl.toStrings();
+ assertEquals("./script/jrake", args[0]);
+ assertEquals("cruise:publish_installers", args[1]);
+ assertEquals("INSTALLER_VERSION=unstable_2_1", args[2]);
+ assertEquals("INSTALLER_PATH=\"/var/lib/cruise-agent/installers\"", args[3]);
+ assertEquals("INSTALLER_DOWNLOAD_SERVER='something'", args[4]);
+ assertEquals("WITHOUT_HELP_DOC=true", args[5]);
+ }
+
+ /**
+ * Some complex real-life command line from
+ * http://blogs.msdn.com/b/astebner/archive/2005/12/13/503471.aspx
+ */
+ public void _testExec36_2() {
+
+ CommandLine cmdl;
+
+ String line = "dotnetfx.exe"
+ + " /q:a "
+ + "/c:\"install.exe /l \"\"c:\\Documents and Settings\\myusername\\Local Settings\\Temp\\netfx.log\"\" /q\"";
+
+ cmdl = CommandLine.parse(line);
+ String[] args = cmdl.toStrings();
+ assertEquals("dotnetfx.exe", args[0]);
+ assertEquals("/q:a", args[1]);
+ assertEquals("/c:\"install.exe /l \"\"c:\\Documents and Settings\\myusername\\Local Settings\\Temp\\netfx.log\"\" /q\"", args[2] );
}
- */
+
+
+
}
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=985229&r1=985228&r2=985229&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 Fri Aug 13 15:14:09 2010
@@ -293,44 +293,6 @@ public class DefaultExecutorTest extends
}
/**
- * Start any processes in a loop to make sure that we do
- * not leave any handles/resources open.
- *
- * @throws Exception the test failed
- */
- public void testExecuteStability() throws Exception {
-
- // make a plain-vanilla test
- for(int i=0; i<1000; i++) {
- Map env = new HashMap();
- env.put("TEST_ENV_VAR", new Integer(i));
- CommandLine cl = new CommandLine(testScript);
- int exitValue = exec.execute(cl,env);
- assertFalse(exec.isFailure(exitValue));
- assertEquals("FOO." + i + ".", baos.toString().trim());
- baos.reset();
- }
-
- // now be nasty and use the watchdog to kill out sub-processes
- for(int i=0; i<100; i++) {
- Map env = new HashMap();
- env.put("TEST_ENV_VAR", new Integer(i));
- DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
- CommandLine cl = new CommandLine(foreverTestScript);
- ExecuteWatchdog watchdog = new ExecuteWatchdog(500);
- exec.setWatchdog(watchdog);
- exec.execute(cl, env, resultHandler);
- try {
- resultHandler.waitFor();
- }
- catch(ExecuteException e) {
- // nothing to do
- }
- baos.reset();
- }
- }
-
- /**
* Invoke the error script but define that the ERROR_STATUS is a good
* exit value and therefore no exception should be thrown.
*
@@ -462,7 +424,9 @@ public class DefaultExecutorTest extends
executor.setStreamHandler( pumpStreamHandler );
int exitValue = executor.execute(cl);
fis.close();
- assertTrue(baos.toString().trim().endsWith("Finished reading from stdin"));
+ String result = baos.toString().trim();
+ System.out.println(result);
+ assertTrue(result.endsWith("Finished reading from stdin"));
assertFalse(exec.isFailure(exitValue));
}
}
@@ -682,7 +646,113 @@ public class DefaultExecutorTest extends
assertTrue("Watchdog should have killed the process",watchdog.killedProcess());
}
-
+ /**
+ * Test EXEC-36 see https://issues.apache.org/jira/browse/EXEC-36
+ *
+ * Original example from Kai Hu
+ */
+ public void testExec36_1() throws Exception {
+
+ CommandLine cmdl;
+
+ /**
+ * ./script/jrake cruise:publish_installers INSTALLER_VERSION=unstable_2_1 \
+ * INSTALLER_PATH="/var/lib/ cruise-agent/installers" INSTALLER_DOWNLOAD_SERVER='something' WITHOUT_HELP_DOC=true
+ */
+
+ String expected = "./script/jrake\n" +
+ "cruise:publish_installers\n" +
+ "INSTALLER_VERSION=unstable_2_1\n" +
+ "INSTALLER_PATH=\"/var/lib/ cruise-agent/installers\"\n" +
+ "INSTALLER_DOWNLOAD_SERVER='something'\n" +
+ "WITHOUT_HELP_DOC=true";
+
+ cmdl = new CommandLine(printArgsScript);
+ cmdl.addArgument("./script/jrake", false);
+ cmdl.addArgument("cruise:publish_installers", false);
+ cmdl.addArgument("INSTALLER_VERSION=unstable_2_1", false);
+ cmdl.addArgument("INSTALLER_PATH=\"/var/lib/ cruise-agent/installers\"", false);
+ cmdl.addArgument("INSTALLER_DOWNLOAD_SERVER='something'", false);
+ cmdl.addArgument("WITHOUT_HELP_DOC=true", false);
+
+ int exitValue = exec.execute(cmdl);
+ String result = baos.toString().trim();
+ System.out.println("=== Expected ===");
+ System.out.println(expected);
+ System.out.println("=== Result ===");
+ System.out.println(result);
+ assertFalse(exec.isFailure(exitValue));
+ assertEquals(expected, result);
+ }
+
+ /**
+ * Test EXEC-36 see https://issues.apache.org/jira/browse/EXEC-36
+ *
+ * Test a complex real example found at
+ * http://blogs.msdn.com/b/astebner/archive/2005/12/13/503471.aspx
+ */
+ public void _testExec36_2() throws Exception {
+
+ CommandLine cmdl;
+
+ // the original command line
+ // dotnetfx.exe /q:a /c:"install.exe /l ""c:\Documents and Settings\myusername\Local Settings\Temp\netfx.log"" /q"
+
+ String expected = "dotnetfx.exe\n" +
+ "/q:a\n" +
+ "/c:\"install.exe /l \"\"c:\\Documents and Settings\\myusername\\Local Settings\\Temp\\netfx.log\"\" /q\"";
+
+ cmdl = new CommandLine(printArgsScript);
+ cmdl.addArgument("dotnetfx.exe", false);
+ cmdl.addArgument("/q:a", false);
+ cmdl.addArgument("/c:\"install.exe /l \"\"c:\\Documents and Settings\\myusername\\Local Settings\\Temp\\netfx.log\"\" /q\"", false);
+
+ int exitValue = exec.execute(cmdl);
+ String result = baos.toString().trim();
+ System.out.println("=== Expected ===");
+ System.out.println(expected);
+ System.out.println("=== Result ===");
+ System.out.println(result);
+ assertFalse(exec.isFailure(exitValue));
+ assertEquals(expected, result);
+ }
+
+ /**
+ * Test EXEC-36 see https://issues.apache.org/jira/browse/EXEC-36
+ *
+ * Test a complex real example found at
+ * http://blogs.msdn.com/b/astebner/archive/2005/12/13/503471.aspx
+ */
+ public void _testExec36_3() throws Exception {
+
+ CommandLine cmdl;
+ File file = new File("c:\\Documents and Settings\\myusername\\Local Settings\\Temp\\netfx.log");
+ Map map = new HashMap();
+ map.put("FILE", file);
+
+ // the original command line
+ // dotnetfx.exe /q:a /c:"install.exe /l ""c:\Documents and Settings\myusername\Local Settings\Temp\netfx.log"" /q"
+
+ String expected = "dotnetfx.exe\n" +
+ "/q:a\n" +
+ "/c:\"install.exe /l \"\"c:\\Documents and Settings\\myusername\\Local Settings\\Temp\\netfx.log\"\" /q\"";
+
+ cmdl = new CommandLine(printArgsScript);
+ cmdl.setSubstitutionMap(map);
+ cmdl.addArgument("dotnetfx.exe", false);
+ cmdl.addArgument("/q:a", false);
+ cmdl.addArgument("/c:\"install.exe /l \"\"${FILE}\"\" /q\"", false);
+
+ int exitValue = exec.execute(cmdl);
+ String result = baos.toString().trim();
+ System.out.println("=== Expected ===");
+ System.out.println(expected);
+ System.out.println("=== Result ===");
+ System.out.println(result);
+ assertFalse(exec.isFailure(exitValue));
+ assertEquals(expected, result);
+ }
+
/**
* Test the patch for EXEC-41 (https://issues.apache.org/jira/browse/EXEC-41).
*
@@ -803,6 +873,48 @@ public class DefaultExecutorTest extends
assertFalse("The watchdog is no longer watching any process", watchdog.isWatching());
}
+ // ======================================================================
+ // === Long running tests
+ // ======================================================================
+
+ /**
+ * Start any processes in a loop to make sure that we do
+ * not leave any handles/resources open.
+ *
+ * @throws Exception the test failed
+ */
+ public void testExecuteStability() throws Exception {
+
+ // make a plain-vanilla test
+ for(int i=0; i<1000; i++) {
+ Map env = new HashMap();
+ env.put("TEST_ENV_VAR", new Integer(i));
+ CommandLine cl = new CommandLine(testScript);
+ int exitValue = exec.execute(cl,env);
+ assertFalse(exec.isFailure(exitValue));
+ assertEquals("FOO." + i + ".", baos.toString().trim());
+ baos.reset();
+ }
+
+ // now be nasty and use the watchdog to kill out sub-processes
+ for(int i=0; i<100; i++) {
+ Map env = new HashMap();
+ env.put("TEST_ENV_VAR", new Integer(i));
+ DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
+ CommandLine cl = new CommandLine(foreverTestScript);
+ ExecuteWatchdog watchdog = new ExecuteWatchdog(500);
+ exec.setWatchdog(watchdog);
+ exec.execute(cl, env, resultHandler);
+ try {
+ resultHandler.waitFor();
+ }
+ catch(ExecuteException e) {
+ // nothing to do
+ }
+ baos.reset();
+ }
+ }
+
// ======================================================================
// === Helper methods