1 /* 2 * Copyright (C) 2014 The Dagger Authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package dagger.producers.internal; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static org.junit.Assert.fail; 21 22 import com.google.common.collect.ImmutableList; 23 import com.google.common.util.concurrent.Futures; 24 import com.google.common.util.concurrent.ListenableFuture; 25 import com.google.common.util.concurrent.SettableFuture; 26 import dagger.internal.Provider; 27 import dagger.producers.Produced; 28 import dagger.producers.Producer; 29 import java.util.Set; 30 import java.util.concurrent.CancellationException; 31 import java.util.concurrent.ExecutionException; 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 import org.junit.runners.JUnit4; 35 36 /** 37 * Tests {@link Producers}. 38 */ 39 @RunWith(JUnit4.class) 40 public class ProducersTest { createFutureProduced_success()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 createFutureProduced_failure()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())) 53 .hasCauseThat() 54 .hasMessageThat() 55 .isEqualTo("monkey"); 56 } 57 createFutureProduced_cancelPropagatesBackwards()58 @Test public void createFutureProduced_cancelPropagatesBackwards() throws Exception { 59 ListenableFuture<String> future = SettableFuture.create(); 60 ListenableFuture<Produced<String>> producedFuture = Producers.createFutureProduced(future); 61 assertThat(producedFuture.isDone()).isFalse(); 62 producedFuture.cancel(false); 63 assertThat(future.isCancelled()).isTrue(); 64 } 65 createFutureProduced_cancelDoesNotPropagateForwards()66 @Test public void createFutureProduced_cancelDoesNotPropagateForwards() throws Exception { 67 ListenableFuture<String> future = SettableFuture.create(); 68 ListenableFuture<Produced<String>> producedFuture = Producers.createFutureProduced(future); 69 assertThat(producedFuture.isDone()).isFalse(); 70 future.cancel(false); 71 assertThat(producedFuture.isCancelled()).isFalse(); 72 assertThat(getProducedException(producedFuture.get())) 73 .hasCauseThat() 74 .isInstanceOf(CancellationException.class); 75 } 76 getProducedException(Produced<T> produced)77 private <T> ExecutionException getProducedException(Produced<T> produced) { 78 try { 79 T value = produced.get(); 80 throw new IllegalArgumentException("produced did not throw, but returned " + value); 81 } catch (ExecutionException e) { 82 return e; 83 } 84 } 85 createFutureSingletonSet_success()86 @Test public void createFutureSingletonSet_success() throws Exception { 87 ListenableFuture<String> future = Futures.immediateFuture("monkey"); 88 ListenableFuture<Set<String>> setFuture = Producers.createFutureSingletonSet(future); 89 assertThat(setFuture.isDone()).isTrue(); 90 assertThat(setFuture.get()).containsExactly("monkey"); 91 } 92 createFutureSingletonSet_failure()93 @Test public void createFutureSingletonSet_failure() throws Exception { 94 ListenableFuture<String> future = Futures.immediateFailedFuture(new RuntimeException("monkey")); 95 ListenableFuture<Set<String>> setFuture = Producers.createFutureSingletonSet(future); 96 assertThat(setFuture.isDone()).isTrue(); 97 try { 98 setFuture.get(); 99 fail(); 100 } catch (ExecutionException e) { 101 assertThat(e).hasCauseThat().hasMessageThat().isEqualTo("monkey"); 102 } 103 } 104 105 @Test allAsSet_success()106 public void allAsSet_success() throws Exception { 107 ListenableFuture<Set<String>> future = 108 Producers.allAsSet( 109 ImmutableList.of( 110 Futures.immediateFuture("monkey"), Futures.immediateFuture("gorilla"))); 111 assertThat(future.isDone()).isTrue(); 112 assertThat(future.get()).containsExactly("monkey", "gorilla"); 113 } 114 115 @Test allAsSet_failure()116 public void allAsSet_failure() throws Exception { 117 ListenableFuture<Set<String>> future = 118 Producers.allAsSet( 119 ImmutableList.of( 120 Futures.immediateFuture("monkey"), 121 Futures.<String>immediateFailedFuture(new RuntimeException("gorilla")))); 122 assertThat(future.isDone()).isTrue(); 123 try { 124 future.get(); 125 fail(); 126 } catch (ExecutionException e) { 127 assertThat(e).hasCauseThat().hasMessageThat().isEqualTo("gorilla"); 128 } 129 } 130 producerFromProvider_doesntCache()131 @Test public void producerFromProvider_doesntCache() throws Exception { 132 Producer<Integer> producer = Producers.producerFromProvider(new Provider<Integer>() { 133 int i = 0; 134 135 @Override public Integer get() { 136 return i++; 137 } 138 }); 139 assertThat(producer.get().get()).isEqualTo(0); 140 assertThat(producer.get().get()).isEqualTo(1); 141 assertThat(producer.get().get()).isEqualTo(2); 142 } 143 } 144