xref: /aosp_15_r20/external/dagger2/javatests/dagger/functional/multibindings/MapKeyWithDefaultTest.java (revision f585d8a307d0621d6060bd7e80091fdcbf94fe27)
1 /*
2  * Copyright (C) 2018 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 import static java.lang.annotation.RetentionPolicy.RUNTIME;
21 
22 import com.google.auto.value.AutoAnnotation;
23 import dagger.Component;
24 import dagger.MapKey;
25 import dagger.Module;
26 import dagger.Provides;
27 import dagger.multibindings.IntoMap;
28 import java.lang.annotation.Retention;
29 import java.util.Map;
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 MapKeyWithDefaultTest {
36   @Retention(RUNTIME)
37   @MapKey(unwrapValue = false)
38   @interface MapKeyWithDefault {
hasDefault()39     boolean hasDefault() default true;
required()40     boolean required();
41   }
42 
43   @Module
44   interface TestModule {
45     @Provides
46     @IntoMap
47     @MapKeyWithDefault(required = false)
justRequired()48     static int justRequired() {
49       return 1;
50     }
51 
52     @Provides
53     @IntoMap
54     @MapKeyWithDefault(required = false, hasDefault = false)
both()55     static int both() {
56       return 2;
57     }
58   }
59 
60   @Component(modules = TestModule.class)
61   interface TestComponent {
map()62     Map<MapKeyWithDefault, Integer> map();
63   }
64 
65   @Test
test()66   public void test() {
67     Map<MapKeyWithDefault, Integer> map = DaggerMapKeyWithDefaultTest_TestComponent.create().map();
68     assertThat(map).hasSize(2);
69     assertThat(map.get(mapKey(true, false))).isEqualTo(1);
70     assertThat(map.get(mapKey(false, false))).isEqualTo(2);
71   }
72 
73   @AutoAnnotation
mapKey(boolean hasDefault, boolean required)74   static MapKeyWithDefault mapKey(boolean hasDefault, boolean required) {
75     return new AutoAnnotation_MapKeyWithDefaultTest_mapKey(hasDefault, required);
76   }
77 }
78