commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sgoes...@apache.org
Subject svn commit: r601109 - in /commons/sandbox/exec/trunk/src: main/java/org/apache/commons/exec/CommandLine.java test/java/org/apache/commons/exec/CommandLineTest.java
Date Tue, 04 Dec 2007 22:15:43 GMT
Author: sgoeschl
Date: Tue Dec  4 14:15:42 2007
New Revision: 601109

URL: http://svn.apache.org/viewvc?rev=601109&view=rev
Log:
SANDBOX-192 Added methods to a pre-quoted arguments to the CommandLine plus a few regression
tests.

Modified:
    commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/CommandLine.java
    commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/CommandLineTest.java

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=601109&r1=601108&r2=601109&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 Tue
Dec  4 14:15:42 2007
@@ -87,17 +87,6 @@
         setExecutable(executable.getAbsolutePath());
     }
 
-    private void setExecutable(final String executable) {
-        if (executable == null) {
-            throw new IllegalArgumentException("Executable can not be null");
-        } else if(executable.trim().length() == 0) {
-            throw new IllegalArgumentException("Executable can not be empty");
-        } else {
-             this.executable = executable.replace('/', File.separatorChar).replace(
-                '\\', File.separatorChar);
-        }
-    }
-
     /**
      * Returns the executable
      * 
@@ -108,18 +97,29 @@
     }
 
     /**
-     * Add multiple arguments
+     * Add multiple arguments. Handles parsing of quotes and whitespace.
      * 
      * @param arguments An array of arguments
      * @return The command line itself
      */
     public CommandLine addArguments(final String[] arguments) {
+        return this.addArguments(arguments, true);
+    }
+
+    /**
+     * Add multiple arguments.
+     *
+     * @param arguments An array of arguments
+     * @param handleQuoting Add the argument with/without handling quoting
+     * @return The command line itself
+     */
+    public CommandLine addArguments(final String[] arguments, boolean handleQuoting) {
         if (arguments != null) {
             for (int i = 0; i < arguments.length; i++) {
-                addArgument(arguments[i]);
+                addArgument(arguments[i], handleQuoting);
             }
         }
-        
+
         return this;
     }
 
@@ -130,27 +130,56 @@
      * @return The command line itself
      */
     public CommandLine addArguments(final String arguments) {
+        return this.addArguments(arguments, true);
+    }
+
+    /**
+     * Add multiple arguments. Handles parsing of quotes and whitespace.
+     *
+     * @param arguments An string containing multiple arguments.
+     * @param handleQuoting Add the argument with/without handling quoting
+     * @return The command line itself
+     */
+    public CommandLine addArguments(final String arguments, boolean handleQuoting) {
         if (arguments != null) {
             String[] argmentsArray = translateCommandline(arguments);
-    
-            addArguments(argmentsArray);
+            addArguments(argmentsArray, handleQuoting);
         }
-        
+
         return this;
     }
 
     /**
      * Add a single argument. Handles quoting.
      * @param argument The argument to add
+     * @return The command line itself
      * @throws IllegalArgumentException If argument contains both single and double quotes
      */
