| 22 | import junit.framework.TestCase; |
| 23 | |
| 24 | public class TaskTest extends TestCase { |
| 25 | private void runTaskTest(Callable<Task<?>> callable) { |
| 26 | try { |
| 27 | Task<?> task = callable.call(); |
| 28 | task.waitForCompletion(); |
| 29 | if (task.isFaulted()) { |
| 30 | Exception error = task.getError(); |
| 31 | if (error instanceof RuntimeException) { |
| 32 | throw (RuntimeException) error; |
| 33 | } |
| 34 | throw new RuntimeException(error); |
| 35 | } else if (task.isCancelled()) { |
| 36 | throw new RuntimeException(new CancellationException()); |
| 37 | } |
| 38 | } catch (Exception e) { |
| 39 | throw new RuntimeException(e); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | public void testPrimitives() { |
| 44 | Task<Integer> complete = Task.forResult(5); |
| 45 | Task<Integer> error = Task.forError(new RuntimeException()); |
| 46 | Task<Integer> cancelled = Task.cancelled(); |
| 47 | |
| 48 | assertTrue(complete.isCompleted()); |
| 49 | assertEquals(5, complete.getResult().intValue()); |
| 50 | assertFalse(complete.isFaulted()); |
| 51 | assertFalse(complete.isCancelled()); |
| 52 | |
| 53 | assertTrue(error.isCompleted()); |
| 54 | assertTrue(error.getError() instanceof RuntimeException); |
| 55 | assertTrue(error.isFaulted()); |
| 56 | assertFalse(error.isCancelled()); |
| 57 | |
| 58 | assertTrue(cancelled.isCompleted()); |
| 59 | assertFalse(cancelled.isFaulted()); |
| 60 | assertTrue(cancelled.isCancelled()); |
| 61 | } |
| 62 | |
| 63 | public void testSynchronousContinuation() { |
| 64 | final Task<Integer> complete = Task.forResult(5); |
| 65 | final Task<Integer> error = Task.forError(new RuntimeException()); |
| 66 | final Task<Integer> cancelled = Task.cancelled(); |
| 67 | |
| 68 | complete.continueWith(new Continuation<Integer, Void>() { |
| 69 | public Void then(Task<Integer> task) { |
| 70 | assertEquals(complete, task); |
| 71 | assertTrue(task.isCompleted()); |
| 72 | assertEquals(5, task.getResult().intValue()); |
| 73 | assertFalse(task.isFaulted()); |
| 74 | assertFalse(task.isCancelled()); |
| 75 | return null; |
| 76 | } |
| 77 | }); |
| 78 | |
| 79 | error.continueWith(new Continuation<Integer, Void>() { |
| 80 | public Void then(Task<Integer> task) { |
| 81 | assertEquals(error, task); |
nothing calls this directly
no outgoing calls
no test coverage detected