Commits

Bryan Turner committed 689a21b Merge

Merging i386/process-utils into atlassian/process-utils for default branch

  • Participants
  • Parent commits ec8d93a, 4381b3f

Comments (0)

Files changed (5)

 5c95a4190871d8cfbc7bf61991bb0b8751288513 atlassian-processutils-1.5-rc3
 323e97095d3a837844b5c8037f1ef158e965d8d8 atlassian-processutils-1.5-rc4
 e9d64cf7f65991c0fee369b37f0268d888d6d025 atlassian-processutils-1.5-rc5
+9a018567077cff394c9985146613db5b675d7240 atlassian-processutils-1.5-rc5.stash
   <name>Atlassian Process Utils</name>
 
   <scm>
-    <connection>scm:hg:ssh://hg@bitbucket.org/i386/atlassian-processutils</connection>
-    <developerConnection>scm:hg:ssh://hg@bitbucket.org/i386/atlassian-processutils</developerConnection>
-    <url>scm:hg:ssh://hg@bitbucket.org/i386/atlassian-processutils</url>
+    <connection>scm:hg:ssh://hg@bitbucket.org/atlassian/atlassian-processutils</connection>
+    <developerConnection>scm:hg:ssh://hg@bitbucket.org/atlassian/atlassian-processutils</developerConnection>
+    <url>scm:hg:ssh://hg@bitbucket.org/atlassian/atlassian-processutils</url>
   </scm>
 
 

File src/test/java/com/atlassian/utils/process/CallEcho.java

 package com.atlassian.utils.process;
 
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
+import java.io.OutputStreamWriter;
 import java.io.UnsupportedEncodingException;
+import java.nio.charset.Charset;
 import java.util.Arrays;
 import java.util.HashMap;
 import java.util.List;
 
     /**
      * Helper method to print out a string as a list of bytes (integers)
-     * 
+     *
      * @param value
      * @return
      */
-    private static String getBytes(String value) throws UnsupportedEncodingException {
+    public static String getBytes(String value) throws UnsupportedEncodingException {
         if (value == null) {
             return null;
         }
         return builder.toString();
     }
 
+    private static String transcode(String input, String charset) throws IOException {
+        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+        // it's expected that java will use the default encoding to w
+        OutputStreamWriter writer = new OutputStreamWriter(outputStream, Charset.defaultCharset());
+        writer.write(input);
+        writer.close();
+
+        return outputStream.toString(charset);
+    }
+    
     /**
      * @param args
      */
             final String[] result = new String[1];
             // we're calling yet another java process here because the echo application on windows is doing character conversions as well
             // using a java process at least gives us control over the encodings it uses. Java will use the default encoding (-Dfile.encoding) 
-            // to convert command line arguments into Java Strings 
+            // to convert command line arguments into Java Strings
             List<String> echoCmd = Arrays.asList("java", "-Dfile.encoding=" + System.getProperty("file.encoding"), "com.atlassian.utils.process.Echo", echo);
+
+            // using the error stream to write debug information
+            System.err.println("Call-Echo: real bytes           : " + getBytes(echo));
+            
+            // this is the expected behaviour; the unicode bytes will be 
+            System.err.println("Call-Echo: expected transcode   : " + getBytes(transcode(echo, Charset.defaultCharset().name())) + " - " + Charset.defaultCharset());
+            System.err.println("Call-Echo: transcoded to UTF-8  : " + getBytes(transcode(echo, "UTF-8")));
+            System.err.println("Call-Echo: transcoded to 8859-1 : " + getBytes(transcode(echo, "ISO-8859-1")));
             Map<String, String> echoEnv = new HashMap<String, String>()
             {{
                 put("CLASSPATH", System.getProperty("java.class.path"));
                         protected void processLine(int lineNum, String line) {
                             result[0] = line;
                         }
-                    }, new LineOutputHandler() {
+                    }, new LineOutputHandler(System.getProperty("file.encoding")) {
                         @Override
                         protected void processLine(int lineNum, String line) {
                             System.err.println(line);

File src/test/java/com/atlassian/utils/process/Echo.java

 package com.atlassian.utils.process;
 
+import java.nio.charset.Charset;
+
 public class Echo {
 	/**
 	 * @param args
 	 */
 	public static void main(String[] args) {
-		System.err.println("Encoding " + System.getProperty("file.encoding"));
-		System.err.println("Platform " + System.getProperty("os.name").toLowerCase());
+        for (String arg : args) {
+            try {
+                // using the error stream to write debug information
+                System.err.println("Echo: received param     " + CallEcho.getBytes(arg) + " in encoding " + Charset.defaultCharset());
+            } catch (Exception e) {
+                // no-op
+            }
+        }
+
 		String sep = "";
 		for (String str : args) {
 			System.out.println(sep + str);

File src/test/java/com/atlassian/utils/process/ExternalProcessTest.java

         Process process = processBuilder.start();
         
         BufferedReader reader = null;
+        BufferedReader errReader = null;
         try {
             reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
+            errReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
             EchoResult result = new EchoResult();
             result.input = reader.readLine();
             result.output = reader.readLine();
+
+            // write the process error stream to System.err. This is not essential for the test, but helps in understanding
+            // what's going on.
+            String line;
+            while ((line = errReader.readLine()) != null) {
+                System.err.println(line);
+            }
             
             return result;
         } finally {
         	assertEquals("UTF-8 - incompatibility detected", result.input, result.output);
         }
     }
-    
-    @Test
+
+    // Test has been disabled because (Ubuntu) Linux appears to always encode external process parameters as UTF-8
+    //@Test
     public void testCommandLineArgumentsWin1252() throws Exception {
-        if (Charset.isSupported("windows-1252")) {
+        if (Charset.isSupported("windows-1252") && Charset.isSupported("iso-8859-1")) {
             EchoResult result = spawnEcho("windows-1252", "iso-8859-1");
             assertEquals("Windows cp 1252 -  incompatibility detected:", result.input, result.output);
         }
             assertEquals("Windows cp 437 - incompatibility detected:", result.input, result.output);
         }
     }
-    
-    @Test
+
+    // Test has been disabled because (Ubuntu) Linux appears to always encode external process parameters as UTF-8
+    //@Test
     public void testCommandLineArgumentsIso88591() throws Exception {
-        EchoResult result = spawnEcho("iso-8859-1", "iso-8859-1");
+        EchoResult result = spawnEcho("ISO-8859-1", "iso-8859-1");
         assertEquals("ISO-8859-1 - incompatibility detected:", result.input, result.output);
     }