-    public void addArgument(final String argument) {
-        if (argument == null)
-            return;
-
-        arguments.add(quoteArgument(argument));
+    public CommandLine addArgument(final String argument) {
+        return this.addArgument(argument, true);
     }
 
+   /**
+    * Add a single argument.
+    * @param argument The argument to add
+    * @param handleQuoting Add the argument with/without handling quoting
+    * @return The command line itself
+    */
+   public CommandLine addArgument(final String argument, boolean handleQuoting) {
+        if (argument == null) {
+           return this;
+        }
+
+        if(handleQuoting) {
+            arguments.add(quoteArgument(argument));
+        }
+        else {
+            arguments.add(argument);
+        }
+
+        return this;
+   }
+
     /**
      * Returns the quoted arguments 
      * @return The quoted arguments
@@ -160,6 +189,8 @@
         return (String[]) arguments.toArray(res);
     }
 
+    // --- Implementation ---------------------------------------------------
+
     /**
      * Put quotes around the given String if necessary.
      * <p>
@@ -201,9 +232,49 @@
         }
     }
 
+
     /**
-     * Crack a command line.
+     * Returns the command line as an array of strings, correctly quoted
+     * for use in executing the command.
+     * @return The command line as an string array
+     */
+    public String[] toStrings() {
+        final String[] result = new String[arguments.size() + 1];
+        result[0] = executable;
+
+        int index = 1;
+        for (Iterator iter = arguments.iterator(); iter.hasNext();) {
+            result[index] = (String) iter.next();
+
+            index++;
+        }
+
+        return result;
+    }
+
+    /**
+     * Stringify operator returns the command line as a string.
      * 
+     * @return the command line
+     */
+    public String toString() {
+        String[] strings = toStrings();
+
+        StringBuffer sb = new StringBuffer();
+
+        for (int i = 0; i < strings.length; i++) {
+            if (i > 0) {
+                sb.append(' ');
+            }
+            sb.append(strings[i]);
+        }
+
+        return sb.toString();
+    }
+
+    /**
+     * Crack a command line.
+     *
      * @param toProcess
      *            the command line to process
      * @return the command line broken into strings. An empty or null toProcess
@@ -277,42 +348,15 @@
         return args;
     }
 
-    /**
-     * Returns the command line as an array of strings, correctly quoted
-     * for use in executing the command.
-     * @return The command line as an string array
-     */
-    public String[] toStrings() {
-        final String[] result = new String[arguments.size() + 1];
-        result[0] = executable;
-
-        int index = 1;
-        for (Iterator iter = arguments.iterator(); iter.hasNext();) {
-            result[index] = (String) iter.next();
-
-            index++;
+    private void setExecutable(final String executable) {
+        if (executable == null) {
+            throw new IllegalArgumentException("Executable can not be null");
+        } else if(executable.trim().length() == 0) {
+            throw new IllegalArgumentException("Executable can not be empty");
+        } else {
+             this.executable = executable.replace('/', File.separatorChar).replace(
+                '\\', File.separatorChar);
         }
-
-        return result;
     }
 
-    /**
-     * Stringify operator returns the command line as a string.
-     * 
-     * @return the command line
-     */
-    public String toString() {
-        String[] strings = toStrings();
-
-        StringBuffer sb = new StringBuffer();
-
-        for (int i = 0; i < strings.length; i++) {
-            if (i > 0) {
-                sb.append(' ');
-            }
-            sb.append(strings[i]);
-        }
-
-        return sb.toString();
-    }
 }

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=601109&r1=601108&r2=601109&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
Tue Dec  4 14:15:42 2007
@@ -203,4 +203,43 @@
             // Expected
         }
     }
+
+   /**
+    * Create a command line with pre-quoted strings to test SANDBOX-192,
+    * e.g. "runMemorySud.cmd", "10", "30", "-XX:+UseParallelGC", "\"-XX:ParallelGCThreads=2\""
+    */
+    public void testComplexAddArgument() {
+        CommandLine cmdl = new CommandLine("runMemorySud.cmd");
+        cmdl.addArgument("10", false);
+        cmdl.addArgument("30", false);
+        cmdl.addArgument("-XX:+UseParallelGC", false);
+        cmdl.addArgument("\"-XX:ParallelGCThreads=2\"", false);
+        assertEquals("runMemorySud.cmd 10 30 -XX:+UseParallelGC \"-XX:ParallelGCThreads=2\"",
cmdl.toString());
+        assertEquals(new String[] {"runMemorySud.cmd", "10", "30", "-XX:+UseParallelGC",
"\"-XX:ParallelGCThreads=2\""}, cmdl.toStrings());
+    }
+
+    /**
+     * Create a command line with pre-quoted strings to test SANDBOX-192,
+     * e.g. "runMemorySud.cmd", "10", "30", "-XX:+UseParallelGC", "\"-XX:ParallelGCThreads=2\""
+     */
+     public void testComplexAddArguments1() {
+         CommandLine cmdl = new CommandLine("runMemorySud.cmd");
+         cmdl.addArguments(new String[] {"10", "30", "-XX:+UseParallelGC", "\"-XX:ParallelGCThreads=2\""},
false);
+         assertEquals("runMemorySud.cmd 10 30 -XX:+UseParallelGC \"-XX:ParallelGCThreads=2\"",
cmdl.toString());
+         assertEquals(new String[] {"runMemorySud.cmd", "10", "30", "-XX:+UseParallelGC",
"\"-XX:ParallelGCThreads=2\""}, cmdl.toStrings());
+     }
+
+    /**
+     * Create a command line with pre-quoted strings to test SANDBOX-192,
+     * e.g. "runMemorySud.cmd", "10", "30", "-XX:+UseParallelGC", "\"-XX:ParallelGCThreads=2\""
+     * Please not that we re forced to add additional single quotes to get the test working
-
+     * don't know if this is a bug or a feature.
+     */
+     public void testComplexAddArguments2() {
+         CommandLine cmdl = new CommandLine("runMemorySud.cmd");
+         cmdl.addArguments("10 30 -XX:+UseParallelGC '\"-XX:ParallelGCThreads=2\"'", false);
+         assertEquals("runMemorySud.cmd 10 30 -XX:+UseParallelGC \"-XX:ParallelGCThreads=2\"",
cmdl.toString());
+         assertEquals(new String[] {"runMemorySud.cmd", "10", "30", "-XX:+UseParallelGC",
"\"-XX:ParallelGCThreads=2\""}, cmdl.toStrings());
+     }
+
 }



Mime
View raw message