Commits

Michael Heemskerk committed c573368

CRUC-4793: Review rework

Comments (0)

Files changed (1)

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

 import java.util.List;
 import java.util.Properties;
 
+/**
+ * Utility class that calls 'echo'. This is implemented as a separate java application (with a main) to allow it to be called
+ * with different -Dfile.encoding settings. See {@link ExternalProcessTest} for more info. 
+ */
 public class CallEcho {
-	
-	  static public String byteToHex(byte b) {
-		    // Returns hex String representation of byte b
-		    char hexDigit[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
-		        'a', 'b', 'c', 'd', 'e', 'f' };
-		    char[] array = { hexDigit[(b >> 4) & 0x0f], hexDigit[b & 0x0f] };
-		    return new String(array);
-		  }
-	  
-	  static public String charToHex(char c) {
-		    // Returns hex String representation of char c
-		    byte hi = (byte) (c >>> 8);
-		    byte lo = (byte) (c & 0xff);
-		    return byteToHex(hi) + byteToHex(lo);
-		  }
-	  
-	  /**
+
+    static public String byteToHex(byte b) {
+        // Returns hex String representation of byte b
+        char hexDigit[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
+        char[] array = { hexDigit[(b >> 4) & 0x0f], hexDigit[b & 0x0f] };
+        return new String(array);
+    }
+
+    static public String charToHex(char c) {
+        // Returns hex String representation of char c
+        byte hi = (byte) (c >>> 8);
+        byte lo = (byte) (c & 0xff);
+        return byteToHex(hi) + byteToHex(lo);
+    }
+
+    /**
      * Helper method to print out a string as a list of bytes (integers)
+     * 
      * @param value
      * @return
      */
         }
         StringBuilder builder = new StringBuilder();
         String sep = "";
-        for (int i = 0 ; i < value.length() ; ++i) {
+        for (int i = 0; i < value.length(); ++i) {
             builder.append(sep).append(charToHex(value.charAt(i)));
             sep = " ";
         }
         return builder.toString();
     }
-    
+
     /**
      * @param args
      */
     public static void main(String[] args) throws IOException, InterruptedException {
         Properties testStrings = new Properties();
         testStrings.load(CallEcho.class.getResourceAsStream("/echostrings.properties"));
-        
+
         StringBuilder echoString = new StringBuilder();
         String separator = "";
         for (String arg : args) {
                 separator = " ";
             }
         }
-        
+
         final String echo = echoString.toString();
         if (!echo.isEmpty()) {
             final String[] result = new String[1];
-            List<String> echoCmd = Arrays.asList("java", "-Dfile.encoding="+System.getProperty("file.encoding"), "com.atlassian.utils.process.Echo", echo);
-            // the command line arguments will be converted using the default encoding (file.encoding). The lineoutputhandler needs to use the same encoding
-            // to process the results. Otherwise, encoding errors will occur.
-            ExternalProcess process = new ExternalProcessBuilder()
-            	.command(echoCmd)
-            	.timeout(2000)
-            	.handlers(new LineOutputHandler(System.getProperty("file.encoding")) {
-                @Override
-                protected void processLine(int lineNum, String line) {
-                    result[0] = line;
-                }
-            }, new LineOutputHandler() {
-				@Override
-				protected void processLine(int lineNum, String line) {
-					System.err.println(line);
-				}
-			}).build();
-            process.setEnvironment(new String[] {"CLASSPATH=" + System.getProperty("java.class.path")});
+            // 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 
+            List<String> echoCmd = Arrays.asList("java", "-Dfile.encoding=" + System.getProperty("file.encoding"), "com.atlassian.utils.process.Echo", echo);
+            
+            // the command line arguments will be converted using the default encoding (file.encoding). 
+            // The lineoutputhandler needs to use the same encoding to process the results. Otherwise, encoding errors will occur.
+            ExternalProcess process = new ExternalProcessBuilder().command(echoCmd).timeout(2000)
+                    .handlers(new LineOutputHandler(System.getProperty("file.encoding")) {
+                        @Override
+                        protected void processLine(int lineNum, String line) {
+                            result[0] = line;
+                        }
+                    }, new LineOutputHandler() {
+                        @Override
+                        protected void processLine(int lineNum, String line) {
+                            System.err.println(line);
+                        }
+                    }).build();
+            process.setEnvironment(new String[] { "CLASSPATH=" + System.getProperty("java.class.path") });
             process.execute();
-            
-            // output the expected and actual strings as a list of bytes to prevent encodings to mess up the results
+
+            // output the expected and actual strings as a list of bytes to
+            // prevent encodings to mess up the results
             System.out.println(getBytes(echo));
             System.out.println(getBytes(result[0]));
         }