1 /* 2 * Copyright (C) 2015 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.functional.producers.badexecutor; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static org.junit.Assert.fail; 21 22 import com.google.common.util.concurrent.Futures; 23 import com.google.common.util.concurrent.ListenableFuture; 24 import com.google.common.util.concurrent.ListeningExecutorService; 25 import com.google.common.util.concurrent.MoreExecutors; 26 import java.util.concurrent.ExecutionException; 27 import java.util.concurrent.RejectedExecutionException; 28 import org.junit.Before; 29 import org.junit.Test; 30 import org.junit.runner.RunWith; 31 import org.junit.runners.JUnit4; 32 33 /** This test verifies behavior when the executor throws {@link RejectedExecutionException}. */ 34 @RunWith(JUnit4.class) 35 public final class BadExecutorTest { 36 private SimpleComponent component; 37 38 @Before setUpComponent()39 public void setUpComponent() { 40 ComponentDependency dependency = 41 new ComponentDependency() { 42 @Override 43 public ListenableFuture<Double> doubleDep() { 44 return Futures.immediateFuture(42.0); 45 } 46 }; 47 ListeningExecutorService executorService = MoreExecutors.newDirectExecutorService(); 48 component = 49 DaggerSimpleComponent.builder() 50 .executorModule(new ExecutorModule(executorService)) 51 .componentDependency(dependency) 52 .build(); 53 executorService.shutdown(); 54 } 55 56 @Test rejectNoArgMethod()57 public void rejectNoArgMethod() throws Exception { 58 try { 59 component.noArgStr().get(); 60 fail(); 61 } catch (ExecutionException e) { 62 assertThat(e).hasCauseThat().isInstanceOf(RejectedExecutionException.class); 63 } 64 } 65 66 @Test rejectSingleArgMethod()67 public void rejectSingleArgMethod() throws Exception { 68 try { 69 component.singleArgInt().get(); 70 fail(); 71 } catch (ExecutionException e) { 72 assertThat(e).hasCauseThat().isInstanceOf(RejectedExecutionException.class); 73 } 74 } 75 76 @Test rejectSingleArgFromComponentDepMethod()77 public void rejectSingleArgFromComponentDepMethod() throws Exception { 78 try { 79 component.singleArgBool().get(); 80 fail(); 81 } catch (ExecutionException e) { 82 assertThat(e).hasCauseThat().isInstanceOf(RejectedExecutionException.class); 83 } 84 } 85 86 @Test doNotRejectComponentDepMethod()87 public void doNotRejectComponentDepMethod() throws Exception { 88 assertThat(component.doubleDep().get()).isEqualTo(42.0); 89 } 90 } 91