• 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 dagger.internal.codegen;
18 
19 import static com.google.testing.compile.CompilationSubject.assertThat;
20 import static dagger.internal.codegen.Compilers.daggerCompiler;
21 
22 import com.google.testing.compile.Compilation;
23 import com.google.testing.compile.JavaFileObjects;
24 import javax.tools.JavaFileObject;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.junit.runners.JUnit4;
28 
29 // Tests an invalid inject constructor that avoids validation in its own library by using
30 // a dependency on jsr330 rather than Dagger gets validated when used in a component.
31 @RunWith(JUnit4.class)
32 public final class InvalidInjectConstructorTest {
33 
34   @Test
usedInvalidConstructorFails()35   public void usedInvalidConstructorFails() {
36     JavaFileObject component =
37         JavaFileObjects.forSourceLines(
38             "test.TestComponent",
39             "package test;",
40             "",
41             "import dagger.Component;",
42             "import dagger.internal.codegen.InvalidInjectConstructor;",
43             "",
44             "@Component",
45             "interface TestComponent {",
46             "  InvalidInjectConstructor invalidInjectConstructor();",
47             "}");
48     Compilation compilation = daggerCompiler().compile(component);
49     assertThat(compilation).failed();
50     assertThat(compilation).hadErrorCount(2);
51     assertThat(compilation)
52         .hadErrorContaining(
53             "Type dagger.internal.codegen.InvalidInjectConstructor may only contain one injected "
54                 + "constructor. Found: ["
55                 + "InvalidInjectConstructor(), "
56                 + "InvalidInjectConstructor(java.lang.String)"
57                 + "]");
58     // TODO(b/215620949): Avoid reporting missing bindings on a type that has errors.
59     assertThat(compilation)
60         .hadErrorContaining(
61             "InvalidInjectConstructor cannot be provided without an @Inject constructor or an "
62                 + "@Provides-annotated method.");
63   }
64 
65   @Test
unusedInvalidConstructorFails()66   public void unusedInvalidConstructorFails() {
67     JavaFileObject component =
68         JavaFileObjects.forSourceLines(
69             "test.TestComponent",
70             "package test;",
71             "",
72             "import dagger.Component;",
73             "import dagger.internal.codegen.InvalidInjectConstructor;",
74             "",
75             "@Component",
76             "interface TestComponent {",
77             // Here we're only using the members injection, but we're testing that we still validate
78             // the constructors
79             "  void inject(InvalidInjectConstructor instance);",
80             "}");
81     Compilation compilation = daggerCompiler().compile(component);
82     assertThat(compilation).failed();
83     assertThat(compilation).hadErrorCount(2);
84     assertThat(compilation)
85         .hadErrorContaining(
86             "Type dagger.internal.codegen.InvalidInjectConstructor may only contain one injected "
87                 + "constructor. Found: ["
88                 + "InvalidInjectConstructor(), "
89                 + "InvalidInjectConstructor(java.lang.String)"
90                 + "]");
91     // TODO(b/215620949): Avoid reporting missing bindings on a type that has errors.
92     assertThat(compilation)
93         .hadErrorContaining(
94             "InvalidInjectConstructor cannot be provided without an @Inject constructor or an "
95                 + "@Provides-annotated method.");
96   }
97 }
98