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.cycle;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import dagger.functional.cycle.Cycles.A;
22 import dagger.functional.cycle.Cycles.BindsCycleComponent;
23 import dagger.functional.cycle.Cycles.C;
24 import dagger.functional.cycle.Cycles.ChildCycleComponent;
25 import dagger.functional.cycle.Cycles.CycleComponent;
26 import dagger.functional.cycle.Cycles.CycleMapComponent;
27 import dagger.functional.cycle.Cycles.S;
28 import dagger.functional.cycle.Cycles.SelfCycleComponent;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.junit.runners.JUnit4;
32 
33 @RunWith(JUnit4.class)
34 public class CycleTest {
35   @Test
providerIndirectionSelfCycle()36   public void providerIndirectionSelfCycle() {
37     SelfCycleComponent selfCycleComponent = DaggerCycles_SelfCycleComponent.create();
38     S s = selfCycleComponent.s();
39     assertThat(s.sProvider.get()).isNotNull();
40   }
41 
42   @Test
providerIndirectionCycle()43   public void providerIndirectionCycle() {
44     CycleComponent cycleComponent = DaggerCycles_CycleComponent.create();
45     A a = cycleComponent.a();
46     C c = cycleComponent.c();
47     assertThat(c.aProvider.get()).isNotNull();
48     assertThat(a.b.c.aProvider.get()).isNotNull();
49     assertThat(a.e.d.b.c.aProvider.get()).isNotNull();
50   }
51 
52   @Test
lazyIndirectionSelfCycle()53   public void lazyIndirectionSelfCycle() {
54     SelfCycleComponent selfCycleComponent = DaggerCycles_SelfCycleComponent.create();
55     S s = selfCycleComponent.s();
56     assertThat(s.sLazy.get()).isNotNull();
57   }
58 
59   @Test
lazyIndirectionCycle()60   public void lazyIndirectionCycle() {
61     CycleComponent cycleComponent = DaggerCycles_CycleComponent.create();
62     A a = cycleComponent.a();
63     C c = cycleComponent.c();
64     assertThat(c.aLazy.get()).isNotNull();
65     assertThat(a.b.c.aLazy.get()).isNotNull();
66     assertThat(a.e.d.b.c.aLazy.get()).isNotNull();
67   }
68 
69   @Test
subcomponentIndirectionCycle()70   public void subcomponentIndirectionCycle() {
71     ChildCycleComponent childCycleComponent = DaggerCycles_CycleComponent.create().child();
72     A a = childCycleComponent.a();
73     assertThat(a.b.c.aProvider.get()).isNotNull();
74     assertThat(a.e.d.b.c.aProvider.get()).isNotNull();
75   }
76 
77   @Test
providerMapIndirectionCycle()78   public void providerMapIndirectionCycle() {
79     CycleMapComponent cycleMapComponent = DaggerCycles_CycleMapComponent.create();
80     assertThat(cycleMapComponent.y()).isNotNull();
81     assertThat(cycleMapComponent.y().mapOfProvidersOfX).containsKey("X");
82     assertThat(cycleMapComponent.y().mapOfProvidersOfX.get("X")).isNotNull();
83     assertThat(cycleMapComponent.y().mapOfProvidersOfX.get("X").get()).isNotNull();
84     assertThat(cycleMapComponent.y().mapOfProvidersOfX.get("X").get().y).isNotNull();
85     assertThat(cycleMapComponent.y().mapOfProvidersOfX).hasSize(1);
86     assertThat(cycleMapComponent.y().mapOfProvidersOfY).containsKey("Y");
87     assertThat(cycleMapComponent.y().mapOfProvidersOfY.get("Y")).isNotNull();
88     assertThat(cycleMapComponent.y().mapOfProvidersOfY.get("Y").get()).isNotNull();
89     assertThat(cycleMapComponent.y().mapOfProvidersOfY.get("Y").get().mapOfProvidersOfX).hasSize(1);
90     assertThat(cycleMapComponent.y().mapOfProvidersOfY.get("Y").get().mapOfProvidersOfY).hasSize(1);
91     assertThat(cycleMapComponent.y().mapOfProvidersOfY).hasSize(1);
92   }
93 
94   /**
95    * Tests that a cycle where a {@code @Binds} binding depends on a binding that has to be deferred
96    * works.
97    */
98   @Test
cycleWithDeferredBinds()99   public void cycleWithDeferredBinds() {
100     BindsCycleComponent bindsCycleComponent = DaggerCycles_BindsCycleComponent.create();
101     assertThat(bindsCycleComponent.bar()).isNotNull();
102   }
103 }
104