test/tools/javac/VersionOpt.java

Wed, 14 Nov 2018 10:18:25 -0800

author
diazhou
date
Wed, 14 Nov 2018 10:18:25 -0800
changeset 3762
7909abb85562
parent 1816
105d1f9c1ab8
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Added tag jdk8u201-b04 for changeset a7f48b9dfb82

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

mercurial