1 /* 2 * Copyright (C) 2022 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/3476 33 @RunWith(JUnit4.class) 34 public class TransitiveInjectMethodTest { 35 @Rule public TemporaryFolder folder = new TemporaryFolder(); 36 37 @Test testBuild()38 public void testBuild() throws IOException { 39 BuildResult result = setupRunner().build(); 40 assertThat(result.task(":app:assemble").getOutcome()).isEqualTo(SUCCESS); 41 } 42 setupRunner()43 private GradleRunner setupRunner() throws IOException { 44 File projectDir = folder.getRoot(); 45 GradleModule.create(projectDir) 46 .addSettingsFile( 47 "include 'app'", 48 "include 'library1'") 49 .addBuildFile( 50 "buildscript {", 51 " ext {", 52 String.format("dagger_version = \"%s\"", System.getProperty("dagger_version")), 53 String.format("kotlin_version = \"%s\"", System.getProperty("kotlin_version")), 54 " }", 55 "}", 56 "", 57 "allprojects {", 58 " repositories {", 59 " mavenCentral()", 60 " mavenLocal()", 61 " }", 62 "}"); 63 64 GradleModule.create(projectDir, "app") 65 .addBuildFile( 66 "plugins {", 67 " id 'java'", 68 " id 'application'", 69 "}", 70 "dependencies {", 71 " implementation project(':library1')", 72 " implementation \"com.google.dagger:dagger:$dagger_version\"", 73 " annotationProcessor \"com.google.dagger:dagger-compiler:$dagger_version\"", 74 "}") 75 .addSrcFile( 76 "MyComponent.java", 77 "package app;", 78 "", 79 "import dagger.BindsInstance;", 80 "import dagger.Component;", 81 "import library1.FooInjector;", 82 "", 83 "@Component", 84 "interface MyComponent extends FooInjector {", 85 " @Component.Factory", 86 " interface Factory {", 87 " MyComponent create(@BindsInstance String str);", 88 " }", 89 "}"); 90 91 GradleModule.create(projectDir, "library1") 92 .addBuildFile( 93 "plugins {", 94 " id 'org.jetbrains.kotlin.jvm' version \"$kotlin_version\"", 95 " id 'org.jetbrains.kotlin.kapt' version \"$kotlin_version\"", 96 "}", 97 "dependencies {", 98 " implementation \"com.google.dagger:dagger:$dagger_version\"", 99 " annotationProcessor \"com.google.dagger:dagger-compiler:$dagger_version\"", 100 "}") 101 .addSrcFile( 102 "Foo.kt", 103 "package library1", 104 "", 105 "import javax.inject.Inject", 106 "", 107 "class Foo {", 108 " @Inject lateinit var str: String", 109 "}") 110 .addSrcFile( 111 "FooInjector.kt", 112 "package library1", 113 "", 114 "interface FooInjector {", 115 " fun inject(foo: Foo)", 116 "}"); 117 118 return GradleRunner.create() 119 .withArguments("--stacktrace", "build") 120 .withProjectDir(projectDir); 121 } 122 } 123