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.android.processor; 18 19 import androidx.room.compiler.processing.util.Source; 20 import dagger.internal.codegen.ComponentProcessor; 21 import dagger.internal.codegen.KspComponentProcessor; 22 import dagger.testing.compile.CompilerTests; 23 import org.junit.Test; 24 import org.junit.runner.RunWith; 25 import org.junit.runners.JUnit4; 26 27 @RunWith(JUnit4.class) 28 public final class DuplicateAndroidInjectorsCheckerTest { 29 @Test conflictingMapKeys()30 public void conflictingMapKeys() { 31 Source activity = 32 CompilerTests.javaSource( 33 "test.TestActivity", 34 "package test;", 35 "", 36 "import android.app.Activity;", 37 "", 38 "public class TestActivity extends Activity {}"); 39 Source injectorFactory = 40 CompilerTests.javaSource( 41 "test.TestInjectorFactory", 42 "package test;", 43 "", 44 "import dagger.android.AndroidInjector;", 45 "import javax.inject.Inject;", 46 "", 47 "class TestInjectorFactory implements AndroidInjector.Factory<TestActivity> {", 48 " @Inject TestInjectorFactory() {}", 49 "", 50 " @Override", 51 " public AndroidInjector<TestActivity> create(TestActivity instance) { return null; }", 52 "}"); 53 Source module = 54 CompilerTests.javaSource( 55 "test.TestModule", 56 "package test;", 57 "", 58 "import android.app.Activity;", 59 "import dagger.Binds;", 60 "import dagger.Module;", 61 "import dagger.android.*;", 62 "import dagger.multibindings.*;", 63 "", 64 "@Module", 65 "interface TestModule {", 66 " @Binds", 67 " @IntoMap", 68 " @ClassKey(TestActivity.class)", 69 " AndroidInjector.Factory<?> classKey(TestInjectorFactory factory);", 70 "", 71 " @Binds", 72 " @IntoMap", 73 " @AndroidInjectionKey(\"test.TestActivity\")", 74 " AndroidInjector.Factory<?> stringKey(TestInjectorFactory factory);", 75 "}"); 76 Source component = 77 CompilerTests.javaSource( 78 "test.TestComponent", 79 "package test;", 80 "", 81 "import android.app.Activity;", 82 "import dagger.Component;", 83 "import dagger.android.DispatchingAndroidInjector;", 84 "", 85 "@Component(modules = TestModule.class)", 86 "interface TestComponent {", 87 " DispatchingAndroidInjector<Activity> dispatchingInjector();", 88 "}"); 89 90 CompilerTests.daggerCompiler(activity, injectorFactory, module, component) 91 .withAdditionalJavacProcessors( 92 ComponentProcessor.withTestPlugins(new DuplicateAndroidInjectorsChecker())) 93 .withAdditionalKspProcessors( 94 KspComponentProcessor.Provider.withTestPlugins(new DuplicateAndroidInjectorsChecker())) 95 .compile( 96 subject -> { 97 subject.compilationDidFail(); 98 subject 99 .hasErrorContaining("Multiple injector factories bound for the same type") 100 .onLineContaining("interface TestComponent"); 101 subject.hasErrorContaining("classKey(test.TestInjectorFactory)"); 102 subject.hasErrorContaining("stringKey(test.TestInjectorFactory)"); 103 subject.hasErrorCount(1); 104 }); 105 } 106 } 107