1 /*
   2  * Copyright (c) 2015, 2016, 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 /*
  25  * @test
  26  * @summary Unit test for java.lang.Runtime.Version.
  27  * @bug 8072379 8144062
  28  */
  29 
  30 import java.lang.reflect.InvocationTargetException;
  31 import java.lang.reflect.Method;
  32 import java.lang.Runtime.Version;
  33 import java.math.BigInteger;
  34 import java.util.stream.Collectors;
  35 import java.util.Arrays;
  36 import java.util.ArrayList;
  37 import java.util.List;
  38 import java.util.Optional;
  39 
  40 import static java.lang.System.out;
  41 
  42 public class Basic {
  43     private static final Class<? extends Throwable> IAE
  44         = IllegalArgumentException.class;
  45     private static final Class<? extends Throwable> NPE
  46         = NullPointerException.class;
  47     private static final Class<? extends Throwable> NFE
  48         = NumberFormatException.class;
  49     private static final Class<?> VERSION = Version.class;
  50 
  51     private static final BigInteger TOO_BIG
  52         = (BigInteger.valueOf(Integer.MAX_VALUE)).add(BigInteger.ONE);
  53     private static final String TOO_BIG_STR = TOO_BIG.toString();
  54 
  55     public static void main(String ... args) {
  56 
  57         //// Tests for parse(), major(), minor(), security(), pre(),
  58         //// build(), opt(), version(), toString()
  59         //   v                          M     m sec pre bld opt
  60 
  61         // $VNUM
  62         test("9",                       9,    0, 0, "", 0, "");
  63         test("9.1",                     9,    1, 0, "", 0, "");
  64         test("9.0.1",                   9,    0, 1, "", 0, "");
  65         test("404.1.2",                 404,  1, 2, "", 0, "");
  66         test("9.1.2.3",                 9,    1, 2, "", 0, "");
  67         test("1000.0.0.0.0.0.99999999", 1000, 0, 0, "", 0, "");
  68 
  69         tryCatch(null,    NPE);
  70         tryCatch("",      IAE);
  71         tryCatch("foo",   IAE);
  72         tryCatch("7a",    IAE);
  73         tryCatch("0",     IAE);
  74         tryCatch("09",    IAE);
  75         tryCatch("9.0",   IAE);
  76         tryCatch("9.0.",  IAE);
  77         tryCatch("1.9,1", IAE);
  78         tryCatch(TOO_BIG_STR, NFE);
  79 
  80         // $PRE
  81         test("9-ea",       9, 0, 0, "ea",       0, "");
  82         test("9-internal", 9, 0, 0, "internal", 0, "");
  83         test("9-0",        9, 0, 0, "0",        0, "");
  84         test("9.2.7-8",    9, 2, 7, "8",        0, "");
  85         test("1-ALL",      1, 0, 0, "ALL",      0, "");
  86         test("2.3.4.5-1a", 2, 3, 4, "1a",       0, "");
  87         test("1-" + TOO_BIG_STR, 1, 0, 0, TOO_BIG_STR, 0, "");
  88 
  89         tryCatch("9:-ea",     IAE);
  90         tryCatch("3.14159-",  IAE);
  91         tryCatch("3.14159-%", IAE);
  92 
  93         // $BUILD
  94         test("9+0",            9, 0,  0,  "",      0,       "");
  95         test("3.14+9999900",   3, 14, 0,  "",      9999900, "");
  96         test("9-pre+105",      9, 0,  0,  "pre",   105,     "");
  97         test("6.0.42-8beta+4", 6, 0,  42, "8beta", 4,       "");
  98 
  99         tryCatch("9+",     IAE);
 100         tryCatch("7+a",    IAE);
 101         tryCatch("9+00",   IAE);
 102         tryCatch("4.2+01", IAE);
 103         tryCatch("4.2+1a", IAE);
 104         tryCatch("1+" + TOO_BIG_STR, NFE);
 105 
 106         // $OPT
 107         test("9+-foo",          9,   0, 0, "",       0,  "foo");
 108         test("9-pre-opt",       9,   0, 0, "pre",    0,  "opt");
 109         test("42+---bar",       42,  0, 0, "",       0,  "--bar");
 110         test("2.91+-8061493-",  2,  91, 0, "",       0,  "8061493-");
 111         test("24+-foo.bar",     24,  0, 0, "",       0,  "foo.bar");
 112         test("9-ribbit+17-...", 9,   0, 0, "ribbit", 17, "...");
 113         test("7+1-" + TOO_BIG_STR, 7,0, 0, "",       1,  TOO_BIG_STR);
 114 
 115         tryCatch("9-pre+-opt", IAE);
 116         tryCatch("1.4142+-",   IAE);
 117         tryCatch("2.9979+-%",  IAE);
 118 
 119         //// Test for Runtime.version()
 120         testVersion();
 121 
 122         //// Test for equals{IgnoreOpt}?(), hashCode(), compareTo{IgnoreOpt}?()
 123         // compare: after "<" == -1, equal == 0, before ">" == 1
 124         //      v0            v1                  eq     eqNO  cmp  cmpNO
 125         testEHC("9",          "9",                true,  true,   0,    0);
 126 
 127         testEHC("8",          "9",                false, false, -1,   -1);
 128         testEHC("9",          "10",               false, false, -1,   -1);
 129         testEHC("9",          "8",                false, false,  1,    1);
 130 
 131         // $OPT comparison
 132         testEHC("9",          "9+-oink",          false, true,  -1,    0);
 133         testEHC("9+-ribbit",  "9+-moo",           false, true,   1,    0);
 134         testEHC("9-quack+3-ribbit",
 135                               "9-quack+3-moo",    false, true,   1,    0);
 136         testEHC("9.1+7",      "9.1+7-moo-baa-la", false, true,  -1,    0);
 137 
 138         // numeric vs. non-numeric $PRE
 139         testEHC("9.1.1.2-2a", "9.1.1.2-12",       false, false,  1,    1);
 140         testEHC("9.1.1.2-12", "9.1.1.2-4",        false, false,  1,    1);
 141 
 142         testEHC("27.16",      "27.16+120",        false, false,  1,    1);
 143         testEHC("10",         "10-ea",            false, false,  1,    1);
 144         testEHC("10.1+1",     "10.1-ea+1",        false, false,  1,    1);
 145         testEHC("10.0.1+22",  "10.0.1+21",        false, false,  1,    1);
 146 
 147         // numeric vs. non-numeric $PRE
 148         testEHC("9.1.1.2-12", "9.1.1.2-a2",       false, false, -1,   -1);
 149         testEHC("9.1.1.2-1",  "9.1.1.2-4",        false, false, -1,   -1);
 150 
 151         testEHC("9-internal", "9",                false, false, -1,   -1);
 152         testEHC("9-ea+120",   "9+120",            false, false, -1,   -1);
 153         testEHC("9-ea+120",   "9+120",            false, false, -1,   -1);
 154         testEHC("9+101",      "9",                false, false, -1,   -1);
 155         testEHC("9+101",      "9+102",            false, false, -1,   -1);
 156         testEHC("1.9-ea",     "9-ea",             false, false, -1,   -1);
 157 
 158         if (fail != 0)
 159             throw new RuntimeException((fail + pass) + " tests: "
 160                                        + fail + " failure(s), first", first);
 161         else
 162             out.println("all " + (fail + pass) + " tests passed");
 163 
 164     }
 165 
 166     private static void test(String s, Integer major, Integer minor,
 167                              Integer sec, String pre, Integer build,
 168                              String opt)
 169     {
 170         Version v = testParse(s);
 171 
 172         testStr(v.toString(), s);
 173 
 174         testInt(v.major(), major);
 175         testInt(v.minor(), minor);
 176         testInt(v.security(), sec);
 177         testStr((v.pre().isPresent() ? v.pre().get() : ""), pre);
 178         testInt((v.build().isPresent() ? v.build().get() : 0), build);
 179         testStr((v.optional().isPresent() ? v.optional().get() : ""), opt);
 180 
 181         testVersion(v.version(), s);
 182     }
 183 
 184     private static Version testParse(String s) {
 185         Version v = Version.parse(s);
 186         pass();
 187         return v;
 188     }
 189 
 190     private static void testInt(int got, int exp) {
 191         if (got != exp) {
 192             fail("testInt()", Integer.toString(exp), Integer.toString(got));
 193         } else {
 194             pass();
 195         }
 196      }
 197 
 198     private static void testStr(String got, String exp) {
 199         if (!got.equals(exp)) {
 200             fail("testStr()", exp, got);
 201         } else {
 202             pass();
 203         }
 204     }
 205 
 206     private static void tryCatch(String s, Class<? extends Throwable> ex) {
 207         Throwable t = null;
 208         try {
 209             Version.parse(s);
 210         } catch (Throwable x) {
 211             if (ex.isAssignableFrom(x.getClass())) {
 212                 t = x;
 213             } else
 214                 x.printStackTrace();
 215         }
 216         if ((t == null) && (ex != null))
 217             fail(s, ex);
 218         else
 219             pass();
 220     }
 221 
 222     private static void testVersion() {
 223         Version current = Runtime.version();
 224         String javaVer = System.getProperty("java.runtime.version");
 225 
 226         // java.runtime.version == $VNUM(\-$PRE)?(\+$BUILD)?(-$OPT)?
 227         String [] jv  = javaVer.split("\\+");
 228         String [] ver = jv[0].split("-");
 229         List<Integer> javaVerVNum
 230             = Arrays.stream(ver[0].split("\\."))
 231             .map(v -> Integer.parseInt(v))
 232             .collect(Collectors.toList());
 233         if (!javaVerVNum.equals(current.version())) {
 234             fail("Runtime.version()", javaVerVNum.toString(),
 235                  current.version().toString());
 236         } else {
 237             pass();
 238         }
 239 
 240         Optional<String> javaVerPre
 241             = (ver.length == 2)
 242             ? Optional.ofNullable(ver[1])
 243             : Optional.empty();
 244         if (!javaVerPre.equals(current.pre())) {
 245             fail("testCurrent() pre()", javaVerPre.toString(),
 246                  current.pre().toString());
 247         } else {
 248             pass();
 249         }
 250 
 251         testEHC(current.toString(), javaVer, true, true, 0, 0);
 252     }
 253 
 254     private static void testVersion(List<Integer> vnum, String s) {
 255         List<Integer> svnum = new ArrayList<Integer>();
 256         StringBuilder sb = new StringBuilder();
 257         for (int i = 0; i < s.length(); i++) {
 258             Character c = s.charAt(i);
 259             if (Character.isDigit(c)) {
 260                 sb.append(c);
 261             } else {
 262                 svnum.add(Integer.parseInt(sb.toString()));
 263                 sb = new StringBuilder();
 264                 if (c == '+' || c == '-') {
 265                     break;
 266                 }
 267             }
 268         }
 269         if (sb.length() > 0) {
 270             svnum.add(Integer.parseInt(sb.toString()));
 271         }
 272 
 273         if (!svnum.equals(vnum)) {
 274             fail("testVersion() equals()", svnum.toString(), vnum.toString());
 275         } else {
 276             pass();
 277         }
 278     }
 279 
 280     private static void testEHC(String s0, String s1, boolean eq, boolean eqNO,
 281                                 int cmp, int cmpNO)
 282     {
 283         Version v0 = Version.parse(s0);
 284         Version v1 = Version.parse(s1);
 285 
 286         testEquals(v0, v1, eq);
 287         testEqualsNO(v0, v1, eqNO);
 288 
 289         testHashCode(v0, v1, eq);
 290 
 291         testCompare(v0, v1, cmp);
 292         testCompareNO(v0, v1, cmpNO);
 293     }
 294 
 295     private static void testEqualsNO(Version v0, Version v1, boolean eq) {
 296         if ((eq && !v0.equalsIgnoreOpt(v1))
 297             || (!eq && v0.equalsIgnoreOpt(v1))) {
 298             fail("equalsIgnoreOpt() " + Boolean.toString(eq),
 299                  v0.toString(), v1.toString());
 300         } else {
 301             pass();
 302         }
 303     }
 304 
 305     private static void testEquals(Version v0, Version v1, boolean eq) {
 306         if ((eq && !v0.equals(v1)) || (!eq && v0.equals(v1))) {
 307             fail("equals() " + Boolean.toString(eq),
 308                  v0.toString(), v1.toString());
 309         } else {
 310             pass();
 311         }
 312     }
 313 
 314     private static void testHashCode(Version v0, Version v1, boolean eq) {
 315         int h0 = v0.hashCode();
 316         int h1 = v1.hashCode();
 317         if (eq) {
 318             testInt(h0, h1);
 319         } else if (h0 == h1) {
 320             fail(String.format("hashCode() %s", h0),
 321                  Integer.toString(h0),
 322                  Integer.toString(h1));
 323         } else { // !eq && (h0 != h1)
 324             pass();
 325         }
 326     }
 327 
 328     private static void testCompareNO(Version v0, Version v1, int compare)
 329     {
 330         try {
 331             Method m = VERSION.getMethod("compareToIgnoreOpt", VERSION);
 332             int cmp = (int) m.invoke(v0, v1);
 333             checkCompare(v0, v1, compare, cmp);
 334         } catch (IllegalAccessException | InvocationTargetException |
 335                  NoSuchMethodException ex) {
 336             fail(String.format("compareToIgnoreOpt() invocation: %s",
 337                                ex.getClass()),
 338                  null);
 339         }
 340     }
 341 
 342     private static void testCompare(Version v0, Version v1, int compare) {
 343         try {
 344             Method m = VERSION.getMethod("compareTo", VERSION);
 345             int cmp = (int) m.invoke(v0, v1);
 346             checkCompare(v0, v1, compare, cmp);
 347         } catch (IllegalAccessException | InvocationTargetException |
 348                  NoSuchMethodException ex) {
 349             fail(String.format("compareTo() invocation: %s", ex.getClass()),
 350                  null);
 351         }
 352     }
 353 
 354     private static void checkCompare(Version v0, Version v1,
 355                                      int compare, int cmp)
 356     {
 357         if (((cmp == 0) && (compare == 0))
 358             || (compare == (cmp / Math.abs(cmp == 0 ? 1 : cmp)))) {
 359             pass();
 360         } else {
 361             fail(String.format("compare() (cmp = %s) (compare = %s)",
 362                                cmp, compare),
 363                  v0.toString(), v1.toString());
 364         }
 365     }
 366 
 367     private static int fail = 0;
 368     private static int pass = 0;
 369 
 370     private static Throwable first;
 371 
 372     static void pass() {
 373         pass++;
 374     }
 375 
 376     static void fail(String fs, Class ex) {
 377         String s = "'" + fs + "'";
 378         if (ex != null)
 379             s += ": " + ex.getName() + " not thrown";
 380         if (first == null)
 381             setFirst(s);
 382         System.err.println("FAILED: " + s);
 383         fail++;
 384     }
 385 
 386     static void fail(String t, String exp, String got) {
 387         String s = t + ": Expected '" + exp + "', got '" + got + "'";
 388         if (first == null)
 389             setFirst(s);
 390         System.err.println("FAILED: " + s);
 391         fail++;
 392      }
 393 
 394     private static void setFirst(String s) {
 395         try {
 396             throw new RuntimeException(s);
 397         } catch (RuntimeException x) {
 398             first = x;
 399         }
 400     }
 401 }