jjg@114: jjg@114: /* vromero@1816: * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved. jjg@114: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@114: * jjg@114: * This code is free software; you can redistribute it and/or modify it jjg@114: * under the terms of the GNU General Public License version 2 only, as jjg@114: * published by the Free Software Foundation. jjg@114: * jjg@114: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@114: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@114: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@114: * version 2 for more details (a copy is included in the LICENSE file that jjg@114: * accompanied this code). jjg@114: * jjg@114: * You should have received a copy of the GNU General Public License version jjg@114: * 2 along with this work; if not, write to the Free Software Foundation, jjg@114: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@114: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. jjg@114: */ jjg@114: jjg@114: /* jjg@114: * @test jjg@114: * @bug 6728697 jjg@114: * @summary tools/javac/versionOpt.sh fails on OpenJDK builds jjg@114: * Test checks the version strings displayed by javac, using jjg@114: * strings that come out of the Java runtime. jjg@114: */ jjg@114: jjg@114: import java.io.File; jjg@114: import java.io.PrintWriter; jjg@114: import java.io.StringWriter; jjg@114: import java.net.URL; jjg@114: jjg@114: public class VersionOpt { jjg@114: public static void main(String... args) throws Exception { jjg@114: new VersionOpt().run(); jjg@114: } jjg@114: jjg@114: void run() throws Exception { jjg@114: // Test functions by comparing the version string from javac against jjg@114: // a "golden" version generated automatically from the underlying JVM. jjg@114: // As such, it is only effective in testing the "standard" compiler, jjg@114: // and not any development version being tested via -Xbootclasspath. jjg@114: // Check the version of the compiler being used, and let the test pass jjg@114: // automatically if is is a development version. jjg@114: Class javacClass = com.sun.tools.javac.Main.class; jjg@114: URL javacURL = getClass().getClassLoader().getResource(javacClass.getName().replace(".", "/") + ".class"); jjg@114: if (!javacURL.getProtocol().equals("jar") || !javacURL.getFile().contains("!")) { jjg@114: System.err.println("javac not found in tools.jar: " + javacURL); jjg@114: System.err.println("rest of test skipped"); jjg@114: return; jjg@114: } jjg@114: String javacHome = javacURL.getFile().substring(0, javacURL.getFile().indexOf("!")); jjg@114: jjg@114: File javaHome = new File(System.getProperty("java.home")); jjg@114: if (javaHome.getName().equals("jre")) jjg@114: javaHome = javaHome.getParentFile(); jjg@114: File toolsJar = new File(new File(javaHome, "lib"), "tools.jar"); jjg@114: vromero@1816: if (!javacHome.equalsIgnoreCase(toolsJar.toURI().toString())) { jjg@114: System.err.println("javac not found in tools.jar: " + javacHome); jjg@114: System.err.println("rest of test skipped"); jjg@114: return; jjg@114: } jjg@114: jjg@114: System.out.println("javac found in " + toolsJar); jjg@114: jjg@114: String javaVersion = System.getProperty("java.version"); jjg@114: String javaRuntimeVersion = System.getProperty("java.runtime.version"); jjg@114: System.out.println("java.version: " + javaVersion); jjg@114: System.out.println("java.runtime.version: " + javaRuntimeVersion); jjg@114: jjg@114: StringWriter sw = new StringWriter(); jjg@114: com.sun.tools.javac.Main.compile(new String[] { "-version" }, new PrintWriter(sw)); jjg@114: String javacVersion = sw.toString().trim(); jjg@114: jjg@114: sw = new StringWriter(); jjg@114: com.sun.tools.javac.Main.compile(new String[] { "-fullversion" }, new PrintWriter(sw)); jjg@114: String javacFullVersion = sw.toString().trim(); jjg@114: System.out.println("javac -version: " + javacVersion); jjg@114: System.out.println("javac -fullversion: " + javacFullVersion); jjg@114: jjg@114: checkEqual("javac -version", javacVersion, "javac " + javaVersion); jjg@114: checkEqual("javac -fullversion", javacFullVersion, "javac full version \"" + javaRuntimeVersion + "\""); jjg@114: jjg@114: if (errors > 0) jjg@114: throw new Exception(errors + " errors found"); jjg@114: } jjg@114: jjg@114: void checkEqual(String kind, String found, String expect) { jjg@114: if (!found.equals(expect)) { jjg@114: System.err.println("error: unexpected value for " + kind); jjg@114: System.err.println("expect: >>" + expect + "<<"); jjg@114: System.err.println(" found: >>" + found + "<<"); jjg@114: errors++; jjg@114: } jjg@114: } jjg@114: jjg@114: int errors; jjg@114: }