Author: carlos
Date: Wed Apr 19 18:02:54 2006
New Revision: 395456

URL: http://svn.apache.org/viewcvs?rev=395456&view=rev
Log:
[MSUREFIRE-88] Surefire fork fails under windows when command has several 
quotes, merged in r395452 from trunk

Added:
    
maven/surefire/branches/surefire-testng/surefire-booter/src/main/java/org/apache/maven/surefire/booter/shell/
    
maven/surefire/branches/surefire-testng/surefire-booter/src/main/java/org/apache/maven/surefire/booter/shell/CmdShell.java
   (with props)
    
maven/surefire/branches/surefire-testng/surefire-booter/src/main/java/org/apache/maven/surefire/booter/shell/CommandShell.java
   (with props)
    
maven/surefire/branches/surefire-testng/surefire-booter/src/main/java/org/apache/maven/surefire/booter/shell/Shell.java
   (with props)
Modified:
    
maven/surefire/branches/surefire-testng/surefire-booter/src/main/java/org/apache/maven/surefire/booter/Commandline.java

Modified: 
maven/surefire/branches/surefire-testng/surefire-booter/src/main/java/org/apache/maven/surefire/booter/Commandline.java
URL: 
http://svn.apache.org/viewcvs/maven/surefire/branches/surefire-testng/surefire-booter/src/main/java/org/apache/maven/surefire/booter/Commandline.java?rev=395456&r1=395455&r2=395456&view=diff
==============================================================================
--- 
maven/surefire/branches/surefire-testng/surefire-booter/src/main/java/org/apache/maven/surefire/booter/Commandline.java
 (original)
+++ 
maven/surefire/branches/surefire-testng/surefire-booter/src/main/java/org/apache/maven/surefire/booter/Commandline.java
 Wed Apr 19 18:02:54 2006
@@ -20,6 +20,10 @@
 import java.util.Arrays;
 import java.util.List;
 
+import org.apache.maven.surefire.booter.shell.CmdShell;
+import org.apache.maven.surefire.booter.shell.CommandShell;
+import org.apache.maven.surefire.booter.shell.Shell;
+
 /**
  * Commandline class copied from plexus-utils with fix for PLX-161, as we can 
not upgrade plexus-utils until it's upgraded in core Maven
  * 
@@ -31,9 +35,7 @@
     extends org.codehaus.plexus.util.cli.Commandline
 {
 
-    private String shell = null;
-
-    private String[] shellArgs = null;
+    private Shell shell;
 
     public Commandline()
     {
@@ -54,13 +56,11 @@
         {
             if ( os.indexOf( "95" ) != -1 || os.indexOf( "98" ) != -1 || 
os.indexOf( "Me" ) != -1 )
             {
-                setShell( "COMMAND.COM" );
-                setShellArgs( new String[] { "/C" } );
+                setShell( new CommandShell() );
             }
             else
             {
-                setShell( "CMD.EXE" );
-                setShellArgs( new String[] { "/X", "/C" } );
+                setShell( new CmdShell() );
             }
         }
     }
@@ -70,99 +70,47 @@
      */
     public String[] getShellCommandline()
     {
-        List commandLine = new ArrayList();
-
-        if ( shell != null )
-        {
-            commandLine.add( getShell() );
-        }
-
-        if ( getShellArgs() != null )
-        {
-            commandLine.addAll( Arrays.asList( getShellArgs() ) );
-        }
 
         if ( getShell() == null )
         {
             if ( executable != null )
             {
+                List commandLine = new ArrayList();
                 commandLine.add( executable );
+                commandLine.addAll( Arrays.asList( getArguments() ) );
+                return (String[]) commandLine.toArray( new String[0] );
             }
-            commandLine.addAll( Arrays.asList( getArguments() ) );
+            else
+            {
+                return getArguments();
+            }
+
         }
         else
         {
-            /* When using a shell we need to quote the full command */
-            StringBuffer sb = new StringBuffer();
-            sb.append( "\"" );
-            if ( executable != null )
-            {
-                sb.append( "\"" );
-                sb.append( executable );
-                sb.append( "\"" );
-            }
-            for ( int i = 0; i < getArguments().length; i++ )
-            {
-                sb.append( " \"" );
-                sb.append( getArguments()[i] );
-                sb.append( "\"" );
-            }
-            sb.append( "\"" );
-            commandLine.add( sb.toString() );
+            return (String[]) getShell().getShellCommandLine( executable, 
getArguments() ).toArray( new String[0] );
         }
-
-        return (String[]) commandLine.toArray( new String[0] );
     }
 
     /**
-     * <p>
-     * Set the shell command to use. If not set explicitly the class will 
autodetect it from the operating system name
-     * </p>
-     * <p>
-     * eg. <code>COMMAND.COM</code> in Win9x and WinMe or <code>CMD.EXE</code> 
in WinNT, Win2000 or WinXP
-     * </p>
+     * Allows to set the shell to be used in this command line.
+     *
+     * @param shell
      * @since 1.2
-     * @param shell shell command
      */
