1 /* |
1 /* |
2 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
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 |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. |
7 * published by the Free Software Foundation. |
68 |
68 |
69 static <V> void checkReady(final FutureTask<V> task) { |
69 static <V> void checkReady(final FutureTask<V> task) { |
70 check(! task.isDone()); |
70 check(! task.isDone()); |
71 check(! task.isCancelled()); |
71 check(! task.isCancelled()); |
72 THROWS(TimeoutException.class, |
72 THROWS(TimeoutException.class, |
73 new Fun(){void f() throws Throwable { |
73 () -> task.get(0L, TimeUnit.SECONDS)); |
74 task.get(0L, TimeUnit.SECONDS); }}); |
|
75 } |
74 } |
76 |
75 |
77 static <V> void checkDone(final FutureTask<V> task) { |
76 static <V> void checkDone(final FutureTask<V> task) { |
78 try { |
77 try { |
79 check(task.isDone()); |
78 check(task.isDone()); |
84 |
83 |
85 static <V> void checkCancelled(final FutureTask<V> task) { |
84 static <V> void checkCancelled(final FutureTask<V> task) { |
86 check(task.isDone()); |
85 check(task.isDone()); |
87 check(task.isCancelled()); |
86 check(task.isCancelled()); |
88 THROWS(CancellationException.class, |
87 THROWS(CancellationException.class, |
89 new Fun(){void f() throws Throwable { |
88 () -> task.get(0L, TimeUnit.SECONDS), |
90 task.get(0L, TimeUnit.SECONDS); }}, |
89 () -> task.get()); |
91 new Fun(){void f() throws Throwable { |
|
92 task.get(); }}); |
|
93 } |
90 } |
94 |
91 |
95 static <V> void checkThrew(final FutureTask<V> task) { |
92 static <V> void checkThrew(final FutureTask<V> task) { |
96 check(task.isDone()); |
93 check(task.isDone()); |
97 check(! task.isCancelled()); |
94 check(! task.isCancelled()); |
98 THROWS(ExecutionException.class, |
95 THROWS(ExecutionException.class, |
99 new Fun(){void f() throws Throwable { |
96 () -> task.get(0L, TimeUnit.SECONDS), |
100 task.get(0L, TimeUnit.SECONDS); }}, |
97 () -> task.get()); |
101 new Fun(){void f() throws Throwable { |
|
102 task.get(); }}); |
|
103 } |
98 } |
104 |
99 |
105 static <V> void cancel(FutureTask<V> task, boolean mayInterruptIfRunning) { |
100 static <V> void cancel(FutureTask<V> task, boolean mayInterruptIfRunning) { |
106 task.cancel(mayInterruptIfRunning); |
101 task.cancel(mayInterruptIfRunning); |
107 checkCancelled(task); |
102 checkCancelled(task); |
201 else fail(x + " not equal to " + y);} |
196 else fail(x + " not equal to " + y);} |
202 public static void main(String[] args) throws Throwable { |
197 public static void main(String[] args) throws Throwable { |
203 try {realMain(args);} catch (Throwable t) {unexpected(t);} |
198 try {realMain(args);} catch (Throwable t) {unexpected(t);} |
204 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); |
199 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); |
205 if (failed > 0) throw new AssertionError("Some tests failed");} |
200 if (failed > 0) throw new AssertionError("Some tests failed");} |
206 private abstract static class Fun {abstract void f() throws Throwable;} |
201 interface Fun {void f() throws Throwable;} |
207 static void THROWS(Class<? extends Throwable> k, Fun... fs) { |
202 static void THROWS(Class<? extends Throwable> k, Fun... fs) { |
208 for (Fun f : fs) |
203 for (Fun f : fs) |
209 try { f.f(); fail("Expected " + k.getName() + " not thrown"); } |
204 try { f.f(); fail("Expected " + k.getName() + " not thrown"); } |
210 catch (Throwable t) { |
205 catch (Throwable t) { |
211 if (k.isAssignableFrom(t.getClass())) pass(); |
206 if (k.isAssignableFrom(t.getClass())) pass(); |