FutureConverter implements Future<T> { @Override public T get() throws ExecutionException, InterruptedException { V v = _future.get(); try { return convert(v); } catch (Throwable throwable) { throw new ExecutionException(throwable); } } FutureConverter(Future<V> future); @Override boolean cancel(boolean mayInterruptIfRunning); @Override T get(); @Override T get(long timeout, TimeUnit timeUnit); @Override boolean isCancelled(); @Override boolean isDone();  }
@Test public void testCovertCausedExecutionException() throws Exception { _futureTask.run(); final Exception exception = new Exception(); Future<Object> future = new FutureConverter<Object, Object>( _futureTask) { @Override protected Object convert(Object v) throws Exception { throw exception; } }; try { future.get(); Assert.fail(); } catch (ExecutionException executionException) { Assert.assertSame(exception, executionException.getCause()); } try { future.get(1, TimeUnit.SECONDS); Assert.fail(); } catch (ExecutionException executionException) { Assert.assertSame(exception, executionException.getCause()); } }
@Test public void testCovertResult() throws Exception { Object result = new Object(); FutureTask<Object> futureTask = new FutureTask<Object>( new Runnable() { @Override public void run() { } }, result); futureTask.run(); Future<Object> future = new NopFutureConverter(futureTask); Assert.assertSame(result, future.get()); Assert.assertSame(result, future.get(1, TimeUnit.SECONDS)); }
@Test public void testExecutionException() throws Exception { final Exception exception = new Exception(); FutureTask<Object> futureTask = new FutureTask<Object>( new Callable<Object>() { @Override public Object call() throws Exception { throw exception; } }); futureTask.run(); Future<Object> future = new NopFutureConverter(futureTask); try { future.get(); Assert.fail(); } catch (ExecutionException executionException) { Assert.assertSame(exception, executionException.getCause()); } try { future.get(1, TimeUnit.SECONDS); Assert.fail(); } catch (ExecutionException executionException) { Assert.assertSame(exception, executionException.getCause()); } }
@Test(timeout = 10000) public void testInterruptionException() throws Exception { Future<Object> future = new NopFutureConverter(_futureTask); Thread currentThread = Thread.currentThread(); currentThread.interrupt(); try { future.get(); Assert.fail(); } catch (InterruptedException interruptedException) { } Assert.assertFalse(currentThread.isInterrupted()); currentThread.interrupt(); try { future.get(1, TimeUnit.MILLISECONDS); Assert.fail(); } catch (InterruptedException interruptedException) { } Assert.assertFalse(currentThread.isInterrupted()); }
@Test public void testTimeoutException() throws Exception { Future<Object> future = new NopFutureConverter(_futureTask); try { future.get(1, TimeUnit.MILLISECONDS); Assert.fail(); } catch (TimeoutException timeoutException) { } }