commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sgoes...@apache.org
Subject svn commit: r985258 - in /commons/proper/exec/trunk/src: main/java/org/apache/commons/exec/util/StringUtils.java test/java/org/apache/commons/exec/CommandLineTest.java test/java/org/apache/commons/exec/DefaultExecutorTest.java
Date Fri, 13 Aug 2010 16:15:03 GMT
Author: sgoeschl
Date: Fri Aug 13 16:15:02 2010
New Revision: 985258

URL: http://svn.apache.org/viewvc?rev=985258&view=rev
Log:
String substitution handles now java.io.File instances in order to create a cross-platform
file name

Modified:
    commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/util/StringUtils.java
    commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/CommandLineTest.java
    commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/DefaultExecutorTest.java

Modified: commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/util/StringUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/util/StringUtils.java?rev=985258&r1=985257&r2=985258&view=diff
==============================================================================
--- commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/util/StringUtils.java
(original)
+++ commons/proper/exec/trunk/src/main/java/org/apache/commons/exec/util/StringUtils.java
Fri Aug 13 16:15:02 2010
@@ -90,8 +90,18 @@ public class StringUtils {
                         }
 
                         if (nameBuf.length() > 0) {
+
+                            String value;
                             Object temp = vars.get(nameBuf.toString());
-                            String value = (temp != null ? temp.toString() : null);
+
+                            if(temp instanceof File) {
+                                // for a file we have to fix the separator chars to allow
+                                // cross-platform compatibility
+                                value = fixFileSeparatorChar(((File) temp).getAbsolutePath());
+                            }
+                            else {
+                                value = (temp != null ? temp.toString() : null);    
+                            }
 
                             if (value != null) {
                                 argBuf.append(value);
@@ -148,7 +158,7 @@ public class StringUtils {
     }
 
     /**
-     * Fixes the file sperator char for the target platform
+     * Fixes the file separator char for the target platform
      * using the following replacement.
      * 
      * <ul>

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=985258&r1=985257&r2=985258&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 Fri
Aug 13 16:15:02 2010
@@ -18,6 +18,7 @@
 
 package org.apache.commons.exec;
 
+import java.io.File;
 import java.util.Arrays;
 import java.util.HashMap;
 
@@ -309,6 +310,8 @@ public class CommandLineTest extends Tes
         HashMap substitutionMap = new HashMap();
         substitutionMap.put("JAVA_HOME", "/usr/local/java");
         substitutionMap.put("appMainClass", "foo.bar.Main");
+        substitutionMap.put("file1", new File("./pom.xml"));
+        substitutionMap.put("file2", new File(".\\temp\\READ ME.txt"));
 
         HashMap incompleteMap = new HashMap();
         incompleteMap.put("JAVA_HOME", "/usr/local/java");
@@ -334,6 +337,10 @@ public class CommandLineTest extends Tes
         assertTrue(cmdl.getExecutable().indexOf("${JAVA_HOME}") < 0 );
         assertTrue(cmdl.getExecutable().indexOf("local") > 0 );
         assertEquals(new String[] {"${appMainClass}"}, cmdl.getArguments());
+
+        // pass a file
+        cmdl = CommandLine.parse("${JAVA_HOME}/bin/java ${appMainClass} ${file1} ${file2}",
substitutionMap);
+        assertTrue(cmdl.getExecutable().indexOf("${file}") < 0 );
     }
 
     /**

Modified: commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/DefaultExecutorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/DefaultExecutorTest.java?rev=985258&r1=985257&r2=985258&view=diff
==============================================================================
--- commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/DefaultExecutorTest.java
(original)
+++ commons/proper/exec/trunk/src/test/java/org/apache/commons/exec/DefaultExecutorTest.java
Fri Aug 13 16:15:02 2010
@@ -519,7 +519,7 @@ public class DefaultExecutorTest extends
             commandLine.addArgument("/h");
             commandLine.addArgument("${file}");
             HashMap map = new HashMap();
-            map.put("file", "./pom.xml");
+            map.put("file", new File("./pom.xml"));
             commandLine.setSubstitutionMap(map);
 
             // asynchronous execution is defined by using a 'resultHandler'
@@ -650,6 +650,7 @@ public class DefaultExecutorTest extends
      * Test EXEC-36 see https://issues.apache.org/jira/browse/EXEC-36
      *
      * Original example from Kai Hu
+     * @throws Exception the test failed
      */
     public void testExec36_1() throws Exception {
 
@@ -723,19 +724,31 @@ public class DefaultExecutorTest extends
      * Test a complex real example found at
      * http://blogs.msdn.com/b/astebner/archive/2005/12/13/503471.aspx
      */
-    public void _testExec36_3() throws Exception {
+    public void testExec36_3() throws Exception {
 
-        CommandLine cmdl;
-        File file = new File("c:\\Documents and Settings\\myusername\\Local Settings\\Temp\\netfx.log");
       
-        Map map = new HashMap();
-        map.put("FILE", file);
+        String expected;
 
         // the original command line
-        // dotnetfx.exe /q:a /c:"install.exe /l ""c:\Documents and Settings\myusername\Local
Settings\Temp\netfx.log"" /q"
+        // dotnetfx.exe /q:a /c:"install.exe /l ""\Documents and Settings\myusername\Local
Settings\Temp\netfx.log"" /q"
 
-        String expected = "dotnetfx.exe\n" +
+        if(OS.isFamilyWindows()) {
+            expected = "dotnetfx.exe\n" +
                 "/q:a\n" +
-                "/c:\"install.exe /l \"\"c:\\Documents and Settings\\myusername\\Local Settings\\Temp\\netfx.log\"\"
/q\"";
+                "/c:\"install.exe /l \"\"\\Documents and Settings\\myusername\\Local Settings\\Temp\\netfx.log\"\"
/q\"";
+        }
+        else if(OS.isFamilyUnix()) {
+            expected = "dotnetfx.exe\n" +
+                "/q:a\n" +
+                "/c:\"install.exe /l \"\"/Documents and Settings/myusername/Local Settings/Temp/netfx.log\"\"
/q\"";
+        }
+        else {
+            return;
+        }
+
+        CommandLine cmdl;
+        File file = new File("/Documents and Settings/myusername/Local Settings/Temp/netfx.log");
+        Map map = new HashMap();
+        map.put("FILE", file);
 
         cmdl = new CommandLine(printArgsScript);
         cmdl.setSubstitutionMap(map);



Mime
View raw message