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 ComplexMapKeysInDifferentOrderTest { 36 @Retention(RUNTIME) 37 @MapKey(unwrapValue = false) 38 @interface ComplexMapKey { i()39 int i(); j()40 int j(); 41 nestKeys()42 NestKey[] nestKeys() default {}; 43 } 44 45 @Retention(RUNTIME) 46 @MapKey(unwrapValue = false) 47 @interface NestKey { i()48 int i() default 0; 49 j()50 String j() default ""; 51 } 52 53 @Module 54 interface TestModule { 55 @Provides 56 @IntoMap 57 @ComplexMapKey(i = 1, j = 2, nestKeys = @NestKey) inOrder()58 static int inOrder() { 59 return 3; 60 } 61 62 @Provides 63 @IntoMap 64 @ComplexMapKey(j = 4, i = 5) backwardsOrder()65 static int backwardsOrder() { 66 return 6; 67 } 68 } 69 70 @Component(modules = TestModule.class) 71 interface TestComponent { map()72 Map<ComplexMapKey, Integer> map(); 73 } 74 75 @Test test()76 public void test() { 77 Map<ComplexMapKey, Integer> map = 78 DaggerComplexMapKeysInDifferentOrderTest_TestComponent.create().map(); 79 assertThat(map) 80 .containsEntry( 81 mapKey( 82 1, 83 2, 84 new NestKey[] { 85 new AutoAnnotation_ComplexMapKeysInDifferentOrderTest_ComplexMapKeyCreator_createNestKey( 86 0, "") 87 }), 88 3); 89 assertThat(map).containsEntry(mapKey(5, 4, new NestKey[] {}), 6); 90 } 91 92 @AutoAnnotation mapKey(int i, int j, NestKey[] nestKeys)93 static ComplexMapKey mapKey(int i, int j, NestKey[] nestKeys) { 94 return new AutoAnnotation_ComplexMapKeysInDifferentOrderTest_mapKey(i, j, nestKeys); 95 } 96 } 97