Tests Producers.
| 37 | * Tests {@link Producers}. |
| 38 | */ |
| 39 | @RunWith(JUnit4.class) |
| 40 | public class ProducersTest { |
| 41 | @Test public void createFutureProduced_success() throws Exception { |
| 42 | ListenableFuture<String> future = Futures.immediateFuture("monkey"); |
| 43 | ListenableFuture<Produced<String>> producedFuture = Producers.createFutureProduced(future); |
| 44 | assertThat(producedFuture.isDone()).isTrue(); |
| 45 | assertThat(producedFuture.get().get()).isEqualTo("monkey"); |
| 46 | } |
| 47 | |
| 48 | @Test public void createFutureProduced_failure() throws Exception { |
| 49 | ListenableFuture<String> future = Futures.immediateFailedFuture(new RuntimeException("monkey")); |
| 50 | ListenableFuture<Produced<String>> producedFuture = Producers.createFutureProduced(future); |
| 51 | assertThat(producedFuture.isDone()).isTrue(); |
| 52 | assertThat(getProducedException(producedFuture.get()).getCause()).hasMessage("monkey"); |
| 53 | } |
| 54 | |
| 55 | @Test public void createFutureProduced_cancelPropagatesBackwards() throws Exception { |
| 56 | ListenableFuture<String> future = SettableFuture.create(); |
| 57 | ListenableFuture<Produced<String>> producedFuture = Producers.createFutureProduced(future); |
| 58 | assertThat(producedFuture.isDone()).isFalse(); |
| 59 | producedFuture.cancel(false); |
| 60 | assertThat(future.isCancelled()).isTrue(); |
| 61 | } |
| 62 | |
| 63 | @Test public void createFutureProduced_cancelDoesNotPropagateForwards() throws Exception { |
| 64 | ListenableFuture<String> future = SettableFuture.create(); |
| 65 | ListenableFuture<Produced<String>> producedFuture = Producers.createFutureProduced(future); |
| 66 | assertThat(producedFuture.isDone()).isFalse(); |
| 67 | future.cancel(false); |
| 68 | assertThat(producedFuture.isCancelled()).isFalse(); |
| 69 | assertThat(getProducedException(producedFuture.get()).getCause()) |
| 70 | .isInstanceOf(CancellationException.class); |
| 71 | } |
| 72 | |
| 73 | private <T> ExecutionException getProducedException(Produced<T> produced) { |
| 74 | try { |
| 75 | T value = produced.get(); |
| 76 | throw new IllegalArgumentException("produced did not throw, but returned " + value); |
| 77 | } catch (ExecutionException e) { |
| 78 | return e; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | @Test public void createFutureSingletonSet_success() throws Exception { |
| 83 | ListenableFuture<String> future = Futures.immediateFuture("monkey"); |
| 84 | ListenableFuture<Set<String>> setFuture = Producers.createFutureSingletonSet(future); |
| 85 | assertThat(setFuture.isDone()).isTrue(); |
| 86 | assertThat(setFuture.get()).containsExactly("monkey"); |
| 87 | } |
| 88 | |
| 89 | @Test public void createFutureSingletonSet_failure() throws Exception { |
| 90 | ListenableFuture<String> future = Futures.immediateFailedFuture(new RuntimeException("monkey")); |
| 91 | ListenableFuture<Set<String>> setFuture = Producers.createFutureSingletonSet(future); |
| 92 | assertThat(setFuture.isDone()).isTrue(); |
| 93 | try { |
| 94 | setFuture.get(); |
| 95 | fail(); |
| 96 | } catch (ExecutionException e) { |
nothing calls this directly
no outgoing calls
no test coverage detected