• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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.multibindings;
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.multibindings.IntoMap;
25 import dagger.multibindings.LazyClassKey;
26 import java.util.Map;
27 import javax.inject.Provider;
28 import javax.inject.Singleton;
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 LazyClassKeyWithGenericsTest {
35   @Component(modules = TestModule.class)
36   @Singleton
37   interface TestComponent {
map()38     Map<Class<?>, String> map();
39 
intMap()40     Provider<Map<Class<?>, Provider<Integer>>> intMap();
41   }
42 
43   @Module
44   interface TestModule {
45     @Provides
46     @IntoMap
47     @LazyClassKey(Thing.class)
provideThingValue()48     static String provideThingValue() {
49       return "Thing";
50     }
51 
52     @Provides
53     @IntoMap
54     @LazyClassKey(GenericThing.class)
provideGenericThingValue()55     static String provideGenericThingValue() {
56       return "GenericThing";
57     }
58 
59     @Provides
60     @IntoMap
61     @LazyClassKey(Thing.class)
provideThingIntValue()62     static Integer provideThingIntValue() {
63       return 2;
64     }
65 
66     @Provides
67     @IntoMap
68     @LazyClassKey(GenericThing.class)
provideGenericThingIntValue()69     static Integer provideGenericThingIntValue() {
70       return 1;
71     }
72   }
73 
74   class Thing {}
75 
76   class GenericThing<T> {}
77 
78   @Test
test()79   public void test() {
80     TestComponent testComponent = DaggerLazyClassKeyWithGenericsTest_TestComponent.create();
81     Map<Class<?>, String> map = testComponent.map();
82     Map<Class<?>, Provider<Integer>> intMap = testComponent.intMap().get();
83     // MapSubject#containsExactly uses entrySet, which is banned from DaggerClassKey map.
84     assertThat(map.get(Thing.class)).isEqualTo("Thing");
85     assertThat(map.get(GenericThing.class)).isEqualTo("GenericThing");
86     assertThat(intMap.get(Thing.class).get()).isEqualTo(2);
87     assertThat(intMap.get(GenericThing.class).get()).isEqualTo(1);
88   }
89 }
90