1 package producerstest.badexecutor; 2 3 import com.google.common.util.concurrent.Futures; 4 import com.google.common.util.concurrent.ListenableFuture; 5 import com.google.common.util.concurrent.ListeningExecutorService; 6 import com.google.common.util.concurrent.MoreExecutors; 7 import java.util.concurrent.ExecutionException; 8 import java.util.concurrent.RejectedExecutionException; 9 import org.junit.Before; 10 import org.junit.Test; 11 import org.junit.runner.RunWith; 12 import org.junit.runners.JUnit4; 13 14 import static com.google.common.truth.Truth.assertThat; 15 import static org.junit.Assert.fail; 16 17 /** This test verifies behavior when the executor throws {@link RejectedExecutionException}. */ 18 @RunWith(JUnit4.class) 19 public final class BadExecutorTest { 20 private SimpleComponent component; 21 22 @Before 23 public void setUpComponent() { 24 ComponentDependency dependency = 25 new ComponentDependency() { 26 @Override 27 public ListenableFuture<Double> doubleDep() { 28 return Futures.immediateFuture(42.0); 29 } 30 }; 31 ListeningExecutorService executorService = MoreExecutors.newDirectExecutorService(); 32 component = 33 DaggerSimpleComponent.builder() 34 .executor(executorService) 35 .componentDependency(dependency) 36 .build(); 37 executorService.shutdown(); 38 } 39 40 @Test 41 public void rejectNoArgMethod() throws Exception { 42 try { 43 component.noArgStr().get(); 44 fail(); 45 } catch (ExecutionException e) { 46 assertThat(e.getCause()).isInstanceOf(RejectedExecutionException.class); 47 } 48 } 49 50 @Test 51 public void rejectSingleArgMethod() throws Exception { 52 try { 53 component.singleArgInt().get(); 54 fail(); 55 } catch (ExecutionException e) { 56 assertThat(e.getCause()).isInstanceOf(RejectedExecutionException.class); 57 } 58 } 59 60 @Test 61 public void rejectSingleArgFromComponentDepMethod() throws Exception { 62 try { 63 component.singleArgBool().get(); 64 fail(); 65 } catch (ExecutionException e) { 66 assertThat(e.getCause()).isInstanceOf(RejectedExecutionException.class); 67 } 68 } 69 70 @Test 71 public void doNotRejectComponentDepMethod() throws Exception { 72 assertThat(component.doubleDep().get()).isEqualTo(42.0); 73 } 74 } 75