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