forked from square/dagger
-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathModuleFactoryGeneratorTest.java
More file actions
1452 lines (1372 loc) · 48.8 KB
/
ModuleFactoryGeneratorTest.java
File metadata and controls
1452 lines (1372 loc) · 48.8 KB
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2014 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.internal.codegen;
import static dagger.internal.codegen.DaggerModuleMethodSubject.Factory.assertThatMethodInUnannotatedClass;
import static dagger.internal.codegen.DaggerModuleMethodSubject.Factory.assertThatModuleMethod;
import androidx.room.compiler.processing.XProcessingEnv;
import androidx.room.compiler.processing.util.Source;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import dagger.testing.compile.CompilerTests;
import dagger.testing.golden.GoldenFileRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class ModuleFactoryGeneratorTest {
private static final Source NON_TYPE_USE_NULLABLE =
CompilerTests.javaSource(
"test.Nullable", // force one-string-per-line format
"package test;",
"",
"public @interface Nullable {}");
@Rule public GoldenFileRule goldenFileRule = new GoldenFileRule();
// TODO(gak): add tests for invalid combinations of scope and qualifier annotations like we have
// for @Inject
@Test public void providesMethodNotInModule() {
assertThatMethodInUnannotatedClass("@Provides String provideString() { return null; }")
.hasError("@Provides methods can only be present within a @Module or @ProducerModule");
}
@Test public void providesMethodAbstract() {
assertThatModuleMethod("@Provides abstract String abstractMethod();")
.hasError("@Provides methods cannot be abstract");
}
@Test public void providesMethodPrivate() {
assertThatModuleMethod("@Provides private String privateMethod() { return null; }")
.hasError("@Provides methods cannot be private");
}
@Test public void providesMethodReturnVoid() {
assertThatModuleMethod("@Provides void voidMethod() {}")
.hasError("@Provides methods must return a value (not void)");
}
@Test
public void providesMethodReturnsProvider() {
assertThatModuleMethod("@Provides Provider<String> provideProvider() {}")
.hasError("@Provides methods must not return framework types");
}
@Test
public void providesMethodReturnsJakartaProvider() {
assertThatModuleMethod("@Provides jakarta.inject.Provider<String> provideProvider() {}")
.hasError("@Provides methods must not return framework types");
}
@Test
public void providesMethodReturnsDaggerInternalProvider() {
assertThatModuleMethod("@Provides dagger.internal.Provider<String> provideProvider() {}")
.hasError("@Provides methods must not return disallowed types");
}
@Test
public void providesIntoSetMethodReturnsDaggerInternalProvider() {
assertThatModuleMethod(
"@Provides @IntoSet dagger.internal.Provider<String> provideProvider() {}")
.hasError("@Provides methods must not return disallowed types");
}
@Test
public void providesMethodReturnsLazy() {
assertThatModuleMethod("@Provides Lazy<String> provideLazy() {}")
.hasError("@Provides methods must not return framework types");
}
@Test
public void providesMethodReturnsMembersInjector() {
assertThatModuleMethod("@Provides MembersInjector<String> provideMembersInjector() {}")
.hasError("@Provides methods must not return framework types");
}
@Test
public void providesMethodReturnsProducer() {
assertThatModuleMethod("@Provides Producer<String> provideProducer() {}")
.hasError("@Provides methods must not return framework types");
}
@Test
public void providesMethodReturnsProduced() {
assertThatModuleMethod("@Provides Produced<String> provideProduced() {}")
.hasError("@Provides methods must not return framework types");
}
@Test public void providesMethodWithTypeParameter() {
assertThatModuleMethod("@Provides <T> String typeParameter() { return null; }")
.hasError("@Provides methods may not have type parameters");
}
@Test public void providesMethodSetValuesWildcard() {
assertThatModuleMethod("@Provides @ElementsIntoSet Set<?> provideWildcard() { return null; }")
.hasError(
"@Provides methods must return a primitive, an array, a type variable, "
+ "or a declared type");
}
@Test public void providesMethodSetValuesRawSet() {
assertThatModuleMethod("@Provides @ElementsIntoSet Set provideSomething() { return null; }")
.hasError("@Provides methods annotated with @ElementsIntoSet cannot return a raw Set");
}
@Test public void providesElementsIntoSetMethodReturnsSetDaggerProvider() {
assertThatModuleMethod(
"@Provides @ElementsIntoSet Set<dagger.internal.Provider<String>> provideProvider() {}")
.hasError("@Provides methods must not return disallowed types");
}
@Test public void providesMethodSetValuesNotASet() {
assertThatModuleMethod(
"@Provides @ElementsIntoSet List<String> provideStrings() { return null; }")
.hasError("@Provides methods annotated with @ElementsIntoSet must return a Set");
}
@Test
public void bindsMethodReturnsProvider() {
assertThatModuleMethod("@Binds abstract Provider<Number> bindsProvider(Provider<Long> impl);")
.hasError("@Binds methods must not return framework types");
}
@Test
public void bindsMethodReturnsDaggerProvider() {
assertThatModuleMethod("@Binds abstract dagger.internal.Provider<Number> "
+ "bindsProvider(dagger.internal.Provider<Long> impl);")
.hasError("@Binds methods must not return disallowed types");
}
@Test public void modulesWithTypeParamsMustBeAbstract() {
Source moduleFile =
CompilerTests.javaSource(
"test.TestModule",
"package test;",
"",
"import dagger.Module;",
"",
"@Module",
"final class TestModule<A> {}");
CompilerTests.daggerCompiler(moduleFile)
.compile(
subject -> {
subject.hasErrorCount(1);
subject.hasErrorContaining("Modules with type parameters must be abstract")
.onSource(moduleFile)
.onLine(6);
});
}
@Test public void provideOverriddenByNoProvide() {
Source parent =
CompilerTests.javaSource(
"test.Parent",
"package test;",
"",
"import dagger.Module;",
"import dagger.Provides;",
"",
"@Module",
"class Parent {",
" @Provides String foo() { return null; }",
"}");
assertThatModuleMethod("String foo() { return null; }")
.withDeclaration("@Module class %s extends Parent { %s }")
.withAdditionalSources(parent)
.hasError(
"Binding methods may not be overridden in modules. Overrides: "
+ "@Provides String test.Parent.foo()");
}
@Test public void provideOverriddenByProvide() {
Source parent =
CompilerTests.javaSource(
"test.Parent",
"package test;",
"",
"import dagger.Module;",
"import dagger.Provides;",
"",
"@Module",
"class Parent {",
" @Provides String foo() { return null; }",
"}");
assertThatModuleMethod("@Provides String foo() { return null; }")
.withDeclaration("@Module class %s extends Parent { %s }")
.withAdditionalSources(parent)
.hasError(
"Binding methods may not override another method. Overrides: "
+ "@Provides String test.Parent.foo()");
}
@Test public void providesOverridesNonProvides() {
Source parent =
CompilerTests.javaSource(
"test.Parent",
"package test;",
"",
"import dagger.Module;",
"",
"@Module",
"class Parent {",
" String foo() { return null; }",
"}");
assertThatModuleMethod("@Provides String foo() { return null; }")
.withDeclaration("@Module class %s extends Parent { %s }")
.withAdditionalSources(parent)
.hasError(
"Binding methods may not override another method. Overrides: "
+ "String test.Parent.foo()");
}
@Test public void validatesIncludedModules() {
Source module =
CompilerTests.javaSource(
"test.Parent",
"package test;",
"",
"import dagger.Module;",
"",
"@Module(",
" includes = {",
" Void.class,",
" String.class,",
" }",
")",
"class TestModule {}");
CompilerTests.daggerCompiler(module)
.compile(
subject -> {
subject.hasErrorCount(2);
// We avoid asserting on the line number because ksp and javac report different lines.
// The main issue here is that ksp doesn't allow reporting errors on individual
// annotation values, it only allows reporting errors on annotations themselves.
subject.hasErrorContaining(
"java.lang.Void is listed as a module, but is not annotated with @Module")
.onSource(module);
subject.hasErrorContaining(
"java.lang.String is listed as a module, but is not annotated with @Module")
.onSource(module);
});
}
@Test public void singleProvidesMethodNoArgs() {
Source moduleFile =
CompilerTests.javaSource(
"test.TestModule",
"package test;",
"",
"import dagger.Module;",
"import dagger.Provides;",
"",
"@Module",
"final class TestModule {",
" @Provides String provideString() {",
" return \"\";",
" }",
"}");
CompilerTests.daggerCompiler(moduleFile)
.compile(
subject -> {
subject.hasErrorCount(0);
subject.generatedSource(
goldenFileRule.goldenSource("test/TestModule_ProvideStringFactory"));
});
}
@Test public void singleProvidesMethodNoArgs_disableNullable() {
Source moduleFile =
CompilerTests.javaSource(
"test.TestModule",
"package test;",
"",
"import dagger.Module;",
"import dagger.Provides;",
"",
"@Module",
"final class TestModule {",
" @Provides String provideString() {",
" return \"\";",
" }",
"}");
CompilerTests.daggerCompiler(moduleFile)
.withProcessingOptions(ImmutableMap.of("dagger.nullableValidation", "WARNING"))
.compile(
subject -> {
subject.hasErrorCount(0);
subject.generatedSource(
goldenFileRule.goldenSource("test/TestModule_ProvideStringFactory"));
});
}
@Test
public void nonTypeUseNullableProvides() {
Source moduleFile =
CompilerTests.javaSource(
"test.TestModule",
"package test;",
"",
"import dagger.Module;",
"import dagger.Provides;",
"",
"@Module",
"final class TestModule {",
" @Provides @Nullable String provideString() { return null; }",
"}");
CompilerTests.daggerCompiler(moduleFile, NON_TYPE_USE_NULLABLE)
.compile(
subject -> {
subject.hasErrorCount(0);
subject.generatedSource(
goldenFileRule.goldenSource("test/TestModule_ProvideStringFactory"));
});
}
@Test
public void kotlinNullableProvides() {
Source moduleFile =
CompilerTests.kotlinSource(
"TestModule.kt",
"package test",
"",
"import dagger.Module;",
"import dagger.Provides;",
"",
"@Module",
"class TestModule {",
" @Provides fun provideString(): String? { return null; }",
"}");
CompilerTests.daggerCompiler(moduleFile)
.compile(
subject -> {
subject.hasErrorCount(0);
boolean isJavac = CompilerTests.backend(subject) == XProcessingEnv.Backend.JAVAC;
subject.generatedSource(
CompilerTests.javaSource(
"test.TestModule_ProvideStringFactory",
"package test;",
"",
"import dagger.internal.DaggerGenerated;",
"import dagger.internal.Factory;",
"import dagger.internal.QualifierMetadata;",
"import dagger.internal.ScopeMetadata;",
"import javax.annotation.processing.Generated;",
isJavac ? "import org.jetbrains.annotations.Nullable;\n" : "",
"@ScopeMetadata",
"@QualifierMetadata",
"@DaggerGenerated",
"@Generated(",
" value = \"dagger.internal.codegen.ComponentProcessor\",",
" comments = \"https://dagger.dev\"",
")",
"@SuppressWarnings({",
" \"unchecked\",",
" \"rawtypes\",",
" \"KotlinInternal\",",
" \"KotlinInternalInJava\",",
" \"cast\",",
" \"deprecation\",",
" \"nullness:initialization.field.uninitialized\"",
"})",
"public final class TestModule_ProvideStringFactory implements"
+ " Factory<String> {",
" private final TestModule module;",
"",
" public TestModule_ProvideStringFactory(TestModule module) {",
" this.module = module;",
" }",
"",
// TODO(b/368129744): KSP should output the @Nullable annotation after this
// bug is fixed.
isJavac ? " @Override\n @Nullable" : " @Override",
" public String get() {",
" return provideString(module);",
" }",
"",
" public static TestModule_ProvideStringFactory create(TestModule module) {",
" return new TestModule_ProvideStringFactory(module);",
" }",
// TODO(b/368129744): KSP should output the @Nullable annotation after this
// bug is fixed.
isJavac ? "\n @Nullable" : "",
" public static String provideString(TestModule instance) {",
" return instance.provideString();",
" }",
"}"));
});
}
@Test public void multipleProvidesMethods() {
Source classXFile =
CompilerTests.javaSource("test.X",
"package test;",
"",
"import javax.inject.Inject;",
"",
"class X {",
" @Inject public String s;",
"}");
Source moduleFile =
CompilerTests.javaSource(
"test.TestModule",
"package test;",
"",
"import dagger.MembersInjector;",
"import dagger.Module;",
"import dagger.Provides;",
"import java.util.Arrays;",
"import java.util.List;",
"",
"@Module",
"final class TestModule {",
" @Provides List<Object> provideObjects(",
" @QualifierA Object a, @QualifierB Object b, MembersInjector<X> xInjector) {",
" return Arrays.asList(a, b);",
" }",
"",
" @Provides @QualifierA Object provideAObject() {",
" return new Object();",
" }",
"",
" @Provides @QualifierB Object provideBObject() {",
" return new Object();",
" }",
"}");
CompilerTests.daggerCompiler(classXFile, moduleFile, QUALIFIER_A, QUALIFIER_B)
.compile(
subject -> {
subject.hasErrorCount(0);
subject.generatedSource(
goldenFileRule.goldenSource("test/TestModule_ProvideObjectsFactory"));
});
}
@Test
public void providesSetElement() {
Source moduleFile =
CompilerTests.javaSource(
"test.TestModule",
"package test;",
"",
"import java.util.logging.Logger;",
"import dagger.Module;",
"import dagger.Provides;",
"import dagger.multibindings.IntoSet;",
"",
"@Module",
"final class TestModule {",
" @Provides @IntoSet String provideString() {",
" return \"\";",
" }",
"}");
CompilerTests.daggerCompiler(moduleFile)
.compile(
subject -> {
subject.hasErrorCount(0);
subject.generatedSource(
goldenFileRule.goldenSource("test/TestModule_ProvideStringFactory"));
});
}
@Test public void providesSetElementWildcard() {
Source moduleFile =
CompilerTests.javaSource(
"test.TestModule",
"package test;",
"",
"import java.util.logging.Logger;",
"import dagger.Module;",
"import dagger.Provides;",
"import dagger.multibindings.IntoSet;",
"import java.util.ArrayList;",
"import java.util.List;",
"",
"@Module",
"final class TestModule {",
" @Provides @IntoSet List<List<?>> provideWildcardList() {",
" return new ArrayList<>();",
" }",
"}");
CompilerTests.daggerCompiler(moduleFile)
.compile(
subject -> {
subject.hasErrorCount(0);
subject.generatedSource(
goldenFileRule.goldenSource("test/TestModule_ProvideWildcardListFactory"));
});
}
@Test public void providesSetValues() {
Source moduleFile =
CompilerTests.javaSource(
"test.TestModule",
"package test;",
"",
"import dagger.Module;",
"import dagger.Provides;",
"import dagger.multibindings.ElementsIntoSet;",
"import java.util.Set;",
"",
"@Module",
"final class TestModule {",
" @Provides @ElementsIntoSet Set<String> provideStrings() {",
" return null;",
" }",
"}");
CompilerTests.daggerCompiler(moduleFile)
.compile(
subject -> {
subject.hasErrorCount(0);
subject.generatedSource(
goldenFileRule.goldenSource("test/TestModule_ProvideStringsFactory"));
});
}
@Test public void multipleProvidesMethodsWithSameName() {
Source moduleFile =
CompilerTests.javaSource("test.TestModule",
"package test;",
"",
"import dagger.Module;",
"import dagger.Provides;",
"",
"@Module",
"final class TestModule {",
" @Provides Object provide(int i) {",
" return i;",
" }",
"",
" @Provides String provide() {",
" return \"\";",
" }",
"}");
CompilerTests.daggerCompiler(moduleFile)
.compile(
subject -> {
subject.hasErrorCount(2);
subject.hasErrorContaining(
"Cannot have more than one binding method with the same name in a single "
+ "module")
.onSource(moduleFile)
.onLine(8);
subject.hasErrorContaining(
"Cannot have more than one binding method with the same name in a single "
+ "module")
.onSource(moduleFile)
.onLine(12);
});
}
@Test
public void providesMethodThrowsChecked() {
Source moduleFile =
CompilerTests.javaSource(
"test.TestModule",
"package test;",
"",
"import dagger.Module;",
"import dagger.Provides;",
"",
"@Module",
"final class TestModule {",
" @Provides int i() throws Exception {",
" return 0;",
" }",
"",
" @Provides String s() throws Throwable {",
" return \"\";",
" }",
"}");
CompilerTests.daggerCompiler(moduleFile)
.compile(
subject -> {
subject.hasErrorCount(2);
subject.hasErrorContaining("@Provides methods may only throw unchecked exceptions")
.onSource(moduleFile)
.onLine(8);
subject.hasErrorContaining("@Provides methods may only throw unchecked exceptions")
.onSource(moduleFile)
.onLine(12);
});
}
@Test
public void providedTypes() {
Source moduleFile =
CompilerTests.javaSource(
"test.TestModule",
"package test;",
"",
"import dagger.Module;",
"import dagger.Provides;",
"import java.io.Closeable;",
"import java.util.Set;",
"",
"@Module",
"final class TestModule {",
" @Provides String string() {",
" return null;",
" }",
"",
" @Provides Set<String> strings() {",
" return null;",
" }",
"",
" @Provides Set<? extends Closeable> closeables() {",
" return null;",
" }",
"",
" @Provides String[] stringArray() {",
" return null;",
" }",
"",
" @Provides int integer() {",
" return 0;",
" }",
"",
" @Provides int[] integers() {",
" return null;",
" }",
"}");
CompilerTests.daggerCompiler(moduleFile).compile(subject -> subject.hasErrorCount(0));
}
@Test
public void privateModule() {
Source moduleFile =
CompilerTests.javaSource(
"test.Enclosing",
"package test;",
"",
"import dagger.Module;",
"",
"final class Enclosing {",
" @Module private static final class PrivateModule {",
" }",
"}");
CompilerTests.daggerCompiler(moduleFile)
.compile(
subject -> {
subject.hasErrorCount(1);
subject.hasErrorContaining("Modules cannot be private")
.onSource(moduleFile)
.onLine(6);
});
}
@Test
public void privateModule_kotlin() {
Source moduleFile =
CompilerTests.kotlinSource(
"test.TestModule.kt",
"package test",
"",
"import dagger.Component",
"import dagger.Module",
"import dagger.Provides",
"",
"@Module",
"private class TestModule {",
" @Provides fun provideInt(): Int = 1",
"}");
CompilerTests.daggerCompiler(moduleFile)
.compile(
subject -> {
subject.hasErrorCount(1);
subject
.hasErrorContaining("Modules cannot be private")
.onSource(moduleFile);
});
}
@Test
public void enclosedInPrivateModule() {
Source moduleFile =
CompilerTests.javaSource("test.Enclosing",
"package test;",
"",
"import dagger.Module;",
"",
"final class Enclosing {",
" private static final class PrivateEnclosing {",
" @Module static final class TestModule {",
" }",
" }",
"}");
CompilerTests.daggerCompiler(moduleFile)
.compile(
subject -> {
subject.hasErrorCount(1);
subject.hasErrorContaining("Modules cannot be enclosed in private types")
.onSource(moduleFile)
.onLine(7);
});
}
@Test
public void publicModuleNonPublicIncludes() {
Source publicModuleFile =
CompilerTests.javaSource("test.PublicModule",
"package test;",
"",
"import dagger.Module;",
"",
"@Module(includes = {",
" BadNonPublicModule.class, OtherPublicModule.class, OkNonPublicModule.class",
"})",
"public final class PublicModule {",
"}");
Source badNonPublicModuleFile =
CompilerTests.javaSource(
"test.BadNonPublicModule",
"package test;",
"",
"import dagger.Module;",
"import dagger.Provides;",
"",
"@Module",
"final class BadNonPublicModule {",
" @Provides",
" int provideInt() {",
" return 42;",
" }",
"}");
Source okNonPublicModuleFile =
CompilerTests.javaSource("test.OkNonPublicModule",
"package test;",
"",
"import dagger.Module;",
"import dagger.Provides;",
"",
"@Module",
"final class OkNonPublicModule {",
" @Provides",
" static String provideString() {",
" return \"foo\";",
" }",
"}");
Source otherPublicModuleFile =
CompilerTests.javaSource("test.OtherPublicModule",
"package test;",
"",
"import dagger.Module;",
"",
"@Module",
"public final class OtherPublicModule {",
"}");
CompilerTests.daggerCompiler(
publicModuleFile,
badNonPublicModuleFile,
okNonPublicModuleFile,
otherPublicModuleFile)
.compile(
subject -> {
subject.hasErrorCount(1);
subject.hasErrorContaining(
"This module is public, but it includes non-public (or effectively non-"
+ "public) modules (test.BadNonPublicModule) that have non-static, non-"
+ "abstract binding methods. Either reduce the visibility of this module"
+ ", make the included modules public, or make all of the binding "
+ "methods on the included modules abstract or static.")
.onSource(publicModuleFile)
.onLine(8);
});
}
@Test
public void genericSubclassedModule() {
Source parent =
CompilerTests.javaSource(
"test.ParentModule",
"package test;",
"",
"import dagger.Module;",
"import dagger.Provides;",
"import dagger.multibindings.IntoSet;",
"import dagger.multibindings.IntoMap;",
"import dagger.multibindings.StringKey;",
"import java.util.List;",
"import java.util.ArrayList;",
"",
"@Module",
"abstract class ParentModule<A extends CharSequence,",
" B,",
" C extends Number & Comparable<C>> {",
" @Provides List<B> provideListB(B b) {",
" List<B> list = new ArrayList<B>();",
" list.add(b);",
" return list;",
" }",
"",
" @Provides @IntoSet B provideBElement(B b) {",
" return b;",
" }",
"",
" @Provides @IntoMap @StringKey(\"b\") B provideBEntry(B b) {",
" return b;",
" }",
"}");
Source numberChild =
CompilerTests.javaSource("test.ChildNumberModule",
"package test;",
"",
"import dagger.Module;",
"import dagger.Provides;",
"",
"@Module",
"class ChildNumberModule extends ParentModule<String, Number, Double> {",
" @Provides Number provideNumber() { return 1; }",
"}");
Source integerChild =
CompilerTests.javaSource("test.ChildIntegerModule",
"package test;",
"",
"import dagger.Module;",
"import dagger.Provides;",
"",
"@Module",
"class ChildIntegerModule extends ParentModule<StringBuilder, Integer, Float> {",
" @Provides Integer provideInteger() { return 2; }",
"}");
Source component =
CompilerTests.javaSource("test.C",
"package test;",
"",
"import dagger.Component;",
"import java.util.List;",
"",
"@Component(modules={ChildNumberModule.class, ChildIntegerModule.class})",
"interface C {",
" List<Number> numberList();",
" List<Integer> integerList();",
"}");
CompilerTests.daggerCompiler(parent, numberChild, integerChild, component)
.compile(
subject -> {
subject.hasErrorCount(0);
subject.generatedSource(
goldenFileRule.goldenSource("test/ParentModule_ProvideListBFactory"));
subject.generatedSource(
goldenFileRule.goldenSource("test/ParentModule_ProvideBElementFactory"));
subject.generatedSource(
goldenFileRule.goldenSource("test/ParentModule_ProvideBEntryFactory"));
subject.generatedSource(
goldenFileRule.goldenSource("test/ChildNumberModule_ProvideNumberFactory"));
subject.generatedSource(
goldenFileRule.goldenSource("test/ChildIntegerModule_ProvideIntegerFactory"));
});
}
@Test public void parameterizedModuleWithStaticProvidesMethodOfGenericType() {
Source moduleFile =
CompilerTests.javaSource(
"test.ParameterizedModule",
"package test;",
"",
"import dagger.Module;",
"import dagger.Provides;",
"import java.util.List;",
"import java.util.ArrayList;",
"import java.util.Map;",
"import java.util.HashMap;",
"",
"@Module abstract class ParameterizedModule<T> {",
" @Provides List<T> provideListT() {",
" return new ArrayList<>();",
" }",
"",
" @Provides static Map<String, Number> provideMapStringNumber() {",
" return new HashMap<>();",
" }",
"",
" @Provides static Object provideNonGenericType() {",
" return new Object();",
" }",
"",
" @Provides static String provideNonGenericTypeWithDeps(Object o) {",
" return o.toString();",
" }",
"}");
CompilerTests.daggerCompiler(moduleFile)
.compile(
subject -> {
subject.hasErrorCount(0);
subject.generatedSource(
goldenFileRule.goldenSource(
"test/ParameterizedModule_ProvideMapStringNumberFactory"));
subject.generatedSource(
goldenFileRule.goldenSource(
"test/ParameterizedModule_ProvideNonGenericTypeFactory"));
subject.generatedSource(
goldenFileRule.goldenSource(
"test/ParameterizedModule_ProvideNonGenericTypeWithDepsFactory"));
});
}
private static final Source QUALIFIER_A =
CompilerTests.javaSource(
"test.QualifierA",
"package test;",
"",
"import javax.inject.Qualifier;",
"",
"@Qualifier @interface QualifierA {}");
private static final Source QUALIFIER_B =
CompilerTests.javaSource(
"test.QualifierB",
"package test;",
"",
"import javax.inject.Qualifier;",
"",
"@Qualifier @interface QualifierB {}");
@Test
public void providesMethodMultipleQualifiersOnMethod() {
Source moduleFile =
CompilerTests.javaSource("test.TestModule",
"package test;",
"",
"import dagger.Module;",
"import dagger.Provides;",
"",
"@Module",
"final class TestModule {",
" @Provides",
" @QualifierA",
" @QualifierB",
" String provideString() {",
" return \"foo\";",
" }",
"}");
CompilerTests.daggerCompiler(moduleFile, QUALIFIER_A, QUALIFIER_B)
.compile(
subject -> {
// There are 2 errors -- 1 per qualifier.
subject.hasErrorCount(2);
subject.hasErrorContaining("may not use more than one @Qualifier")
.onSource(moduleFile)
.onLine(9);
subject.hasErrorContaining("may not use more than one @Qualifier")
.onSource(moduleFile)
.onLine(10);
});
}
@Test
public void providesMethodMultipleQualifiersOnParameter() {
Source moduleFile =
CompilerTests.javaSource(
"test.TestModule",
"package test;",
"",
"import dagger.Module;",
"import dagger.Provides;",
"",
"@Module",
"final class TestModule {",
" @Provides",
" static String provideString(",
" @QualifierA",
" @QualifierB",
" Object object) {",
" return \"foo\";",
" }",
"}");
CompilerTests.daggerCompiler(moduleFile, QUALIFIER_A, QUALIFIER_B)
.compile(
subject -> {
// There are two errors -- 1 per qualifier.