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/11/14 01:04:45 UTC
svn commit: r713875 - in /commons/proper/exec/trunk/src:
main/java/org/apache/commons/exec/CommandLine.java
test/java/org/apache/commons/exec/CommandLineTest.java
Author: sgoeschl
Date: Thu Nov 13 16:04:44 2008
New Revision: 713875
URL: http://svn.apache.org/viewvc?rev=713875&view=rev
Log:
Providing a more correct toString() representation which does quoting
Modified:
commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/CommandLine.java
commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/CommandLineTest.java
Modified: commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/CommandLine.java
URL: http://svn.apache.org/viewvc/commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/CommandLine.java?rev=713875&r1=713874&r2=713875&view=diff
==============================================================================
--- commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/CommandLine.java (original)
+++ commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/CommandLine.java Thu Nov 13 16:04:44 2008
@@ -248,8 +248,7 @@
}
/**
- * Returns the command line as an array of strings, correctly quoted
- * for use in executing the command.
+ * Returns the command line as an array of strings.
*
* @return The command line as an string array
*/
@@ -262,11 +261,32 @@
/**
* Stringify operator returns the command line as a string.
+ * Parameters are correctly quoted when containing a space or
+ * left untouched if the are already quoted.
*
- * @return the command line
+ * @return the command line as single string
*/
public String toString() {
- return StringUtils.toString(toStrings(), " ");
+ StringBuffer result = new StringBuffer();
+ String[] currArguments = this.getArguments();
+
+ result.append(StringUtils.quoteArgument(this.getExecutable()));
+ result.append(' ');
+
+ for(int i=0; i<currArguments.length; i++) {
+ String currArgument = currArguments[i];
+ if( StringUtils.isQuoted(currArgument)) {
+ result.append(currArgument);
+ }
+ else {
+ result.append(StringUtils.quoteArgument(currArgument));
+ }
+ if(i<currArguments.length-1) {
+ result.append(' ');
+ }
+ }
+
+ return result.toString().trim();
}
// --- Implementation ---------------------------------------------------
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=713875&r1=713874&r2=713875&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 Thu Nov 13 16:04:44 2008
@@ -366,4 +366,26 @@
assertEquals("foo.bar.Main", result[2]);
assertEquals("C:\\Document And Settings\\documents\\432432.pdf", result[3]);
}
+
+ /**
+ * Test the toString() method
+ */
+ public void testToString() throws Exception {
+ CommandLine cmdl;
+ HashMap params = new HashMap();
+
+ // use no arguments
+ cmdl = CommandLine.parse("AcroRd32.exe", params);
+ assertEquals(cmdl.toString(), "AcroRd32.exe");
+
+ // use an argument containing spaces
+ params.put("file", "C:\\Document And Settings\\documents\\432432.pdf");
+ cmdl = CommandLine.parse("AcroRd32.exe /p /h '${file}'", params);
+ assertEquals(cmdl.toString(), "AcroRd32.exe /p /h \"C:\\Document And Settings\\documents\\432432.pdf\"");
+
+ // use an argument without spaces
+ params.put("file", "C:\\documents\\432432.pdf");
+ cmdl = CommandLine.parse("AcroRd32.exe /p /h '${file}'", params);
+ assertEquals(cmdl.toString(), "AcroRd32.exe /p /h C:\\documents\\432432.pdf");
+ }
}