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