1 /* 2 * Copyright (c) 2010, 2015, 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 /* @test 25 * @bug 6994413 26 * @summary Check the JDK and JVM version returned by sun.misc.Version 27 * matches the versions defined in the system properties 28 * @modules java.base/sun.misc 29 * @compile -XDignore.symbol.file Version.java 30 * @run main Version 31 */ 32 33 import static sun.misc.Version.*; 34 import java.util.regex.Matcher; 35 import java.util.regex.Pattern; 36 37 public class Version { 38 39 public static void main(String[] args) throws Exception { 40 VersionInfo jdk = newVersionInfo(System.getProperty("java.runtime.version")); 41 VersionInfo v1 = new VersionInfo(jdkMajorVersion(), 42 jdkMinorVersion(), 43 jdkSecurityVersion(), 44 jdkPatchVersion(), 45 jdkBuildNumber()); 46 System.out.println("JDK version = " + jdk + " " + v1); 47 if (!jdk.equals(v1)) { 48 throw new RuntimeException("Unmatched version: " + jdk + " vs " + v1); 49 } 50 VersionInfo jvm = newVersionInfo(System.getProperty("java.vm.version")); 51 VersionInfo v2 = new VersionInfo(jvmMajorVersion(), 52 jvmMinorVersion(), 53 jvmSecurityVersion(), 54 jvmPatchVersion(), 55 jvmBuildNumber()); 56 System.out.println("JVM version = " + jvm + " " + v2); 57 if (!jvm.equals(v2)) { 58 throw new RuntimeException("Unmatched version: " + jvm + " vs " + v2); 59 } 60 } 61 62 static class VersionInfo { 63 final int major; 64 final int minor; 65 final int security; 66 final int patch; 67 final int build; 68 VersionInfo(int major, int minor, int security, 69 int patch, int build) { 70 this.major = major; 71 this.minor = minor; 72 this.security = security; 73 this.patch = patch; 74 this.build = build; 75 } 76 77 VersionInfo(int[] fields) { 78 this.major = fields[0]; 79 this.minor = fields[1]; 80 this.security = fields[2]; 81 this.patch = fields[3]; 82 this.build = fields[4]; 83 } 84 85 public boolean equals(VersionInfo v) { 86 return (this.major == v.major && this.minor == v.minor && 87 this.security == v.security && this.patch == v.patch && 88 this.build == v.build); 89 } 90 91 public String toString() { 92 StringBuilder sb = new StringBuilder(); 93 sb.append(major + "." + minor + "." + security); 94 if (patch > 0) { 95 sb.append("." + patch); 96 } 97 98 sb.append("+" + build); 99 return sb.toString(); 100 } 101 } 102 103 private static VersionInfo newVersionInfo(String version) throws Exception { 104 // Version string fromat as defined by JEP-223 105 String jep223Pattern = 106 "^([0-9]+)(\\.([0-9]+))?(\\.([0-9]+))?(\\.([0-9]+))?" + // $VNUM 107 "(-([a-zA-Z]+))?(\\.([a-zA-Z]+))?" + // $PRE 108 "(\\+([0-9]+))?" + // Build Number 109 "(([-a-zA-Z0-9.]+))?$"; // $OPT 110 111 // Pattern group index for: Major, Minor, Security, Patch, Build 112 int[] groups = {1, 3, 5, 7, 13}; 113 // Default values for Major, Minor, Security, Patch, Build 114 int[] versionFields = {0, 0, 0, 0, 0}; 115 116 Pattern pattern = Pattern.compile(jep223Pattern); 117 Matcher matcher = pattern.matcher(version); 118 if (matcher.matches()) { 119 for (int i = 0; i < versionFields.length; i++) { 120 String field = matcher.group(groups[i]); 121 versionFields[i] = (field != null) ? Integer.parseInt(field) : 0; 122 } 123 } 124 125 VersionInfo vi = new VersionInfo(versionFields); 126 System.out.printf("newVersionInfo: input=%s output=%s\n", version, vi); 127 return vi; 128 } 129 }