1 /*
2  * Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /*
25  * @test
26  * @bug 6728697
27  * @summary tools/javac/versionOpt.sh fails on OpenJDK builds
28  * Test checks the version strings displayed by javac, using
29  * strings that come out of the Java runtime.
30  * @modules jdk.compiler
31  */
32 
33 import java.io.File;
34 import java.io.PrintWriter;
35 import java.io.StringWriter;
36 import java.net.URL;
37 
38 public class VersionOpt {
main(String... args)39     public static void main(String... args) throws Exception {
40         new VersionOpt().run();
41     }
42 
run()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 --patch-modules.
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 location = javacClass.getProtectionDomain().getCodeSource().getLocation();
52         if (!location.toString().equals("jrt:/jdk.compiler")) {
53             System.err.println("javac not found in system image: " + location);
54             System.err.println("rest of test skipped");
55             return;
56         }
57 
58         System.out.println("javac found in " + location);
59 
60         String javaVersion = System.getProperty("java.version");
61         String javaRuntimeVersion = System.getProperty("java.runtime.version");
62         System.out.println("java.version: " + javaVersion);
63         System.out.println("java.runtime.version: " + javaRuntimeVersion);
64 
65         StringWriter sw = new StringWriter();
66         com.sun.tools.javac.Main.compile(new String[] { "-version" }, new PrintWriter(sw));
67         String javacVersion = sw.toString().trim();
68 
69         sw = new StringWriter();
70         com.sun.tools.javac.Main.compile(new String[] { "-fullversion" }, new PrintWriter(sw));
71         String javacFullVersion = sw.toString().trim();
72         System.out.println("javac -version: " + javacVersion);
73         System.out.println("javac -fullversion: " + javacFullVersion);
74 
75         checkEqual("javac -version", javacVersion, "javac " + javaVersion);
76         checkEqual("javac -fullversion", javacFullVersion, "javac full version \"" + javaRuntimeVersion + "\"");
77 
78         if (errors > 0)
79             throw new Exception(errors + " errors found");
80     }
81 
checkEqual(String kind, String found, String expect)82     void checkEqual(String kind, String found, String expect) {
83         if (!found.equals(expect)) {
84             System.err.println("error: unexpected value for " + kind);
85             System.err.println("expect: >>" + expect + "<<");
86             System.err.println(" found: >>" + found + "<<");
87             errors++;
88         }
89     }
90 
91     int errors;
92 }
93