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 2007/11/30 00:24:39 UTC

svn commit: r599627 - in /commons/sandbox/exec/trunk: ./ src/main/java/org/apache/commons/exec/ src/main/java/org/apache/commons/exec/launcher/ src/test/java/org/apache/commons/exec/ src/test/java/org/apache/commons/exec/environment/

Author: sgoeschl
Date: Thu Nov 29 15:24:30 2007
New Revision: 599627

URL: http://svn.apache.org/viewvc?rev=599627&view=rev
Log:
SANDBOX-204 Cleaning up the code, adding more regression tests, fixing broken Cobertura coverage report, fixing javadocs

Modified:
    commons/sandbox/exec/trunk/pom.xml
    commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/CommandLine.java
    commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/ExecuteWatchdog.java
    commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/LogOutputStream.java
    commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/LogStreamHandler.java
    commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/StreamPumper.java
    commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/CommandLauncher.java
    commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/VmsCommandLauncher.java
    commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/CommandLineTest.java
    commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/DefaultExecutorTest.java
    commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/environment/EnvironmentUtilTest.java

Modified: commons/sandbox/exec/trunk/pom.xml
URL: http://svn.apache.org/viewvc/commons/sandbox/exec/trunk/pom.xml?rev=599627&r1=599626&r2=599627&view=diff
==============================================================================
--- commons/sandbox/exec/trunk/pom.xml (original)
+++ commons/sandbox/exec/trunk/pom.xml Thu Nov 29 15:24:30 2007
@@ -15,6 +15,7 @@
       <groupId>commons-logging</groupId>
       <artifactId>commons-logging-api</artifactId>
       <version>1.0.4</version>
+      <scope>compile</scope>
     </dependency>
     <dependency>
       <groupId>junit</groupId>
@@ -70,6 +71,17 @@
       <url>scp://people.apache.org/www/commons.apache.org/sandbox/exec/</url>
     </site>
   </distributionManagement>
+
+  <!-- Avoid broken Cobertura 2.1 plugin -->
+  <reporting>
+    <plugins>
+      <plugin>
+        <groupId>org.codehaus.mojo</groupId>
+        <artifactId>cobertura-maven-plugin</artifactId>
+        <version>2.0</version>
+      </plugin>
+    </plugins>
+  </reporting>
 
   <!-- Compiler source and target JVM (see parent pom) -->
   <properties>

Modified: commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/CommandLine.java
URL: http://svn.apache.org/viewvc/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/CommandLine.java?rev=599627&r1=599626&r2=599627&view=diff
==============================================================================
--- commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/CommandLine.java (original)
+++ commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/CommandLine.java Thu Nov 29 15:24:30 2007
@@ -19,12 +19,7 @@
 package org.apache.commons.exec;
 
 import java.io.File;
-import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.Iterator;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.ListIterator;
 import java.util.StringTokenizer;
 import java.util.Vector;
 
@@ -54,6 +49,7 @@
      * @param line
      *            the line: the first element becomes the executable, the rest
      *            the arguments
