test/tools/javac/multicatch/Pos11.java

Tue, 14 May 2013 18:02:48 -0700

author
sogoel
date
Tue, 14 May 2013 18:02:48 -0700
changeset 1756
53b389eb39c1
parent 0
959103a6100f
permissions
-rw-r--r--

8013163: Convert 4 tools multicatch tests to jtreg format
Reviewed-by: jjg

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 */
aoqi@0 23
aoqi@0 24 /*
aoqi@0 25 * @test
aoqi@0 26 * @bug 8013163
aoqi@0 27 * @author sogoel
aoqi@0 28 * @summary Test multiple nested multi-catch blocks with Exception hierarchies
aoqi@0 29 * @run main Pos11
aoqi@0 30 */
aoqi@0 31
aoqi@0 32 /*
aoqi@0 33 * For this test, exception hierarchy used:
aoqi@0 34 *
aoqi@0 35 * Throwable
aoqi@0 36 * / \
aoqi@0 37 * Exception Error
aoqi@0 38 * | | |
aoqi@0 39 * A B D
aoqi@0 40 * |
aoqi@0 41 * C
aoqi@0 42 * |
aoqi@0 43 * E
aoqi@0 44 * As an exception is thrown within a nested try-catch block, outer catch blocks
aoqi@0 45 * will catch an exception or its child exceptions, so the same exception can
aoqi@0 46 * be caught and rethrown multiple times.
aoqi@0 47 */
aoqi@0 48
aoqi@0 49 public class Pos11 {
aoqi@0 50
aoqi@0 51 public static String results = "";
aoqi@0 52 public static String sExpected = "-AB:A-AB:B-CD:C-AB:C-CD:D-Throwable:D-CD:E" +
aoqi@0 53 "-AB:E-Exception:Exception-Throwable:Exception";
aoqi@0 54
aoqi@0 55 enum TestExceptions {
aoqi@0 56 A("A"),
aoqi@0 57 B("B"),
aoqi@0 58 C("C"),
aoqi@0 59 D("D"),
aoqi@0 60 E("E"),
aoqi@0 61 U("U");
aoqi@0 62
aoqi@0 63 String exType;
aoqi@0 64 TestExceptions(String type) {
aoqi@0 65 this.exType = type;
aoqi@0 66 }
aoqi@0 67 }
aoqi@0 68
aoqi@0 69 public static void main(String... args) {
aoqi@0 70 Pos11 pos11 = new Pos11();
aoqi@0 71 for(TestExceptions t : TestExceptions.values()) {
aoqi@0 72 pos11.rethrower(t.exType);
aoqi@0 73 }
aoqi@0 74 if (results.compareTo(sExpected) != 0)
aoqi@0 75 throw new RuntimeException("FAIL: final strings did not match:\n"
aoqi@0 76 + results + "!=\n" + sExpected);
aoqi@0 77 System.out.println("PASS");
aoqi@0 78 }
aoqi@0 79
aoqi@0 80 void rethrower(String T) {
aoqi@0 81 try { /* try1 */
aoqi@0 82 try { /* try2 */
aoqi@0 83 try { /* try3 */
aoqi@0 84 try { /* try4 */
aoqi@0 85 switch (T) {
aoqi@0 86 case "A":
aoqi@0 87 throw new A();
aoqi@0 88 case "B":
aoqi@0 89 throw new B();
aoqi@0 90 case "C":
aoqi@0 91 throw new C();
aoqi@0 92 case "D":
aoqi@0 93 throw new D();
aoqi@0 94 case "E":
aoqi@0 95 throw new E();
aoqi@0 96 default:
aoqi@0 97 throw new Exception(
aoqi@0 98 new Throwable());
aoqi@0 99 }
aoqi@0 100 } catch ( final C|D cd) {
aoqi@0 101 results=results.concat("-CD:" + cd.getClass().getSimpleName());
aoqi@0 102 throw cd;
aoqi@0 103 }
aoqi@0 104 } catch (final A|B ab) {
aoqi@0 105 results=results.concat("-AB:" + ab.getClass().getSimpleName());
aoqi@0 106 }
aoqi@0 107 } catch (final Exception e ) {
aoqi@0 108 results=results.concat("-Exception:" + e.getClass().getSimpleName());
aoqi@0 109 throw e;
aoqi@0 110 }
aoqi@0 111 } catch (Throwable t) {
aoqi@0 112 results=results.concat("-Throwable:" + t.getClass().getSimpleName());
aoqi@0 113 }
aoqi@0 114 }
aoqi@0 115
aoqi@0 116 // Test Exception
aoqi@0 117 static class A extends Exception {}
aoqi@0 118
aoqi@0 119 // Test Exception
aoqi@0 120 static class B extends Exception {}
aoqi@0 121
aoqi@0 122 // Test Exception
aoqi@0 123 static class C extends B {}
aoqi@0 124
aoqi@0 125 // Not a descendant of Exception
aoqi@0 126 static class D extends Error {}
aoqi@0 127
aoqi@0 128 // Test Exception
aoqi@0 129 static class E extends C {}
aoqi@0 130
aoqi@0 131 }
aoqi@0 132

mercurial