You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by ay...@apache.org on 2007/03/29 14:40:48 UTC

svn commit: r523687 - in /harmony/enhanced/classlib/trunk: modules/archive/ modules/auth/ modules/instrument/ modules/logging/ modules/luni/ modules/security/ support/src/test/java/tests/support/

Author: ayza
Date: Thu Mar 29 05:40:46 2007
New Revision: 523687

URL: http://svn.apache.org/viewvc?view=rev&rev=523687
Log:
Support_Exec class was refactored, new functionality was added. With this fix Support_Exec.execJava() will supply extra VM arguments from hy.test.vmargs to forked JVM. See HARMONY-3311 for details.  

Modified:
    harmony/enhanced/classlib/trunk/modules/archive/build.xml
    harmony/enhanced/classlib/trunk/modules/auth/build.xml
    harmony/enhanced/classlib/trunk/modules/instrument/build.xml
    harmony/enhanced/classlib/trunk/modules/logging/build.xml
    harmony/enhanced/classlib/trunk/modules/luni/build.xml
    harmony/enhanced/classlib/trunk/modules/security/build.xml
    harmony/enhanced/classlib/trunk/support/src/test/java/tests/support/Support_Exec.java

Modified: harmony/enhanced/classlib/trunk/modules/archive/build.xml
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/archive/build.xml?view=diff&rev=523687&r1=523686&r2=523687
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/archive/build.xml (original)
+++ harmony/enhanced/classlib/trunk/modules/archive/build.xml Thu Mar 29 05:40:46 2007
@@ -228,6 +228,8 @@
                jvm="${test.jre.home}/bin/java">
 
             <jvmarg line="${hy.test.vmargs}" />
+            <!-- Used by Support_Exec.execJava() -->
+            <jvmarg value="-Dhy.test.vmargs=${hy.test.vmargs}" />
 
             <classpath>
                 <pathelement path="${hy.archive.bin.test}"/>

Modified: harmony/enhanced/classlib/trunk/modules/auth/build.xml
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/auth/build.xml?view=diff&rev=523687&r1=523686&r2=523687
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/auth/build.xml (original)
+++ harmony/enhanced/classlib/trunk/modules/auth/build.xml Thu Mar 29 05:40:46 2007
@@ -208,6 +208,9 @@
                jvm="${test.jre.home}/bin/java">
 
             <jvmarg line="${hy.test.vmargs}" />
+            <!-- Used by Support_Exec.execJava() -->
+            <jvmarg value="-Dhy.test.vmargs=${hy.test.vmargs}" />
+
 
             <!-- to pick up junit.jar -->
             <jvmarg value="-Xbootclasspath/a:${hy.auth.bin.test}${path.separator}../../${junit.jar}${path.separator}${hy.hdk}/build/test/support.jar"/>

Modified: harmony/enhanced/classlib/trunk/modules/instrument/build.xml
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/instrument/build.xml?view=diff&rev=523687&r1=523686&r2=523687
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/instrument/build.xml (original)
+++ harmony/enhanced/classlib/trunk/modules/instrument/build.xml Thu Mar 29 05:40:46 2007
@@ -201,6 +201,8 @@
             jvm="${test.jre.home}/bin/java">
 
             <jvmarg line="${hy.test.vmargs}" />
+            <!-- Used by Support_Exec.execJava() -->
+            <jvmarg value="-Dhy.test.vmargs=${hy.test.vmargs}" />
 
             <classpath>
                 <pathelement path="${hy.instrument.bin.test}"/>

Modified: harmony/enhanced/classlib/trunk/modules/logging/build.xml
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/logging/build.xml?view=diff&rev=523687&r1=523686&r2=523687
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/logging/build.xml (original)
+++ harmony/enhanced/classlib/trunk/modules/logging/build.xml Thu Mar 29 05:40:46 2007
@@ -178,6 +178,8 @@
                jvm="${test.jre.home}/bin/java">
 
             <jvmarg line="${hy.test.vmargs}" />
+            <!-- Used by Support_Exec.execJava() -->
+            <jvmarg value="-Dhy.test.vmargs=${hy.test.vmargs}" />
 
         	<!-- Required by various tests that set security manager etc -->
             <jvmarg value="-Djava.security.policy=../../support/src/test/resources/config/testing.policy" />

