| 6 | import java.util.concurrent.TimeoutException; |
| 7 | |
| 8 | public class FutureTaskTest { |
| 9 | private static final int DELAY_TIME = 10; |
| 10 | |
| 11 | public static void main(String[] args) throws InterruptedException, ExecutionException { |
| 12 | isDoneTest(false); |
| 13 | isDoneTest(true); |
| 14 | getCallableResultTest(); |
| 15 | getRunnableResultTest(); |
| 16 | getTimeoutFail(); |
| 17 | getExecutionExceptionTest(); |
| 18 | } |
| 19 | |
| 20 | private static void isDoneTest(final boolean throwException) { |
| 21 | RunnableFuture<?> future = new FutureTask<Object>(new Runnable() { |
| 22 | @Override |
| 23 | public void run() { |
| 24 | if (throwException) { |
| 25 | throw new RuntimeException(); |
| 26 | } |
| 27 | } |
| 28 | }, null); |
| 29 | |
| 30 | // should finish the future |
| 31 | future.run(); |
| 32 | |
| 33 | if (! future.isDone()) { |
| 34 | throw new RuntimeException("Future should be done"); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | private static void getCallableResultTest() throws InterruptedException, ExecutionException { |
| 39 | final Object result = new Object(); |
| 40 | FutureTask<Object> future = new FutureTask<Object>(new Callable<Object>() { |
| 41 | @Override |
| 42 | public Object call() throws Exception { |
| 43 | return result; |
| 44 | } |
| 45 | }); |
| 46 | |
| 47 | future.run(); |
| 48 | if (future.get() != result) { |
| 49 | throw new RuntimeException("Bad result returned: " + future.get()); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | private static void getRunnableResultTest() throws InterruptedException, ExecutionException { |
| 54 | final Object result = new Object(); |
| 55 | FutureTask<Object> future = new FutureTask<Object>(new Runnable() { |
| 56 | @Override |
| 57 | public void run() { |
| 58 | // nothing here |
| 59 | } |
| 60 | }, result); |
| 61 | |
| 62 | future.run(); |
| 63 | if (future.get() != result) { |
| 64 | throw new RuntimeException("Bad result returned: " + future.get()); |
| 65 | } |
nothing calls this directly
no outgoing calls
no test coverage detected