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