• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
21 import java.io.File;
22 import java.io.IOException;
23 import org.gradle.testkit.runner.BuildResult;
24 import org.gradle.testkit.runner.GradleRunner;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.rules.TemporaryFolder;
28 import org.junit.runner.RunWith;
29 import org.junit.runners.JUnit4;
30 
31 // This is a regression test for https://github.com/google/dagger/issues/3522
32 @RunWith(JUnit4.class)
33 public class InjectClassNonDaggerMethodTest {
34   private static final String JAVAC_ERROR_MESSAGE =
35       "Foo.java:8: error: cannot find symbol";
36 
37   private static final String DAGGER_ERROR_MESSAGE =
38       "InjectProcessingStep was unable to process 'Foo()' because 'MissingClass' could not be "
39           + "resolved";
40 
41   @Rule public TemporaryFolder folder = new TemporaryFolder();
42 
43   @Test
testInjectMethod()44   public void testInjectMethod() throws IOException {
45     BuildResult result = setupRunner(/* useInject= */ true).buildAndFail();
46 
47     // Assert that the inject method has both a javac error and a dagger error.
48     assertThat(result.getOutput()).contains("Task :library1:compileJava FAILED");
49     assertThat(result.getOutput()).contains(JAVAC_ERROR_MESSAGE);
50     assertThat(result.getOutput()).contains(DAGGER_ERROR_MESSAGE);
51   }
52 
53   @Test
testNonInjectMethod()54   public void testNonInjectMethod() throws IOException {
55     BuildResult result = setupRunner(/* useInject= */ false).buildAndFail();
56 
57     // Assert that the non-inject method has a javac error but not a dagger error.
58     assertThat(result.getOutput()).contains("Task :library1:compileJava FAILED");
59     assertThat(result.getOutput()).contains(JAVAC_ERROR_MESSAGE);
60     assertThat(result.getOutput()).doesNotContain(DAGGER_ERROR_MESSAGE);
61   }
62 
setupRunner(boolean useInject)63   private GradleRunner setupRunner(boolean useInject) throws IOException {
64     File projectDir = folder.getRoot();
65     GradleModule.create(projectDir)
66         .addSettingsFile(
67             "include 'app'",
68             "include 'library1'")
69         .addBuildFile(
70             "buildscript {",
71             "  ext {",
72             String.format("dagger_version = \"%s\"", System.getProperty("dagger_version")),
73             String.format("kotlin_version = \"%s\"", System.getProperty("kotlin_version")),
74             "  }",
75             "}",
76             "",
77             "allprojects {",
78             "  repositories {",
79             "    mavenCentral()",
80             "    mavenLocal()",
81             "  }",
82             "}");
83 
84     GradleModule.create(projectDir, "app")
85         .addBuildFile(
86             "plugins {",
87             "  id 'java'",
88             "  id 'application'",
89             "}",
90             "dependencies {",
91             "  implementation project(':library1')",
92             "  implementation \"com.google.dagger:dagger:$dagger_version\"",
93             "  annotationProcessor \"com.google.dagger:dagger-compiler:$dagger_version\"",
94             "}")
95         .addSrcFile(
96             "MyComponent.java",
97             "package app;",
98             "",
99             "import dagger.Component;",
100             "import library1.Foo;",
101             "",
102             "@Component",
103             "interface MyComponent {",
104             "  Foo foo();",
105             "}");
106 
107     GradleModule.create(projectDir, "library1")
108         .addBuildFile(
109             "plugins {",
110             "  id 'java'",
111             "  id 'java-library'",
112             "}",
113             "dependencies {",
114             "  implementation \"com.google.dagger:dagger:$dagger_version\"",
115             "  annotationProcessor \"com.google.dagger:dagger-compiler:$dagger_version\"",
116             "}")
117         .addSrcFile(
118             "Foo.java",
119             "package library1;",
120             "",
121             "import javax.inject.Inject;",
122             "",
123             "class Foo {",
124             "  @Inject Foo() {}",
125             "",
126             useInject
127                 ? "  @Inject void method(MissingClass missingClass) {}"
128                 : "  void method(MissingClass missingClass) {}",
129             "}");
130 
131     return GradleRunner.create()
132         .withArguments("--stacktrace", "build")
133         .withProjectDir(projectDir);
134   }
135 }
136