jdk/test/tools/launcher/VersionCheck.java
author ksrini
Fri, 20 Nov 2009 11:01:32 -0800
changeset 4340 ec6ba551fa78
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6367077: Purge LD_LIBRARY_PATH usage from the launcher 6899834: (launcher) remove the solaris libjvm.so symlink Summary: Fixes other related issues as well. Reviewed-by: darcy, ohair, xlu, martin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 6545058 6611182
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary validate and test -version, -fullversion, and internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @compile VersionCheck.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @run main VersionCheck
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.lang.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.BufferedReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.InputStreamReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.Reader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public class VersionCheck {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static String javaBin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    // A known set of programs we know for sure will behave correctly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static String[] programs = new String[]{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        "appletviewer",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        "extcheck",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        "idlj",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        "jar",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        "jarsigner",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        "javac",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        "javadoc",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        "javah",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        "javap",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        "jconsole",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        "jdb",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        "jhat",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        "jinfo",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        "jmap",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        "jps",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        "jstack",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        "jstat",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        "jstatd",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        "keytool",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        "native2ascii",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        "orbd",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        "pack200",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        "policytool",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        "rmic",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        "rmid",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        "rmiregistry",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        "schemagen",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        "serialver",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        "servertool",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        "tnameserv",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        "wsgen",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        "wsimport",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        "xjc"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    // expected reference strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    static String refVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    static String refFullVersion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private static List<String> getProcessStreamAsList(boolean javaDebug,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                                                       String... argv) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        List<String> out = new ArrayList<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        List<String> javaCmds = new ArrayList<String>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        String prog = javaBin + File.separator + argv[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        if (System.getProperty("os.name").startsWith("Windows")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            prog = prog.concat(".exe");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        javaCmds.add(prog);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        for (int i = 1; i < argv.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            javaCmds.add(argv[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        ProcessBuilder pb = new ProcessBuilder(javaCmds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        Map<String, String> env = pb.environment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        if (javaDebug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            env.put("_JAVA_LAUNCHER_DEBUG", "true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            Process p = pb.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            BufferedReader r = (javaDebug) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                new BufferedReader(new InputStreamReader(p.getInputStream())) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                new BufferedReader(new InputStreamReader(p.getErrorStream())) ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            String s = r.readLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            while (s != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                out.add(s.trim());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                s = r.readLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            p.waitFor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            p.destroy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            ex.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            throw new RuntimeException(ex.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        return out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    static String getVersion(String... argv) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        List<String> alist = getProcessStreamAsList(false, argv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (alist.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            throw new AssertionError("unexpected process returned null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        StringBuilder out = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        // remove the HotSpot line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        for (String x : alist) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            if (!x.contains("HotSpot")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                out = out.append(x + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        return out.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    static boolean compareVersionStrings() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        int failcount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        for (String x : programs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            String testStr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            testStr = getVersion(x, "-J-version");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            if (refVersion.compareTo(testStr) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                failcount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                System.out.println("Error: " + x +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                                   " fails -J-version comparison");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                System.out.println("Expected:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                System.out.print(refVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                System.out.println("Actual:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                System.out.print(testStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            testStr = getVersion(x, "-J-fullversion");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            if (refFullVersion.compareTo(testStr) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                failcount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                System.out.println("Error: " + x +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                                   " fails -J-fullversion comparison");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                System.out.println("Expected:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                System.out.print(refFullVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                System.out.println("Actual:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                System.out.print(testStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        System.out.println("Version Test: " + failcount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        return failcount == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    static boolean compareInternalStrings() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        int failcount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        String bStr = refVersion.substring(refVersion.lastIndexOf("build") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                                           "build".length() + 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                                           refVersion.lastIndexOf(")"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        String[] vStr = bStr.split("\\.|-|_");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        String jdkMajor = vStr[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        String jdkMinor = vStr[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        String jdkMicro = vStr[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        String jdkBuild = vStr[vStr.length - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        String expectedDotVersion = "dotversion:" + jdkMajor + "." + jdkMinor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        String expectedFullVersion = "fullversion:" + bStr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        List<String> alist = getProcessStreamAsList(true, "java", "-version");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        if (!alist.contains(expectedDotVersion)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            System.out.println("Error: could not find " + expectedDotVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            failcount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        if (!alist.contains(expectedFullVersion)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            System.out.println("Error: could not find " + expectedFullVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            failcount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        System.out.println("Internal Strings Test: " + failcount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        return failcount == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    // Initialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    static void init() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        String javaHome = System.getProperty("java.home");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        if (javaHome.endsWith("jre")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            javaHome = new File(javaHome).getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        javaBin = javaHome + File.separator + "bin";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        refVersion = getVersion("java", "-version");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        refFullVersion = getVersion("java", "-fullversion");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        if (compareVersionStrings() && compareInternalStrings()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            System.out.println("All Version string comparisons: PASS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            throw new AssertionError("Some tests failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
}