1 /* 2 * Copyright (C) 2014 Google, Inc. 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 package dagger.producers.internal; 17 18 import com.google.common.util.concurrent.Futures; 19 import com.google.common.util.concurrent.ListenableFuture; 20 import com.google.common.util.concurrent.SettableFuture; 21 import dagger.producers.Produced; 22 import dagger.producers.Producer; 23 import java.util.Set; 24 import java.util.concurrent.CancellationException; 25 import java.util.concurrent.ExecutionException; 26 import javax.inject.Provider; 27 import org.junit.Test; 28 import org.junit.runner.RunWith; 29 import org.junit.runners.JUnit4; 30 31 import static com.google.common.truth.Truth.assertThat; 32 import static org.junit.Assert.fail; 33 34 /** 35 * Tests {@link Producers}. 36 */ 37 @RunWith(JUnit4.class) 38 public class ProducersTest { 39 @Test public void createFutureProduced_success() throws Exception { 40 ListenableFuture<String> future = Futures.immediateFuture("monkey"); 41 ListenableFuture<Produced<String>> producedFuture = Producers.createFutureProduced(future); 42 assertThat(producedFuture.isDone()).isTrue(); 43 assertThat(producedFuture.get().get()).isEqualTo("monkey"); 44 } 45 46 @Test public void createFutureProduced_failure() throws Exception { 47 ListenableFuture<String> future = Futures.immediateFailedFuture(new RuntimeException("monkey")); 48 ListenableFuture<Produced<String>> producedFuture = Producers.createFutureProduced(future); 49 assertThat(producedFuture.isDone()).isTrue(); 50 assertThat(getProducedException(producedFuture.get()).getCause()).hasMessage("monkey"); 51 } 52 53 @Test public void createFutureProduced_cancelPropagatesBackwards() throws Exception { 54 ListenableFuture<String> future = SettableFuture.create(); 55 ListenableFuture<Produced<String>> producedFuture = Producers.createFutureProduced(future); 56 assertThat(producedFuture.isDone()).isFalse(); 57 producedFuture.cancel(false); 58 assertThat(future.isCancelled()).isTrue(); 59 } 60 61 @Test public void createFutureProduced_cancelDoesNotPropagateForwards() throws Exception { 62 ListenableFuture<String> future = SettableFuture.create(); 63 ListenableFuture<Produced<String>> producedFuture = Producers.createFutureProduced(future); 64 assertThat(producedFuture.isDone()).isFalse(); 65 future.cancel(false); 66 assertThat(producedFuture.isCancelled()).isFalse(); 67 assertThat(getProducedException(producedFuture.get()).getCause()) 68 .isInstanceOf(CancellationException.class); 69 } 70 71 private <T> ExecutionException getProducedException(Produced<T> produced) { 72 try { 73 produced.get(); 74 throw new IllegalArgumentException("produced did not throw"); 75 } catch (ExecutionException e) { 76 return e; 77 } 78 } 79 80 @Test public void createFutureSingletonSet_success() throws Exception { 81 ListenableFuture<String> future = Futures.immediateFuture("monkey"); 82 ListenableFuture<Set<String>> setFuture = Producers.createFutureSingletonSet(future); 83 assertThat(setFuture.isDone()).isTrue(); 84 assertThat(setFuture.get()).containsExactly("monkey"); 85 } 86 87 @Test public void createFutureSingletonSet_failure() throws Exception { 88 ListenableFuture<String> future = Futures.immediateFailedFuture(new RuntimeException("monkey")); 89 ListenableFuture<Set<String>> setFuture = Producers.createFutureSingletonSet(future); 90 assertThat(setFuture.isDone()).isTrue(); 91 try { 92 setFuture.get(); 93 fail(); 94 } catch (ExecutionException e) { 95 assertThat(e.getCause()).hasMessage("monkey"); 96 } 97 } 98 99 @Test public void producerFromProvider() throws Exception { 100 Producer<Integer> producer = Producers.producerFromProvider(new Provider<Integer>() { 101 int i = 0; 102 103 @Override public Integer get() { 104 return i++; 105 } 106 }); 107 assertThat(producer.get().get()).isEqualTo(0); 108 assertThat(producer.get().get()).isEqualTo(0); 109 assertThat(producer.get().get()).isEqualTo(0); 110 } 111 } 112