1 /* 2 * Copyright (c) 2015, 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 jdk.OracleVersion. 27 * @bug 8072379 28 */ 29 30 import java.math.BigInteger; 31 32 import jdk.OracleVersion; 33 import static java.lang.System.out; 34 35 public class Basic { 36 private static final Class<? extends Throwable> IAE 37 = IllegalArgumentException.class; 38 private static final Class<? extends Throwable> NFE 39 = NumberFormatException.class; 40 41 private static final BigInteger TOO_BIG 42 = (BigInteger.valueOf(Integer.MAX_VALUE)).add(BigInteger.ONE); 43 private static final String TOO_BIG_STR = TOO_BIG.toString(); 44 45 public static void main(String ... args) { 46 47 //// Tests for parse() and patch() 48 // v patch 49 50 // $PATCH 51 test("9", 0); 52 test("10.1", 0); 53 test("1.0.1", 0); 54 test("9.1.2.3", 3); 55 test("9.1.2.3.4", 3); 56 test("1000.0.0.99999999", 99999999); 57 58 tryCatch("2.0.0.1.", IAE); 59 tryCatch("2.0.0.u1", IAE); 60 tryCatch("42.0.0.07", IAE); 61 tryCatch("1.0.0." + TOO_BIG_STR, NFE); 62 63 if (fail != 0) 64 throw new RuntimeException((fail + pass) + " tests: " 65 + fail + " failure(s), first", first); 66 else 67 out.println("all " + (fail + pass) + " tests passed"); 68 69 } 70 71 private static void test(String s, Integer patch) { 72 OracleVersion v = testParse(s); 73 74 testInt(v.patch(), patch); 75 } 76 77 private static OracleVersion testParse(String s) { 78 OracleVersion v = OracleVersion.parse(s); 79 pass(); 80 return v; 81 } 82 83 private static void testInt(int got, int exp) { 84 if (got != exp) { 85 fail("testInt()", Integer.toString(exp), Integer.toString(got)); 86 } else { 87 pass(); 88 } 89 } 90 91 private static void tryCatch(String s, Class<? extends Throwable> ex) { 92 Throwable t = null; 93 try { 94 OracleVersion.parse(s); 95 } catch (Throwable x) { 96 if (ex.isAssignableFrom(x.getClass())) { 97 t = x; 98 } else 99 x.printStackTrace(); 100 } 101 if ((t == null) && (ex != null)) 102 fail(s, ex); 103 else 104 pass(); 105 } 106 107 private static int fail = 0; 108 private static int pass = 0; 109 110 private static Throwable first; 111 112 static void pass() { 113 pass++; 114 } 115 116 static void fail(String fs, Class ex) { 117 String s = "'" + fs + "': " + ex.getName() + " not thrown"; 118 if (first == null) 119 setFirst(s); 120 System.err.println("FAILED: " + s); 121 fail++; 122 } 123 124 static void fail(String t, String exp, String got) { 125 String s = t + ": Expected '" + exp + "', got '" + got + "'"; 126 if (first == null) 127 setFirst(s); 128 System.err.println("FAILED: " + s); 129 fail++; 130 } 131 132 private static void setFirst(String s) { 133 try { 134 throw new RuntimeException(s); 135 } catch (RuntimeException x) { 136 first = x; 137 } 138 } 139 }