langtools/test/tools/javac/VersionOpt.java
author lana
Thu, 21 Jan 2016 09:46:01 -0800
changeset 35340 38f7386ed942
parent 30730 d3ce7619db2c
child 36526 3b41f1c69604
permissions -rw-r--r--
Added tag jdk-9+102 for changeset 7e3feb2ba5b3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1261
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
     1
/*
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27850
diff changeset
     2
 * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
1261
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
     4
 *
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
     8
 *
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    13
 * accompanied this code).
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    14
 *
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    18
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1261
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1261
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1261
diff changeset
    21
 * questions.
1261
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    22
 */
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    23
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    24
/*
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    25
 * @test
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    26
 * @bug 6728697
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    27
 * @summary tools/javac/versionOpt.sh fails on OpenJDK builds
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    28
 * Test checks the version strings displayed by javac, using
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    29
 * strings that come out of the Java runtime.
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27850
diff changeset
    30
 * @modules jdk.compiler
1261
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    31
 */
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    32
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    33
import java.io.File;
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    34
import java.io.PrintWriter;
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    35
import java.io.StringWriter;
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    36
import java.net.URL;
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    37
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    38
public class VersionOpt {
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    39
    public static void main(String... args) throws Exception {
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    40
        new VersionOpt().run();
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    41
    }
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    42
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    43
    void run() throws Exception {
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    44
        // Test functions by comparing the version string from javac against
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    45
        // a "golden" version generated automatically from the underlying JVM.
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    46
        // As such, it is only effective in testing the "standard" compiler,
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    47
        // and not any development version being tested via -Xbootclasspath.
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    48
        // Check the version of the compiler being used, and let the test pass
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    49
        // automatically if is is a development version.
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    50
        Class<?> javacClass = com.sun.tools.javac.Main.class;
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    51
        URL javacURL = getClass().getClassLoader().getResource(javacClass.getName().replace(".", "/") + ".class");
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    52
        if (!javacURL.getProtocol().equals("jar") || !javacURL.getFile().contains("!")) {
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    53
            System.err.println("javac not found in tools.jar: " + javacURL);
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    54
            System.err.println("rest of test skipped");
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    55
            return;
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    56
        }
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    57
        String javacHome = javacURL.getFile().substring(0, javacURL.getFile().indexOf("!"));
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    58
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    59
        File javaHome = new File(System.getProperty("java.home"));
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    60
        if (javaHome.getName().equals("jre"))
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    61
            javaHome = javaHome.getParentFile();
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    62
        File toolsJar = new File(new File(javaHome, "lib"), "tools.jar");
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    63
18385
3d41b3b9f305 7113519: test/tools/javac/VersionOpt.java passes on windows
vromero
parents: 5520
diff changeset
    64
        if (!javacHome.equalsIgnoreCase(toolsJar.toURI().toString())) {
1261
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    65
            System.err.println("javac not found in tools.jar: " + javacHome);
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    66
            System.err.println("rest of test skipped");
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    67
            return;
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    68
        }
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    69
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    70
        System.out.println("javac found in " + toolsJar);
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    71
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    72
        String javaVersion = System.getProperty("java.version");
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    73
        String javaRuntimeVersion = System.getProperty("java.runtime.version");
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    74
        System.out.println("java.version: " + javaVersion);
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    75
        System.out.println("java.runtime.version: " + javaRuntimeVersion);
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    76
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    77
        StringWriter sw = new StringWriter();
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    78
        com.sun.tools.javac.Main.compile(new String[] { "-version" }, new PrintWriter(sw));
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    79
        String javacVersion = sw.toString().trim();
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    80
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    81
        sw = new StringWriter();
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    82
        com.sun.tools.javac.Main.compile(new String[] { "-fullversion" }, new PrintWriter(sw));
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    83
        String javacFullVersion = sw.toString().trim();
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    84
        System.out.println("javac -version: " + javacVersion);
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    85
        System.out.println("javac -fullversion: " + javacFullVersion);
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    86
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    87
        checkEqual("javac -version", javacVersion, "javac " + javaVersion);
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    88
        checkEqual("javac -fullversion", javacFullVersion, "javac full version \"" + javaRuntimeVersion + "\"");
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    89
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    90
        if (errors > 0)
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    91
            throw new Exception(errors + " errors found");
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    92
    }
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    93
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    94
    void checkEqual(String kind, String found, String expect) {
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    95
        if (!found.equals(expect)) {
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    96
            System.err.println("error: unexpected value for " + kind);
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    97
            System.err.println("expect: >>" + expect + "<<");
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    98
            System.err.println(" found: >>" + found + "<<");
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    99
            errors++;
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
   100
        }
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
   101
    }
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
   102
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
   103
    int errors;
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
   104
}