-    public void setShell( String shell )
+    public void setShell( Shell shell )
     {
         this.shell = shell;
     }
 
     /**
-     * Get the shell command to use
+     * Get the shell to be used in this command line.
+     *
      * @since 1.2
-     * @return
      */
-    public String getShell()
+    public Shell getShell()
     {
         return shell;
-    }
-
-    /**
-     * <p>
-     * Shell arguments to use when using a shell command. If not set 
explicitly the class will autodetect it from the operating system name
-     * </p>
-     * <p>
-     * eg. <code>/C</code> for <code>COMMAND.COM</code> and <code>/X /C</code> 
for <code>CMD.EXE</code>
-     * </p>
-     * @see setShell
-     * @since 1.2
-     * @param shellArgs
-     */
-    public void setShellArgs( String[] shellArgs )
-    {
-        this.shellArgs = shellArgs;
-    }
-
-    /**
-     * Get the shell arguments to use with the shell command
-     * @since 1.2
-     * @return the arguments
-     */
-    public String[] getShellArgs()
-    {
-        return shellArgs;
     }
 
 }

Added: 
maven/surefire/branches/surefire-testng/surefire-booter/src/main/java/org/apache/maven/surefire/booter/shell/CmdShell.java
URL: 
http://svn.apache.org/viewcvs/maven/surefire/branches/surefire-testng/surefire-booter/src/main/java/org/apache/maven/surefire/booter/shell/CmdShell.java?rev=395456&view=auto
==============================================================================
--- 
maven/surefire/branches/surefire-testng/surefire-booter/src/main/java/org/apache/maven/surefire/booter/shell/CmdShell.java
 (added)
+++ 
maven/surefire/branches/surefire-testng/surefire-booter/src/main/java/org/apache/maven/surefire/booter/shell/CmdShell.java
 Wed Apr 19 18:02:54 2006
@@ -0,0 +1,54 @@
+package org.apache.maven.surefire.booter.shell;
+
+import java.util.Arrays;
+import java.util.List;
+
+/*
+ * Copyright 2001-2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * <p>
+ * Class with patches copied from plexus-utils with fix for PLX-161,
+ * as we can not upgrade plexus-utils until it's upgraded in core Maven
+ * </p>
+ * 
+ * TODO deprecate when plexus-utils 1.2 can be used
+ *
+ * @author <a href="mailto:[EMAIL PROTECTED]">Carlos Sanchez</a>
+ */
+public class CmdShell
+    extends Shell
+{
+    public CmdShell()
+    {
+        setShellCommand( "cmd.exe" );
+        setShellArgs( new String[]{"/X", "/C"} );
+    }
+
+    /**
+     * Specific implementation that quotes the all the command line
+     */
+    public List getCommandLine( String executable, String[] arguments )
+    {
+        StringBuffer sb = new StringBuffer();
+        sb.append( "\"" );
+        sb.append( super.getCommandLine( executable, arguments ).get( 0 ) );
+        sb.append( "\"" );
+
+        return Arrays.asList( new String[]{sb.toString()} );
+    }
+
+}

Propchange: 
maven/surefire/branches/surefire-testng/surefire-booter/src/main/java/org/apache/maven/surefire/booter/shell/CmdShell.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
maven/surefire/branches/surefire-testng/surefire-booter/src/main/java/org/apache/maven/surefire/booter/shell/CmdShell.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Added: 
maven/surefire/branches/surefire-testng/surefire-booter/src/main/java/org/apache/maven/surefire/booter/shell/CommandShell.java
URL: 
http://svn.apache.org/viewcvs/maven/surefire/branches/surefire-testng/surefire-booter/src/main/java/org/apache/maven/surefire/booter/shell/CommandShell.java?rev=395456&view=auto
==============================================================================
--- 
maven/surefire/branches/surefire-testng/surefire-booter/src/main/java/org/apache/maven/surefire/booter/shell/CommandShell.java
 (added)
+++ 
maven/surefire/branches/surefire-testng/surefire-booter/src/main/java/org/apache/maven/surefire/booter/shell/CommandShell.java
 Wed Apr 19 18:02:54 2006
@@ -0,0 +1,38 @@
+package org.apache.maven.surefire.booter.shell;
+
+/*
+ * Copyright 2001-2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * <p>
+ * Class with patches copied from plexus-utils with fix for PLX-161,
+ * as we can not upgrade plexus-utils until it's upgraded in core Maven
+ * </p>
+ * 
+ * TODO deprecate when plexus-utils 1.2 can be used
+ *
+ * @author <a href="mailto:[EMAIL PROTECTED]">Carlos Sanchez</a>
+ */
+public class CommandShell
+    extends Shell
+{
+    public CommandShell()
+    {
+        setShellCommand( "command.com" );
+        setShellArgs( new String[]{"/C"} );
+    }
+
+}