+     * @return the parsed command line
      * @throws IllegalArgumentException If line is null or all whitespace
      */
     public static CommandLine parse(final String line) {
@@ -75,6 +71,8 @@
 
     /**
      * Create a command line without any arguments.
+     *
+     * @param executable the executable
      */
     public CommandLine(String executable) {
         setExecutable(executable);
@@ -82,6 +80,8 @@
 
     /**
      * Create a command line without any arguments.
+     *
+     * @param  executable the executable 
      */
     public CommandLine(File executable) {
         setExecutable(executable.getAbsolutePath());
@@ -167,7 +167,9 @@
      * contains double quotes, use single quotes - else surround the argument by
      * double quotes.
      * </p>
-     * 
+     *
+     * @param argument the argument to be quoted
+     * @return the quoted argument
      * @throws IllegalArgumentException If argument contains both types of quotes
      */
     private static String quoteArgument(final String argument) {

Modified: commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/ExecuteWatchdog.java
URL: http://svn.apache.org/viewvc/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/ExecuteWatchdog.java?rev=599627&r1=599626&r2=599627&view=diff
==============================================================================
--- commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/ExecuteWatchdog.java (original)
+++ commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/ExecuteWatchdog.java Thu Nov 29 15:24:30 2007
@@ -33,7 +33,7 @@
  * }
  * </pre>
  * 
- * @see org.apache.commons.exec.Execute
+ * @see org.apache.commons.exec.Executor
  * @see org.apache.commons.exec.Watchdog
  */
 public class ExecuteWatchdog implements TimeoutObserver {
@@ -63,15 +63,6 @@
     public ExecuteWatchdog(final long timeout) {
         watchdog = new Watchdog(timeout);
         watchdog.addTimeoutObserver(this);
-    }
-
-    /**
-     * @see #ExecuteWatchdog(long)
-     * @deprecated Use constructor with a long type instead. (1.4.x
-     *             compatibility)
-     */
-    public ExecuteWatchdog(final int timeout) {
-        this((long) timeout);
     }
 
     /**

Modified: commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/LogOutputStream.java
URL: http://svn.apache.org/viewvc/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/LogOutputStream.java?rev=599627&r1=599626&r2=599627&view=diff
==============================================================================
--- commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/LogOutputStream.java (original)
+++ commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/LogOutputStream.java Thu Nov 29 15:24:30 2007
@@ -31,9 +31,10 @@
  * TODO: This class can be split to implement other line based processing of
  * data written to the stream.
  */
-public class LogOutputStream extends OutputStream {
+public class LogOutputStream
+        extends OutputStream {
 
-    private static Log log = LogFactory.getLog(LogOutputStream.class);
+    private static Log log = LogFactory.getLog( LogOutputStream.class);
 
     /** Initial buffer size. */
     private static final int INTIAL_SIZE = 132;
@@ -110,8 +111,8 @@
     /**
      * Logs a line to the log system of ant.
      * 
-     * @param line
-     *            the line to log.
+     * @param line the line to log.
+     * @param level the log level to use
      */
     protected void processLine(final String line, final int level) {
         log.debug(line);
@@ -127,6 +128,9 @@
         super.close();
     }
 
+    /**
+     * @return the trace level of the log system
+     */
     public int getMessageLevel() {
         return level;
     }

Modified: commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/LogStreamHandler.java
URL: http://svn.apache.org/viewvc/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/LogStreamHandler.java?rev=599627&r1=599626&r2=599627&view=diff
==============================================================================
--- commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/LogStreamHandler.java (original)
+++ commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/LogStreamHandler.java Thu Nov 29 15:24:30 2007
@@ -18,12 +18,14 @@
 
 package org.apache.commons.exec;
 
-import java.io.IOException;
+import org.apache.commons.exec.PumpStreamHandler;
 
 /**
- * Logs standard output and error of a subprocess to the log system of ant.
+ * Logs standard output and error to the system streams.
  */
-public class LogStreamHandler extends PumpStreamHandler {
+public class LogStreamHandler
+        extends PumpStreamHandler
+{
 
     /**
      * Creates log stream handler.
@@ -42,11 +44,5 @@
      */
     public void stop() {
         super.stop();
-        try {
-            getErr().close();
-            getOut().close();
-        } catch (IOException e) {
-            // ignore
-        }
     }
 }

Modified: commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/StreamPumper.java
URL: http://svn.apache.org/viewvc/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/StreamPumper.java?rev=599627&r1=599626&r2=599627&view=diff
==============================================================================
--- commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/StreamPumper.java (original)
+++ commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/StreamPumper.java Thu Nov 29 15:24:30 2007
@@ -30,7 +30,7 @@
     // TODO: make SIZE an instance variable.
     // TODO: add a status flag to note if an error occurred in run.
 
-    private static final int SIZE = 128;
+    private static final int SIZE = 1024;
 
     private InputStream is;
 

Modified: commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/CommandLauncher.java
URL: http://svn.apache.org/viewvc/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/CommandLauncher.java?rev=599627&r1=599626&r2=599627&view=diff
==============================================================================
--- commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/CommandLauncher.java (original)
+++ commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/CommandLauncher.java Thu Nov 29 15:24:30 2007
@@ -23,7 +23,6 @@
 import java.util.Map;
 
 import org.apache.commons.exec.CommandLine;
-import org.apache.commons.exec.environment.EnvironmentUtil;
 
 public interface CommandLauncher {
 

Modified: commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/VmsCommandLauncher.java
URL: http://svn.apache.org/viewvc/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/VmsCommandLauncher.java?rev=599627&r1=599626&r2=599627&view=diff
==============================================================================
--- commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/VmsCommandLauncher.java (original)
+++ commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/VmsCommandLauncher.java Thu Nov 29 15:24:30 2007
@@ -24,8 +24,8 @@
 import java.io.PrintWriter;
 import java.util.Iterator;
 import java.util.Map;
-import java.util.Set;
 import java.util.Map.Entry;
+import java.util.Set;
 
 import org.apache.commons.exec.CommandLine;
 

Modified: commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/CommandLineTest.java
URL: http://svn.apache.org/viewvc/commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/CommandLineTest.java?rev=599627&r1=599626&r2=599627&view=diff
==============================================================================
--- commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/CommandLineTest.java (original)
+++ commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/CommandLineTest.java Thu Nov 29 15:24:30 2007
@@ -35,6 +35,8 @@
         CommandLine cmdl = new CommandLine("test");
         assertEquals("test", cmdl.toString());
         assertEquals(new String[] {"test"}, cmdl.toStrings());
+        assertEquals("test", cmdl.getExecutable());
+        assertTrue(cmdl.getArguments().length == 0);
     }
 
     public void testExecutableZeroLengthString() {

Modified: commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/DefaultExecutorTest.java
URL: http://svn.apache.org/viewvc/commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/DefaultExecutorTest.java?rev=599627&r1=599626&r2=599627&view=diff
==============================================================================
--- commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/DefaultExecutorTest.java (original)
+++ commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/DefaultExecutorTest.java Thu Nov 29 15:24:30 2007
@@ -20,6 +20,7 @@
 
 import java.io.ByteArrayOutputStream;
 import java.io.File;
+import java.io.IOException;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -27,18 +28,24 @@
 
 public class DefaultExecutorTest extends TestCase {
 
-    
     private Executor exec = new DefaultExecutor();
     private File testDir = new File("src/test/scripts");
     private ByteArrayOutputStream baos;
     private File testScript = TestUtil.resolveScriptForOS(testDir + "/test");
     private File errorTestScript = TestUtil.resolveScriptForOS(testDir + "/error");
+    private File foreverTestScript = TestUtil.resolveScriptForOS(testDir + "/forever");
+    private File nonExistingTestScript = TestUtil.resolveScriptForOS(testDir + "/grmpffffff");
+
 
     protected void setUp() throws Exception {
         baos = new ByteArrayOutputStream();
         exec.setStreamHandler(new PumpStreamHandler(baos, baos));
     }
 
+    protected void tearDown() throws Exception {
+        baos.close();
+    }
+
     public void testExecute() throws Exception {
         CommandLine cl = new CommandLine(testScript);
 
@@ -47,6 +54,29 @@
         assertEquals(0, exitValue);
     }
 
+    public void testExecuteWithWorkingDirectory() throws Exception {
+        File workingDir = new File(".");
+        CommandLine cl = new CommandLine(testScript);
+        exec.setWorkingDirectory(new File("."));
+        int exitValue = exec.execute(cl);
+        assertEquals("FOO..", baos.toString().trim());
+        assertEquals(0, exitValue);
+        assertEquals(exec.getWorkingDirectory(), workingDir);
+    }
+
+    public void testExecuteWithInvalidWorkingDirectory() throws Exception {
+        File workingDir = new File("/foo/bar");
+        CommandLine cl = new CommandLine(testScript);
+        exec.setWorkingDirectory(workingDir);
+        try {
+            exec.execute(cl);
+            fail("Expected exception due to invalid working directory");
+        }
+        catch(IOException e) {
+            return;
+        }
+    }
+
     public void testExecuteWithError() throws Exception {
         CommandLine cl = new CommandLine(errorTestScript);
         
@@ -107,10 +137,103 @@
         assertTrue(handler.getException() instanceof ExecuteException);
         assertEquals("FOO..", baos.toString().trim());
     }
-    
-    
 
-    protected void tearDown() throws Exception {
-        baos.close();
+    /**
+     * Start a scipt looping forever and check if the ExecuteWatchdog
+     * kicks in killing the run away process.
+     */
+    public void testExecuteWatchdog() throws Exception {
+        long timeout = 10000;
+        CommandLine cl = new CommandLine(foreverTestScript);
+        DefaultExecutor executor = new DefaultExecutor();
+        executor.setWorkingDirectory(new File("."));
+        ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout);
+        executor.setWatchdog(watchdog);
+        try {
+            executor.execute(cl);
+        }
+        catch(ExecuteException e) {
+            assertTrue( executor.getWatchdog().killedProcess() );
+            return;
+        }
+        catch(Throwable t) {
+            fail(t.getMessage());    
+        }
+
+        fail("Process was not killed");
     }
+
+    /**
+     * Try to start an non-existing application which should result
+     * in an exception.
+     */
+    public void testExecuteNonExistingApplication() throws Exception {
+        CommandLine cl = new CommandLine(nonExistingTestScript);
+        DefaultExecutor executor = new DefaultExecutor();
+        try {
+            executor.execute(cl);
+        }
+        catch( IOException e) {
+            // expected
+            return;
+        }
+        fail("Got no exception when executing an non-existing application");                
+    }
+
+    /**
+     * Try to start an non-existing application asynchronously which should result
+     * in an exception.
+     */
+    public void testExecuteAsyncWithNonExistingApplication() throws Exception {
+        CommandLine cl = new CommandLine(nonExistingTestScript);
+        MockExecuteResultHandler handler = new MockExecuteResultHandler();
+        exec.execute(cl, handler);
+        Thread.sleep(2000);
+        assertEquals(Executor.INVALID_EXITVALUE, handler.getExitValue());
+        assertTrue(handler.getException() instanceof ExecuteException);
+    }
+
+    /**
+     * Start any processes in a loop to make sure that we do
+     * not leave any handles/resources open.
+     */
+    public void testExecuteStability() throws Exception {
+        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);
+            assertEquals(0, exitValue);
+            assertEquals("FOO." + i + ".", baos.toString().trim());
+            baos.reset();
+        }
+    }
+
+    /**
+     * Invoke the error script but define that "1" is a good
+     * exit value and therefore no exception should be thrown.
+     */
+    public void testExecuteWithCustomExitValue1() throws Exception {
+        // consider "1" s good exit value
+        exec.setExitValue(1);
+        CommandLine cl = new CommandLine(errorTestScript);
+        exec.execute(cl);
+    }
+
+    /**
+     * Invoke the error script but define that "1" is a bad
+     * exit value and therefore no exception should be thrown.
+     */
+    public void testExecuteWithCustomExitValue2() throws Exception {
+        // consider "2" s good exit value and fail
+        CommandLine cl = new CommandLine(errorTestScript);
+        exec.setExitValue(2);
+        try{
+            exec.execute(cl);
+            fail("Must throw ExecuteException");
+        } catch(ExecuteException e) {
+            assertEquals(1, e.getExitValue());
+            return;
+        }
+    }    
 }

Modified: commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/environment/EnvironmentUtilTest.java
URL: http://svn.apache.org/viewvc/commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/environment/EnvironmentUtilTest.java?rev=599627&r1=599626&r2=599627&view=diff
==============================================================================
--- commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/environment/EnvironmentUtilTest.java (original)
+++ commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/environment/EnvironmentUtilTest.java Thu Nov 29 15:24:30 2007
@@ -41,6 +41,18 @@
         
         TestUtil.assertEquals(expected, envStrings, false);
     }
-    
 
+    /**
+     * Test to access the environment variables of the current
+     * process.
+     */
+    public void testGetProcEnvironment() throws IOException {
+        Map procEnvironment = EnvironmentUtil.getProcEnvironment();
+        assertTrue(procEnvironment.size() > 0);
+        String[] envArgs = EnvironmentUtil.toStrings(procEnvironment);
+        for(int i=0; i<envArgs.length; i++) {
+            assertNotNull(envArgs[i]);
+            assertTrue(envArgs[i].length() > 0);
+        }
+    }
 }