Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/misc/Version/Version.java
38838 views
/*1* Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 699441325* @summary Check the JDK and JVM version returned by sun.misc.Version26* matches the versions defined in the system properties27* @compile -XDignore.symbol.file Version.java28* @run main Version29*/3031import java.util.regex.*;32import static sun.misc.Version.*;3334public class Version {3536public static void main(String[] args) throws Exception {37VersionInfo jdk = jdkVersionInfo(System.getProperty("java.runtime.version"));38VersionInfo v1 = new VersionInfo(jdkMajorVersion(),39jdkMinorVersion(),40jdkMicroVersion(),41jdkUpdateVersion(),42jdkSpecialVersion(),43jdkBuildNumber());44System.out.println("JDK version = " + jdk + " " + v1);45if (!jdk.equals(v1)) {46throw new RuntimeException("Unmatched version: " + jdk + " vs " + v1);47}48VersionInfo jvm = jvmVersionInfo(System.getProperty("java.vm.version"));49VersionInfo v2 = new VersionInfo(jvmMajorVersion(),50jvmMinorVersion(),51jvmMicroVersion(),52jvmUpdateVersion(),53jvmSpecialVersion(),54jvmBuildNumber());55System.out.println("JVM version = " + jvm + " " + v2);56if (!jvm.equals(v2)) {57throw new RuntimeException("Unmatched version: " + jvm + " vs " + v2);58}59}6061static class VersionInfo {62final int major;63final int minor;64final int micro;65final int update;66final String special;67final int build;68VersionInfo(int major, int minor, int micro,69int update, String special, int build) {70this.major = major;71this.minor = minor;72this.micro = micro;73this.update = update;74this.special = special;75this.build = build;76}7778public boolean equals(VersionInfo v) {79return (this.major == v.major && this.minor == v.minor &&80this.micro == v.micro && this.update == v.update &&81this.special.equals(v.special) && this.build == v.build);82}8384public String toString() {85StringBuilder sb = new StringBuilder();86sb.append(major + "." + minor + "." + micro);87if (update > 0) {88sb.append("_" + update);89}9091if (!special.isEmpty()) {92sb.append(special);93}94sb.append("-b" + build);95return sb.toString();96}97}9899private static VersionInfo jdkVersionInfo(String version) throws Exception {100// valid format of the version string is:101// <major>.<minor>[.<micro>][_uu[c]][-<identifier>]-bxx102int major = 0;103int minor = 0;104int micro = 0;105int update = 0;106String special = "";107int build = 0;108109String regex = "^([0-9]{1,2})"; // major110regex += "\\."; // separator111regex += "([0-9]{1,2})"; // minor112regex += "(\\."; // separator113regex += "([0-9]{1,2})"; // micro114regex += ")?"; // micro is optional115regex += "(_";116regex += "([0-9]{2,3})"; // update117regex += "([a-z])?"; // special char (optional)118regex += ")?"; // _uu[c] is optional119regex += ".*"; // -<identifier>120regex += "(\\-b([0-9]{1,3}$))"; // JDK -bxx121122Pattern p = Pattern.compile(regex);123Matcher m = p.matcher(version);124m.matches();125126major = Integer.parseInt(m.group(1));127minor = Integer.parseInt(m.group(2));128micro = (m.group(4) == null) ? 0 : Integer.parseInt(m.group(4));129update = (m.group(6) == null) ? 0 : Integer.parseInt(m.group(6));130special = (m.group(7) == null) ? "" : m.group(7);131build = Integer.parseInt(m.group(9));132133VersionInfo vi = new VersionInfo(major, minor, micro, update, special, build);134System.out.printf("jdkVersionInfo: input=%s output=%s\n", version, vi);135return vi;136}137138private static VersionInfo jvmVersionInfo(String version) throws Exception {139try {140// valid format of the version string is:141// <major>.<minor>-bxx[-<identifier>][-<debug_flavor>]142int major = 0;143int minor = 0;144int build = 0;145146String regex = "^([0-9]{1,2})"; // major147regex += "\\."; // separator148regex += "([0-9]{1,3})"; // minor149regex += "(\\-b([0-9]{1,3}))"; // JVM -bxx150regex += ".*";151152Pattern p = Pattern.compile(regex);153Matcher m = p.matcher(version);154m.matches();155156major = Integer.parseInt(m.group(1));157minor = Integer.parseInt(m.group(2));158build = Integer.parseInt(m.group(4));159160VersionInfo vi = new VersionInfo(major, minor, 0, 0, "", build);161System.out.printf("jvmVersionInfo: input=%s output=%s\n", version, vi);162return vi;163} catch (IllegalStateException e) {164// local builds may also follow the jdkVersionInfo format165return jdkVersionInfo(version);166}167}168}169170171