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