• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.assisted;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import dagger.Component;
22 import dagger.Module;
23 import dagger.Provides;
24 import dagger.Subcomponent;
25 import dagger.assisted.Assisted;
26 import dagger.assisted.AssistedFactory;
27 import dagger.assisted.AssistedInject;
28 import dagger.multibindings.IntoSet;
29 import java.util.Set;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.junit.runners.JUnit4;
33 
34 @RunWith(JUnit4.class)
35 public final class AssistedFactoryWithMultibindingsTest {
36   @Component(modules = ParentModule.class)
37   interface ParentComponent {
38     // Factory for assisted injection binding with multi binding contribution.
multibindingFooFactory()39     MultibindingFooFactory multibindingFooFactory();
40 
childComponent()41     ChildComponent.Builder childComponent();
42   }
43 
44   static final class AssistedDep {}
45 
46   static final class MultibindingFoo {
47     private final AssistedDep assistedDep;
48     private final Set<String> stringSet;
49 
50     @AssistedInject
MultibindingFoo(@ssisted AssistedDep assistedDep, Set<String> stringSet)51     MultibindingFoo(@Assisted AssistedDep assistedDep, Set<String> stringSet) {
52       this.assistedDep = assistedDep;
53       this.stringSet = stringSet;
54     }
55 
assistedDep()56     AssistedDep assistedDep() {
57       return assistedDep;
58     }
59 
stringSet()60     Set<String> stringSet() {
61       return stringSet;
62     }
63   }
64 
65   @Subcomponent(modules = ChildModule.class)
66   static interface ChildComponent {
multibindingFooFactory()67     MultibindingFooFactory multibindingFooFactory();
68 
69     @Subcomponent.Builder
70     interface Builder {
build()71       ChildComponent build();
72     }
73   }
74 
75   @Module(subcomponents = ChildComponent.class)
76   static class ParentModule {
77     @Provides
78     @IntoSet
parentString()79     String parentString() {
80       return "parent";
81     }
82   }
83 
84   @Module
85   static class ChildModule {
86     @Provides
87     @IntoSet
childString()88     String childString() {
89       return "child";
90     }
91   }
92 
93   @AssistedFactory
94   interface MultibindingFooFactory {
createFoo(AssistedDep factoryAssistedDep1)95     MultibindingFoo createFoo(AssistedDep factoryAssistedDep1);
96   }
97 
98   @Test
testAssistedFactoryWithMultibinding()99   public void testAssistedFactoryWithMultibinding() {
100     AssistedDep assistedDep1 = new AssistedDep();
101     ParentComponent parent = DaggerAssistedFactoryWithMultibindingsTest_ParentComponent.create();
102     ChildComponent child = parent.childComponent().build();
103     MultibindingFoo foo1 = parent.multibindingFooFactory().createFoo(assistedDep1);
104     MultibindingFoo foo2 = child.multibindingFooFactory().createFoo(assistedDep1);
105     assertThat(foo1.assistedDep()).isEqualTo(foo2.assistedDep);
106     assertThat(foo1.stringSet()).containsExactly("parent");
107     assertThat(foo2.stringSet()).containsExactly("child", "parent");
108   }
109 }
110