FileDocCategorySizeDatePackage
Support_Exec.javaAPI DocAndroid 1.5 API7374Wed May 06 22:41:06 BST 2009tests.support

Support_Exec

public class Support_Exec extends TestCase

Fields Summary
static final boolean
againstDalvik
Constructors Summary
Methods Summary
public static voidcheckStderr(java.lang.Object[] execArgs)

        StringBuilder errBuf = (StringBuilder) execArgs[1];

        synchronized (errBuf) {
            if (errBuf.length() > 0) {
                fail(errBuf.toString());
            }
        }
    
public static java.lang.Object[]execAndDigestOutput(java.lang.String[] cmdLine, java.lang.String[] envp)


//        System.out.println("Commandline BEGIN");
//        for (int i = 0; i < cmdLine.length; i++) {
//            System.out.println(cmdLine[i]);
//        }
//        System.out.println("END");

        final Process proc = Runtime.getRuntime().exec(cmdLine, envp);
        final StringBuilder errBuf = new StringBuilder();

        Thread errThread = new Thread(new Runnable() {
            public void run() {
                synchronized (errBuf) {
                    InputStream err;
                    int result;
                    byte[] bytes = new byte[1024];

                    synchronized (proc) {
                        proc.notifyAll();
                    }

                    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) {
                        ByteArrayOutputStream out = new ByteArrayOutputStream();
                        PrintStream printer = new PrintStream(out);

                        e.printStackTrace();
                        e.printStackTrace(printer);
                        printer.close();
                        errBuf.append(new String(out.toByteArray()));
                    }
                }
            }
        });

        synchronized (proc) {
            errThread.start();
            // wait for errThread to start
            proc.wait();
        }

        return new Object[] { proc, errBuf };
    
public static java.lang.StringexecJava(java.lang.String[] args, java.lang.String[] classpath, boolean displayOutput)
This function returns the output of the process as a string

        String platform = System.getProperty("java.vendor");
        againstDalvik = (platform.contains("Android"));
    
        Object[] arr =
                execJavaCommon(args, classpath, null, displayOutput, true);

        return getProcessOutput(arr, displayOutput);
    
public static java.lang.StringexecJava(java.lang.String[] args, java.lang.String[] classpath, java.lang.String[] envp, boolean displayOutput)
This function returns the output of the process as a string

        Object[] arr =
                execJavaCommon(args, classpath, envp, displayOutput, false);

        return getProcessOutput(arr, displayOutput);
    
public static java.lang.Object[]execJava2(java.lang.String[] args, java.lang.String[] classpath, boolean displayOutput)

        return execJavaCommon(args, classpath, null, displayOutput, true);
    
private static java.lang.Object[]execJavaCommon(java.lang.String[] args, java.lang.String[] classpath, java.lang.String[] envp, boolean displayOutput, boolean appendToSystemClassPath)

        // 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;

        execArgs = new ArrayList<String>(3 + args.length);

        // construct the name of executable file
        if (againstDalvik) {
            execArgs.add("dalvikvm");
        } else {
            execArgs.add("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());
            }
        }

        // add custom args given as parameter
        for (String arg : args) {
            execArgs.add(arg);
        }

        if (displayOutput) {
            // Construct command line string and print it to stdout.
            command = new StringBuilder(execArgs.get(0));
            for (int i = 1; i < execArgs.size(); i++) {
                command.append(" ");
                command.append(execArgs.get(i));
            }
        }

        // execute java process
        return execAndDigestOutput(execArgs.toArray(new String[execArgs.size()]), envp);
    
public static java.lang.StringgetProcessOutput(java.lang.Object[] arr, boolean displayOutput)

        Process proc = (Process) arr[0];
        StringBuilder output = new StringBuilder();
        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(arr);
        proc.destroy();

        return output.toString();