[MSHARED-749] - Commandline does not thrown CommandLineException when uneven number of quotation marks used
diff --git a/src/main/java/org/apache/maven/shared/utils/cli/Arg.java b/src/main/java/org/apache/maven/shared/utils/cli/Arg.java
index bd1fa08..e0eccf3 100644
--- a/src/main/java/org/apache/maven/shared/utils/cli/Arg.java
+++ b/src/main/java/org/apache/maven/shared/utils/cli/Arg.java
@@ -34,7 +34,7 @@
/**
* @param line The line of arguments.
*/
- void setLine( String line );
+ void setLine( String line ) throws CommandLineException;
/**
* @param value The file to be set.
diff --git a/src/main/java/org/apache/maven/shared/utils/cli/Commandline.java b/src/main/java/org/apache/maven/shared/utils/cli/Commandline.java
index 94fbdcc..db13770 100644
--- a/src/main/java/org/apache/maven/shared/utils/cli/Commandline.java
+++ b/src/main/java/org/apache/maven/shared/utils/cli/Commandline.java
@@ -92,19 +92,11 @@
*
* @param toProcess The command to process
*/
- public Commandline( String toProcess )
+ public Commandline( String toProcess ) throws CommandLineException
{
setDefaultShell();
- String[] tmp = new String[0];
- try
- {
- tmp = CommandLineUtils.translateCommandline( toProcess );
- }
- catch ( Exception e )
- {
- System.err.println( "Error translating Commandline." );
- }
- if ( ( tmp != null ) && ( tmp.length > 0 ) )
+ String[] tmp = CommandLineUtils.translateCommandline( toProcess );
+ if ( ( tmp.length > 0 ) )
{
setExecutable( tmp[0] );
for ( int i = 1; i < tmp.length; i++ )
@@ -484,7 +476,7 @@
/**
* {@inheritDoc}
*/
- public void setLine( String line )
+ public void setLine( String line ) throws CommandLineException
{
if ( line == null )
{
@@ -494,9 +486,10 @@
{
parts = CommandLineUtils.translateCommandline( line );
}
- catch ( Exception e )
+ catch ( CommandLineException e )
{
System.err.println( "Error translating Commandline." );
+ throw( e );
}
}
diff --git a/src/test/java/org/apache/maven/shared/utils/cli/CommandLineUtilsTest.java b/src/test/java/org/apache/maven/shared/utils/cli/CommandLineUtilsTest.java
index d64fdd8..5b5ec2a 100644
--- a/src/test/java/org/apache/maven/shared/utils/cli/CommandLineUtilsTest.java
+++ b/src/test/java/org/apache/maven/shared/utils/cli/CommandLineUtilsTest.java
@@ -19,6 +19,7 @@
* under the License.
*/
+import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@@ -102,6 +103,31 @@
@Test
+ public void givenASingleQuoteMarkInArgument_whenExecutingCode_thenNoExceptionIsThrown() throws Exception {
+ new Commandline("echo \"let's go\"").execute();
+ }
+
+ @Test
+ public void givenADoubleQuoteMarkInArgument_whenExecutingCode_thenCommandLineExceptionIsThrown() throws Exception {
+ try {
+ new Commandline("echo \"let\"s go\"").execute();
+ } catch (CommandLineException e) {
+ assertTrue(true);
+ return;
+ }
+ fail("Exception was not thrown when given invalid (3 unescaped double quote) input");
+ }
+
+
+ @Test
+ public void givenASingleQuoteMarkInArgument_whenExecutingCode_thenExitCode0Returned() throws Exception {
+ final Process p = new Commandline("echo \"let's go\"").execute();
+ // Note, this sleep should be removed when java version reaches Java 8
+ Thread.sleep(1000);
+ assertEquals(0, p.exitValue());
+ }
+
+ @Test
public void givenASingleQuoteMarkInArgument_whenTranslatingToCmdLineArgs_thenTheQuotationMarkIsNotEscaped() throws Exception
{
final String command = "echo \"let's go\"";