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