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.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 @RunWith(Parameterized.class) 28 public class ConflictingEntryPointsTest { 29 @Parameters(name = "{0}") parameters()30 public static ImmutableList<Object[]> parameters() { 31 return CompilerMode.TEST_PARAMETERS; 32 } 33 34 private final CompilerMode compilerMode; 35 ConflictingEntryPointsTest(CompilerMode compilerMode)36 public ConflictingEntryPointsTest(CompilerMode compilerMode) { 37 this.compilerMode = compilerMode; 38 } 39 40 @Test covariantType()41 public void covariantType() { 42 Source base1 = 43 CompilerTests.javaSource( 44 "test.Base1", // 45 "package test;", 46 "", 47 "interface Base1 {", 48 " Long foo();", 49 "}"); 50 Source base2 = 51 CompilerTests.javaSource( 52 "test.Base2", // 53 "package test;", 54 "", 55 "interface Base2 {", 56 " Number foo();", 57 "}"); 58 Source component = 59 CompilerTests.javaSource( 60 "test.TestComponent", 61 "package test;", 62 "", 63 "import dagger.BindsInstance;", 64 "import dagger.Component;", 65 "", 66 "@Component", 67 "interface TestComponent extends Base1, Base2 {", 68 "", 69 " @Component.Builder", 70 " interface Builder {", 71 " @BindsInstance Builder foo(Long foo);", 72 " @BindsInstance Builder foo(Number foo);", 73 " TestComponent build();", 74 " }", 75 "}"); 76 CompilerTests.daggerCompiler(base1, base2, component) 77 .withProcessingOptions(compilerMode.processorOptions()) 78 .compile( 79 subject -> { 80 subject.hasErrorCount(1); 81 subject.hasErrorContaining( 82 String.join( 83 "\n", 84 "can only implement the method once. Found:", 85 " Long test.Base1.foo()", 86 " Number test.Base2.foo()")) 87 .onSource(component) 88 .onLineContaining("interface TestComponent"); 89 }); 90 } 91 92 @Test covariantTypeFromGenericSupertypes()93 public void covariantTypeFromGenericSupertypes() { 94 Source base1 = 95 CompilerTests.javaSource( 96 "test.Base1", // 97 "package test;", 98 "", 99 "interface Base1<T> {", 100 " T foo();", 101 "}"); 102 Source base2 = 103 CompilerTests.javaSource( 104 "test.Base2", // 105 "package test;", 106 "", 107 "interface Base2<T> {", 108 " T foo();", 109 "}"); 110 Source component = 111 CompilerTests.javaSource( 112 "test.TestComponent", 113 "package test;", 114 "", 115 "import dagger.BindsInstance;", 116 "import dagger.Component;", 117 "", 118 "@Component", 119 "interface TestComponent extends Base1<Long>, Base2<Number> {", 120 "", 121 " @Component.Builder", 122 " interface Builder {", 123 " @BindsInstance Builder foo(Long foo);", 124 " @BindsInstance Builder foo(Number foo);", 125 " TestComponent build();", 126 " }", 127 "}"); 128 CompilerTests.daggerCompiler(base1, base2, component) 129 .withProcessingOptions(compilerMode.processorOptions()) 130 .compile( 131 subject -> { 132 subject.hasErrorCount(1); 133 subject.hasErrorContaining( 134 String.join( 135 "\n", 136 "can only implement the method once. Found:", 137 " Long test.Base1.foo()", 138 " Number test.Base2.foo()")) 139 .onSource(component) 140 .onLineContaining("interface TestComponent"); 141 }); 142 } 143 144 @Test differentQualifier()145 public void differentQualifier() { 146 Source base1 = 147 CompilerTests.javaSource( 148 "test.Base1", // 149 "package test;", 150 "", 151 "interface Base1 {", 152 " Object foo();", 153 "}"); 154 Source base2 = 155 CompilerTests.javaSource( 156 "test.Base2", // 157 "package test;", 158 "", 159 "import javax.inject.Named;", 160 "", 161 "interface Base2 {", 162 " @Named(\"foo\") Object foo();", 163 "}"); 164 Source component = 165 CompilerTests.javaSource( 166 "test.TestComponent", 167 "package test;", 168 "", 169 "import dagger.BindsInstance;", 170 "import dagger.Component;", 171 "import javax.inject.Named;", 172 "", 173 "@Component", 174 "interface TestComponent extends Base1, Base2 {", 175 "", 176 " @Component.Builder", 177 " interface Builder {", 178 " @BindsInstance Builder foo(Object foo);", 179 " @BindsInstance Builder namedFoo(@Named(\"foo\") Object foo);", 180 " TestComponent build();", 181 " }", 182 "}"); 183 CompilerTests.daggerCompiler(base1, base2, component) 184 .withProcessingOptions(compilerMode.processorOptions()) 185 .compile( 186 subject -> { 187 subject.hasErrorCount(1); 188 subject.hasErrorContaining( 189 String.join( 190 "\n", 191 "can only implement the method once. Found:", 192 " Object test.Base1.foo()", 193 " @Named(\"foo\") Object test.Base2.foo()")) 194 .onSource(component) 195 .onLineContaining("interface TestComponent"); 196 }); 197 } 198 199 @Test sameKey()200 public void sameKey() { 201 Source base1 = 202 CompilerTests.javaSource( 203 "test.Base1", // 204 "package test;", 205 "", 206 "interface Base1 {", 207 " Object foo();", 208 "}"); 209 Source base2 = 210 CompilerTests.javaSource( 211 "test.Base2", // 212 "package test;", 213 "", 214 "interface Base2 {", 215 " Object foo();", 216 "}"); 217 Source component = 218 CompilerTests.javaSource( 219 "test.TestComponent", 220 "package test;", 221 "", 222 "import dagger.BindsInstance;", 223 "import dagger.Component;", 224 "", 225 "@Component", 226 "interface TestComponent extends Base1, Base2 {", 227 "", 228 " @Component.Builder", 229 " interface Builder {", 230 " @BindsInstance Builder foo(Object foo);", 231 " TestComponent build();", 232 " }", 233 "}"); 234 CompilerTests.daggerCompiler(base1, base2, component) 235 .withProcessingOptions(compilerMode.processorOptions()) 236 .compile(subject -> subject.hasErrorCount(0)); 237 } 238 239 @Test sameQualifiedKey()240 public void sameQualifiedKey() { 241 Source base1 = 242 CompilerTests.javaSource( 243 "test.Base1", // 244 "package test;", 245 "", 246 "import javax.inject.Named;", 247 "", 248 "interface Base1 {", 249 " @Named(\"foo\") Object foo();", 250 "}"); 251 Source base2 = 252 CompilerTests.javaSource( 253 "test.Base2", // 254 "package test;", 255 "", 256 "import javax.inject.Named;", 257 "", 258 "interface Base2 {", 259 " @Named(\"foo\") Object foo();", 260 "}"); 261 Source component = 262 CompilerTests.javaSource( 263 "test.TestComponent", 264 "package test;", 265 "", 266 "import dagger.BindsInstance;", 267 "import dagger.Component;", 268 "import javax.inject.Named;", 269 "", 270 "@Component", 271 "interface TestComponent extends Base1, Base2 {", 272 "", 273 " @Component.Builder", 274 " interface Builder {", 275 " @BindsInstance Builder foo(@Named(\"foo\") Object foo);", 276 " TestComponent build();", 277 " }", 278 "}"); 279 CompilerTests.daggerCompiler(base1, base2, component) 280 .withProcessingOptions(compilerMode.processorOptions()) 281 .compile(subject -> subject.hasErrorCount(0)); 282 } 283 } 284