jdk/test/tools/launcher/TestHelper.java
author mchung
Tue, 29 Mar 2011 15:50:55 -0700
changeset 9013 eedac0b9f552
parent 5506 202f599c92aa
child 9244 84d85c90d5db
permissions -rw-r--r--
7024172: Move BufferPoolMXBean and PlatformLoggingMXBean java.lang.management Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1323
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
     1
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
     2
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4340
diff changeset
     3
 * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
1323
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
     4
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
     5
 *
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
     6
 * This code is free software; you can redistribute it and/or modify it
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
     7
 * under the terms of the GNU General Public License version 2 only, as
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
     8
 * published by the Free Software Foundation.
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
     9
 *
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    13
 * version 2 for more details (a copy is included in the LICENSE file that
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    14
 * accompanied this code).
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    15
 *
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License version
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    19
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4340
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4340
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4340
diff changeset
    22
 * questions.
1323
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    23
 */
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    24
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    25
import javax.tools.ToolProvider;
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    26
import java.io.BufferedReader;
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    27
import java.io.File;
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    28
import java.io.FileNotFoundException;
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    29
import java.io.FileOutputStream;
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    30
import java.io.InputStreamReader;
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    31
import java.io.PrintStream;
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    32
import java.util.ArrayList;
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    33
import java.util.List;
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    34
import java.util.Map;
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    35
import javax.tools.JavaCompiler;
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    36
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    37
/**
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    38
 * This class provides some common utilites for the launcher tests.
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    39
 */
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    40
public enum TestHelper {
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    41
    INSTANCE;
4340
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
    42
    static final String JAVAHOME = System.getProperty("java.home");
1323
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    43
    static final boolean isSDK = JAVAHOME.endsWith("jre");
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    44
    static final String javaCmd;
4340
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
    45
    static final String java64Cmd;
1323
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    46
    static final String javacCmd;
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    47
    static final JavaCompiler compiler;
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    48
4340
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
    49
    static final boolean debug = Boolean.getBoolean("TestHelper.Debug");
1323
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    50
    static final boolean isWindows =
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    51
            System.getProperty("os.name", "unknown").startsWith("Windows");
4340
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
    52
    static final boolean is64Bit =
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
    53
            System.getProperty("sun.arch.data.model").equals("64");
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
    54
    static final boolean is32Bit =
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
    55
            System.getProperty("sun.arch.data.model").equals("32");
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
    56
    static final boolean isSolaris =
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
    57
            System.getProperty("os.name", "unknown").startsWith("SunOS");
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
    58
    static final boolean isLinux =
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
    59
            System.getProperty("os.name", "unknown").startsWith("Linux");
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
    60
    static final boolean isDualMode = isSolaris;
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
    61
    static final boolean isSparc = System.getProperty("os.arch").startsWith("sparc");
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
    62
1323
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    63
    static int testExitValue = 0;
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    64
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    65
    static {
4340
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
    66
        if (is64Bit && is32Bit) {
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
    67
            throw new RuntimeException("arch model cannot be both 32 and 64 bit");
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
    68
        }
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
    69
        if (!is64Bit && !is32Bit) {
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
    70
            throw new RuntimeException("arch model is not 32 or 64 bit ?");
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
    71
        }
1323
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    72
        compiler = ToolProvider.getSystemJavaCompiler();
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    73
        File binDir = (isSDK) ? new File((new File(JAVAHOME)).getParentFile(), "bin")
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    74
            : new File(JAVAHOME, "bin");
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    75
        File javaCmdFile = (isWindows)
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    76
                ? new File(binDir, "java.exe")
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    77
                : new File(binDir, "java");
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    78
        javaCmd = javaCmdFile.getAbsolutePath();
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    79
        if (!javaCmdFile.canExecute()) {
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    80
            throw new RuntimeException("java <" + TestHelper.javaCmd + "> must exist");
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    81
        }
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    82
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    83
        File javacCmdFile = (isWindows)
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    84
                ? new File(binDir, "javac.exe")
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    85
                : new File(binDir, "javac");
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    86
        javacCmd = javacCmdFile.getAbsolutePath();
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    87
        if (!javacCmdFile.canExecute()) {
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    88
            throw new RuntimeException("java <" + javacCmd + "> must exist");
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
    89
        }
4340
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
    90
        if (isSolaris) {
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
    91
            File sparc64BinDir = new File(binDir,isSparc ? "sparcv9" : "amd64");
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
    92
            File java64CmdFile= new File(sparc64BinDir, "java");
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
    93
            if (java64CmdFile.exists() && java64CmdFile.canExecute()) {
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
    94
                java64Cmd = java64CmdFile.getAbsolutePath();
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
    95
            } else {
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
    96
                java64Cmd = null;
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
    97
            }
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
    98
        } else {
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
    99
            java64Cmd = null;
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
   100
        }
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
   101
    }
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
   102
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
   103
    /*
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
   104
     * usually the jre/lib/arch-name is the same as os.arch, except for x86.
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
   105
     */
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
   106
    static String getJreArch() {
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
   107
        String arch = System.getProperty("os.arch");
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
   108
        return arch.equals("x86") ? "i386" : arch;
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
   109
    }
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
   110
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
   111
    /*
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
   112
     * A convenience method to create a jar with jar file name and defs
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
   113
     */
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
   114
    static void createJar(File jarName, String... mainDefs)
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
   115
            throws FileNotFoundException{
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
   116
        createJar(null, jarName, new File("Foo"), mainDefs);
1323
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   117
    }
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   118
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   119
    /*
1329
ce13b59a8401 4459600: java -jar fails to run Main-Class if classname followed by whitespace.
ksrini
parents: 1323
diff changeset
   120
     * A convenience method to create a java file, compile and jar it up, using
ce13b59a8401 4459600: java -jar fails to run Main-Class if classname followed by whitespace.
ksrini
parents: 1323
diff changeset
   121
     * the sole class file name in the jar, as the Main-Class attribute value.
1323
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   122
     */
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   123
    static void createJar(File jarName, File mainClass, String... mainDefs)
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   124
            throws FileNotFoundException {
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   125
            createJar(null, jarName, mainClass, mainDefs);
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   126
    }
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   127
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   128
    /*
1329
ce13b59a8401 4459600: java -jar fails to run Main-Class if classname followed by whitespace.
ksrini
parents: 1323
diff changeset
   129
     * A generic jar file creator to create a java file, compile it
ce13b59a8401 4459600: java -jar fails to run Main-Class if classname followed by whitespace.
ksrini
parents: 1323
diff changeset
   130
     * and jar it up, a specific Main-Class entry name in the
ce13b59a8401 4459600: java -jar fails to run Main-Class if classname followed by whitespace.
ksrini
parents: 1323
diff changeset
   131
     * manifest can be specified or a null to use the sole class file name
ce13b59a8401 4459600: java -jar fails to run Main-Class if classname followed by whitespace.
ksrini
parents: 1323
diff changeset
   132
     * as the Main-Class attribute value.
1323
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   133
     */
1329
ce13b59a8401 4459600: java -jar fails to run Main-Class if classname followed by whitespace.
ksrini
parents: 1323
diff changeset
   134
    static void createJar(String mEntry, File jarName, File mainClass,
ce13b59a8401 4459600: java -jar fails to run Main-Class if classname followed by whitespace.
ksrini
parents: 1323
diff changeset
   135
            String... mainDefs) throws FileNotFoundException {
1323
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   136
        if (jarName.exists()) {
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   137
            jarName.delete();
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   138
        }
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   139
        PrintStream ps = new PrintStream(new FileOutputStream(mainClass + ".java"));
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   140
        ps.println("public class Foo {");
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   141
        if (mainDefs != null) {
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   142
            for (String x : mainDefs) {
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   143
                ps.println(x);
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   144
            }
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   145
        }
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   146
        ps.println("}");
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   147
        ps.close();
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   148
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   149
        String compileArgs[] = {
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   150
            mainClass + ".java"
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   151
        };
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   152
        if (compiler.run(null, null, null, compileArgs) != 0) {
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   153
            throw new RuntimeException("compilation failed " + mainClass + ".java");
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   154
        }
1329
ce13b59a8401 4459600: java -jar fails to run Main-Class if classname followed by whitespace.
ksrini
parents: 1323
diff changeset
   155
        if (mEntry == null) {
1323
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   156
            mEntry = mainClass.getName();
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   157
        }
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   158
        String jarArgs[] = {
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   159
            (debug) ? "cvfe" : "cfe",
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   160
            jarName.getAbsolutePath(),
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   161
            mEntry,
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   162
            mainClass.getName() + ".class"
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   163
        };
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   164
        sun.tools.jar.Main jarTool =
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   165
                new sun.tools.jar.Main(System.out, System.err, "JarCreator");
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   166
        if (!jarTool.run(jarArgs)) {
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   167
            throw new RuntimeException("jar creation failed " + jarName);
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   168
        }
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   169
    }
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   170
4340
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
   171
    static TestResult doExec(String...cmds) {
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
   172
        return doExec(null, cmds);
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
   173
    }
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
   174
1323
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   175
    /*
1329
ce13b59a8401 4459600: java -jar fails to run Main-Class if classname followed by whitespace.
ksrini
parents: 1323
diff changeset
   176
     * A method which executes a java cmd and returns the results in a container
1323
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   177
     */
4340
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
   178
    static TestResult doExec(Map<String, String> envToSet, String...cmds) {
1323
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   179
        String cmdStr = "";
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   180
        for (String x : cmds) {
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   181
            cmdStr = cmdStr.concat(x + " ");
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   182
        }
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   183
        ProcessBuilder pb = new ProcessBuilder(cmds);
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   184
        Map<String, String> env = pb.environment();
4340
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
   185
        if (envToSet != null) {
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
   186
            env.putAll(envToSet);
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
   187
        }
1323
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   188
        BufferedReader rdr = null;
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   189
        try {
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   190
            List<String> outputList = new ArrayList<String>();
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   191
            pb.redirectErrorStream(true);
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   192
            Process p = pb.start();
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   193
            rdr = new BufferedReader(new InputStreamReader(p.getInputStream()));
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   194
            String in = rdr.readLine();
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   195
            while (in != null) {
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   196
                outputList.add(in);
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   197
                in = rdr.readLine();
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   198
            }
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   199
            p.waitFor();
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   200
            p.destroy();
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   201
            return new TestHelper.TestResult(cmdStr, p.exitValue(), outputList);
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   202
        } catch (Exception ex) {
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   203
            ex.printStackTrace();
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   204
            throw new RuntimeException(ex.getMessage());
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   205
        }
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   206
    }
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   207
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   208
    /*
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   209
     * A class to encapsulate the test results and stuff, with some ease
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   210
     * of use methods to check the test results.
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   211
     */
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   212
    static class TestResult {
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   213
        StringBuilder status;
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   214
        int exitValue;
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   215
        List<String> testOutput;
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   216
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   217
        public TestResult(String str, int rv, List<String> oList) {
4340
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
   218
            status = new StringBuilder("Executed command: " + str + "\n");
1323
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   219
            exitValue = rv;
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   220
            testOutput = oList;
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   221
        }
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   222
4340
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
   223
        void appendStatus(String x) {
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
   224
            status = status.append("  " + x + "\n");
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
   225
        }
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
   226
1323
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   227
        void checkNegative() {
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   228
            if (exitValue == 0) {
4340
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
   229
                appendStatus("Error: test must not return 0 exit value");
1323
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   230
                testExitValue++;
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   231
            }
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   232
        }
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   233
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   234
        void checkPositive() {
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   235
            if (exitValue != 0) {
4340
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
   236
                appendStatus("Error: test did not return 0 exit value");
1323
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   237
                testExitValue++;
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   238
            }
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   239
        }
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   240
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   241
        boolean isOK() {
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   242
            return exitValue == 0;
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   243
        }
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   244
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   245
        boolean isZeroOutput() {
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   246
            if (!testOutput.isEmpty()) {
4340
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
   247
                appendStatus("Error: No message from cmd please");
1323
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   248
                testExitValue++;
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   249
                return false;
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   250
            }
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   251
            return true;
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   252
        }
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   253
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   254
        boolean isNotZeroOutput() {
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   255
            if (testOutput.isEmpty()) {
4340
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
   256
                appendStatus("Error: Missing message");
1323
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   257
                testExitValue++;
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   258
                return false;
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   259
            }
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   260
            return true;
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   261
        }
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   262
4340
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
   263
        @Override
1323
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   264
        public String toString() {
4340
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
   265
            status = status.append("++++Test Output Begin++++\n");
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
   266
            for (String x : testOutput) {
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
   267
                appendStatus(x);
1323
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   268
            }
4340
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
   269
            status = status.append("++++Test Output End++++\n");
1323
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   270
            return status.toString();
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   271
        }
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   272
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   273
        boolean contains(String str) {
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   274
            for (String x : testOutput) {
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   275
                if (x.contains(str)) {
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   276
                    return true;
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   277
                }
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   278
            }
4340
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
   279
            appendStatus("Error: string <" + str + "> not found");
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
   280
            testExitValue++;
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
   281
            return false;
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
   282
        }
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
   283
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
   284
        boolean matches(String stringToMatch) {
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
   285
          for (String x : testOutput) {
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
   286
                if (x.matches(stringToMatch)) {
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
   287
                    return true;
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
   288
                }
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
   289
            }
ec6ba551fa78 6367077: Purge LD_LIBRARY_PATH usage from the launcher
ksrini
parents: 1329
diff changeset
   290
            appendStatus("Error: string <" + stringToMatch + "> not found");
1323
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   291
            testExitValue++;
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   292
            return false;
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   293
        }
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   294
    }
e14a3b3536cd 6742159: (launcher) improve the java launching mechanism
ksrini
parents:
diff changeset
   295
}