langtools/test/tools/javac/VersionOpt.java
author jeff
Wed, 22 Jun 2011 10:10:54 -0700
changeset 9916 c35ecac744a5
parent 5520 86e4b9a9da40
child 18385 3d41b3b9f305
permissions -rw-r--r--
7057046: Add embedded license to THIRD PARTY README Reviewed-by: lana
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
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
     2
/*
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1261
diff changeset
     3
 * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved.
1261
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
     4
 * 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
     5
 *
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
     6
 * 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
     7
 * 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
     8
 * published by the Free Software Foundation.
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
     9
 *
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    10
 * 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
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    12
 * 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
    13
 * 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
    14
 * accompanied this code).
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    15
 *
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    16
 * 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
    17
 * 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
    18
 * 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
    19
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1261
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1261
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1261
diff changeset
    22
 * questions.
1261
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
/*
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    26
 * @test
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    27
 * @bug 6728697
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    28
 * @summary tools/javac/versionOpt.sh fails on OpenJDK builds
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    29
 * Test checks the version strings displayed by javac, using
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    30
 * strings that come out of the Java runtime.
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
f66c569f60ce 6728697: tools/javac/versionOpt.sh fails on OpenJDK builds
jjg
parents:
diff changeset
    64
        if (!javacHome.equals(toolsJar.toURI().toString())){
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
}