Propchange: 
maven/surefire/branches/surefire-testng/surefire-booter/src/main/java/org/apache/maven/surefire/booter/shell/CommandShell.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
maven/surefire/branches/surefire-testng/surefire-booter/src/main/java/org/apache/maven/surefire/booter/shell/CommandShell.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Added: 
maven/surefire/branches/surefire-testng/surefire-booter/src/main/java/org/apache/maven/surefire/booter/shell/Shell.java
URL: 
http://svn.apache.org/viewcvs/maven/surefire/branches/surefire-testng/surefire-booter/src/main/java/org/apache/maven/surefire/booter/shell/Shell.java?rev=395456&view=auto
==============================================================================
--- 
maven/surefire/branches/surefire-testng/surefire-booter/src/main/java/org/apache/maven/surefire/booter/shell/Shell.java
 (added)
+++ 
maven/surefire/branches/surefire-testng/surefire-booter/src/main/java/org/apache/maven/surefire/booter/shell/Shell.java
 Wed Apr 19 18:02:54 2006
@@ -0,0 +1,149 @@
+package org.apache.maven.surefire.booter.shell;
+
+/*
+ * Copyright 2001-2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import org.apache.maven.surefire.util.NestedRuntimeException;
+import org.codehaus.plexus.util.cli.CommandLineException;
+import org.codehaus.plexus.util.cli.Commandline;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * <p>
+ * Class with patches copied from plexus-utils with fix for PLX-161,
+ * as we can not upgrade plexus-utils until it's upgraded in core Maven
+ * </p>
+ * 
+ * TODO deprecate when plexus-utils 1.2 can be used
+ *
+ * @author <a href="mailto:[EMAIL PROTECTED]">Carlos Sanchez</a>
+ */
+public class Shell
+{
+    private String shellCommand;
+
+    private String[] shellArgs;
+
+    /**
+     * Set the command to execute the shell (eg. COMMAND.COM, /bin/bash,...)
+     *
+     * @param shellCommand
+     */
+    public void setShellCommand( String shellCommand )
+    {
+        this.shellCommand = shellCommand;
+    }
+
+    /**
+     * Get the command to execute the shell
+     *
+     * @return
+     */
+    public String getShellCommand()
+    {
+        return shellCommand;
+    }
+
+    /**
+     * Set the shell arguments when calling a command line (not the executable 
arguments)
+     * (eg. /X /C for CMD.EXE)
+     *
+     * @param shellArgs
+     */
+    public void setShellArgs( String[] shellArgs )
+    {
+        this.shellArgs = shellArgs;
+    }
+
+    /**
+     * Get the shell arguments
+     *
+     * @return
+     */
+    public String[] getShellArgs()
+    {
+        return shellArgs;
+    }
+
+    /**
+     * Get the command line for the provided executable and arguments in this 
shell
+     *
+     * @param executable executable that the shell has to call
+     * @param arguments  arguments for the executable, not the shell
+     * @return List with one String object with executable and arguments 
quoted as needed
+     */
+    public List getCommandLine( String executable, String[] arguments )
+    {
+
+        List commandLine = new ArrayList();
+        try
+        {
+            StringBuffer sb = new StringBuffer();
+
+            if ( executable != null )
+            {
+                sb.append( Commandline.quoteArgument( executable ) );
+            }
+            for ( int i = 0; i < arguments.length; i++ )
+            {
+                sb.append( " " );
+                sb.append( Commandline.quoteArgument( arguments[i] ) );
+            }
+
+            commandLine.add( sb.toString() );
+        }
+        catch ( CommandLineException e )
+        {
+            throw new NestedRuntimeException( e );
+        }
+
+        return commandLine;
+    }
+
+    /**
+     * Get the full command line to execute, including shell command, shell 
arguments,
+     * executable and executable arguments
+     *
+     * @param executable executable that the shell has to call
+     * @param arguments  arguments for the executable, not the shell
+     * @return List of String objects, whose array version is suitable to be 
used as argument
+     *         of Runtime.getRuntime().exec()
+     */
+    public List getShellCommandLine( String executable, String[] arguments )
+    {
+
+        List commandLine = new ArrayList();
+
+        if ( getShellCommand() != null )
+        {
+            commandLine.add( getShellCommand() );
+        }
+
+        if ( getShellArgs() != null )
+        {
+            commandLine.addAll( Arrays.asList( getShellArgs() ) );
+        }
+
+        commandLine.addAll( getCommandLine( executable, arguments ) );
+
+        return commandLine;
+
+    }
+
+}

Propchange: 
maven/surefire/branches/surefire-testng/surefire-booter/src/main/java/org/apache/maven/surefire/booter/shell/Shell.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
maven/surefire/branches/surefire-testng/surefire-booter/src/main/java/org/apache/maven/surefire/booter/shell/Shell.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"


Reply via email to