blob: 9a0f7cbddd1677005d37bf8f83cb146aaa9f6758 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2007 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 * @test
26 * @bug 6415572
27 * @summary Check exceptional behavior in run and done methods
28 */
29
30import java.util.concurrent.*;
31
32public class Throw {
33
34 @SuppressWarnings("deprecation")
35 static void THROW(final Throwable t) {
36 if (t != null)
37 Thread.currentThread().stop(t);
38 }
39
40 Callable<Void> thrower(final Throwable t) {
41 return new Callable<Void>() { public Void call() {
42 THROW(t); return null; }};
43 }
44
45 @SuppressWarnings("serial")
46 private static class DoneError extends Error {}
47
48 @SuppressWarnings("serial")
49 private static class DoneException extends RuntimeException {}
50
51 static class MyFutureTask extends FutureTask<Void> {
52 MyFutureTask(Callable<Void> task) { super(task); }
53 public boolean runAndReset() { return super.runAndReset(); }
54 }
55
56 MyFutureTask checkTask(final MyFutureTask task) {
57 check(! task.isCancelled());
58 check(! task.isDone());
59 return task;
60 }
61
62 MyFutureTask taskFor(final Throwable callableThrowable,
63 final Throwable doneThrowable) {
64 return checkTask(
65 new MyFutureTask(thrower(callableThrowable)) {
66 protected void done() { THROW(doneThrowable); }});
67 }
68
69 void test(String[] args) throws Throwable {
70 final Throwable[] callableThrowables = {
71 null, new Exception(), new Error(), new RuntimeException() };
72 final Throwable[] doneThrowables = {
73 new DoneError(), new DoneException() };
74 for (final Throwable c : callableThrowables) {
75 for (final Throwable d : doneThrowables) {
76 THROWS(d.getClass(),
77 new F(){void f(){
78 taskFor(c, d).cancel(false);}},
79 new F(){void f(){
80 taskFor(c, d).run();}});
81 if (c != null)
82 THROWS(d.getClass(),
83 new F(){void f(){
84 taskFor(c, d).runAndReset();}});
85 }
86
87 try {
88 final MyFutureTask task = taskFor(c, null);
89 check(task.cancel(false));
90 THROWS(CancellationException.class,
91 new F(){void f() throws Throwable { task.get(); }});
92 } catch (Throwable t) { unexpected(t); }
93
94 if (c != null) {
95 final MyFutureTask task = taskFor(c, null);
96 task.run();
97 try {
98 task.get();
99 fail("Expected ExecutionException");
100 } catch (ExecutionException ee) {
101 equal(c.getClass(), ee.getCause().getClass());
102 } catch (Throwable t) { unexpected(t); }
103 }
104
105 if (c != null) {
106 final MyFutureTask task = taskFor(c, null);
107 task.runAndReset();
108 try {
109 task.get();
110 fail("Expected ExecutionException");
111 } catch (ExecutionException ee) {
112 check(c.getClass().isInstance(ee.getCause()));
113 } catch (Throwable t) { unexpected(t); }
114 }
115 }
116 }
117
118 //--------------------- Infrastructure ---------------------------
119 volatile int passed = 0, failed = 0;
120 void pass() {passed++;}
121 void fail() {failed++; Thread.dumpStack();}
122 void fail(String msg) {System.err.println(msg); fail();}
123 void unexpected(Throwable t) {failed++; t.printStackTrace();}
124 void check(boolean cond) {if (cond) pass(); else fail();}
125 void equal(Object x, Object y) {
126 if (x == null ? y == null : x.equals(y)) pass();
127 else fail(x + " not equal to " + y);}
128 public static void main(String[] args) throws Throwable {
129 new Throw().instanceMain(args);}
130 void instanceMain(String[] args) throws Throwable {
131 try {test(args);} catch (Throwable t) {unexpected(t);}
132 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
133 if (failed > 0) throw new AssertionError("Some tests failed");}
134 abstract class F {abstract void f() throws Throwable;}
135 void THROWS(Class<? extends Throwable> k, F... fs) {
136 for (F f : fs)
137 try {f.f(); fail("Expected " + k.getName() + " not thrown");}
138 catch (Throwable t) {
139 if (k.isAssignableFrom(t.getClass())) pass();
140 else unexpected(t);}}
141}