aboutsummaryrefslogtreecommitdiff
path: root/javatests/dagger/hilt/processor/internal/definecomponent/DefineComponentProcessorTest.java
blob: a8c5fbe9caf63ce046972f5b7a3e946ec908b088 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
 * Copyright (C) 2019 The Dagger Authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package dagger.hilt.processor.internal.definecomponent;

import static com.google.testing.compile.CompilationSubject.assertThat;
import static com.google.testing.compile.Compiler.javac;

import com.google.testing.compile.Compilation;
import com.google.testing.compile.Compiler;
import com.google.testing.compile.JavaFileObjects;
import dagger.hilt.processor.internal.GeneratedImport;
import javax.tools.JavaFileObject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public final class DefineComponentProcessorTest {

  @Test
  public void testDefineComponentOutput() {
    JavaFileObject component =
        JavaFileObjects.forSourceLines(
            "test.FooComponent",
            "package test;",
            "",
            "import dagger.hilt.components.SingletonComponent;",
            "import dagger.hilt.DefineComponent;",
            "",
            "@DefineComponent(parent = SingletonComponent.class)",
            "interface FooComponent {",
            "  static int staticField = 1;",
            "  static int staticMethod() { return staticField; }",
            "}");

    JavaFileObject builder =
        JavaFileObjects.forSourceLines(
            "test.FooComponentBuilder",
            "package test;",
            "",
            "import dagger.hilt.DefineComponent;",
            "",
            "@DefineComponent.Builder",
            "interface FooComponentBuilder {",
            "  static int staticField = 1;",
            "  static int staticMethod() { return staticField; }",
            "",
            "  FooComponent create();",
            "}");

    JavaFileObject componentOutput =
        JavaFileObjects.forSourceLines(
            "dagger.hilt.processor.internal.definecomponent.codegen._test_FooComponent",
            "package dagger.hilt.processor.internal.definecomponent.codegen;",
            "",
            "import dagger.hilt.internal.definecomponent.DefineComponentClasses;",
            GeneratedImport.IMPORT_GENERATED_ANNOTATION,
            "",
            "@DefineComponentClasses(component = \"test.FooComponent\")",
            "@Generated(\"" + DefineComponentProcessor.class.getName() + "\")",
            "public class _test_FooComponent {}");

    JavaFileObject builderOutput =
        JavaFileObjects.forSourceLines(
            "dagger.hilt.processor.internal.definecomponent.codegen._test_FooComponentBuilder",
            "package dagger.hilt.processor.internal.definecomponent.codegen;",
            "",
            "import dagger.hilt.internal.definecomponent.DefineComponentClasses;",
            GeneratedImport.IMPORT_GENERATED_ANNOTATION,
            "",
            "@DefineComponentClasses(builder = \"test.FooComponentBuilder\")",
            "@Generated(\"" + DefineComponentProcessor.class.getName() + "\")",
            "public class _test_FooComponentBuilder {}");

    Compilation compilation = compiler().compile(component, builder);
    assertThat(compilation).succeeded();
    assertThat(compilation)
        .generatedSourceFile(sourceName(componentOutput))
        .hasSourceEquivalentTo(componentOutput);
    assertThat(compilation)
        .generatedSourceFile(sourceName(builderOutput))
        .hasSourceEquivalentTo(builderOutput);
  }

  private static String sourceName(JavaFileObject fileObject) {
    return fileObject.getName().replace(".java", "").replace('.', '/');
  }

  @Test
  public void testDefineComponentClass_fails() {
    JavaFileObject component =
        JavaFileObjects.forSourceLines(
            "test.FooComponent",
            "package test;",
            "",
            "import dagger.hilt.components.SingletonComponent;",
            "import dagger.hilt.DefineComponent;",
            "",
            "@DefineComponent( parent = SingletonComponent.class )",
            "abstract class FooComponent {}");

    Compilation compilation = compiler().compile(component);
    assertThat(compilation).failed();
    assertThat(compilation).hadErrorCount(1);
    assertThat(compilation)
        .hadErrorContaining(
            "@DefineComponent is only allowed on interfaces. Found: test.FooComponent");
  }

  @Test
  public void testDefineComponentWithTypeParameters_fails() {
    JavaFileObject component =
        JavaFileObjects.forSourceLines(
            "test.FooComponent",
            "package test;",
            "",
            "import dagger.hilt.components.SingletonComponent;",
            "import dagger.hilt.DefineComponent;",
            "",
            "@DefineComponent( parent = SingletonComponent.class )",
            "interface FooComponent<T> {}");

    Compilation compilation = compiler().compile(component);
    assertThat(compilation).failed();
    assertThat(compilation).hadErrorCount(1);
    assertThat(compilation)
        .hadErrorContaining("@DefineComponent test.FooComponent<T>, cannot have type parameters.");
  }

  @Test
  public void testDefineComponentWithInvalidComponent_fails() {
    JavaFileObject component =
        JavaFileObjects.forSourceLines(
            "test.FooComponent",
            "package test;",
            "",
            "import dagger.hilt.DefineComponent;",
            "import dagger.hilt.android.qualifiers.ApplicationContext;",
            "",
            "@DefineComponent( parent = ApplicationContext.class )",
            "interface FooComponent {}");

    Compilation compilation = compiler().compile(component);
    assertThat(compilation).failed();
    assertThat(compilation).hadErrorCount(1);
    assertThat(compilation)
        .hadErrorContaining(
            "@DefineComponent test.FooComponent, references a type not annotated with "
                + "@DefineComponent: dagger.hilt.android.qualifiers.ApplicationContext")
        .inFile(component);
  }

  @Test
  public void testDefineComponentExtendsInterface_fails() {
    JavaFileObject component =
        JavaFileObjects.forSourceLines(
            "test.FooComponent",
            "package test;",
            "",
            "import dagger.hilt.components.SingletonComponent;",
            "import dagger.hilt.DefineComponent;",
            "",
            "interface Foo {}",
            "",
            "@DefineComponent( parent = SingletonComponent.class )",
            "interface FooComponent extends Foo {}");

    Compilation compilation = compiler().compile(component);
    assertThat(compilation).failed();
    assertThat(compilation).hadErrorCount(1);
    assertThat(compilation)
        .hadErrorContaining(
            "@DefineComponent test.FooComponent, cannot extend a super class or interface."
                + " Found: test.Foo");
  }

  @Test
  public void testDefineComponentNonStaticMethod_fails() {
    JavaFileObject component =
        JavaFileObjects.forSourceLines(
            "test.FooComponent",
            "package test;",
            "",
            "import dagger.hilt.components.SingletonComponent;",
            "import dagger.hilt.DefineComponent;",
            "",
            "@DefineComponent( parent = SingletonComponent.class )",
            "interface FooComponent {",
            "  int nonStaticMethod();",
            "}");

    Compilation compilation = compiler().compile(component);
    assertThat(compilation).failed();
    assertThat(compilation).hadErrorCount(1);
    assertThat(compilation)
        .hadErrorContaining(
            "@DefineComponent test.FooComponent, cannot have non-static methods. "
                + "Found: [nonStaticMethod()]");
  }

  @Test
  public void testDefineComponentDependencyCycle_fails() {
    JavaFileObject component1 =
        JavaFileObjects.forSourceLines(
            "test.Component1",
            "package test;",
            "",
            "import dagger.hilt.DefineComponent;",
            "",
            "@DefineComponent(parent = Component2.class)",
            "interface Component1 {}");

    JavaFileObject component2 =
        JavaFileObjects.forSourceLines(
            "test.Component2",
            "package test;",
            "",
            "import dagger.hilt.DefineComponent;",
            "",
            "@DefineComponent(parent = Component1.class)",
            "interface Component2 {}");

    Compilation compilation = compiler().compile(component1, component2);
    assertThat(compilation).failed();
    assertThat(compilation).hadErrorCount(2);
    assertThat(compilation)
        .hadErrorContaining(
            "@DefineComponent cycle: test.Component1 -> test.Component2 -> test.Component1");
    assertThat(compilation)
        .hadErrorContaining(
            "@DefineComponent cycle: test.Component2 -> test.Component1 -> test.Component2");
  }

  @Test
  public void testDefineComponentNoParent_fails() {
    JavaFileObject component =
        JavaFileObjects.forSourceLines(
            "test.FooComponent",
            "package test;",
            "",
            "import dagger.hilt.DefineComponent;",
            "",
            "@DefineComponent",
            "interface FooComponent {}");
    Compilation compilation = compiler().compile(component);
    assertThat(compilation)
        .hadErrorContaining("@DefineComponent test.FooComponent is missing a parent declaration.");
  }

  @Test
  public void testDefineComponentBuilderClass_fails() {
    JavaFileObject builder =
        JavaFileObjects.forSourceLines(
            "test.FooComponentBuilder",
            "package test;",
            "",
            "import dagger.hilt.DefineComponent;",
            "",
            "@DefineComponent.Builder",
            "abstract class FooComponentBuilder {}");

    Compilation compilation = compiler().compile(builder);
    assertThat(compilation).failed();
    assertThat(compilation).hadErrorCount(1);
    assertThat(compilation)
        .hadErrorContaining(
            "@DefineComponent.Builder is only allowed on interfaces. "
                + "Found: test.FooComponentBuilder");
  }

  @Test
  public void testDefineComponentBuilderWithTypeParameters_fails() {
    JavaFileObject builder =
        JavaFileObjects.forSourceLines(
            "test.FooComponentBuilder",
            "package test;",
            "",
            "import dagger.hilt.DefineComponent;",
            "",
            "@DefineComponent.Builder",
            "interface FooComponentBuilder<T> {}");

    Compilation compilation = compiler().compile(builder);
    assertThat(compilation).failed();
    assertThat(compilation).hadErrorCount(1);
    assertThat(compilation)
        .hadErrorContaining(
            "@DefineComponent.Builder test.FooComponentBuilder<T>, cannot have type "
                + "parameters.");
  }

  @Test
  public void testDefineComponentBuilderExtendsInterface_fails() {
    JavaFileObject builder =
        JavaFileObjects.forSourceLines(
            "test.FooComponentBuilder",
            "package test;",
            "",
            "import dagger.hilt.DefineComponent;",
            "",
            "interface Foo {}",
            "",
            "@DefineComponent.Builder",
            "interface FooComponentBuilder extends Foo {}");

    Compilation compilation = compiler().compile(builder);
    assertThat(compilation).failed();
    assertThat(compilation).hadErrorCount(1);
    assertThat(compilation)
        .hadErrorContaining(
            "@DefineComponent.Builder test.FooComponentBuilder, cannot extend a super class "
                + "or interface. Found: test.Foo");
  }

  @Test
  public void testDefineComponentBuilderNoBuilderMethod_fails() {
    JavaFileObject component =
        JavaFileObjects.forSourceLines(
            "test.FooComponent",
            "package test;",
            "",
            "import dagger.hilt.DefineComponent;",
            "",
            "@DefineComponent.Builder",
            "interface FooComponentBuilder {}");

    Compilation compilation = compiler().compile(component);
    assertThat(compilation).failed();
    assertThat(compilation).hadErrorCount(1);
    assertThat(compilation)
        .hadErrorContaining(
            "@DefineComponent.Builder test.FooComponentBuilder, must have exactly 1 build "
                + "method that takes no parameters. Found: []");
  }

  @Test
  public void testDefineComponentBuilderPrimitiveReturnType_fails() {
    JavaFileObject component =
        JavaFileObjects.forSourceLines(
            "test.FooComponent",
            "package test;",
            "",
            "import dagger.hilt.DefineComponent;",
            "",
            "@DefineComponent.Builder",
            "interface FooComponentBuilder {",
            "  int nonStaticMethod();",
            "}");

    Compilation compilation = compiler().compile(component);
    assertThat(compilation).failed();
    assertThat(compilation).hadErrorCount(1);
    assertThat(compilation)
        .hadErrorContaining(
            "@DefineComponent.Builder method, test.FooComponentBuilder#nonStaticMethod(), "
                + "must return a @DefineComponent type. Found: int");
  }

  @Test
  public void testDefineComponentBuilderWrongReturnType_fails() {
    JavaFileObject component =
        JavaFileObjects.forSourceLines(
            "test.FooComponent",
            "package test;",
            "",
            "import dagger.hilt.DefineComponent;",
            "",
            "interface Foo {}",
            "",
            "@DefineComponent.Builder",
            "interface FooComponentBuilder {",
            "  Foo build();",
            "}");

    Compilation compilation = compiler().compile(component);
    assertThat(compilation).failed();
    assertThat(compilation).hadErrorCount(1);
    assertThat(compilation)
        .hadErrorContaining(
            "@DefineComponent.Builder method, test.FooComponentBuilder#build(), must return "
                + "a @DefineComponent type. Found: test.Foo");
  }

  private static Compiler compiler() {
    return javac().withProcessors(new DefineComponentProcessor());
  }
}