• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.membersinject;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import dagger.BindsInstance;
22 import dagger.Component;
23 import dagger.MembersInjector;
24 import dagger.functional.multipackage.DaggerMembersInjectionVisibilityComponent;
25 import dagger.functional.multipackage.MembersInjectionVisibilityComponent;
26 import dagger.functional.multipackage.a.AGrandchild;
27 import dagger.functional.multipackage.a.AParent;
28 import dagger.functional.multipackage.b.BChild;
29 import javax.inject.Inject;
30 import javax.inject.Provider;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.junit.runners.JUnit4;
34 
35 @RunWith(JUnit4.class)
36 public class MembersInjectTest {
testMembersInject_arrays()37   @Test public void testMembersInject_arrays() {
38     MembersInjectComponent component = DaggerMembersInjectComponent.builder().build();
39 
40     ChildOfStringArray childOfStringArray = new ChildOfStringArray();
41     component.inject(childOfStringArray);
42   }
43 
testMembersInject_nestedArrays()44   @Test public void testMembersInject_nestedArrays() {
45     MembersInjectComponent component = DaggerMembersInjectComponent.builder().build();
46 
47     ChildOfArrayOfParentOfStringArray childOfArrayOfParentOfStringArray =
48         new ChildOfArrayOfParentOfStringArray();
49     component.inject(childOfArrayOfParentOfStringArray);
50   }
51 
testMembersInject_primitives()52   @Test public void testMembersInject_primitives() {
53     MembersInjectComponent component = DaggerMembersInjectComponent.builder().build();
54 
55     ChildOfPrimitiveIntArray childOfPrimitiveIntArray = new ChildOfPrimitiveIntArray();
56     component.inject(childOfPrimitiveIntArray);
57   }
58 
59   @Test
testMembersInject_overrides()60   public void testMembersInject_overrides() {
61     MembersInjectionVisibilityComponent component =
62         DaggerMembersInjectionVisibilityComponent.create();
63     AParent aParent = new AParent();
64     component.inject(aParent);
65     assertThat(aParent.aParentField()).isNotNull();
66     assertThat(aParent.aParentMethod()).isNotNull();
67 
68     BChild aChild = new BChild();
69     component.inject(aChild);
70     assertThat(aChild.aParentField()).isNotNull();
71     assertThat(aChild.aParentMethod()).isNull();
72     assertThat(aChild.aChildField()).isNotNull();
73     assertThat(aChild.aChildMethod()).isNotNull();
74 
75     AGrandchild aGrandchild = new AGrandchild();
76     component.inject(aGrandchild);
77     assertThat(aGrandchild.aParentField()).isNotNull();
78     assertThat(aGrandchild.aParentMethod()).isNotNull();
79     assertThat(aGrandchild.aChildField()).isNotNull();
80     assertThat(aGrandchild.aChildMethod()).isNull();
81     assertThat(aGrandchild.aGrandchildField()).isNotNull();
82     assertThat(aGrandchild.aGrandchildMethod()).isNotNull();
83   }
84 
85   @Test
testNonRequestedMembersInjector()86   public void testNonRequestedMembersInjector() {
87     NonRequestedChild child = new NonRequestedChild();
88     Provider<String> provider =
89         new Provider<String>() {
90           @Override
91           public String get() {
92             return "field!";
93           }
94         };
95     MembersInjector<NonRequestedChild> injector = new NonRequestedChild_MembersInjector(provider);
96     injector.injectMembers(child);
97     assertThat(child.t).isEqualTo("field!");
98   }
99 
100   public static final class A extends B {
101     // No injected members
102   }
103 
104   public static class B extends C {
105     // No injected members
106   }
107 
108   public static class C {
109     @Inject String value;
110   }
111 
112   @Component
113   interface NonLocalMembersComponent {
getAMembersInjector()114     MembersInjector<A> getAMembersInjector();
115 
116     @Component.Factory
117     interface Factory {
create(@indsInstance String value)118       NonLocalMembersComponent create(@BindsInstance String value);
119     }
120   }
121 
122   @Test
testNonLocalMembersInjection()123   public void testNonLocalMembersInjection() {
124     MembersInjector<A> membersInjector = DaggerMembersInjectTest_NonLocalMembersComponent.factory()
125         .create("test").getAMembersInjector();
126     A testA = new A();
127     membersInjector.injectMembers(testA);
128     assertThat(testA.value).isEqualTo("test");
129   }
130 }
131