• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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.subcomponent;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import dagger.functional.producers.subcomponent.SubcomponentsWithBoundExecutor.ChildComponent;
22 import dagger.functional.producers.subcomponent.SubcomponentsWithBoundExecutor.ExecutorModule;
23 import dagger.functional.producers.subcomponent.SubcomponentsWithBoundExecutor.GrandchildComponent;
24 import dagger.functional.producers.subcomponent.SubcomponentsWithBoundExecutor.GrandchildComponentWithoutBuilder;
25 import dagger.functional.producers.subcomponent.SubcomponentsWithBoundExecutor.ParentComponent;
26 import dagger.functional.producers.subcomponent.SubcomponentsWithBoundExecutor.ParentProductionComponent;
27 import java.util.concurrent.atomic.AtomicInteger;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.junit.runners.JUnit4;
32 
33 @RunWith(JUnit4.class)
34 public final class SubcomponentWithBoundExecutorTest {
35   private ParentComponent parentComponent;
36   private ParentProductionComponent parentProductionComponent;
37   private final AtomicInteger executorConstructionCount = new AtomicInteger();
38   private final AtomicInteger executionCount = new AtomicInteger();
39 
40   @Before
setUp()41   public void setUp() {
42     parentComponent =
43         DaggerSubcomponentsWithBoundExecutor_ParentComponent.builder()
44             .executorModule(new ExecutorModule(executorConstructionCount, executionCount))
45             .build();
46     parentProductionComponent =
47         DaggerSubcomponentsWithBoundExecutor_ParentProductionComponent.builder()
48             .executorModule(new ExecutorModule(executorConstructionCount, executionCount))
49             .build();
50   }
51 
52   @Test
topLevelComponent_child()53   public void topLevelComponent_child() throws Exception {
54     ChildComponent child = parentComponent.newChildComponentBuilder().build();
55     assertThat(child.fromChild().get()).isEqualTo("child:parent");
56     assertThat(executorConstructionCount.get()).isEqualTo(1);
57     assertThat(executionCount.get()).isEqualTo(1);
58   }
59 
60   @Test
topLevelComponent_injectsChildBuilder()61   public void topLevelComponent_injectsChildBuilder() throws Exception {
62     ChildComponent child = parentComponent.injectsChildBuilder().childBuilder().build();
63     assertThat(child.fromChild().get()).isEqualTo("child:parent");
64     assertThat(executorConstructionCount.get()).isEqualTo(1);
65     assertThat(executionCount.get()).isEqualTo(1);
66   }
67 
68   @Test
topLevelComponent_grandchild()69   public void topLevelComponent_grandchild() throws Exception {
70     ChildComponent child = parentComponent.newChildComponentBuilder().build();
71     GrandchildComponent grandchild = child.newGrandchildComponentBuilder().build();
72     assertThat(grandchild.fromGrandchild().get()).isEqualTo("grandchild:child:parent");
73     assertThat(executorConstructionCount.get()).isEqualTo(1);
74     assertThat(executionCount.get()).isEqualTo(2);
75   }
76 
77   @Test
topLevelComponent_grandchildWithoutBuilder()78   public void topLevelComponent_grandchildWithoutBuilder() throws Exception {
79     ChildComponent child = parentComponent.newChildComponentBuilder().build();
80     GrandchildComponentWithoutBuilder grandchild = child.newGrandchildComponent();
81     assertThat(grandchild.fromGrandchild().get()).isEqualTo("grandchild:child:parent");
82     assertThat(executorConstructionCount.get()).isEqualTo(1);
83     assertThat(executionCount.get()).isEqualTo(2);
84   }
85 
86   @Test
topLevelProductionComponent_child()87   public void topLevelProductionComponent_child() throws Exception {
88     ChildComponent child = parentProductionComponent.newChildComponentBuilder().build();
89     assertThat(child.fromChild().get()).isEqualTo("child:parentproduction");
90     assertThat(executorConstructionCount.get()).isEqualTo(1);
91     assertThat(executionCount.get()).isEqualTo(2);
92   }
93 
94   @Test
topLevelProductionComponent_grandchild()95   public void topLevelProductionComponent_grandchild() throws Exception {
96     ChildComponent child = parentProductionComponent.newChildComponentBuilder().build();
97     GrandchildComponent grandchild = child.newGrandchildComponentBuilder().build();
98     assertThat(grandchild.fromGrandchild().get()).isEqualTo("grandchild:child:parentproduction");
99     assertThat(executorConstructionCount.get()).isEqualTo(1);
100     assertThat(executionCount.get()).isEqualTo(3);
101   }
102 
103   @Test
topLevelProductionComponent_grandchildWithoutBuilder()104   public void topLevelProductionComponent_grandchildWithoutBuilder() throws Exception {
105     ChildComponent child = parentProductionComponent.newChildComponentBuilder().build();
106     GrandchildComponentWithoutBuilder grandchild = child.newGrandchildComponent();
107     assertThat(grandchild.fromGrandchild().get()).isEqualTo("grandchild:child:parentproduction");
108     assertThat(executorConstructionCount.get()).isEqualTo(1);
109     assertThat(executionCount.get()).isEqualTo(3);
110   }
111 }
112