Modified: harmony/enhanced/classlib/trunk/modules/luni/build.xml
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/build.xml?view=diff&rev=523687&r1=523686&r2=523687
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/build.xml (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/build.xml Thu Mar 29 05:40:46 2007
@@ -390,6 +390,8 @@
                    jvm="${test.jre.home}/bin/java">
 
                 <jvmarg line="${hy.test.vmargs}" />
+                <!-- Used by Support_Exec.execJava() -->
+                <jvmarg value="-Dhy.test.vmargs=${hy.test.vmargs}" />
 
                 <junit-elements />
 

Modified: harmony/enhanced/classlib/trunk/modules/security/build.xml
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/security/build.xml?view=diff&rev=523687&r1=523686&r2=523687
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/security/build.xml (original)
+++ harmony/enhanced/classlib/trunk/modules/security/build.xml Thu Mar 29 05:40:46 2007
@@ -424,6 +424,8 @@
                    dir="${basedir}"
                    jvm="${test.jre.home}/bin/java">
                 <jvmarg line="${hy.test.vmargs}" />
+                <!-- Used by Support_Exec.execJava() -->
+                <jvmarg value="-Dhy.test.vmargs=${hy.test.vmargs}" />
 
                 <junit-elements />
 

Modified: harmony/enhanced/classlib/trunk/support/src/test/java/tests/support/Support_Exec.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/support/src/test/java/tests/support/Support_Exec.java?view=diff&rev=523687&r1=523686&r2=523687
==============================================================================
--- harmony/enhanced/classlib/trunk/support/src/test/java/tests/support/Support_Exec.java (original)
+++ harmony/enhanced/classlib/trunk/support/src/test/java/tests/support/Support_Exec.java Thu Mar 29 05:40:46 2007
@@ -22,187 +22,166 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.PrintStream;
+import java.util.ArrayList;
+import java.util.StringTokenizer;
 
 import junit.framework.TestCase;
 
 public class Support_Exec extends TestCase {
 
+    /**
+     *  This function returns the output of the process as a string
+     */
 	public static String execJava(String[] args, String[] classpath,
 			boolean displayOutput) throws IOException, InterruptedException {
-		// this function returns the output of the process as a string
-		Object[] execArgs = execJava2(args, classpath, displayOutput);
-		Process proc = (Process) execArgs[0];
-
-		StringBuffer output = new StringBuffer();
-		InputStream in = proc.getInputStream();
-		int result;
-		byte[] bytes = new byte[1024];
-		while ((result = in.read(bytes)) != -1) {
-			output.append(new String(bytes, 0, result));
-			if (displayOutput) {
-                System.out.write(bytes, 0, result);
-            }
-		}
-		in.close();
-		proc.waitFor();
-		checkStderr(execArgs);
-		proc.destroy();
-		return output.toString();
+        Object[] arr =
+                execJavaCommon(args, classpath, null, displayOutput, true);
+
+        return getProcessOutput(arr, displayOutput);
 	}
-	
+
+    /**
+     * This function returns the output of the process as a string
+     */
 	public static String execJava(String[] args, String[] classpath, String[] envp,
 			boolean displayOutput) throws IOException, InterruptedException {
-		// this function returns the output of the process as a string
-		Object[] execArgs = execJavaNoSystemClasspath(args, classpath, envp, displayOutput);
-		Process proc = (Process) execArgs[0];
-
-		StringBuffer output = new StringBuffer();
-		InputStream in = proc.getInputStream();
-		int result;
-		byte[] bytes = new byte[1024];
-		while ((result = in.read(bytes)) != -1) {
-			output.append(new String(bytes, 0, result));
-			if (displayOutput)
-				System.out.write(bytes, 0, result);
-		}
-		in.close();
-		proc.waitFor();
-		checkStderr(execArgs);
-		proc.destroy();
-		return output.toString();
+		Object[] arr =
+                execJavaCommon(args, classpath, envp, displayOutput, false);
+
+        return getProcessOutput(arr, displayOutput);
 	}
 
+    private static String getProcessOutput(Object[] arr, boolean displayOutput)
+            throws IOException, InterruptedException {
+        Process proc = (Process) arr[0];
+        StringBuilder output = new StringBuilder();
+        InputStream in = proc.getInputStream();
+        int result;
+        byte[] bytes = new byte[1024];
 
-	public static void checkStderr(Object[] execArgs) {
-		StringBuffer errBuf = (StringBuffer) execArgs[1];
-		synchronized (errBuf) {
-			if (errBuf.length() > 0) {
-				fail(errBuf.toString());
-			}
-		}
-	}
-	
-	private static Object[] execJavaNoSystemClasspath(String[] args, String[] classpath, String[] envp,
-			final boolean displayOutput) throws IOException, InterruptedException {
-		// this function returns the resulting process from the exec
-		int baseArgs = 0;
-		String[] execArgs = null;
-        baseArgs = (classpath == null) ? 1 : 3;
-        execArgs = new String[baseArgs + args.length];
-        String executable = System.getProperty("java.home");
-        if (!executable.endsWith(File.separator))
-            executable += File.separator;
-        executable += "bin" + File.separator;
-        execArgs[0] = executable + "java";
-        
-		String classPathString = "";
-		if (classpath != null) {
-            for (int i = 0; i < classpath.length; i++) {
-            	if( i != 0) {
-            		classPathString += File.pathSeparator;
-            	}
-                classPathString +=  classpath[i];
+        while ((result = in.read(bytes)) != -1) {
+            output.append(new String(bytes, 0, result));
+
+            if (displayOutput) {
+                System.out.write(bytes, 0, result);
             }
-            execArgs[1] = "-cp";
-            execArgs[2] = classPathString;
-		}
+        }
 
-        for (int i = 0; i < args.length; i++)
-            execArgs[baseArgs + i] = args[i];
+        in.close();
+        proc.waitFor();
+        checkStderr(arr);
+        proc.destroy();
 
-		final Process proc = Runtime.getRuntime().exec(execArgs, envp);
-		final StringBuffer errBuf = new StringBuffer();
-		Thread errThread = new Thread(new Runnable() {
-			public void run() {
-				synchronized (errBuf) {
-					synchronized (proc) {
-						proc.notifyAll();
-					}
-					InputStream err = proc.getErrorStream();
-					int result;
-					byte[] bytes = new byte[1024];
-					try {
-						while ((result = err.read(bytes)) != -1) {
-							if(displayOutput) {
-							   System.err.write(bytes, 0, result);
-							}
-							errBuf.append(new String(bytes));
-						}
-						err.close();
-					} catch (IOException e) {
-						e.printStackTrace();
-						ByteArrayOutputStream out = new ByteArrayOutputStream();
-						PrintStream printer = new PrintStream(out);
-						e.printStackTrace(printer);
-						printer.close();
-						errBuf.append(new String(out.toByteArray()));
-					}
-				}
+        return output.toString();
+    }
+
+    public static void checkStderr(Object[] execArgs) {
+		StringBuilder errBuf = (StringBuilder) execArgs[1];
+
+        synchronized (errBuf) {
+			if (errBuf.length() > 0) {
+				fail(errBuf.toString());
 			}
-		});
-		synchronized (proc) {
-			errThread.start();
-			// wait for errThread to start
-			proc.wait();
 		}
-		return new Object[] { proc, errBuf };
 	}
 
-
-	public static Object[] execJava2(String[] args, String[] classpath,
+    public static Object[] execJava2(String[] args, String[] classpath,
 			boolean displayOutput) throws IOException, InterruptedException {
-		// this function returns the resulting process from the exec
-		int baseArgs = 0;
-		String[] execArgs = null;
-//		String vendor = System.getProperty("java.vendor");
-		String classPathString = "";
-		if (classpath != null)
-            for (String element : classpath) {
-                classPathString += File.pathSeparator + element;
-            }
+        return execJavaCommon(args, classpath, null, displayOutput, true);
+    }
+
+    private static Object[] execJavaCommon(String[] args, String[] classpath,
+			String[] envp, boolean displayOutput, boolean appendToSystemClassPath)
+            throws IOException, InterruptedException {
+        // this function returns the resulting process from the exec
+		ArrayList<String> execArgs = null;
+		StringBuilder classPathString = new StringBuilder();
+        StringBuilder command;
+        String executable;
+        String testVMArgs;
+        StringTokenizer st;
 
-        baseArgs = 3;
-        execArgs = new String[baseArgs + args.length];
-        String executable = System.getProperty("java.home");
+        execArgs = new ArrayList<String>(3 + args.length);
+
+        // construct the name of executable file
+        executable = System.getProperty("java.home");
         if (!executable.endsWith(File.separator)) {
             executable += File.separator;
         }
         executable += "bin" + File.separator;
-        execArgs[0] = executable + "java";
-        execArgs[1] = "-cp";
-        execArgs[2] = System.getProperty("java.class.path") + classPathString;
-
-        for (int i = 0; i < args.length; i++) {
-            execArgs[baseArgs + i] = args[i];
-        }
-        StringBuffer command = new StringBuffer(execArgs[0]);
-        for (int i = 1; i < execArgs.length; i++) {
-            command.append(" " + execArgs[i]);
+        execArgs.add(executable + "java");
+
+        // add classpath string
+        if (classpath != null) {
+            for (String element : classpath) {
+                classPathString.append(File.pathSeparator);
+                classPathString.append(element);
+            }
+        }
+        if (appendToSystemClassPath) {
+            execArgs.add("-cp");
+            execArgs.add(System.getProperty("java.class.path") +
+                    classPathString);
+        } else {
+            if (classpath != null) {
+                execArgs.add("-cp");
+                execArgs.add(classPathString.toString());
+            }
+        }
+
+        // parse hy.test.vmargs if was given
+        testVMArgs = System.getProperty("hy.test.vmargs");
+        if (testVMArgs != null) {
+            st = new StringTokenizer(testVMArgs, " ");
+
+            while (st.hasMoreTokens()) {
+                execArgs.add(st.nextToken());
+            }
         }
-        System.out.println();
-        System.out.println("Exec: " + command.toString());
 
-		final Process proc = Runtime.getRuntime().exec(execArgs);
-		final StringBuffer errBuf = new StringBuffer();
+        // add custom args given as parameter
+        for (String arg : args) {
+            execArgs.add(arg);
+        }
+
+        // construct command line string and print it to stdout
+        //if (displayOutput) {
+            command = new StringBuilder(execArgs.get(0));
+            for (int i = 1; i < execArgs.size(); i++) {
+                command.append(" ");
+                command.append(execArgs.get(i));
+            }
+            System.out.println();
+            System.out.println("Exec: " + command.toString());
+        //}
+
+        // execute java process
+        final Process proc = Runtime.getRuntime().exec(
+                execArgs.toArray(new String[execArgs.size()]), envp);
+		final StringBuilder errBuf = new StringBuilder();
 		Thread errThread = new Thread(new Runnable() {
 			public void run() {
 				synchronized (errBuf) {
-					synchronized (proc) {
+                    InputStream err;
+                    int result;
+                    byte[] bytes = new byte[1024];
+
+                    synchronized (proc) {
 						proc.notifyAll();
 					}
-					InputStream err = proc.getErrorStream();
-					int result;
-					byte[] bytes = new byte[1024];
-					try {
+
+                    err = proc.getErrorStream();
+                    try {
 						while ((result = err.read(bytes)) != -1) {
 							System.err.write(bytes, 0, result);
 							errBuf.append(new String(bytes));
 						}
 						err.close();
 					} catch (IOException e) {
-						e.printStackTrace();
 						ByteArrayOutputStream out = new ByteArrayOutputStream();
 						PrintStream printer = new PrintStream(out);
+
+                        e.printStackTrace();
 						e.printStackTrace(printer);
 						printer.close();
 						errBuf.append(new String(out.toByteArray()));
@@ -210,12 +189,14 @@
 				}
 			}
 		});
-		synchronized (proc) {
+
+        synchronized (proc) {
 			errThread.start();
 			// wait for errThread to start
 			proc.wait();
 		}
-		return new Object[] { proc, errBuf };
+
+        return new Object[] { proc, errBuf };
 	}
 
 }