3001N/A/*
6019N/A * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
3001N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3001N/A *
3001N/A * This code is free software; you can redistribute it and/or modify it
3001N/A * under the terms of the GNU General Public License version 2 only, as
3001N/A * published by the Free Software Foundation.
3001N/A *
3001N/A * This code is distributed in the hope that it will be useful, but WITHOUT
3001N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3001N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
3001N/A * version 2 for more details (a copy is included in the LICENSE file that
3001N/A * accompanied this code).
3001N/A *
3001N/A * You should have received a copy of the GNU General Public License version
3001N/A * 2 along with this work; if not, write to the Free Software Foundation,
3001N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3001N/A *
3001N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3001N/A * or visit www.oracle.com if you need additional information or have any
3001N/A * questions.
3001N/A */
3001N/A
3001N/A/* @test
3001N/A * @bug 6994413
3001N/A * @summary Check the JDK and JVM version returned by sun.misc.Version
3001N/A * matches the versions defined in the system properties
3001N/A * @compile -XDignore.symbol.file Version.java
3001N/A * @run main Version
3001N/A */
3001N/A
6019N/Aimport java.util.regex.*;
3001N/Aimport static sun.misc.Version.*;
6019N/A
3001N/Apublic class Version {
3001N/A
3001N/A public static void main(String[] args) throws Exception {
6020N/A VersionInfo jdk = jdkVersionInfo(System.getProperty("java.runtime.version"));
3001N/A VersionInfo v1 = new VersionInfo(jdkMajorVersion(),
3001N/A jdkMinorVersion(),
3001N/A jdkMicroVersion(),
3001N/A jdkUpdateVersion(),
3001N/A jdkSpecialVersion(),
3001N/A jdkBuildNumber());
3001N/A System.out.println("JDK version = " + jdk + " " + v1);
3001N/A if (!jdk.equals(v1)) {
3001N/A throw new RuntimeException("Unmatched version: " + jdk + " vs " + v1);
3001N/A }
6020N/A VersionInfo jvm = jvmVersionInfo(System.getProperty("java.vm.version"));
3001N/A VersionInfo v2 = new VersionInfo(jvmMajorVersion(),
3001N/A jvmMinorVersion(),
3001N/A jvmMicroVersion(),
3001N/A jvmUpdateVersion(),
3001N/A jvmSpecialVersion(),
3001N/A jvmBuildNumber());
3001N/A System.out.println("JVM version = " + jvm + " " + v2);
3001N/A if (!jvm.equals(v2)) {
3001N/A throw new RuntimeException("Unmatched version: " + jvm + " vs " + v2);
3001N/A }
3001N/A }
3001N/A
3001N/A static class VersionInfo {
3001N/A final int major;
3001N/A final int minor;
3001N/A final int micro;
3001N/A final int update;
3001N/A final String special;
3001N/A final int build;
3001N/A VersionInfo(int major, int minor, int micro,
3001N/A int update, String special, int build) {
3001N/A this.major = major;
3001N/A this.minor = minor;
3001N/A this.micro = micro;
3001N/A this.update = update;
3001N/A this.special = special;
3001N/A this.build = build;
3001N/A }
3001N/A
3001N/A public boolean equals(VersionInfo v) {
3001N/A return (this.major == v.major && this.minor == v.minor &&
3001N/A this.micro == v.micro && this.update == v.update &&
3001N/A this.special.equals(v.special) && this.build == v.build);
3001N/A }
3001N/A
3001N/A public String toString() {
3001N/A StringBuilder sb = new StringBuilder();
3001N/A sb.append(major + "." + minor + "." + micro);
3001N/A if (update > 0) {
3001N/A sb.append("_" + update);
3001N/A }
3001N/A
3001N/A if (!special.isEmpty()) {
3001N/A sb.append(special);
3001N/A }
3001N/A sb.append("-b" + build);
3001N/A return sb.toString();
3001N/A }
3001N/A }
3001N/A
6020N/A private static VersionInfo jdkVersionInfo(String version) throws Exception {
3001N/A // valid format of the version string is:
6020N/A // <major>.<minor>[.<micro>][_uu[c]][-<identifier>]-bxx
3001N/A int major = 0;
3001N/A int minor = 0;
3001N/A int micro = 0;
3001N/A int update = 0;
3001N/A String special = "";
3001N/A int build = 0;
6019N/A
6020N/A String regex = "^([0-9]{1,2})"; // major
6019N/A regex += "\\."; // separator
6019N/A regex += "([0-9]{1,2})"; // minor
6019N/A regex += "(\\."; // separator
6019N/A regex += "([0-9]{1,2})"; // micro
6019N/A regex += ")?"; // micro is optional
6019N/A regex += "(_";
6019N/A regex += "([0-9]{2})"; // update
6019N/A regex += "([a-z])?"; // special char (optional)
6019N/A regex += ")?"; // _uu[c] is optional
6020N/A regex += ".*"; // -<identifier>
6020N/A regex += "(\\-b([0-9]{1,3}$))"; // JDK -bxx
6019N/A
6019N/A Pattern p = Pattern.compile(regex);
6019N/A Matcher m = p.matcher(version);
6019N/A m.matches();
6019N/A
6019N/A major = Integer.parseInt(m.group(1));
6019N/A minor = Integer.parseInt(m.group(2));
6019N/A micro = (m.group(4) == null) ? 0 : Integer.parseInt(m.group(4));
6019N/A update = (m.group(6) == null) ? 0 : Integer.parseInt(m.group(6));
6019N/A special = (m.group(7) == null) ? "" : m.group(7);
6020N/A build = Integer.parseInt(m.group(9));
6019N/A
3874N/A VersionInfo vi = new VersionInfo(major, minor, micro, update, special, build);
3874N/A System.out.printf("newVersionInfo: input=%s output=%s\n", version, vi);
3874N/A return vi;
3001N/A }
6020N/A
6020N/A private static VersionInfo jvmVersionInfo(String version) throws Exception {
6020N/A // valid format of the version string is:
6020N/A // <major>.<minor>-bxx[-<identifier>][-<debug_flavor>]
6020N/A int major = 0;
6020N/A int minor = 0;
6020N/A int build = 0;
6020N/A
6020N/A String regex = "^([0-9]{1,2})"; // major
6020N/A regex += "\\."; // separator
6020N/A regex += "([0-9]{1,2})"; // minor
6020N/A regex += "(\\-b([0-9]{1,3}))"; // JVM -bxx
6020N/A regex += ".*";
6020N/A
6020N/A Pattern p = Pattern.compile(regex);
6020N/A Matcher m = p.matcher(version);
6020N/A m.matches();
6020N/A
6020N/A major = Integer.parseInt(m.group(1));
6020N/A minor = Integer.parseInt(m.group(2));
6020N/A build = Integer.parseInt(m.group(4));
6020N/A
6020N/A VersionInfo vi = new VersionInfo(major, minor, 0, 0, "", build);
6020N/A System.out.printf("newVersionInfo: input=%s output=%s\n", version, vi);
6020N/A return vi;
6020N/A }
3001N/A}