Author: lcorneliussen
Date: Wed Dec 21 16:32:46 2011
New Revision: 1221795
URL: http://svn.apache.org/viewvc?rev=1221795&view=rev
Log:
[NPANDAY-509] CommandExecutor is confused with MSDeploy-style commandline
switches starting with "-" and containing both ":" and "="
o Temporary solution for Windows - have to figure out Mac/Linux! (but is
currently only used for MSBUILD + CSPACK anyway)
Added:
incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/execution/CapturingLogOutputStream.java
incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/execution/CommandExecutorSkeleton.java
incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/execution/CommonsExecCommandExecutor.java
incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/execution/CommonsExecLogStreamHandler.java
incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/execution/PlexusUtilsCommandExecutor.java
- copied, changed from r1221688,
incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/execution/DefaultCommandExecutor.java
incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/impl/CommonsExecNetExecutable.java
- copied, changed from r1221689,
incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/impl/DefaultNetExecutable.java
incubator/npanday/trunk/components/dotnet-executable/src/test/groovy/
incubator/npanday/trunk/components/dotnet-executable/src/test/groovy/npanday/
incubator/npanday/trunk/components/dotnet-executable/src/test/groovy/npanday/executable/
incubator/npanday/trunk/components/dotnet-executable/src/test/groovy/npanday/executable/execution/
incubator/npanday/trunk/components/dotnet-executable/src/test/groovy/npanday/executable/execution/CommandExecutorTest.groovy
Removed:
incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/execution/DefaultCommandExecutor.java
incubator/npanday/trunk/components/dotnet-executable/src/test/java/
Modified:
incubator/npanday/trunk/components/dotnet-executable/pom.xml
incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/CommandExecutor.java
incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/NetExecutable.java
Modified: incubator/npanday/trunk/components/dotnet-executable/pom.xml
URL:
http://svn.apache.org/viewvc/incubator/npanday/trunk/components/dotnet-executable/pom.xml?rev=1221795&r1=1221794&r2=1221795&view=diff
==============================================================================
--- incubator/npanday/trunk/components/dotnet-executable/pom.xml (original)
+++ incubator/npanday/trunk/components/dotnet-executable/pom.xml Wed Dec 21
16:32:46 2011
@@ -53,7 +53,12 @@ under the License.
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
- </dependency>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.commons</groupId>
+ <artifactId>commons-exec</artifactId>
+ <version>1.1</version>
+ </dependency>
</dependencies>
<build>
<plugins>
@@ -61,6 +66,11 @@ under the License.
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-maven-plugin</artifactId>
</plugin>
+ <plugin>
+ <!-- enable groovy testing -->
+ <groupId>org.codehaus.gmaven</groupId>
+ <artifactId>gmaven-plugin</artifactId>
+ </plugin>
</plugins>
</build>
</project>
Modified:
incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/CommandExecutor.java
URL:
http://svn.apache.org/viewvc/incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/CommandExecutor.java?rev=1221795&r1=1221794&r2=1221795&view=diff
==============================================================================
---
incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/CommandExecutor.java
(original)
+++
incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/CommandExecutor.java
Wed Dec 21 16:32:46 2011
@@ -18,7 +18,7 @@
*/
package npanday.executable;
-import npanday.executable.execution.DefaultCommandExecutor;
+import npanday.executable.execution.PlexusUtilsCommandExecutor;
import org.codehaus.plexus.logging.Logger;
import java.io.File;
@@ -120,7 +120,8 @@ public interface CommandExecutor
*/
public static CommandExecutor createDefaultCommmandExecutor()
{
- return new DefaultCommandExecutor();
+ // TODO: at some point we should switch this one to
CommonsExecCommandExecutor
+ return new PlexusUtilsCommandExecutor();
}
}
Modified:
incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/NetExecutable.java
URL:
http://svn.apache.org/viewvc/incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/NetExecutable.java?rev=1221795&r1=1221794&r2=1221795&view=diff
==============================================================================
---
incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/NetExecutable.java
(original)
+++
incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/NetExecutable.java
Wed Dec 21 16:32:46 2011
@@ -23,7 +23,6 @@ import npanday.PlatformUnsupportedExcept
import npanday.vendor.Vendor;
import java.util.List;
-import java.io.File;
/**
* Provides services for executing programs.
@@ -55,23 +54,6 @@ public interface NetExecutable
void execute() throws ExecutionException, PlatformUnsupportedException;
/**
- * Returns the executable file name that this compiler will use to compile
the application.
- *
- * @return the executable file name that this compiler will use to compile
the application
- * @throws npanday.executable.ExecutionException
- *
- */
- String getExecutable()
- throws ExecutionException;
-
- /**
- * Returns the parent directory of the executable.
- *
- * @return the parent directory of the executable
- */
- File getExecutionPath();
-
- /**
* Initialize this executable.
*
* @param npandayContext
Added:
incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/execution/CapturingLogOutputStream.java
URL:
http://svn.apache.org/viewvc/incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/execution/CapturingLogOutputStream.java?rev=1221795&view=auto
==============================================================================
---
incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/execution/CapturingLogOutputStream.java
(added)
+++
incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/execution/CapturingLogOutputStream.java
Wed Dec 21 16:32:46 2011
@@ -0,0 +1,43 @@
+package npanday.executable.execution;
+
+import org.apache.commons.exec.LogOutputStream;
+
+/**
+ * Stream that captures and logs lines to a plexus logger.
+ * Designed for anonymous implementation.
+ *
+ * Access the captured output through {@link #toString}.
+ *
+ * @author <a href="mailto:[email protected]">Lars Corneliussen</a>
+ */
+public abstract class CapturingLogOutputStream
+ extends LogOutputStream
+{
+ private StringBuffer contents = new StringBuffer();
+
+ private String NEW_LINE = System.getProperty("line.separator");
+
+ @Override
+ protected final void processLine(String line, int level) {
+ processLine( line );
+
+ contents.append( line );
+ contents.append( NEW_LINE );
+ }
+
+ /**
+ * Override this to log to a logger.
+ *
+ * @param line
+ */
+ protected abstract void processLine( String line );
+
+ /**
+ * Returns the captured output.
+ */
+ @Override
+ public String toString()
+ {
+ return contents.toString();
+ }
+}
Added:
incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/execution/CommandExecutorSkeleton.java
URL:
http://svn.apache.org/viewvc/incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/execution/CommandExecutorSkeleton.java?rev=1221795&view=auto
==============================================================================
---
incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/execution/CommandExecutorSkeleton.java
(added)
+++
incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/execution/CommandExecutorSkeleton.java
Wed Dec 21 16:32:46 2011
@@ -0,0 +1,49 @@
+package npanday.executable.execution;
+
+import npanday.executable.CommandExecutor;
+import npanday.executable.ExecutionException;
+import org.codehaus.plexus.logging.Logger;
+import org.codehaus.plexus.logging.console.ConsoleLogger;
+
+import java.io.File;
+import java.util.List;
+
+/**
+ * @author <a href="mailto:[email protected]">Lars Corneliussen</a>
+ */
+public abstract class CommandExecutorSkeleton
+ implements CommandExecutor
+{
+ /**
+ * Instance of a plugin logger.
+ */
+ private Logger logger;
+
+ public void setLogger( Logger logger )
+ {
+ this.logger = logger;
+ }
+
+ public void executeCommand( String executable, List<String> commands )
throws ExecutionException
+ {
+ executeCommand( executable, commands, null, true );
+ }
+
+ public void executeCommand( String executable, List<String> commands,
boolean failsOnErrorOutput ) throws
+ ExecutionException
+ {
+ executeCommand( executable, commands, null, failsOnErrorOutput );
+ }
+
+ public abstract void executeCommand(
+ String executable, List<String> commands, File workingDirectory,
boolean failsOnErrorOutput ) throws
+ ExecutionException;
+
+ public Logger getLogger()
+ {
+ if (logger == null)
+ logger = new ConsoleLogger( Logger.LEVEL_DEBUG, "implicit");
+
+ return logger;
+ }
+}
Added:
incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/execution/CommonsExecCommandExecutor.java
URL:
http://svn.apache.org/viewvc/incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/execution/CommonsExecCommandExecutor.java?rev=1221795&view=auto
==============================================================================
---
incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/execution/CommonsExecCommandExecutor.java
(added)
+++
incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/execution/CommonsExecCommandExecutor.java
Wed Dec 21 16:32:46 2011
@@ -0,0 +1,182 @@
+package npanday.executable.execution;
+
+import com.google.common.base.Joiner;
+import npanday.executable.ExecutionException;
+import org.apache.commons.exec.CommandLine;
+import org.apache.commons.exec.DefaultExecutor;
+import org.apache.commons.exec.ExecuteException;
+import org.apache.commons.exec.PumpStreamHandler;
+import org.apache.commons.exec.util.StringUtils;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.List;
+
+import static com.google.common.collect.Lists.newArrayList;
+
+/**
+ * New command executor based on commons-exec instead of plexus-utils.
+ *
+ * @author <a href="mailto:[email protected]">Lars Corneliussen</a>
+ */
+public class CommonsExecCommandExecutor
+ extends CommandExecutorSkeleton
+{
+ private static final Joiner JOIN_ON_SPACE = Joiner.on( " " );
+
+ private OutputStream standardOutHandler;
+
+ private OutputStream errorOutHandler;
+
+ private int result;
+
+ private boolean hasErrorOutput = false;
+
+ private CommandLine commandLine;
+
+ private boolean hasLoggedStartInfo;
+
+ public CommonsExecCommandExecutor()
+ {
+ setupOutputHandlers();
+ }
+
+ @Override
+ public void executeCommand(
+ String executable, List<String> commands, File workingDirectory,
boolean failsOnErrorOutput ) throws
+ ExecutionException
+ {
+ // TODO: This is for Windows only; in context of NPANDAY-509
+
+ /*
+ * We have to use cmd /X /C "...", because only then
+ * we can pass arguments like -x="a b" - which, in this case,
+ * was required by MSDeploy.
+ *
+ * Without cmd, using commons-exc or plexus-utils' cli, -x="a b" will
+ * get quoted once more.
+ *
+ * Consideration: We could also exchange the
DefaultExecutor.launcher; but it is private :/
+ */
+
+ commandLine = new CommandLine( "cmd" );
+
+ commandLine.addArgument( "/X" );
+ commandLine.addArgument( "/C" );
+
+ List<String> shellArgs = newArrayList();
+
+ shellArgs.add( StringUtils.quoteArgument( executable ) );
+
+ for ( String arg : commands )
+ {
+ // NPANDAY-509: if an argument ends with " or ', we are quite sure,
+ // quoting has been taken care of on the outside
+ final boolean endsWithQuote = arg.endsWith( "'" ) || arg.endsWith(
"\"" );
+ final boolean handleQuoting = endsWithQuote ? false : true;
+
+ if ( handleQuoting )
+ {
+ shellArgs.add( StringUtils.quoteArgument( arg ) );
+ }
+ else
+ {
+ shellArgs.add( arg );
+ }
+ }
+
+ // cmd \X \C "<doesn't want the "quotes" to be escaped here>"
+ commandLine.addArgument( "\"" + JOIN_ON_SPACE.join( shellArgs ) +
"\"", false);
+
+ DefaultExecutor executor = new DefaultExecutor();
+ executor.setExitValue( 0 );
+
+ executor.setStreamHandler( new PumpStreamHandler( standardOutHandler,
errorOutHandler ) );
+
+ if ( workingDirectory != null )
+ {
+ executor.setWorkingDirectory( workingDirectory );
+ }
+
+ try
+ {
+ result = executor.execute( commandLine );
+ mayLogEndInfo();
+ }
+ catch ( ExecuteException e )
+ {
+ throw new ExecutionException(
+ "NPANDAY-125-002: Error occurred when executing " +
commandLine.toString(), e
+ );
+ }
+ catch ( IOException e )
+ {
+ throw new ExecutionException(
+ "NPANDAY-125-000: IO error occurred when executing " +
commandLine.toString(), e
+ );
+ }
+
+ if ( failsOnErrorOutput && hasErrorOutput )
+ {
+ throw new ExecutionException(
+ "NPANDAY-125-001: Execution passed, but had error outputs: " +
commandLine.toString()
+ );
+ }
+ }
+
+ private void setupOutputHandlers()
+ {
+ standardOutHandler = new CapturingLogOutputStream()
+ {
+ protected void processLine( String line )
+ {
+ mayLogStartInfo();
+ getLogger().info( "| " + line );
+ }
+ };
+
+ errorOutHandler = new CapturingLogOutputStream()
+ {
+ @Override
+ protected void processLine( String line )
+ {
+ mayLogStartInfo();
+ getLogger().error( "| " + line );
+ hasErrorOutput = true;
+ }
+ };
+ }
+
+ private void mayLogStartInfo()
+ {
+ if (!hasLoggedStartInfo)
+ {
+ getLogger().info( "+--[ RUNNING: " + commandLine + "]");
+ }
+ hasLoggedStartInfo = true;
+ }
+
+ private void mayLogEndInfo()
+ {
+ if (hasLoggedStartInfo)
+ {
+ getLogger().info( "+--[ DONE" + (hasErrorOutput ? " WITH ERRORS" :
"") + " ]" );
+ }
+ }
+
+ public int getResult()
+ {
+ return result;
+ }
+
+ public String getStandardOut()
+ {
+ return standardOutHandler.toString();
+ }
+
+ public String getStandardError()
+ {
+ return errorOutHandler.toString();
+ }
+}
Added:
incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/execution/CommonsExecLogStreamHandler.java
URL:
http://svn.apache.org/viewvc/incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/execution/CommonsExecLogStreamHandler.java?rev=1221795&view=auto
==============================================================================
---
incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/execution/CommonsExecLogStreamHandler.java
(added)
+++
incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/execution/CommonsExecLogStreamHandler.java
Wed Dec 21 16:32:46 2011
@@ -0,0 +1,44 @@
+package npanday.executable.execution;
+
+import org.apache.commons.exec.ExecuteStreamHandler;
+import org.codehaus.plexus.logging.Logger;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+/**
+ * @author <a href="mailto:[email protected]">Lars Corneliussen</a>
+ */
+public class CommonsExecLogStreamHandler
+ implements ExecuteStreamHandler
+{
+ public CommonsExecLogStreamHandler( Logger logger )
+ {
+ }
+
+ public void setProcessInputStream( OutputStream os ) throws IOException
+ {
+
+ }
+
+ public void setProcessErrorStream( InputStream is ) throws IOException
+ {
+ //To change body of implemented methods use File | Settings | File
Templates.
+ }
+
+ public void setProcessOutputStream( InputStream is ) throws IOException
+ {
+ //To change body of implemented methods use File | Settings | File
Templates.
+ }
+
+ public void start() throws IOException
+ {
+ //To change body of implemented methods use File | Settings | File
Templates.
+ }
+
+ public void stop()
+ {
+ //To change body of implemented methods use File | Settings | File
Templates.
+ }
+}
Copied:
incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/execution/PlexusUtilsCommandExecutor.java
(from r1221688,
incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/execution/DefaultCommandExecutor.java)
URL:
http://svn.apache.org/viewvc/incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/execution/PlexusUtilsCommandExecutor.java?p2=incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/execution/PlexusUtilsCommandExecutor.java&p1=incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/execution/DefaultCommandExecutor.java&r1=1221688&r2=1221795&rev=1221795&view=diff
==============================================================================
---
incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/execution/DefaultCommandExecutor.java
(original)
+++
incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/execution/PlexusUtilsCommandExecutor.java
Wed Dec 21 16:32:46 2011
@@ -1,9 +1,7 @@
package npanday.executable.execution;
import npanday.PathUtil;
-import npanday.executable.CommandExecutor;
import npanday.executable.ExecutionException;
-import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.util.cli.CommandLineException;
import org.codehaus.plexus.util.cli.CommandLineUtils;
import org.codehaus.plexus.util.cli.Commandline;
@@ -17,13 +15,9 @@ import java.util.List;
* @author Shane Isbell
* @author <a href="mailto:[email protected]">Lars Corneliussen</a>
*/
-public class DefaultCommandExecutor
- implements CommandExecutor
+public class PlexusUtilsCommandExecutor
+ extends CommandExecutorSkeleton
{
- /**
- * Instance of a plugin logger.
- */
- private Logger logger;
/**
* Standard Out
@@ -40,22 +34,7 @@ public class DefaultCommandExecutor
*/
private int result;
- public void setLogger( Logger logger )
- {
- this.logger = logger;
- }
-
- public void executeCommand( String executable, List<String> commands )
throws ExecutionException
- {
- executeCommand( executable, commands, null, true );
- }
-
- public void executeCommand( String executable, List<String> commands,
boolean failsOnErrorOutput ) throws
- ExecutionException
- {
- executeCommand( executable, commands, null, failsOnErrorOutput );
- }
-
+ @Override
public void executeCommand(
String executable, List<String> commands, File workingDirectory,
boolean failsOnErrorOutput ) throws
ExecutionException
@@ -64,8 +43,8 @@ public class DefaultCommandExecutor
{
commands = new ArrayList<String>();
}
- stdOut = new StandardStreamConsumer( logger );
- stdErr = new ErrorStreamConsumer( logger );
+ stdOut = new StandardStreamConsumer( getLogger() );
+ stdErr = new ErrorStreamConsumer( getLogger() );
Commandline commandline = new CustomCommandline();
@@ -95,7 +74,7 @@ public class DefaultCommandExecutor
}
else if ( workingDirectory != null && !workingDirectory.exists() )
{
- logger.info(
+ getLogger().info(
"NPANDAY-040-006: Did not find executable path for " +
executable + ", " + "will try system path"
);
}
@@ -103,18 +82,10 @@ public class DefaultCommandExecutor
try
{
result = CommandLineUtils.executeCommandLine( commandline, stdOut,
stdErr );
- if ( logger != null )
- {
- logger.debug(
- "NPANDAY-040-005: Executed command: Commandline = " +
commandline + ", Result = " + result
- );
- }
- else
- {
- System.out.println(
- "NPANDAY-040-004: Executed command: Commandline = " +
commandline + ", Result = " + result
- );
- }
+ getLogger().debug(
+ "NPANDAY-040-005: Executed command: Commandline = " +
commandline + ", Result = " + result
+ );
+
if ( ( failsOnErrorOutput && stdErr.hasError() ) || result != 0 )
{
throw new ExecutionException(
Copied:
incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/impl/CommonsExecNetExecutable.java
(from r1221689,
incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/impl/DefaultNetExecutable.java)
URL:
http://svn.apache.org/viewvc/incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/impl/CommonsExecNetExecutable.java?p2=incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/impl/CommonsExecNetExecutable.java&p1=incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/impl/DefaultNetExecutable.java&r1=1221689&r2=1221795&rev=1221795&view=diff
==============================================================================
---
incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/impl/DefaultNetExecutable.java
(original)
+++
incubator/npanday/trunk/components/dotnet-executable/src/main/java/npanday/executable/impl/CommonsExecNetExecutable.java
Wed Dec 21 16:32:46 2011
@@ -26,6 +26,7 @@ import npanday.executable.CommandFilter;
import npanday.executable.ExecutableContext;
import npanday.executable.ExecutionException;
import npanday.executable.NetExecutable;
+import npanday.executable.execution.CommonsExecCommandExecutor;
import npanday.vendor.Vendor;
import org.codehaus.plexus.logging.Logger;
@@ -39,7 +40,7 @@ import java.util.List;
*
* @author Shane Isbell
*/
-public class DefaultNetExecutable
+public class CommonsExecNetExecutable
implements NetExecutable
{
@@ -59,75 +60,37 @@ public class DefaultNetExecutable
return Collections.unmodifiableList(filter.filter( commands ));
}
- public File getExecutionPath()
+ private String getExecutable()
{
- String executable;
- try
- {
- executable = getExecutable();
- }
- catch ( ExecutionException e )
- {
- return null;
- }
+ String executable = executableContext.getExecutableName();
Collection<String> executablePaths =
executableContext.getProbingPaths();
if ( executablePaths != null && executablePaths.size() > 0 )
{
for ( String executablePath : executablePaths )
{
- if ( PathUtil.containsExecutable(executablePath, executable) )
+ File executableFile = PathUtil.getExecutable( new
File(executablePath), executable );
+ if ( executableFile != null )
{
- logger.info("NPANDAY-070-003: Found executable path for "
+ executable + ": \"" + executablePath + "\"");
- return new File( executablePath );
+ logger.info("NPANDAY-126-001: Found executable file for "
+ executable + ": \"" + executableFile + "\"");
+
+ return executableFile.getAbsolutePath();
}
}
}
- logger.warn("NPANDAY-070-004: Did not find path for " + executable + "
in " + executablePaths);
- return null;
+ logger.warn( "NPANDAY-126-002: Did not find path for " + executable +
" in " + executablePaths );
+
+ return executable;
}
public void execute()
throws ExecutionException, PlatformUnsupportedException
{
- innerExecute();
- }
-
- protected void innerExecute() throws ExecutionException,
PlatformUnsupportedException
- {
List<String> commands = getCommands();
- CommandExecutor commandExecutor =
CommandExecutor.Factory.createDefaultCommmandExecutor();
+ CommandExecutor commandExecutor = new CommonsExecCommandExecutor();
commandExecutor.setLogger( logger );
-
- try
- {
- commandExecutor.executeCommand( getExecutable(), commands,
getExecutionPath(), true );
- }
- catch ( ExecutionException e )
- {
- throw new ExecutionException( "NPANDAY-070-000: Execution Path = "
+
- ( ( getExecutionPath() != null ) ?
getExecutionPath().getAbsolutePath() : "unknown" ) + ", Command = " +
- commands, e );
- }
-
- // This check is too broad, as seen in Issue #9903
- // I have not been able to identify an error it is trying to catch
that is not already reported by the exit code above
- //
- //if ( commandExecutor.getStandardOut().contains( "error" ) )
- //{
- // throw new ExecutionException( "NPANDAY-070-001: Command = " +
commands );
- //}
- }
-
- public String getExecutable()
- throws ExecutionException
- {
- if ( executableContext == null )
- {
- throw new ExecutionException( "NPANDAY-070-002: Executable has not
been initialized with a context" );
- }
- return executableContext.getExecutableName();
+ commandExecutor.executeCommand( getExecutable(), commands, null, true
);
}
public Vendor getVendor()
@@ -141,4 +104,4 @@ public class DefaultNetExecutable
this.logger = executableContext.getLogger();
commands = executableContext.getCommands();
}
-}
+}
\ No newline at end of file
Added:
incubator/npanday/trunk/components/dotnet-executable/src/test/groovy/npanday/executable/execution/CommandExecutorTest.groovy
URL:
http://svn.apache.org/viewvc/incubator/npanday/trunk/components/dotnet-executable/src/test/groovy/npanday/executable/execution/CommandExecutorTest.groovy?rev=1221795&view=auto
==============================================================================
---
incubator/npanday/trunk/components/dotnet-executable/src/test/groovy/npanday/executable/execution/CommandExecutorTest.groovy
(added)
+++
incubator/npanday/trunk/components/dotnet-executable/src/test/groovy/npanday/executable/execution/CommandExecutorTest.groovy
Wed Dec 21 16:32:46 2011
@@ -0,0 +1,209 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+ */
+package npanday.executable.execution;
+
+
+import npanday.executable.CommandExecutor
+import npanday.executable.ExecutionException
+import org.codehaus.plexus.logging.Logger
+import org.codehaus.plexus.logging.console.ConsoleLogger
+import org.codehaus.plexus.util.cli.Arg
+import org.codehaus.plexus.util.cli.Commandline
+import org.junit.Test
+import static org.junit.Assert.*
+
+public class CommandExecutorTest
+{
+ private static final String MKDIR = "mkdir";
+
+ private String parentPath;
+
+ private List<String> params = new ArrayList<String>();
+
+ private CommandExecutor cmdExecutor;
+
+ public CommandExecutorTest()
+ {
+ File f = new File( "test" );
+ parentPath = System.getProperty( "user.dir" ) + File.separator +
"target" + File.separator + "test-resources";
+
+ File parentPathFile = new File( parentPath );
+ if ( !parentPathFile.exists() )
+ {
+ parentPathFile.mkdir();
+ }
+
+ cmdExecutor = CommandExecutor.Factory.createDefaultCommmandExecutor();
+ cmdExecutor.setLogger( new ConsoleLogger( Logger.LEVEL_DEBUG, "Command
Executor") );
+ }
+
+ @Test
+ public void testArgsWithFile()
+ throws ExecutionException
+ {
+ Commandline cli = new Commandline();
+ cli.setExecutable( "echo" );
+ final Arg arg = cli.createArg();
+ arg.setValue( "-x:y=" );
+ arg.setFile( new File("c:\te mp") );
+ cli.addArg( arg );
+
+ }
+
+ @Test
+ public void testParamWithNoSpaces()
+ throws ExecutionException
+ {
+ String path = parentPath + File.separator + "sampledirectory";
+
+ params.clear();
+ params.add( path );
+
+ cmdExecutor.executeCommand( MKDIR, params );
+ File dir = new File( path );
+
+ assertTrue( dir.exists() );
+
+ if ( dir.exists() )
+ {
+ dir.delete();
+ }
+ }
+
+ @Test
+ public void testParamWithSpaces()
+ throws ExecutionException
+ {
+ String path = parentPath + File.separator + "sample directory";
+
+ params.clear();
+ params.add( path );
+
+ cmdExecutor.executeCommand( MKDIR, params );
+ File dir = new File( path );
+
+ assertTrue( dir.exists() );
+
+ if ( dir.exists() )
+ {
+ dir.delete();
+ }
+ }
+
+ @Test
+ /**
+ test is related to NPANDAY-366
+ */
+ public void testTooLongCommandName()
+ throws ExecutionException
+ {
+ // we are only interested in exectuing this test
+ // on Windows, to catch the "Command line to long" issue for
cmd.exe.
+ if (!isWindows())
+ return;
+
+ params.clear();
+
+ cmdExecutor.setLogger( new ConsoleLogger( 0, null ) );
+
+ try
+ {
+ cmdExecutor.executeCommand( repeat( 'x', 260 ), params, null,
false );
+ fail( "Expected the command to fail!" );
+ }
+ catch ( ExecutionException e )
+ {
+ System.out.println( cmdExecutor.toString() );
+ // the message is language-specific, but better to ensure an error
than
+ // ignoring the test
+ // assertEquals( "The input line is too long.",
cmdExecutor.getStandardError() );
+ assertEquals( 1, cmdExecutor.getResult() );
+ }
+ }
+
+ @Test
+ /**
+ test is related to NPANDAY-366
+ */
+ public void testTooLongCommandName_withSpace()
+ throws ExecutionException
+ {
+ // we are only interested in exectuing this test
+ // on Windows, to catch the "Command line to long" issue for
cmd.exe.
+ if (!isWindows())
+ return;
+
+ params.clear();
+
+ cmdExecutor.setLogger( new ConsoleLogger( 0, null ) );
+
+ try
+ {
+ cmdExecutor.executeCommand( "echo " + repeat( 'x', 255 ), params,
null, false );
+ fail( "Expected the command to fail!" );
+ }
+ catch ( ExecutionException e )
+ {
+ System.out.println( cmdExecutor.toString() );
+ // the message is language-specific, but better to ensure an error
than
+ // ignoring the test
+ // assertEquals( "The input line is too long.",
cmdExecutor.getStandardError() );
+ assertEquals( 1, cmdExecutor.getResult() );
+ }
+ }
+
+
+ @Test
+ /**
+ test is related to NPANDAY-366
+ */
+ public void testLongCommand()
+ throws ExecutionException
+ {
+ params.clear();
+
+ cmdExecutor.setLogger( new ConsoleLogger( 0, null ) );
+ params.add( repeat( 'a', 260 ) );
+
+ cmdExecutor.executeCommand( "echo", params, null, false );
+ System.out.println( cmdExecutor.toString() );
+
+ assertEquals( repeat( 'a', 260 ), cmdExecutor.getStandardOut() );
+ }
+
+ private static String repeat( String c, int i )
+ {
+ String tst = "";
+ for ( int j = 0; j < i; j++ )
+ {
+ tst = tst + c;
+ }
+ return tst;
+ }
+
+ /**
+ * Simple check if the test is executed on Windows...
+ */
+ private static boolean isWindows()
+ {
+ String osName = System.getProperty("os.name");
+ boolean isWin = (osName.toLowerCase().indexOf("win")) >= 0;
+ return isWin;
+ }
+}
\ No newline at end of file