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 public class Version { 35 36 public static void main(String[] args) throws Exception { 37 VersionInfo jdk = newVersionInfo(System.getProperty("java.runtime.version")); 38 VersionInfo v1 = new VersionInfo(jdkMajorVersion(), 39 jdkMinorVersion(), 40 jdkSecurityVersion(), 41 jdkUpdateVersion(), 42 jdkSpecialVersion(), 43 jdkBuildNumber()); 44 System.out.println("JDK version = " + jdk + " " + v1); 45 if (!jdk.equals(v1)) { 46 throw new RuntimeException("Unmatched version: " + jdk + " vs " + v1); 47 } 48 VersionInfo jvm = newVersionInfo(System.getProperty("java.vm.version")); 49 VersionInfo v2 = new VersionInfo(jvmMajorVersion(), 50 jvmMinorVersion(), 51 jvmSecurityVersion(), 52 jvmUpdateVersion(), 53 jvmSpecialVersion(), 54 jvmBuildNumber()); 55 System.out.println("JVM version = " + jvm + " " + v2); 56 if (!jvm.equals(v2)) { 57 throw new RuntimeException("Unmatched version: " + jvm + " vs " + v2); 58 } 59 } 60 61 static class VersionInfo { 62 final int major; 63 final int minor; 64 final int micro; 65 final int update; 66 final String special; 67 final int build; 68 VersionInfo(int major, int minor, int micro, 69 int update, String special, int build) { 70 this.major = major; 71 this.minor = minor; 72 this.micro = micro; 73 this.update = update; 74 this.special = special; 75 this.build = build; 76 } 77 78 public boolean equals(VersionInfo v) { 79 return (this.major == v.major && this.minor == v.minor && 80 this.micro == v.micro && this.update == v.update && 81 this.special.equals(v.special) && this.build == v.build); 82 } 83 84 public String toString() { 85 StringBuilder sb = new StringBuilder(); 86 sb.append(major + "." + minor + "." + micro); 87 if (update > 0) { 88 sb.append("_" + update); 89 } 90 91 if (!special.isEmpty()) { 92 sb.append(special); 93 } 94 sb.append("-b" + build); 95 return sb.toString(); 96 } 97 } 98 99 private static VersionInfo newVersionInfo(String version) throws Exception { 100 // valid format of the version string is: 101 // n.n.n[_uu[c]][-<identifer>]-bxx 102 int major = 0; 103 int minor = 0; 104 int micro = 0; 105 int update = 0; 106 String special = ""; 107 int build = 0; 108 CharSequence cs = version; 109 if (cs.length() >= 5) { 110 if (Character.isDigit(cs.charAt(0)) && cs.charAt(1) == '.' && 111 Character.isDigit(cs.charAt(2)) && cs.charAt(3) == '.' && 112 Character.isDigit(cs.charAt(4))) { 113 major = Character.digit(cs.charAt(0), 10); 114 minor = Character.digit(cs.charAt(2), 10); 115 micro = Character.digit(cs.charAt(4), 10); 116 cs = cs.subSequence(5, cs.length()); 117 } else if (Character.isDigit(cs.charAt(0)) && 118 Character.isDigit(cs.charAt(1)) && cs.charAt(2) == '.' && 119 Character.isDigit(cs.charAt(3))) { 120 // HSX has nn.n[n] (major.minor) version 121 major = Integer.valueOf(version.substring(0, 2)).intValue(); 122 if (Character.isDigit(cs.charAt(4))) { 123 minor = Integer.valueOf(version.substring(3, 5)).intValue(); 124 cs = cs.subSequence(5, cs.length()); 125 } 126 else { 127 minor = Character.digit(cs.charAt(3), 10); 128 cs = cs.subSequence(4, cs.length()); 129 } 130 } 131 if (cs.charAt(0) == '_' && cs.length() >= 3 && 132 Character.isDigit(cs.charAt(1)) && 133 Character.isDigit(cs.charAt(2))) { 134 int nextChar = 3; 135 String uu = cs.subSequence(1, 3).toString(); 136 update = Integer.valueOf(uu).intValue(); 137 if (cs.length() >= 4) { 138 char c = cs.charAt(3); 139 if (c >= 'a' && c <= 'z') { 140 special = Character.toString(c); 141 nextChar++; 142 } 143 } 144 cs = cs.subSequence(nextChar, cs.length()); 145 } 146 if (cs.charAt(0) == '-') { 147 // skip the first character 148 // valid format: <identifier>-bxx or bxx 149 // non-product VM will have -debug|-release appended 150 cs = cs.subSequence(1, cs.length()); 151 String[] res = cs.toString().split("-"); 152 for (int i = res.length - 1; i >= 0; i--) { 153 String s = res[i]; 154 if (s.charAt(0) == 'b') { 155 try { 156 build = Integer.parseInt(s.substring(1, s.length())); 157 break; 158 } catch (NumberFormatException nfe) { 159 // ignore 160 } 161 } 162 } 163 } 164 } 165 VersionInfo vi = new VersionInfo(major, minor, micro, update, special, build); 166 System.out.printf("newVersionInfo: input=%s output=%s\n", version, vi); 167 return vi; 168 } 169 } | 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 = "^([0-9]+)(\\.([0-9]+))?(\\.([0-9]+))?(\\.([0-9]+))?(-([a-zA-Z]+))?(\\.([a-zA-Z]+))?(\\+([0-9]+))?(([-a-zA-Z0-9.]+))?$"; 106 // Pattern group index for: Major, Minor, Security, Patch, Build 107 int[] groups = {1, 3, 5, 7, 13}; 108 // Default values for Major, Minor, Security, Patch, Build 109 int[] versionFields = {0, 0, 0, 0, 0}; 110 111 Pattern pattern = Pattern.compile(jep223Pattern); 112 Matcher matcher = pattern.matcher(version); 113 if (matcher.matches()) { 114 for (int i = 0; i < versionFields.length; i++) { 115 String field = matcher.group(groups[i]); 116 versionFields[i] = (field != null) ? Integer.parseInt(field) : 0; 117 } 118 } 119 120 VersionInfo vi = new VersionInfo(versionFields); 121 System.out.printf("newVersionInfo: input=%s output=%s\n", version, vi); 122 return vi; 123 } 124 } |