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 2008/04/21 22:32:45 UTC

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

Author: sgoeschl
Date: Mon Apr 21 13:32:43 2008
New Revision: 650249

URL: http://svn.apache.org/viewvc?rev=650249&view=rev
Log:
Improve the implementation and regression tests to be able to pass the tests on OpenVMS

Modified:
    commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/DefaultExecutor.java
    commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/Executor.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/CommandLauncherImpl.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/DefaultExecutorTest.java

Modified: commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/DefaultExecutor.java
URL: http://svn.apache.org/viewvc/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/DefaultExecutor.java?rev=650249&r1=650248&r2=650249&view=diff
==============================================================================
--- commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/DefaultExecutor.java (original)
+++ commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/DefaultExecutor.java Mon Apr 21 13:32:43 2008
@@ -192,47 +192,34 @@
     }
 
 
-    /*
-     * Define the exit code of the process to considered
-     * successful.
-     */
-    public void setExitValue( int value ) {
+    /** @see org.apache.commons.exec.Executor#setExitValue(int) */
+    public void setExitValue(final int value) {
         this.setExitValues(new int[] {value});
     }
 
 
-    /*
-     * Define the exist code of the process to considered
-     * successful.
-     */
-    public void setExitValues( int[] values ) {
+    /** @see org.apache.commons.exec.Executor#setExitValues(int[]) */
+    public void setExitValues(final int[] values) {
         this.exitValues = values;
     }
 
-    /**
-     * Checks whether <code>exitValue</code> signals a failure on the current
-     * system (OS specific).
-     * <p>
-     * <b>Note</b> that this method relies on the conventions of the OS, it
-     * will return false results if the application you are running doesn't
-     * follow these conventions. One notable exception is the Java VM provided
-     * by HP for OpenVMS - it will return 0 if successful (like on any other
-     * platform), but this signals a failure on OpenVMS. So if you execute a new
-     * Java VM on OpenVMS, you cannot trust this method.
-     * </p>
-     * 
-     * @param exitValue
-     *            the exit value (return code) to be checked
-     * @return <code>true</code> if <code>exitValue</code> signals a failure
-     */
-    public static boolean isFailure(final int exitValue) {
-        if (OS.isFamilyOpenVms()) {
-            // even exit value signals failure
-            return (exitValue % 2) == 0;
-        } else {
-            // non zero exit value signals failure
-            return exitValue != 0;
+    /** @see org.apache.commons.exec.Executor#isFailure(int) */
+    public boolean isFailure(final int exitValue) {
+
+        if(this.exitValues == null) {
+            return false;
+        }
+        else if(this.exitValues.length == 0) {
+            return this.launcher.isFailure(exitValue);
+        }
+        else {
+            for(int i=0; i<this.exitValues.length; i++) {
+                if(this.exitValues[i] == exitValue) {
+                    return false;
+                }
+            }
         }
+        return true;
     }
 
     /**
@@ -358,7 +345,7 @@
                 }
             }
 
-            if(!this.isSuccess(exitValue)) {
+            if(this.isFailure(exitValue)) {
                 throw new ExecuteException("Process exited with an error: " + exitValue, exitValue);
             }
 
@@ -369,23 +356,5 @@
               this.getProcessDestroyer().remove(process);
             }
         }
-    }
-
-    private boolean isSuccess(final int exitValue) {
-
-        if(this.exitValues == null) {
-            return true;
-        }
-        else if(this.exitValues.length == 0) {
-            return !isFailure(exitValue);
-        }
-        else {
-            for(int i=0; i<this.exitValues.length; i++) {
-                if(this.exitValues[i] == exitValue) {
-                    return true;
-                }
-            }
-        }
-        return false;
     }
 }

Modified: commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/Executor.java
URL: http://svn.apache.org/viewvc/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/Executor.java?rev=650249&r1=650248&r2=650249&view=diff
==============================================================================
--- commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/Executor.java (original)
+++ commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/Executor.java Mon Apr 21 13:32:43 2008
@@ -25,13 +25,13 @@
 public interface Executor {
 
     /** Invalid exit code. * */
-    int INVALID_EXITVALUE = Integer.MAX_VALUE;
+    int INVALID_EXITVALUE = 0xdeadbeef;
 
     /*
      * Define the exit code of the process to considered
      * successful.
      */
-    void setExitValue(int value);
+    void setExitValue(final int value);
 
     /*
      * Define the exit code of the process to considered
@@ -42,7 +42,17 @@
      *  <li>null to indicate to skip checking of exit codes</li>
      * </ul>
      */
-    void setExitValues(int[] values);
+    void setExitValues(final int[] values);
+
+    /**
+     * Checks whether <code>exitValue</code> signals a failure. If no
+     * exit values are set than the default conventions of the OS is
+     * used. 
+     *
+     * @param exitValue the exit value (return code) to be checked
+     * @return <code>true</code> if <code>exitValue</code> signals a failure
+     */
+    boolean isFailure(final int exitValue);
 
     /*
      * StreamHandlers are used for providing input, 

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=650249&r1=650248&r2=650249&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 Mon Apr 21 13:32:43 2008
@@ -61,4 +61,22 @@
      */
     Process exec(final CommandLine cmd, final Map env,
             final File workingDir) throws IOException;
+
+
+    /**
+     * Checks whether <code>exitValue</code> signals a failure on the current
+     * system (OS specific).
+     * <p>
+     * <b>Note</b> that this method relies on the conventions of the OS, it
+     * will return false results if the application you are running doesn't
+     * follow these conventions. One notable exception is the Java VM provided
+     * by HP for OpenVMS - it will return 0 if successful (like on any other
+     * platform), but this signals a failure on OpenVMS. So if you execute a new
+     * Java VM on OpenVMS, you cannot trust this method.
+     * </p>
+     *
+     * @param exitValue the exit value (return code) to be checked
+     * @return <code>true</code> if <code>exitValue</code> signals a failure
+     */
+    boolean isFailure(final int exitValue);
 }

Modified: commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/CommandLauncherImpl.java
URL: http://svn.apache.org/viewvc/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/CommandLauncherImpl.java?rev=650249&r1=650248&r2=650249&view=diff
==============================================================================
--- commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/CommandLauncherImpl.java (original)
+++ commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/CommandLauncherImpl.java Mon Apr 21 13:32:43 2008
@@ -45,4 +45,11 @@
 
     public abstract Process exec(final CommandLine cmd, final Map env,
             final File workingDir) throws IOException;
+
+    /** @see org.apache.commons.exec.launcher.CommandLauncher#isFailure(int) */    
+    public boolean isFailure(final int exitValue)
+    {
+        // non zero exit value signals failure
+        return exitValue != 0;
+    }
 }

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=650249&r1=650248&r2=650249&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 Mon Apr 21 13:32:43 2008
@@ -64,6 +64,13 @@
         return super.exec(vmsCmd, env, workingDir);
     }
 
+    /** @see org.apache.commons.exec.launcher.CommandLauncher#isFailure(int) */
+    public boolean isFailure( final int exitValue )
+    {
+        // even exit value signals failure
+        return (exitValue % 2) == 0;        
+    }
+
     /*
      * Writes the command into a temporary DCL script and returns the
      * corresponding File object. The script will be deleted on exit.

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=650249&r1=650248&r2=650249&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 Mon Apr 21 13:32:43 2008
@@ -51,7 +51,7 @@
 
         int exitValue = exec.execute(cl);
         assertEquals("FOO..", baos.toString().trim());
-        assertEquals(0, exitValue);
+        assertFalse(exec.isFailure(exitValue));
     }
 
     public void testExecuteWithWorkingDirectory() throws Exception {
@@ -60,7 +60,7 @@
         exec.setWorkingDirectory(new File("."));
         int exitValue = exec.execute(cl);
         assertEquals("FOO..", baos.toString().trim());
-        assertEquals(0, exitValue);
+        assertFalse(exec.isFailure(exitValue));
         assertEquals(exec.getWorkingDirectory(), workingDir);
     }
 
@@ -84,7 +84,7 @@
             exec.execute(cl);
             fail("Must throw ExecuteException");
         } catch(ExecuteException e) {
-            assertEquals(1, e.getExitValue());
+            assertTrue(exec.isFailure(e.getExitValue()));
         }
     }
 
@@ -94,7 +94,7 @@
         int exitValue = exec.execute(cl);
 
         assertEquals("FOO..BAR", baos.toString().trim());
-        assertEquals(0, exitValue);
+        assertFalse(exec.isFailure(exitValue));
     }
 
     public void testExecuteWithEnv() throws Exception {
@@ -106,7 +106,7 @@
         int exitValue = exec.execute(cl, env);
 
         assertEquals("FOO.XYZ.", baos.toString().trim());
-        assertEquals(0, exitValue);
+        assertFalse(exec.isFailure(exitValue));
     }
 
     public void testExecuteAsync() throws Exception {
@@ -119,7 +119,7 @@
         // wait for script to run
         Thread.sleep(2000);
         
-        assertEquals(0, handler.getExitValue());
+        assertFalse(exec.isFailure(handler.getExitValue()));
         assertEquals("FOO..", baos.toString().trim());
     }
 
@@ -133,7 +133,7 @@
         // wait for script to run
         Thread.sleep(2000);
         
-        assertEquals(1, handler.getExitValue());
+        assertTrue(exec.isFailure(handler.getExitValue()));
         assertTrue(handler.getException() instanceof ExecuteException);
         assertEquals("FOO..", baos.toString().trim());
     }
@@ -237,7 +237,7 @@
             env.put("TEST_ENV_VAR", new Integer(i));
             CommandLine cl = new CommandLine(testScript);
             int exitValue = exec.execute(cl,env);
-            assertEquals(0, exitValue);
+            assertFalse(exec.isFailure(exitValue));
             assertEquals("FOO." + i + ".", baos.toString().trim());
             baos.reset();
         }
@@ -266,7 +266,7 @@
             exec.execute(cl);
             fail("Must throw ExecuteException");
         } catch(ExecuteException e) {
-            assertEquals(1, e.getExitValue());
+            assertTrue(exec.isFailure(e.getExitValue()));
             return;
         }
     }
@@ -286,7 +286,7 @@
       int exitValue = exec.execute(cl);
 
       assertEquals("FOO..", baos.toString().trim());
-      assertEquals(0, exitValue);
+      assertFalse(exec.isFailure(exitValue));
       assertTrue(processDestroyer.size() == 0);
       assertTrue(processDestroyer.isAddedAsShutdownHook() == false);
     }