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/3401 33 // This issues occurs specifically when the subcomponent factory method is defined in a separate 34 // kotlin library from the component that implements the subcomponent factory method. 35 @RunWith(JUnit4.class) 36 public class TransitiveSubcomponentTest { 37 @Rule public TemporaryFolder folder = new TemporaryFolder(); 38 39 @Test testBuild()40 public void testBuild() throws IOException { 41 BuildResult result = setupRunner().build(); 42 assertThat(result.task(":app:assemble").getOutcome()).isEqualTo(SUCCESS); 43 } 44 setupRunner()45 private GradleRunner setupRunner() throws IOException { 46 File projectDir = folder.getRoot(); 47 GradleModule.create(projectDir) 48 .addSettingsFile( 49 "include 'app'", 50 "include 'library1'") 51 .addBuildFile( 52 "buildscript {", 53 " ext {", 54 String.format("dagger_version = \"%s\"", System.getProperty("dagger_version")), 55 String.format("kotlin_version = \"%s\"", System.getProperty("kotlin_version")), 56 " }", 57 "}", 58 "", 59 "allprojects {", 60 " repositories {", 61 " mavenCentral()", 62 " mavenLocal()", 63 " }", 64 "}"); 65 66 GradleModule.create(projectDir, "app") 67 .addBuildFile( 68 "plugins {", 69 " id 'java'", 70 " id 'application'", 71 "}", 72 "dependencies {", 73 " implementation project(':library1')", 74 " implementation \"com.google.dagger:dagger:$dagger_version\"", 75 " annotationProcessor \"com.google.dagger:dagger-compiler:$dagger_version\"", 76 "}") 77 .addSrcFile( 78 "MyComponent.java", 79 "package app;", 80 "", 81 "import dagger.Component;", 82 "import library1.MySubcomponent1;", 83 "", 84 "@Component", 85 "interface MyComponent {", 86 " MySubcomponent1 subcomponent1();", 87 "}"); 88 89 GradleModule.create(projectDir, "library1") 90 .addBuildFile( 91 "plugins {", 92 " id 'org.jetbrains.kotlin.jvm' version \"$kotlin_version\"", 93 " id 'org.jetbrains.kotlin.kapt' version \"$kotlin_version\"", 94 "}", 95 "dependencies {", 96 " implementation \"com.google.dagger:dagger:$dagger_version\"", 97 " annotationProcessor \"com.google.dagger:dagger-compiler:$dagger_version\"", 98 "}") 99 .addSrcFile( 100 "MyModule.kt", 101 "package library1", 102 "", 103 "import dagger.Module", 104 "import dagger.Provides", 105 "", 106 "@Module", 107 "public class MyModule(private val int: Int) {", 108 " @Provides public fun provideInt(): Int = int", 109 "}") 110 .addSrcFile( 111 "MySubcomponent1.kt", 112 "package library1", 113 "", 114 "import dagger.Subcomponent", 115 "", 116 "@Subcomponent", 117 "public interface MySubcomponent1 {", 118 " public fun subcomponent2(myModule: MyModule): MySubcomponent2", 119 "}") 120 .addSrcFile( 121 "MySubcomponent2.kt", 122 "package library1", 123 "", 124 "import dagger.Subcomponent", 125 "", 126 "@Subcomponent(modules = [MyModule::class])", 127 "public interface MySubcomponent2 {", 128 " public fun integer(): Int", 129 "}"); 130 131 return GradleRunner.create() 132 .withArguments("--stacktrace", "build") 133 .withProjectDir(projectDir); 134 } 135 } 136