< prev index next >

test/jdk/java/lang/Runtime/Version/Basic.java

Print this page
rev 48062 : 8192833: JEP 322: Time-Based Release Versioning


  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      }


 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 }


  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 
  42     private static final Class<? extends Throwable> IAE
  43         = IllegalArgumentException.class;
  44     private static final Class<? extends Throwable> NPE
  45         = NullPointerException.class;
  46     private static final Class<? extends Throwable> NFE
  47         = NumberFormatException.class;
  48 
  49     private static final BigInteger TOO_BIG
  50         = (BigInteger.valueOf(Integer.MAX_VALUE)).add(BigInteger.ONE);
  51     private static final String TOO_BIG_STR = TOO_BIG.toString();
  52 
  53     public static void main(String ... args) {
  54 
  55         //// Tests for parse(), feature(), interim(), update(), patch(),
  56         //// pre(), build(), optional(), version(), and toString()
  57         //   v                          f     i  u  p pre bld opt
  58 
  59         // $VNUM
  60         test("9",                       9,    0, 0, 0, "", 0, "");
  61         test("9.1",                     9,    1, 0, 0, "", 0, "");
  62         test("9.0.1",                   9,    0, 1, 0, "", 0, "");
  63         test("9.0.0.1",                 9,    0, 0, 1, "", 0, "");
  64         test("9.0.0.0.1",               9,    0, 0, 0, "", 0, "");
  65         test("404.1.2",                 404,  1, 2, 0, "", 0, "");
  66         test("9.1.2.3",                 9,    1, 2, 3, "", 0, "");
  67         test("1000.0.0.0.0.0.99999999", 1000, 0, 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, 0, "ea",       0, "");
  82         test("9-internal", 9, 0, 0, 0, "internal", 0, "");
  83         test("9-0",        9, 0, 0, 0, "0",        0, "");
  84         test("9.2.7-8",    9, 2, 7, 0, "8",        0, "");
  85         test("1-ALL",      1, 0, 0, 0, "ALL",      0, "");
  86         test("2.3.4.5-1a", 2, 3, 4, 5, "1a",       0, "");
  87         test("1-" + TOO_BIG_STR, 1, 0, 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, "",      0,       "");
  95         test("3.14+9999900",   3, 14, 0,  0, "",      9999900, "");
  96         test("9-pre+105",      9, 0,  0,  0, "pre",   105,     "");
  97         test("6.0.42-8beta+4", 6, 0,  42, 0, "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, "",       0,  "foo");
 108         test("9-pre-opt",       9,   0, 0, 0, "pre",    0,  "opt");
 109         test("42+---bar",       42,  0, 0, 0, "",       0,  "--bar");
 110         test("2.91+-8061493-",  2,  91, 0, 0, "",       0,  "8061493-");
 111         test("24+-foo.bar",     24,  0, 0, 0, "",       0,  "foo.bar");
 112         test("9-ribbit+17-...", 9,   0, 0, 0, "ribbit", 17, "...");
 113         test("7+1-" + TOO_BIG_STR, 7,0, 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{IgnoreOptional}?(), hashCode(),
 123         //// compareTo{IgnoreOptional}?()
 124         // compare: after "<" == -1, equal == 0, before ">" == 1
 125         //      v0            v1                  eq     eqNO  cmp  cmpNO
 126         testEHC("9",          "9",                true,  true,   0,    0);
 127 
 128         testEHC("8",          "9",                false, false, -1,   -1);
 129         testEHC("9",          "10",               false, false, -1,   -1);
 130         testEHC("9",          "8",                false, false,  1,    1);
 131 
 132         testEHC("10.512.1",   "10.512.2",         false, false, -1,   -1);
 133         testEHC("10.512.0.1", "10.512.0.2",       false, false, -1,   -1);
 134         testEHC("10.512.0.0.1", "10.512.0.0.2",   false, false, -1,   -1);
 135         testEHC("512.10.1",   "512.11.1",         false, false, -1,   -1);
 136 
 137         // $OPT comparison
 138         testEHC("9",          "9+-oink",          false, true,  -1,    0);
 139         testEHC("9+-ribbit",  "9+-moo",           false, true,   1,    0);
 140         testEHC("9-quack+3-ribbit",
 141                               "9-quack+3-moo",    false, true,   1,    0);
 142         testEHC("9.1+7",      "9.1+7-moo-baa-la", false, true,  -1,    0);
 143 
 144         // numeric vs. non-numeric $PRE
 145         testEHC("9.1.1.2-2a", "9.1.1.2-12",       false, false,  1,    1);
 146         testEHC("9.1.1.2-12", "9.1.1.2-4",        false, false,  1,    1);
 147 
 148         testEHC("27.16",      "27.16+120",        false, false, -1,   -1);
 149         testEHC("10",         "10-ea",            false, false,  1,    1);
 150         testEHC("10.1+1",     "10.1-ea+1",        false, false,  1,    1);
 151         testEHC("10.0.1+22",  "10.0.1+21",        false, false,  1,    1);
 152 
 153         // numeric vs. non-numeric $PRE
 154         testEHC("9.1.1.2-12", "9.1.1.2-a2",       false, false, -1,   -1);
 155         testEHC("9.1.1.2-1",  "9.1.1.2-4",        false, false, -1,   -1);
 156 
 157         testEHC("9-internal", "9",                false, false, -1,   -1);
 158         testEHC("9-ea+120",   "9+120",            false, false, -1,   -1);
 159         testEHC("9-ea+120",   "9+120",            false, false, -1,   -1);
 160         testEHC("9+101",      "9",                false, false,  1,    1);
 161         testEHC("9+101",      "9+102",            false, false, -1,   -1);
 162         testEHC("1.9-ea",     "9-ea",             false, false, -1,   -1);
 163 
 164         if (fail != 0)
 165             throw new RuntimeException((fail + pass) + " tests: "
 166                                        + fail + " failure(s), first", first);
 167         else
 168             out.println("all " + (fail + pass) + " tests passed");
 169 
 170     }
 171 
 172     private static void test(String s, Integer feature, Integer interim,
 173                              Integer update, Integer patch,
 174                              String pre, Integer build, String opt)
 175     {
 176         Version v = testParse(s);
 177 
 178         testStr(v.toString(), s);
 179 
 180         testInt(v.feature(), feature);
 181         testInt(v.major(), feature);
 182         testInt(v.interim(), interim);
 183         testInt(v.minor(), interim);
 184         testInt(v.update(), update);
 185         testInt(v.security(), update);
 186         testInt(v.patch(), patch);
 187         testStr((v.pre().isPresent() ? v.pre().get() : ""), pre);
 188         testInt((v.build().isPresent() ? v.build().get() : 0), build);
 189         testStr((v.optional().isPresent() ? v.optional().get() : ""), opt);
 190 
 191         testVersion(v.version(), s);
 192     }
 193 
 194     private static Version testParse(String s) {
 195         Version v = Version.parse(s);
 196         pass();
 197         return v;
 198     }
 199 
 200     private static void testInt(int got, int exp) {
 201         if (got != exp) {
 202             fail("testInt()", Integer.toString(exp), Integer.toString(got));
 203         } else {
 204             pass();
 205         }
 206      }


 373             setFirst(s);
 374         System.err.println("FAILED: " + s);
 375         fail++;
 376     }
 377 
 378     static void fail(String t, String exp, String got) {
 379         String s = t + ": Expected '" + exp + "', got '" + got + "'";
 380         if (first == null)
 381             setFirst(s);
 382         System.err.println("FAILED: " + s);
 383         fail++;
 384      }
 385 
 386     private static void setFirst(String s) {
 387         try {
 388             throw new RuntimeException(s);
 389         } catch (RuntimeException x) {
 390             first = x;
 391         }
 392     }
 393 
 394 }
< prev index next >