1 /* 2 * Copyright (C) 2020 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 com.google.common.collect.ImmutableList; 21 import dagger.testing.compile.CompilerTests; 22 import org.junit.Test; 23 import org.junit.runner.RunWith; 24 import org.junit.runners.Parameterized; 25 import org.junit.runners.Parameterized.Parameters; 26 27 28 @RunWith(Parameterized.class) 29 public class ComponentDependenciesTest { 30 @Parameters(name = "{0}") parameters()31 public static ImmutableList<Object[]> parameters() { 32 return CompilerMode.TEST_PARAMETERS; 33 } 34 35 private final CompilerMode compilerMode; 36 ComponentDependenciesTest(CompilerMode compilerMode)37 public ComponentDependenciesTest(CompilerMode compilerMode) { 38 this.compilerMode = compilerMode; 39 } 40 41 @Test dependenciesWithTwoOfSameMethodOnDifferentInterfaces_fail()42 public void dependenciesWithTwoOfSameMethodOnDifferentInterfaces_fail() { 43 Source interfaceOne = 44 CompilerTests.javaSource( 45 "test.One", 46 "package test;", 47 "", 48 "interface One {", 49 " String getOne();", 50 "}"); 51 Source interfaceTwo = 52 CompilerTests.javaSource( 53 "test.Two", 54 "package test;", 55 "", 56 "interface Two {", 57 " String getTwo();", 58 "}"); 59 Source mergedInterface = 60 CompilerTests.javaSource( 61 "test.Merged", 62 "package test;", 63 "", 64 "interface Merged extends One, Two {}"); 65 Source componentFile = 66 CompilerTests.javaSource( 67 "test.TestComponent", 68 "package test;", 69 "", 70 "import dagger.Component;", 71 "", 72 "@Component(dependencies = Merged.class)", 73 "interface TestComponent {", 74 " String getString();", 75 "}"); 76 CompilerTests.daggerCompiler(interfaceOne, interfaceTwo, mergedInterface, componentFile) 77 .withProcessingOptions(compilerMode.processorOptions()) 78 .compile( 79 subject -> { 80 subject.hasErrorCount(1); 81 subject.hasErrorContaining("DuplicateBindings"); 82 }); 83 } 84 85 @Test dependenciesWithTwoOfSameMethodOnDifferentInterfaces_producers_fail()86 public void dependenciesWithTwoOfSameMethodOnDifferentInterfaces_producers_fail() { 87 Source interfaceOne = 88 CompilerTests.javaSource( 89 "test.One", 90 "package test;", 91 "", 92 "import com.google.common.util.concurrent.ListenableFuture;", 93 "", 94 "interface One {", 95 " ListenableFuture<String> getOne();", 96 "}"); 97 Source interfaceTwo = 98 CompilerTests.javaSource( 99 "test.Two", 100 "package test;", 101 "", 102 "import com.google.common.util.concurrent.ListenableFuture;", 103 "", 104 "interface Two {", 105 " ListenableFuture<String> getTwo();", 106 "}"); 107 Source mergedInterface = 108 CompilerTests.javaSource( 109 "test.Merged", 110 "package test;", 111 "", 112 "interface Merged extends One, Two {}"); 113 Source componentFile = 114 CompilerTests.javaSource( 115 "test.TestComponent", 116 "package test;", 117 "", 118 "import com.google.common.util.concurrent.ListenableFuture;", 119 "import dagger.producers.ProductionComponent;", 120 "", 121 "@ProductionComponent(dependencies = Merged.class)", 122 "interface TestComponent {", 123 " ListenableFuture<String> getString();", 124 "}"); 125 CompilerTests.daggerCompiler(interfaceOne, interfaceTwo, mergedInterface, componentFile) 126 .withProcessingOptions(compilerMode.processorOptions()) 127 .compile( 128 subject -> { 129 subject.hasErrorCount(1); 130 subject.hasErrorContaining("DuplicateBindings"); 131 }); 132 } 133 134 @Test dependenciesWithTwoOfSameMethodButDifferentNullability_fail()135 public void dependenciesWithTwoOfSameMethodButDifferentNullability_fail() { 136 Source interfaceOne = 137 CompilerTests.javaSource( 138 "test.One", 139 "package test;", 140 "", 141 "interface One {", 142 " String getString();", 143 "}"); 144 Source interfaceTwo = 145 CompilerTests.javaSource( 146 "test.Two", 147 "package test;", 148 "import javax.annotation.Nullable;", 149 "", 150 "interface Two {", 151 " @Nullable String getString();", 152 "}"); 153 Source mergedInterface = 154 CompilerTests.javaSource( 155 "test.Merged", 156 "package test;", 157 "", 158 "interface Merged extends One, Two {}"); 159 Source componentFile = 160 CompilerTests.javaSource( 161 "test.TestComponent", 162 "package test;", 163 "", 164 "import dagger.Component;", 165 "", 166 "@Component(dependencies = Merged.class)", 167 "interface TestComponent {", 168 " String getString();", 169 "}"); 170 CompilerTests.daggerCompiler(interfaceOne, interfaceTwo, mergedInterface, componentFile) 171 .withProcessingOptions(compilerMode.processorOptions()) 172 .compile( 173 subject -> { 174 subject.hasErrorCount(1); 175 subject.hasErrorContaining("DuplicateBindings"); 176 }); 177 } 178 } 179