Path: blob/master/test/jdk/java/lang/RuntimeTests/Version/Basic.java
41153 views
/*1* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @summary Unit test for java.lang.Runtime.Version26* @bug 8072379 8144062 8161236 8160956 819487927*/2829import java.lang.Runtime.Version;30import java.math.BigInteger;31import java.util.ArrayList;32import java.util.Arrays;33import java.util.List;34import java.util.Optional;35import java.util.stream.Collectors;3637import static java.lang.System.out;3839public class Basic {4041private static final Class<? extends Throwable> IAE42= IllegalArgumentException.class;43private static final Class<? extends Throwable> NPE44= NullPointerException.class;45private static final Class<? extends Throwable> NFE46= NumberFormatException.class;4748private static final BigInteger TOO_BIG49= (BigInteger.valueOf(Integer.MAX_VALUE)).add(BigInteger.ONE);50private static final String TOO_BIG_STR = TOO_BIG.toString();5152public static void main(String ... args) {5354//// Tests for parse(), feature(), interim(), update(), patch(),55//// pre(), build(), optional(), version(), and toString()56// v f i u p pre bld opt5758// $VNUM59test("9", 9, 0, 0, 0, "", 0, "");60test("9.1", 9, 1, 0, 0, "", 0, "");61test("9.0.1", 9, 0, 1, 0, "", 0, "");62test("9.0.0.1", 9, 0, 0, 1, "", 0, "");63test("9.0.0.0.1", 9, 0, 0, 0, "", 0, "");64test("404.1.2", 404, 1, 2, 0, "", 0, "");65test("9.1.2.3", 9, 1, 2, 3, "", 0, "");66test("1000.0.0.0.0.0.99999999", 1000, 0, 0, 0, "", 0, "");6768tryCatch(null, NPE);69tryCatch("", IAE);70tryCatch("foo", IAE);71tryCatch("7a", IAE);72tryCatch("0", IAE);73tryCatch("09", IAE);74tryCatch("9.0", IAE);75tryCatch("9.0.", IAE);76tryCatch("1.9,1", IAE);77tryCatch(TOO_BIG_STR, NFE);7879// $PRE80test("9-ea", 9, 0, 0, 0, "ea", 0, "");81test("9-internal", 9, 0, 0, 0, "internal", 0, "");82test("9-0", 9, 0, 0, 0, "0", 0, "");83test("9.2.7-8", 9, 2, 7, 0, "8", 0, "");84test("1-ALL", 1, 0, 0, 0, "ALL", 0, "");85test("2.3.4.5-1a", 2, 3, 4, 5, "1a", 0, "");86test("1-" + TOO_BIG_STR, 1, 0, 0, 0, TOO_BIG_STR, 0, "");8788tryCatch("9:-ea", IAE);89tryCatch("3.14159-", IAE);90tryCatch("3.14159-%", IAE);9192// $BUILD93test("9+0", 9, 0, 0, 0, "", 0, "");94test("3.14+9999900", 3, 14, 0, 0, "", 9999900, "");95test("9-pre+105", 9, 0, 0, 0, "pre", 105, "");96test("6.0.42-8beta+4", 6, 0, 42, 0, "8beta", 4, "");9798tryCatch("9+", IAE);99tryCatch("7+a", IAE);100tryCatch("9+00", IAE);101tryCatch("4.2+01", IAE);102tryCatch("4.2+1a", IAE);103tryCatch("1+" + TOO_BIG_STR, NFE);104105// $OPT106test("9+-foo", 9, 0, 0, 0, "", 0, "foo");107test("9-pre-opt", 9, 0, 0, 0, "pre", 0, "opt");108test("42+---bar", 42, 0, 0, 0, "", 0, "--bar");109test("2.91+-8061493-", 2, 91, 0, 0, "", 0, "8061493-");110test("24+-foo.bar", 24, 0, 0, 0, "", 0, "foo.bar");111test("9-ribbit+17-...", 9, 0, 0, 0, "ribbit", 17, "...");112test("7+1-" + TOO_BIG_STR, 7,0, 0, 0, "", 1, TOO_BIG_STR);113114tryCatch("9-pre+-opt", IAE);115tryCatch("1.4142+-", IAE);116tryCatch("2.9979+-%", IAE);117tryCatch("10--ea", IAE);118119//// Test for Runtime.version()120testVersion();121122//// Test for equals{IgnoreOptional}?(), hashCode(),123//// compareTo{IgnoreOptional}?()124// compare: after "<" == -1, equal == 0, before ">" == 1125// v0 v1 eq eqNO cmp cmpNO126testEHC("9", "9", true, true, 0, 0);127128testEHC("8", "9", false, false, -1, -1);129testEHC("9", "10", false, false, -1, -1);130testEHC("9", "8", false, false, 1, 1);131132testEHC("10.512.1", "10.512.2", false, false, -1, -1);133testEHC("10.512.0.1", "10.512.0.2", false, false, -1, -1);134testEHC("10.512.0.0.1", "10.512.0.0.2", false, false, -1, -1);135testEHC("512.10.1", "512.11.1", false, false, -1, -1);136137// $OPT comparison138testEHC("9", "9+-oink", false, true, -1, 0);139testEHC("9+-ribbit", "9+-moo", false, true, 1, 0);140testEHC("9-quack+3-ribbit",141"9-quack+3-moo", false, true, 1, 0);142testEHC("9.1+7", "9.1+7-moo-baa-la", false, true, -1, 0);143144// numeric vs. non-numeric $PRE145testEHC("9.1.1.2-2a", "9.1.1.2-12", false, false, 1, 1);146testEHC("9.1.1.2-12", "9.1.1.2-4", false, false, 1, 1);147148testEHC("27.16", "27.16+120", false, false, -1, -1);149testEHC("10", "10-ea", false, false, 1, 1);150testEHC("10.1+1", "10.1-ea+1", false, false, 1, 1);151testEHC("10.0.1+22", "10.0.1+21", false, false, 1, 1);152153// numeric vs. non-numeric $PRE154testEHC("9.1.1.2-12", "9.1.1.2-a2", false, false, -1, -1);155testEHC("9.1.1.2-1", "9.1.1.2-4", false, false, -1, -1);156157testEHC("9-internal", "9", false, false, -1, -1);158testEHC("9-ea+120", "9+120", false, false, -1, -1);159testEHC("9-ea+120", "9+120", false, false, -1, -1);160testEHC("9+101", "9", false, false, 1, 1);161testEHC("9+101", "9+102", false, false, -1, -1);162testEHC("1.9-ea", "9-ea", false, false, -1, -1);163164if (fail != 0)165throw new RuntimeException((fail + pass) + " tests: "166+ fail + " failure(s), first", first);167else168out.println("all " + (fail + pass) + " tests passed");169170}171172private static void test(String s, Integer feature, Integer interim,173Integer update, Integer patch,174String pre, Integer build, String opt)175{176Version v = testParse(s);177178testStr(v.toString(), s);179180testInt(v.feature(), feature);181testInt(v.major(), feature);182testInt(v.interim(), interim);183testInt(v.minor(), interim);184testInt(v.update(), update);185testInt(v.security(), update);186testInt(v.patch(), patch);187testStr((v.pre().isPresent() ? v.pre().get() : ""), pre);188testInt((v.build().isPresent() ? v.build().get() : 0), build);189testStr((v.optional().isPresent() ? v.optional().get() : ""), opt);190191testVersion(v.version(), s);192}193194private static Version testParse(String s) {195Version v = Version.parse(s);196pass();197return v;198}199200private static void testInt(int got, int exp) {201if (got != exp) {202fail("testInt()", Integer.toString(exp), Integer.toString(got));203} else {204pass();205}206}207208private static void testStr(String got, String exp) {209if (!got.equals(exp)) {210fail("testStr()", exp, got);211} else {212pass();213}214}215216private static void tryCatch(String s, Class<? extends Throwable> ex) {217Throwable t = null;218try {219Version.parse(s);220} catch (Throwable x) {221if (ex.isAssignableFrom(x.getClass())) {222t = x;223} else224x.printStackTrace();225}226if ((t == null) && (ex != null))227fail(s, ex);228else229pass();230}231232private static void testVersion() {233Version current = Runtime.version();234String javaVer = System.getProperty("java.runtime.version");235236// java.runtime.version == $VNUM(\-$PRE)?(\+$BUILD)?(-$OPT)?237String [] jv = javaVer.split("\\+");238String [] ver = jv[0].split("-");239List<Integer> javaVerVNum240= Arrays.stream(ver[0].split("\\."))241.map(Integer::parseInt)242.collect(Collectors.toList());243if (!javaVerVNum.equals(current.version())) {244fail("Runtime.version()", javaVerVNum.toString(),245current.version().toString());246} else {247pass();248}249250Optional<String> javaVerPre251= (ver.length == 2)252? Optional.ofNullable(ver[1])253: Optional.empty();254if (!javaVerPre.equals(current.pre())) {255fail("testCurrent() pre()", javaVerPre.toString(),256current.pre().toString());257} else {258pass();259}260261testEHC(current.toString(), javaVer, true, true, 0, 0);262}263264private static void testVersion(List<Integer> vnum, String s) {265List<Integer> svnum = new ArrayList<>();266StringBuilder sb = new StringBuilder();267for (int i = 0; i < s.length(); i++) {268Character c = s.charAt(i);269if (Character.isDigit(c)) {270sb.append(c);271} else {272svnum.add(Integer.parseInt(sb.toString()));273sb = new StringBuilder();274if (c == '+' || c == '-') {275break;276}277}278}279if (sb.length() > 0) {280svnum.add(Integer.parseInt(sb.toString()));281}282283if (!svnum.equals(vnum)) {284fail("testVersion() equals()", svnum.toString(), vnum.toString());285} else {286pass();287}288}289290private static void testEHC(String s0, String s1, boolean eq, boolean eqNO,291int cmp, int cmpNO)292{293Version v0 = Version.parse(s0);294Version v1 = Version.parse(s1);295296testEquals(v0, v1, eq);297testEqualsNO(v0, v1, eqNO);298299testHashCode(v0, v1, eq);300301testCompare(v0, v1, cmp);302testCompareNO(v0, v1, cmpNO);303}304305private static void testEqualsNO(Version v0, Version v1, boolean eq) {306if (eq == v0.equalsIgnoreOptional(v1)) {307pass();308} else {309fail("equalsIgnoreOptional() " + Boolean.toString(eq),310v0.toString(), v1.toString());311}312}313314private static void testEquals(Version v0, Version v1, boolean eq) {315if (eq == v0.equals(v1)) {316pass();317} else {318fail("equals() " + Boolean.toString(eq),319v0.toString(), v1.toString());320}321}322323private static void testHashCode(Version v0, Version v1, boolean eq) {324int h0 = v0.hashCode();325int h1 = v1.hashCode();326if (eq) {327testInt(h0, h1);328} else if (h0 == h1) {329fail(String.format("hashCode() %s", h0),330Integer.toString(h0),331Integer.toString(h1));332} else { // !eq && (h0 != h1)333pass();334}335}336337private static void testCompareNO(Version v0, Version v1, int compare) {338int cmp = v0.compareToIgnoreOptional(v1);339checkCompare(v0, v1, compare, cmp);340}341342private static void testCompare(Version v0, Version v1, int compare) {343int cmp = v0.compareTo(v1);344checkCompare(v0, v1, compare, cmp);345}346347private static void checkCompare(Version v0, Version v1,348int expected, int actual)349{350if (Integer.signum(expected) == Integer.signum(actual)) {351pass();352} else {353fail(String.format("compare() (actual = %s) (expected = %s)",354actual, expected),355v0.toString(), v1.toString());356}357}358359private static int fail = 0;360private static int pass = 0;361362private static Throwable first;363364static void pass() {365pass++;366}367368static void fail(String fs, Class ex) {369String s = "'" + fs + "'";370if (ex != null)371s += ": " + ex.getName() + " not thrown";372if (first == null)373setFirst(s);374System.err.println("FAILED: " + s);375fail++;376}377378static void fail(String t, String exp, String got) {379String s = t + ": Expected '" + exp + "', got '" + got + "'";380if (first == null)381setFirst(s);382System.err.println("FAILED: " + s);383fail++;384}385386private static void setFirst(String s) {387try {388throw new RuntimeException(s);389} catch (RuntimeException x) {390first = x;391}392}393394}395396397