1 /*
   2  * Copyright (c) 2010, 2014, 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 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                                          jdkMicroVersion(),
  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                                          jvmMicroVersion(),
  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 }
--- EOF ---