jdk/test/tools/launcher/profiles/VersionCheck.java
author jlaskey
Tue, 19 Feb 2013 09:47:02 -0400
changeset 16248 e4bcc4b4f897
parent 15683 3acd10709925
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15683
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
     1
/*
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
     2
 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
     4
 *
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
     8
 *
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    13
 * accompanied this code).
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    14
 *
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    18
 *
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    21
 * questions.
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    22
 */
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    23
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    24
/**
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    25
 * @test
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    26
 * @bug 8003256
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    27
 * @compile -XDignore.symbol.file VersionCheck.java
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    28
 * @run main VersionCheck
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    29
 * @summary Tests that "java -version" includes the name of the profile and that
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    30
 *     it matches the name in the release file
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    31
 */
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    32
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    33
import java.nio.file.*;
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    34
import java.io.*;
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    35
import java.util.Properties;
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    36
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    37
public class VersionCheck {
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    38
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    39
    static final String JAVA_HOME = System.getProperty("java.home");
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    40
    static final String OS_NAME = System.getProperty("os.name");
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    41
    static final String OS_ARCH = System.getProperty("os.arch");
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    42
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    43
    static final String JAVA_CMD =
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    44
            OS_NAME.startsWith("Windows") ? "java.exe" : "java";
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    45
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    46
    static final boolean NEED_D64 =
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    47
            OS_NAME.equals("SunOS") &&
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    48
            (OS_ARCH.equals("sparcv9") || OS_ARCH.equals("amd64"));
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    49
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    50
    /**
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    51
     * Returns {@code true} if the given class is present.
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    52
     */
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    53
    static boolean isPresent(String cn) {
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    54
        try {
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    55
            Class.forName(cn);
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    56
            return true;
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    57
        } catch (ClassNotFoundException ignore) {
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    58
            return false;
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    59
        }
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    60
    }
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    61
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    62
    /**
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    63
     * Determines the profile by checking whether specific classes are present.
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    64
     * Returns the empty string if this runtime does not appear to be a profile
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    65
     * of Java SE.
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    66
     */
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    67
    static String probeProfile() {
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    68
        if (isPresent("java.awt.Window"))
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    69
            return "";
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    70
        if (isPresent("java.lang.management.ManagementFactory"))
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    71
            return "compact3";
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    72
        if (isPresent("java.sql.DriverManager"))
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    73
            return "compact2";
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    74
        return "compact1";
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    75
    }
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    76
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    77
    /**
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    78
     * Execs java with the given parameters. The method blocks until the
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    79
     * process terminates. Returns a {@code ByteArrayOutputStream} with any
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    80
     * stdout or stderr from the process.
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    81
     */
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    82
    static ByteArrayOutputStream execJava(String... args)
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    83
        throws IOException
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    84
    {
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    85
        StringBuilder sb = new StringBuilder();
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    86
        sb.append(Paths.get(JAVA_HOME, "bin", JAVA_CMD).toString());
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    87
        if (NEED_D64)
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    88
            sb.append(" -d64");
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    89
        for (String arg: args) {
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    90
            sb.append(' ');
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    91
            sb.append(arg);
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    92
        }
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    93
        String[] cmd = sb.toString().split(" ");
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    94
        ProcessBuilder pb = new ProcessBuilder(cmd);
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    95
        pb.redirectErrorStream(true);
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    96
        Process p = pb.start();
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    97
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    98
        byte[] buf = new byte[1024];
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
    99
        int n;
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   100
        do {
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   101
            n = p.getInputStream().read(buf);
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   102
            if (n > 0)
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   103
               baos.write(buf, 0, n);
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   104
        } while (n > 0);
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   105
        try {
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   106
            int exitCode = p.waitFor();
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   107
            if (exitCode != 0)
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   108
                throw new RuntimeException("Exit code: " + exitCode);
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   109
        } catch (InterruptedException e) {
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   110
            throw new RuntimeException("Should not happen");
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   111
        }
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   112
        return baos;
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   113
    }
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   114
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   115
    public static void main(String[] args) throws IOException {
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   116
        String reported = sun.misc.Version.profileName();
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   117
        String probed = probeProfile();
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   118
        if (!reported.equals(probed)) {
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   119
            throw new RuntimeException("sun.misc.Version reports: " + reported
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   120
               + ", but probing reports: " + probed);
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   121
        }
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   122
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   123
        String profile = probed;
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   124
        boolean isFullJre = (profile.length() == 0);
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   125
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   126
        // check that java -version includes "profile compactN"
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   127
        String expected = "profile " + profile;
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   128
        System.out.println("Checking java -version ...");
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   129
        ByteArrayOutputStream baos = execJava("-version");
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   130
        ByteArrayInputStream bain = new ByteArrayInputStream(baos.toByteArray());
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   131
        BufferedReader reader = new BufferedReader(new InputStreamReader(bain));
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   132
        boolean found = false;
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   133
        String line;
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   134
        while ((line = reader.readLine()) != null) {
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   135
            if (line.contains(expected)) {
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   136
                found = true;
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   137
                break;
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   138
            }
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   139
        }
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   140
        if (found && isFullJre)
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   141
           throw new RuntimeException(expected + " found in java -version output");
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   142
        if (!found && !isFullJre)
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   143
            throw new RuntimeException("java -version did not include " + expected);
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   144
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   145
        // check that the profile name matches the release file
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   146
        System.out.println("Checking release file ...");
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   147
        Properties props = new Properties();
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   148
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   149
        Path home = Paths.get(JAVA_HOME);
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   150
        if (home.getFileName().toString().equals("jre"))
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   151
            home = home.getParent();
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   152
        Path release = home.resolve("release");
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   153
        try (InputStream in = Files.newInputStream(release)) {
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   154
            props.load(in);
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   155
        }
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   156
        String value = props.getProperty("JAVA_PROFILE");
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   157
        if (isFullJre) {
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   158
            if (value != null)
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   159
                throw new RuntimeException("JAVA_PROFILE should not be present");
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   160
        } else {
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   161
            if (value == null)
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   162
                throw new RuntimeException("JAVA_PROFILE not present in release file");
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   163
            if (!value.equals("\"" + profile + "\""))
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   164
                throw new RuntimeException("Unexpected value of JAVA_PROFILE: " + value);
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   165
        }
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   166
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   167
        System.out.println("Test passed.");
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   168
    }
3acd10709925 8003256: (profiles) Add support for profile identification
alanb
parents:
diff changeset
   169
}