You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ng...@apache.org on 2006/03/12 13:08:01 UTC
svn commit: r385278 - in /jakarta/commons/sandbox/exec/trunk/src:
main/java/org/apache/commons/exec/
main/java/org/apache/commons/exec/environment/
main/java/org/apache/commons/exec/launcher/
test/java/org/apache/commons/exec/
Author: ngn
Date: Sun Mar 12 04:07:59 2006
New Revision: 385278
URL: http://svn.apache.org/viewcvs?rev=385278&view=rev
Log:
Simplifying the CommandLine class. Now failing early when arguments are incorrect.
Adding more tests for CommandLine
Removed:
jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/CommandLineArgument.java
Modified:
jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/CommandLine.java
jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/Exec.java
jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/environment/DefaultProcessingEnvironment.java
jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/environment/OpenVmsProcessingEnvironment.java
jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/CommandLauncherImpl.java
jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/Java13CommandLauncher.java
jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/OS2CommandLauncher.java
jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/VmsCommandLauncher.java
jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/WinNTCommandLauncher.java
jakarta/commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/CommandLineTest.java
jakarta/commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/ExecTest.java
Modified: jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/CommandLine.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/CommandLine.java?rev=385278&r1=385277&r2=385278&view=diff
==============================================================================
--- jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/CommandLine.java (original)
+++ jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/CommandLine.java Sun Mar 12 04:07:59 2006
@@ -19,6 +19,8 @@
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;
@@ -27,15 +29,14 @@
/**
* CommandLine objects help handling command lines specifying processes to
- * execute. The class can be used to define a command line as nested elements or
- * as a helper to define a command line by an application.
+ * execute. The class can be used to a command line by an application.
*/
public class CommandLine {
private static final String SINGLE_QUOTE = "\'";
private static final String DOUBLE_QUOTE = "\"";
-
+
/**
* The arguments of the command.
*/
@@ -49,117 +50,106 @@
/**
* Create a command line from a string.
*
- * @param toProcess
+ * @param line
* the line: the first element becomes the executable, the rest
* the arguments
+ * @throws IllegalArgumentException If line is null or all whitespace
*/
- public CommandLine(final String toProcess) {
- super();
- String[] tmp = translateCommandline(toProcess);
- if (tmp != null && tmp.length > 0) {
- setExecutable(tmp[0]);
+ public static CommandLine parse(final String line) {
+ if (line == null) {
+ throw new IllegalArgumentException("Command line can not be null");
+ } else if (line.trim().length() == 0) {
+ throw new IllegalArgumentException("Command line can not be empty");
+ } else {
+ String[] tmp = translateCommandline(line);
+
+ CommandLine cl = new CommandLine(tmp[0]);
for (int i = 1; i < tmp.length; i++) {
- createArgument(tmp[i]);
+ cl.addArgument(tmp[i]);
}
- }
- }
- /**
- * Create an empty command line.
- */
- public CommandLine() {
- super();
+ return cl;
+ }
}
/**
- * Creates an argument object.
- * <p>
- * Each commandline object has at most one instance of the argument class.
- * This method calls <code>this.createArgument(false)</code>.
- * </p>
- *
- * @return the argument object.
+ * Create a command line without any arguments.
*/
- private CommandLineArgument createArgument(final String value) {
- CommandLineArgument argument = new CommandLineArgument(value);
- arguments.addElement(argument);
- return argument;
+ public CommandLine(String executable) {
+ setExecutable(executable);
}
- public void setExecutable(final String executable) {
- if (executable == null || executable.length() == 0) {
- return;
- }
- this.executable = executable.replace('/', File.separatorChar).replace(
+ 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
+ *
+ * @return The executable
+ */
public String getExecutable() {
return executable;
}
- public void addArguments(final String[] line) {
- for (int i = 0; i < line.length; i++) {
- createArgument(line[i]);
+ /**
+ * Add multiple arguments
+ *
+ * @param arguments An array of arguments
+ * @return The command line itself
+ */
+ public CommandLine addArguments(final String[] arguments) {
+ if (arguments != null) {
+ for (int i = 0; i < arguments.length; i++) {
+ addArgument(arguments[i]);
+ }
}
- }
-
- public void addArgument(final String arg) {
- createArgument(arg);
- }
-
- public String[] getCommandline() {
- List commands = new LinkedList();
- ListIterator list = commands.listIterator();
- addCommandToList(list);
- final String[] result = new String[commands.size()];
- return (String[]) commands.toArray(result);
+
+ return this;
}
/**
- * Add the entire command, including (optional) executable to a list.
+ * Add multiple arguments. Handles parsing of quotes and whitespace.
*
- * @param list
+ * @param arguments An string containing multiple arguments.
+ * @return The command line itself
*/
- private void addCommandToList(final ListIterator list) {
- if (executable != null) {
- list.add(executable);
+ public CommandLine addArguments(final String arguments) {
+ if (arguments != null) {
+ String[] argmentsArray = translateCommandline(arguments);
+
+ addArguments(argmentsArray);
}
- addArgumentsToList(list);
- }
-
- public String[] getArguments() {
- List result = new ArrayList(arguments.size() * 2);
- addArgumentsToList(result.listIterator());
- String[] res = new String[result.size()];
- return (String[]) result.toArray(res);
+
+ return this;
}
/**
- * append all the arguments to the tail of a supplied list
- *
- * @param list
+ * Add a single argument. Handles quoting.
+ * @param argument The argument to add
+ * @throws IllegalArgumentException If argument contains both single and double quotes
*/
- private void addArgumentsToList(final ListIterator list) {
- for (int i = 0; i < arguments.size(); i++) {
- CommandLineArgument arg = (CommandLineArgument) arguments
- .elementAt(i);
- String[] s = arg.getParts();
- if (s != null) {
- for (int j = 0; j < s.length; j++) {
- list.add(s[j]);
- }
- }
- }
+ public void addArgument(final String argument) {
+ if (argument == null)
+ return;
+
+ arguments.add(quoteArgument(argument));
}
/**
- * Stringify operator returns the command line as a string.
- *
- * @return the command line
+ * Returns the quoted arguments
+ * @return The quoted arguments
*/
- public String toString() {
- return toString(getCommandline());
+ public String[] getArguments() {
+ String[] res = new String[arguments.size()];
+ return (String[]) arguments.toArray(res);
}
/**
@@ -170,48 +160,38 @@
* double quotes.
* </p>
*
+ * @throws IllegalArgumentException If argument contains both types of quotes
*/
- public static String quoteArgument(final String argument) {
+ private static String quoteArgument(final String argument) {
+ String cleanedArgument = argument.trim();
+
+ while(cleanedArgument.startsWith(SINGLE_QUOTE) || cleanedArgument.startsWith(DOUBLE_QUOTE)) {
+ cleanedArgument = cleanedArgument.substring(1);
+ }
+ while(cleanedArgument.endsWith(SINGLE_QUOTE) || cleanedArgument.endsWith(DOUBLE_QUOTE)) {
+ cleanedArgument = cleanedArgument.substring(0, cleanedArgument.length() - 1);
+ }
+
+
final StringBuffer buf = new StringBuffer();
- if (argument.indexOf(DOUBLE_QUOTE) > -1) {
- if (argument.indexOf(SINGLE_QUOTE) > -1) {
+ if (cleanedArgument.indexOf(DOUBLE_QUOTE) > -1) {
+ if (cleanedArgument.indexOf(SINGLE_QUOTE) > -1) {
throw new IllegalArgumentException(
- "Can\'t handle single and double quotes in same argument");
+ "Can't handle single and double quotes in same argument");
} else {
- return buf.append(SINGLE_QUOTE).append(argument).append(SINGLE_QUOTE).toString();
+ return buf.append(SINGLE_QUOTE).append(cleanedArgument).append(
+ SINGLE_QUOTE).toString();
}
- } else if (argument.indexOf(SINGLE_QUOTE) > -1 || argument.indexOf(" ") > -1) {
- return buf.append(DOUBLE_QUOTE).append(argument).append(DOUBLE_QUOTE).toString();
+ } else if (cleanedArgument.indexOf(SINGLE_QUOTE) > -1
+ || cleanedArgument.indexOf(" ") > -1) {
+ return buf.append(DOUBLE_QUOTE).append(cleanedArgument).append(
+ DOUBLE_QUOTE).toString();
} else {
- return argument;
+ return cleanedArgument;
}
}
/**
- * Quotes the parts of the given array in way that makes them usable as
- * command line arguments.
- *
- * @return empty string for null or no command, else every argument split by
- * spaces and quoted by quoting rules
- */
- public static String toString(final String[] line) {
- // empty path return empty string
- if (line == null || line.length == 0) {
- return "";
- }
-
- // path containing one or more elements
- final StringBuffer result = new StringBuffer();
- for (int i = 0; i < line.length; i++) {
- if (i > 0) {
- result.append(' ');
- }
- result.append(quoteArgument(line[i]));
- }
- return result.toString();
- }
-
- /**
* Crack a command line.
*
* @param toProcess
@@ -219,7 +199,7 @@
* @return the command line broken into strings. An empty or null toProcess
* parameter results in a zero sized array
*/
- public static String[] translateCommandline(final String toProcess) {
+ private static String[] translateCommandline(final String toProcess) {
if (toProcess == null || toProcess.length() == 0) {
// no command? no string
return new String[0];
@@ -278,8 +258,8 @@
}
if (state == inQuote || state == inDoubleQuote) {
- throw new IllegalArgumentException(
- "Unbalanced quotes in " + toProcess);
+ throw new IllegalArgumentException("Unbalanced quotes in "
+ + toProcess);
}
String[] args = new String[v.size()];
@@ -288,33 +268,41 @@
}
/**
- * size operator. This actually creates the command line, so it is not a
- * zero cost operation.
- *
- * @return number of elements in the command, including the executable
+ * 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 int size() {
- return getCommandline().length;
+ 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;
}
/**
- * Generate a deep clone of the contained object.
+ * Stringify operator returns the command line as a string.
*
- * @return a clone of the contained object
- * @throws CloneNotSupportedException
+ * @return the command line
*/
- public Object clone() throws CloneNotSupportedException {
- CommandLine c = (CommandLine) super.clone();
- c.arguments = (Vector) arguments.clone();
- return c;
- }
+ public String toString() {
+ String[] strings = toStrings();
- public void clear() {
- executable = null;
- arguments.removeAllElements();
- }
+ StringBuffer sb = new StringBuffer();
+
+ for (int i = 0; i < strings.length; i++) {
+ if (i > 0) {
+ sb.append(' ');
+ }
+ sb.append(strings[i]);
+ }
- public void clearArgs() {
- arguments.removeAllElements();
+ return sb.toString();
}
}
Modified: jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/Exec.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/Exec.java?rev=385278&r1=385277&r2=385278&view=diff
==============================================================================
--- jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/Exec.java (original)
+++ jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/Exec.java Sun Mar 12 04:07:59 2006
@@ -39,8 +39,6 @@
private Long timeout = null;
- private String executable;
-
private boolean resolveExecutable = false;
private boolean spawn = false;
@@ -227,7 +225,7 @@
environment = env;
}
- cmdl.setExecutable(resolveExecutable(executable, false));
+ //cmdl.setExecutable(resolveExecutable(executable, false));
checkConfiguration(cmdl);
try {
@@ -250,7 +248,7 @@
environment = env;
}
- cmdl.setExecutable(resolveExecutable(executable, false));
+ //cmdl.setExecutable(resolveExecutable(executable, false));
checkConfiguration(cmdl);
try {
Modified: jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/environment/DefaultProcessingEnvironment.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/environment/DefaultProcessingEnvironment.java?rev=385278&r1=385277&r2=385278&view=diff
==============================================================================
--- jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/environment/DefaultProcessingEnvironment.java (original)
+++ jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/environment/DefaultProcessingEnvironment.java Sun Mar 12 04:07:59 2006
@@ -116,40 +116,47 @@
}
protected CommandLine getProcEnvCommand() {
- CommandLine commandLine = new CommandLine();
+ String executable;
+ String[] arguments = null;
if (OS.isFamilyOS2()) {
// OS/2 - use same mechanism as Windows 2000
- commandLine.setExecutable("cmd");
- commandLine.addArguments(new String[] {"/c", "set"});
+ executable = "cmd";
+
+ arguments = new String[] {"/c", "set"};
} else if (OS.isFamilyWindows()) {
// Determine if we're running under XP/2000/NT or 98/95
if (OS.isFamilyWin9x()) {
- commandLine.setExecutable("command.com");
+ executable = "command.com";
// Windows 98/95
} else {
- commandLine.setExecutable("cmd");
+ executable = "cmd";
// Windows XP/2000/NT/2003
}
- commandLine.addArguments(new String[] {"/c", "set"});
+ arguments = new String[] {"/c", "set"};
} else if (OS.isFamilyZOS() || OS.isFamilyUnix()) {
// On most systems one could use: /bin/sh -c env
// Some systems have /bin/env, others /usr/bin/env, just try
if (new File("/bin/env").canRead()) {
- commandLine.setExecutable("/bin/env");
+ executable = "/bin/env";
} else if (new File("/usr/bin/env").canRead()) {
- commandLine.setExecutable("/usr/bin/env");
+ executable = "/usr/bin/env";
} else {
// rely on PATH
- commandLine.setExecutable("env");
+ executable = "env";
}
} else if (OS.isFamilyNetware() || OS.isFamilyOS400()) {
// rely on PATH
- commandLine.setExecutable("env");
+ executable = "env";
} else {
// MAC OS 9 and previous
// TODO: I have no idea how to get it, someone must fix it
- commandLine = null;
+ executable = null;
+ }
+ CommandLine commandLine = null;
+ if(executable != null) {
+ commandLine = new CommandLine(executable);
+ commandLine.addArguments(arguments);
}
return commandLine;
}
Modified: jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/environment/OpenVmsProcessingEnvironment.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/environment/OpenVmsProcessingEnvironment.java?rev=385278&r1=385277&r2=385278&view=diff
==============================================================================
--- jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/environment/OpenVmsProcessingEnvironment.java (original)
+++ jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/environment/OpenVmsProcessingEnvironment.java Sun Mar 12 04:07:59 2006
@@ -41,8 +41,7 @@
}
protected CommandLine getProcEnvCommand() {
- CommandLine commandLine = new CommandLine();
- commandLine.setExecutable("show");
+ CommandLine commandLine = new CommandLine("show");
commandLine.addArgument("logical");
return commandLine;
}
Modified: jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/CommandLauncherImpl.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/CommandLauncherImpl.java?rev=385278&r1=385277&r2=385278&view=diff
==============================================================================
--- jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/CommandLauncherImpl.java (original)
+++ jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/CommandLauncherImpl.java Sun Mar 12 04:07:59 2006
@@ -38,7 +38,7 @@
envVar = EnvironmentUtil.toStrings(env);
}
- return Runtime.getRuntime().exec(cmd.getCommandline(),
+ return Runtime.getRuntime().exec(cmd.toStrings(),
envVar);
}
Modified: jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/Java13CommandLauncher.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/Java13CommandLauncher.java?rev=385278&r1=385277&r2=385278&view=diff
==============================================================================
--- jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/Java13CommandLauncher.java (original)
+++ jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/Java13CommandLauncher.java Sun Mar 12 04:07:59 2006
@@ -58,7 +58,7 @@
envVars = EnvironmentUtil.toStrings(env);
}
- return Runtime.getRuntime().exec(cmd.getCommandline(),
+ return Runtime.getRuntime().exec(cmd.toStrings(),
envVars, workingDir);
}
}
Modified: jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/OS2CommandLauncher.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/OS2CommandLauncher.java?rev=385278&r1=385277&r2=385278&view=diff
==============================================================================
--- jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/OS2CommandLauncher.java (original)
+++ jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/OS2CommandLauncher.java Sun Mar 12 04:07:59 2006
@@ -56,10 +56,9 @@
return exec(cmd, env);
}
- CommandLine newCmd = new CommandLine();
- newCmd.setExecutable("cmd");
+ CommandLine newCmd = new CommandLine("cmd");
newCmd.addArgument("/c");
- newCmd.addArguments(cmd.getCommandline());
+ newCmd.addArguments(cmd.toStrings());
return exec(newCmd, env);
}
Modified: jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/VmsCommandLauncher.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/VmsCommandLauncher.java?rev=385278&r1=385277&r2=385278&view=diff
==============================================================================
--- jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/VmsCommandLauncher.java (original)
+++ jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/VmsCommandLauncher.java Sun Mar 12 04:07:59 2006
@@ -40,8 +40,9 @@
*/
public Process exec(final CommandLine cmd, final Map env)
throws IOException {
- CommandLine vmsCmd = new CommandLine();
- vmsCmd.setExecutable(createCommandFile(cmd, env).getPath());
+ CommandLine vmsCmd = new CommandLine(
+ createCommandFile(cmd, env).getPath()
+ );
return super.exec(vmsCmd, env);
}
@@ -54,8 +55,9 @@
*/
public Process exec(final CommandLine cmd, final Map env,
final File workingDir) throws IOException {
- CommandLine vmsCmd = new CommandLine();
- vmsCmd.setExecutable(createCommandFile(cmd, env).getPath());
+ CommandLine vmsCmd = new CommandLine(
+ createCommandFile(cmd, env).getPath()
+ );
return super.exec(vmsCmd, env, workingDir);
}
Modified: jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/WinNTCommandLauncher.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/WinNTCommandLauncher.java?rev=385278&r1=385277&r2=385278&view=diff
==============================================================================
--- jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/WinNTCommandLauncher.java (original)
+++ jakarta/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/launcher/WinNTCommandLauncher.java Sun Mar 12 04:07:59 2006
@@ -53,10 +53,9 @@
// Use cmd.exe to change to the specified directory before running
// the command
- CommandLine newCmd = new CommandLine();
- newCmd.setExecutable("cmd");
+ CommandLine newCmd = new CommandLine("cmd");
newCmd.addArgument("/c");
- newCmd.addArguments(cmd.getCommandline());
+ newCmd.addArguments(cmd.toStrings());
return exec(newCmd, env);
}
Modified: jakarta/commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/CommandLineTest.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/CommandLineTest.java?rev=385278&r1=385277&r2=385278&view=diff
==============================================================================
--- jakarta/commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/CommandLineTest.java (original)
+++ jakarta/commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/CommandLineTest.java Sun Mar 12 04:07:59 2006
@@ -17,45 +17,187 @@
package org.apache.commons.exec;
+import java.util.Arrays;
+
+import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
public class CommandLineTest extends TestCase {
- public void testSetExecutable() {
- CommandLine cmdl = new CommandLine();
- cmdl.setExecutable("test");
+ private void assertEquals(String[] expected, String[] actual) {
+ if (!Arrays.equals(expected, actual)) {
+ throw new AssertionFailedError("Arrays not equal");
+ }
+ }
+
+ public void testExecutable() {
+ CommandLine cmdl = new CommandLine("test");
assertEquals("test", cmdl.toString());
+ assertEquals(new String[] {"test"}, cmdl.toStrings());
+ }
+
+ public void testExecutableZeroLengthString() {
+ try {
+ CommandLine cmdl = new CommandLine("");
+ fail("Must throw IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ // Expected
+ }
+ }
+
+ public void testExecutableWhitespaceString() {
+ try {
+ CommandLine cmdl = new CommandLine(" ");
+ fail("Must throw IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ // Expected
+ }
+ }
+
+ public void testNullExecutable() {
+ try {
+ CommandLine cmdl = new CommandLine(null);
+ fail("Must throw IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ // Expected
+ }
}
- public void testSetArguments() {
- CommandLine cmdl = new CommandLine();
- cmdl.setExecutable("test");
+ public void testAddArgument() {
+ CommandLine cmdl = new CommandLine("test");
+
cmdl.addArgument("foo");
cmdl.addArgument("bar");
assertEquals("test foo bar", cmdl.toString());
+ assertEquals(new String[] {"test", "foo", "bar"}, cmdl.toStrings());
}
- public void testSetArgumentsWithSpace() {
- CommandLine cmdl = new CommandLine();
- cmdl.setExecutable("test");
+ public void testAddNullArgument() {
+ CommandLine cmdl = new CommandLine("test");
+
+ cmdl.addArgument(null);
+ assertEquals("test", cmdl.toString());
+ assertEquals(new String[] {"test"}, cmdl.toStrings());
+ }
+
+ public void testAddArgumentWithSpace() {
+ CommandLine cmdl = new CommandLine("test");
cmdl.addArgument("foo");
cmdl.addArgument("ba r");
assertEquals("test foo \"ba r\"", cmdl.toString());
+ assertEquals(new String[] {"test", "foo", "\"ba r\""}, cmdl.toStrings());
}
- public void testSetArgumentsWithQuote() {
- CommandLine cmdl = new CommandLine();
- cmdl.setExecutable("test");
+ public void testAddArgumentWithQuote() {
+ CommandLine cmdl = new CommandLine("test");
cmdl.addArgument("foo");
cmdl.addArgument("ba\"r");
assertEquals("test foo 'ba\"r'", cmdl.toString());
+ assertEquals(new String[] {"test", "foo", "'ba\"r'"}, cmdl.toStrings());
+ }
+
+ public void testAddArgumentWithQuotesAround() {
+ CommandLine cmdl = new CommandLine("test");
+ cmdl.addArgument("\'foo\'");
+ cmdl.addArgument("\"bar\"");
+ cmdl.addArgument("\"fe z\"");
+ assertEquals("test foo bar \"fe z\"", cmdl.toString());
+ assertEquals(new String[] {"test", "foo", "bar", "\"fe z\""}, cmdl.toStrings());
}
- public void testSetArgumentsWithSingleQuote() {
- CommandLine cmdl = new CommandLine();
- cmdl.setExecutable("test");
+ public void testAddArgumentWithSingleQuote() {
+ CommandLine cmdl = new CommandLine("test");
+
cmdl.addArgument("foo");
cmdl.addArgument("ba'r");
assertEquals("test foo \"ba'r\"", cmdl.toString());
+ assertEquals(new String[] {"test", "foo", "\"ba\'r\""}, cmdl
+ .toStrings());
+ }
+
+ public void testAddArgumentWithBothQuotes() {
+ CommandLine cmdl = new CommandLine("test");
+
+ try {
+ cmdl.addArgument("b\"a'r");
+ fail("IllegalArgumentException should be thrown");
+ } catch (IllegalArgumentException e) {
+ // OK, expected
+ }
+ }
+
+ public void testAddArguments() {
+ CommandLine cmdl = new CommandLine("test");
+ cmdl.addArguments("foo bar");
+ assertEquals("test foo bar", cmdl.toString());
+ assertEquals(new String[] {"test", "foo", "bar"}, cmdl.toStrings());
+ }
+
+ public void testAddArgumentsWithQuotes() {
+ CommandLine cmdl = new CommandLine("test");
+ cmdl.addArguments("'foo' \"bar\"");
+ assertEquals("test foo bar", cmdl.toString());
+ assertEquals(new String[] {"test", "foo", "bar"}, cmdl.toStrings());
+ }
+
+ public void testAddArgumentsWithQuotesAndSpaces() {
+ CommandLine cmdl = new CommandLine("test");
+ cmdl.addArguments("'fo o' \"ba r\"");
+ assertEquals("test \"fo o\" \"ba r\"", cmdl.toString());
+ assertEquals(new String[] {"test", "\"fo o\"", "\"ba r\""}, cmdl
+ .toStrings());
+ }
+
+ public void testAddArgumentsArray() {
+ CommandLine cmdl = new CommandLine("test");
+ cmdl.addArguments(new String[] {"foo", "bar"});
+ assertEquals("test foo bar", cmdl.toString());
+ assertEquals(new String[] {"test", "foo", "bar"}, cmdl.toStrings());
+ }
+
+ public void testAddArgumentsArrayNull() {
+ CommandLine cmdl = new CommandLine("test");
+ cmdl.addArguments((String[]) null);
+ assertEquals("test", cmdl.toString());
+ assertEquals(new String[] {"test"}, cmdl.toStrings());
+ }
+
+ public void testParse() {
+ CommandLine cmdl = CommandLine.parse("test foo bar");
+ assertEquals("test foo bar", cmdl.toString());
+ assertEquals(new String[] {"test", "foo", "bar"}, cmdl.toStrings());
+ }
+
+ public void testParseWithQuotes() {
+ CommandLine cmdl = CommandLine.parse("test \"foo\" \'ba r\'");
+ assertEquals("test foo \"ba r\"", cmdl.toString());
+ assertEquals(new String[] {"test", "foo", "\"ba r\""}, cmdl.toStrings());
+ }
+
+ public void testParseWithUnevenQuotes() {
+ try {
+ CommandLine.parse("test \"foo bar");
+ fail("IllegalArgumentException must be thrown due to uneven quotes");
+ } catch (IllegalArgumentException e) {
+ // Expected
+ }
+ }
+
+ public void testParseWithNull() {
+ try {
+ CommandLine.parse(null);
+ fail("IllegalArgumentException must be thrown due to incorrect command line");
+ } catch (IllegalArgumentException e) {
+ // Expected
+ }
+ }
+
+ public void testParseWithOnlyWhitespace() {
+ try {
+ CommandLine.parse(" ");
+ fail("IllegalArgumentException must be thrown due to incorrect command line");
+ } catch (IllegalArgumentException e) {
+ // Expected
+ }
}
}
Modified: jakarta/commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/ExecTest.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/ExecTest.java?rev=385278&r1=385277&r2=385278&view=diff
==============================================================================
--- jakarta/commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/ExecTest.java (original)
+++ jakarta/commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/ExecTest.java Sun Mar 12 04:07:59 2006
@@ -36,8 +36,7 @@
public void testExecute() throws Exception {
Exec exec = new Exec();
- CommandLine cl = new CommandLine();
- cl.setExecutable(testScript);
+ CommandLine cl = new CommandLine(testScript);
exec.execute(cl, baos, baos);
@@ -47,8 +46,7 @@
public void testExecuteWithArg() throws Exception {
Exec exec = new Exec();
- CommandLine cl = new CommandLine();
- cl.setExecutable(testScript);
+ CommandLine cl = new CommandLine(testScript);
cl.addArgument("BAR");
exec.execute(cl, baos, baos);
@@ -59,8 +57,7 @@
Map env = new HashMap();
env.put("TEST_ENV_VAR", "XYZ");
- CommandLine cl = new CommandLine();
- cl.setExecutable(testScript);
+ CommandLine cl = new CommandLine(testScript);
Exec exec = new Exec();
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org