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