1 /* 2 * Copyright (C) 2015 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.internal.codegen; 18 19 import androidx.room.compiler.processing.util.Source; 20 import dagger.testing.compile.CompilerTests; 21 import org.junit.Test; 22 import org.junit.runner.RunWith; 23 import org.junit.runners.JUnit4; 24 25 @RunWith(JUnit4.class) 26 public class RepeatedModuleValidationTest { 27 private static final Source MODULE_FILE = 28 CompilerTests.javaSource( 29 "test.TestModule", 30 "package test;", 31 "", 32 "import dagger.Module;", 33 "", 34 "@Module", 35 "final class TestModule {}"); 36 37 @Test moduleRepeatedInSubcomponentFactoryMethod()38 public void moduleRepeatedInSubcomponentFactoryMethod() { 39 Source subcomponentFile = 40 CompilerTests.javaSource( 41 "test.TestSubcomponent", 42 "package test;", 43 "", 44 "import dagger.Subcomponent;", 45 "", 46 "@Subcomponent(modules = TestModule.class)", 47 "interface TestSubcomponent {", 48 "}"); 49 Source componentFile = 50 CompilerTests.javaSource( 51 "test.TestComponent", 52 "package test;", 53 "", 54 "import dagger.Component;", 55 "", 56 "@Component(modules = TestModule.class)", 57 "interface TestComponent {", 58 " TestSubcomponent newTestSubcomponent(TestModule module);", 59 "}"); 60 CompilerTests.daggerCompiler(MODULE_FILE, subcomponentFile, componentFile) 61 .compile( 62 subject -> { 63 subject.hasErrorCount(1); 64 subject.hasErrorContaining("TestModule is present in test.TestComponent.") 65 .onSource(componentFile) 66 .onLine(7); 67 }); 68 } 69 70 @Test moduleRepeatedInSubcomponentBuilderMethod()71 public void moduleRepeatedInSubcomponentBuilderMethod() { 72 Source subcomponentFile = 73 CompilerTests.javaSource( 74 "test.TestSubcomponent", 75 "package test;", 76 "", 77 "import dagger.Subcomponent;", 78 "", 79 "@Subcomponent(modules = TestModule.class)", 80 "interface TestSubcomponent {", 81 " @Subcomponent.Builder", 82 " interface Builder {", 83 " Builder testModule(TestModule testModule);", 84 " TestSubcomponent build();", 85 " }", 86 "}"); 87 Source componentFile = 88 CompilerTests.javaSource( 89 "test.TestComponent", 90 "package test;", 91 "", 92 "import dagger.Component;", 93 "", 94 "@Component(modules = TestModule.class)", 95 "interface TestComponent {", 96 " TestSubcomponent.Builder newTestSubcomponentBuilder();", 97 "}"); 98 CompilerTests.daggerCompiler(MODULE_FILE, subcomponentFile, componentFile) 99 .compile(subject -> subject.hasErrorCount(0)); 100 // TODO(gak): assert about the warning when we have that ability 101 } 102 103 @Test moduleRepeatedButNotPassed()104 public void moduleRepeatedButNotPassed() { 105 Source subcomponentFile = 106 CompilerTests.javaSource( 107 "test.TestSubcomponent", 108 "package test;", 109 "", 110 "import dagger.Subcomponent;", 111 "", 112 "@Subcomponent(modules = TestModule.class)", 113 "interface TestSubcomponent {", 114 "}"); 115 Source componentFile = 116 CompilerTests.javaSource( 117 "test.TestComponent", 118 "package test;", 119 "", 120 "import dagger.Component;", 121 "", 122 "@Component(modules = TestModule.class)", 123 "interface TestComponent {", 124 " TestSubcomponent newTestSubcomponent();", 125 "}"); 126 CompilerTests.daggerCompiler(MODULE_FILE, subcomponentFile, componentFile) 127 .compile(subject -> subject.hasErrorCount(0)); 128 } 129 } 130