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