• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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 import static dagger.internal.codegen.base.ComponentCreatorAnnotation.SUBCOMPONENT_BUILDER;
22 import static dagger.internal.codegen.binding.ErrorMessages.creatorMessagesFor;
23 
24 import com.google.testing.compile.Compilation;
25 import com.google.testing.compile.JavaFileObjects;
26 import dagger.internal.codegen.binding.ErrorMessages;
27 import javax.tools.JavaFileObject;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.junit.runners.JUnit4;
31 
32 /** Tests for {@link dagger.Subcomponent.Builder} validation. */
33 @RunWith(JUnit4.class)
34 public class SubcomponentBuilderValidationTest {
35 
36   private static final ErrorMessages.ComponentCreatorMessages MSGS =
37       creatorMessagesFor(SUBCOMPONENT_BUILDER);
38 
39   @Test
testMoreThanOneArgFails()40   public void testMoreThanOneArgFails() {
41     JavaFileObject childComponentFile = JavaFileObjects.forSourceLines("test.ChildComponent",
42         "package test;",
43         "",
44         "import dagger.Subcomponent;",
45         "",
46         "@Subcomponent",
47         "abstract class ChildComponent {",
48         "  @Subcomponent.Builder",
49         "  interface Builder {",
50         "    ChildComponent build();",
51         "    Builder set(String s, Integer i);",
52         "    Builder set(Number n, Double d);",
53         "  }",
54         "}");
55     Compilation compilation = daggerCompiler().compile(childComponentFile);
56     assertThat(compilation).failed();
57     assertThat(compilation)
58         .hadErrorContaining(MSGS.setterMethodsMustTakeOneArg())
59         .inFile(childComponentFile)
60         .onLine(10);
61     assertThat(compilation)
62         .hadErrorContaining(MSGS.setterMethodsMustTakeOneArg())
63         .inFile(childComponentFile)
64         .onLine(11);
65   }
66 
67   @Test
testInheritedMoreThanOneArgFails()68   public void testInheritedMoreThanOneArgFails() {
69     JavaFileObject childComponentFile = JavaFileObjects.forSourceLines("test.ChildComponent",
70         "package test;",
71         "",
72         "import dagger.Subcomponent;",
73         "",
74         "@Subcomponent",
75         "abstract class ChildComponent {",
76         "  interface Parent {",
77         "    ChildComponent build();",
78         "    Builder set1(String s, Integer i);",
79         "  }",
80         "",
81         "  @Subcomponent.Builder",
82         "  interface Builder extends Parent {}",
83         "}");
84     Compilation compilation = daggerCompiler().compile(childComponentFile);
85     assertThat(compilation).failed();
86     assertThat(compilation)
87         .hadErrorContaining(
88             String.format(
89                 MSGS.inheritedSetterMethodsMustTakeOneArg(),
90                 "set1(java.lang.String,java.lang.Integer)"))
91         .inFile(childComponentFile)
92         .onLine(13);
93   }
94 
95   @Test
testSetterReturningNonVoidOrBuilderFails()96   public void testSetterReturningNonVoidOrBuilderFails() {
97     JavaFileObject childComponentFile = JavaFileObjects.forSourceLines("test.ChildComponent",
98         "package test;",
99         "",
100         "import dagger.Subcomponent;",
101         "",
102         "@Subcomponent",
103         "abstract class ChildComponent {",
104         "  @Subcomponent.Builder",
105         "  interface Builder {",
106         "    ChildComponent build();",
107         "    String set(Integer i);",
108         "  }",
109         "}");
110     Compilation compilation = daggerCompiler().compile(childComponentFile);
111     assertThat(compilation).failed();
112     assertThat(compilation)
113         .hadErrorContaining(MSGS.setterMethodsMustReturnVoidOrBuilder())
114         .inFile(childComponentFile)
115         .onLine(10);
116   }
117 
118   @Test
testInheritedSetterReturningNonVoidOrBuilderFails()119   public void testInheritedSetterReturningNonVoidOrBuilderFails() {
120     JavaFileObject childComponentFile = JavaFileObjects.forSourceLines("test.ChildComponent",
121         "package test;",
122         "",
123         "import dagger.Subcomponent;",
124         "",
125         "@Subcomponent",
126         "abstract class ChildComponent {",
127         "  interface Parent {",
128         "    ChildComponent build();",
129         "    String set(Integer i);",
130         "  }",
131         "",
132         "  @Subcomponent.Builder",
133         "  interface Builder extends Parent {}",
134         "}");
135     Compilation compilation = daggerCompiler().compile(childComponentFile);
136     assertThat(compilation).failed();
137     assertThat(compilation)
138         .hadErrorContaining(
139             String.format(
140                 MSGS.inheritedSetterMethodsMustReturnVoidOrBuilder(), "set(java.lang.Integer)"))
141         .inFile(childComponentFile)
142         .onLine(13);
143   }
144 
145   @Test
testGenericsOnSetterMethodFails()146   public void testGenericsOnSetterMethodFails() {
147     JavaFileObject childComponentFile = JavaFileObjects.forSourceLines("test.ChildComponent",
148         "package test;",
149         "",
150         "import dagger.Subcomponent;",
151         "",
152         "@Subcomponent",
153         "abstract class ChildComponent {",
154         "  @Subcomponent.Builder",
155         "  interface Builder {",
156         "    ChildComponent build();",
157         "    <T> Builder set(T t);",
158         "  }",
159         "}");
160     Compilation compilation = daggerCompiler().compile(childComponentFile);
161     assertThat(compilation).failed();
162     assertThat(compilation)
163         .hadErrorContaining(MSGS.methodsMayNotHaveTypeParameters())
164         .inFile(childComponentFile)
165         .onLine(10);
166   }
167 
168   @Test
testGenericsOnInheritedSetterMethodFails()169   public void testGenericsOnInheritedSetterMethodFails() {
170     JavaFileObject childComponentFile = JavaFileObjects.forSourceLines("test.ChildComponent",
171         "package test;",
172         "",
173         "import dagger.Subcomponent;",
174         "",
175         "@Subcomponent",
176         "abstract class ChildComponent {",
177         "  interface Parent {",
178         "    ChildComponent build();",
179         "    <T> Builder set(T t);",
180         "  }",
181         "",
182         "  @Subcomponent.Builder",
183         "  interface Builder extends Parent {}",
184         "}");
185     Compilation compilation = daggerCompiler().compile(childComponentFile);
186     assertThat(compilation).failed();
187     assertThat(compilation)
188         .hadErrorContaining(
189             String.format(MSGS.inheritedMethodsMayNotHaveTypeParameters(), "<T>set(T)"))
190         .inFile(childComponentFile)
191         .onLine(13);
192   }
193 }
194