1 /* 2 * Copyright (C) 2021 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 buildtests; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static org.gradle.testkit.runner.TaskOutcome.SUCCESS; 21 22 import java.io.File; 23 import java.io.IOException; 24 import org.gradle.testkit.runner.BuildResult; 25 import org.gradle.testkit.runner.GradleRunner; 26 import org.junit.Rule; 27 import org.junit.Test; 28 import org.junit.rules.TemporaryFolder; 29 import org.junit.runner.RunWith; 30 import org.junit.runners.JUnit4; 31 32 // This is a regression test for https://github.com/google/dagger/issues/3133 33 @RunWith(JUnit4.class) 34 public class TransitiveMapKeyTest { 35 @Rule public TemporaryFolder folder = new TemporaryFolder(); 36 37 @Test testTransitiveMapKey_WithImplementation()38 public void testTransitiveMapKey_WithImplementation() throws IOException { 39 BuildResult result = setupRunnerWith("implementation").buildAndFail(); 40 assertThat(result.getOutput()).contains("Task :app:compileJava FAILED"); 41 assertThat(result.getOutput()) 42 .contains( 43 "Missing map key annotation for method: library1.MyModule#provideString(). " 44 + "That method was annotated with: [" 45 + "@dagger.Provides, " 46 + "@dagger.multibindings.IntoMap, " 47 + "@library2.MyMapKey(\"some-key\")" 48 + "]"); 49 } 50 51 @Test testTransitiveMapKey_WithApi()52 public void testTransitiveMapKey_WithApi() throws IOException { 53 // Test that if we use an "api" dependency for the custom map key things work properly. 54 BuildResult result = setupRunnerWith("api").build(); 55 assertThat(result.task(":app:assemble").getOutcome()).isEqualTo(SUCCESS); 56 } 57 setupRunnerWith(String dependencyType)58 private GradleRunner setupRunnerWith(String dependencyType) throws IOException { 59 File projectDir = folder.getRoot(); 60 GradleModule.create(projectDir) 61 .addSettingsFile( 62 "include 'app'", 63 "include 'library1'", 64 "include 'library2'") 65 .addBuildFile( 66 "buildscript {", 67 " ext {", 68 String.format("dagger_version = \"%s\"", System.getProperty("dagger_version")), 69 " }", 70 "}", 71 "", 72 "allprojects {", 73 " repositories {", 74 " mavenCentral()", 75 " mavenLocal()", 76 " }", 77 "}"); 78 79 GradleModule.create(projectDir, "app") 80 .addBuildFile( 81 "plugins {", 82 " id 'java'", 83 " id 'application'", 84 "}", 85 "dependencies {", 86 " implementation project(':library1')", 87 " implementation \"com.google.dagger:dagger:$dagger_version\"", 88 " annotationProcessor \"com.google.dagger:dagger-compiler:$dagger_version\"", 89 "}") 90 .addSrcFile( 91 "MyComponent.java", 92 "package app;", 93 "", 94 "import dagger.Component;", 95 "import library1.MyModule;", 96 "import java.util.Map;", 97 "", 98 "@Component(modules = MyModule.class)", 99 "public interface MyComponent {", 100 " Map<String, String> multiMap();", 101 "}"); 102 103 GradleModule.create(projectDir, "library1") 104 .addBuildFile( 105 "plugins {", 106 " id 'java'", 107 " id 'java-library'", 108 "}", 109 "dependencies {", 110 dependencyType + " project(':library2')", 111 " implementation \"com.google.dagger:dagger:$dagger_version\"", 112 " annotationProcessor \"com.google.dagger:dagger-compiler:$dagger_version\"", 113 "}") 114 .addSrcFile( 115 "MyModule.java", 116 "package library1;", 117 "", 118 "import dagger.Module;", 119 "import dagger.Provides;", 120 "import dagger.multibindings.IntoMap;", 121 "import library2.MyMapKey;", 122 "", 123 "@Module", 124 "public interface MyModule {", 125 " @Provides", 126 " @IntoMap", 127 " @MyMapKey(\"some-key\")", 128 " static String provideString() {", 129 " return \"\";", 130 " }", 131 "}"); 132 133 GradleModule.create(projectDir, "library2") 134 .addBuildFile( 135 "plugins {", 136 " id 'java'", 137 " id 'java-library'", 138 "}", 139 "dependencies {", 140 " implementation \"com.google.dagger:dagger:$dagger_version\"", 141 " annotationProcessor \"com.google.dagger:dagger-compiler:$dagger_version\"", 142 "}") 143 .addSrcFile( 144 "MyMapKey.java", 145 "package library2;", 146 "", 147 "import dagger.MapKey;", 148 "", 149 "@MapKey", 150 "public @interface MyMapKey {", 151 " String value();", 152 "}"); 153 154 return GradleRunner.create() 155 .withArguments("--stacktrace", "build") 156 .withProjectDir(projectDir); 157 } 158 } 159