--- /dev/null Tue Nov 24 14:27:24 2015 +++ new/test/jdk/OracleVersion/Basic.java Tue Nov 24 14:27:24 2015 @@ -0,0 +1,139 @@ +/* + * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @summary Unit test for jdk.OracleVersion. + * @bug 8072379 + */ + +import java.math.BigInteger; + +import jdk.OracleVersion; +import static java.lang.System.out; + +public class Basic { + private static final Class IAE + = IllegalArgumentException.class; + private static final Class NFE + = NumberFormatException.class; + + private static final BigInteger TOO_BIG + = (BigInteger.valueOf(Integer.MAX_VALUE)).add(BigInteger.ONE); + private static final String TOO_BIG_STR = TOO_BIG.toString(); + + public static void main(String ... args) { + + //// Tests for parse() and patch() + // v patch + + // $PATCH + test("9", 0); + test("10.1", 0); + test("1.0.1", 0); + test("9.1.2.3", 3); + test("9.1.2.3.4", 3); + test("1000.0.0.99999999", 99999999); + + tryCatch("2.0.0.1.", IAE); + tryCatch("2.0.0.u1", IAE); + tryCatch("42.0.0.07", IAE); + tryCatch("1.0.0." + TOO_BIG_STR, NFE); + + if (fail != 0) + throw new RuntimeException((fail + pass) + " tests: " + + fail + " failure(s), first", first); + else + out.println("all " + (fail + pass) + " tests passed"); + + } + + private static void test(String s, Integer patch) { + OracleVersion v = testParse(s); + + testInt(v.patch(), patch); + } + + private static OracleVersion testParse(String s) { + OracleVersion v = OracleVersion.parse(s); + pass(); + return v; + } + + private static void testInt(int got, int exp) { + if (got != exp) { + fail("testInt()", Integer.toString(exp), Integer.toString(got)); + } else { + pass(); + } + } + + private static void tryCatch(String s, Class ex) { + Throwable t = null; + try { + OracleVersion.parse(s); + } catch (Throwable x) { + if (ex.isAssignableFrom(x.getClass())) { + t = x; + } else + x.printStackTrace(); + } + if ((t == null) && (ex != null)) + fail(s, ex); + else + pass(); + } + + private static int fail = 0; + private static int pass = 0; + + private static Throwable first; + + static void pass() { + pass++; + } + + static void fail(String fs, Class ex) { + String s = "'" + fs + "': " + ex.getName() + " not thrown"; + if (first == null) + setFirst(s); + System.err.println("FAILED: " + s); + fail++; + } + + static void fail(String t, String exp, String got) { + String s = t + ": Expected '" + exp + "', got '" + got + "'"; + if (first == null) + setFirst(s); + System.err.println("FAILED: " + s); + fail++; + } + + private static void setFirst(String s) { + try { + throw new RuntimeException(s); + } catch (RuntimeException x) { + first = x; + } + } +}