/
AnnotationUtilsTests.java
1023 lines (811 loc) · 30.7 KB
/
AnnotationUtilsTests.java
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 2015-2023 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v2.0 which
* accompanies this distribution and is available at
*
* https://www.eclipse.org/legal/epl-v20.html
*/
package org.junit.platform.commons.util;
import static java.util.stream.Collectors.toList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertIterableEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.platform.commons.util.AnnotationUtils.findAnnotatedFields;
import static org.junit.platform.commons.util.AnnotationUtils.findAnnotatedMethods;
import static org.junit.platform.commons.util.AnnotationUtils.findAnnotation;
import static org.junit.platform.commons.util.AnnotationUtils.findPublicAnnotatedFields;
import static org.junit.platform.commons.util.AnnotationUtils.findRepeatableAnnotations;
import static org.junit.platform.commons.util.AnnotationUtils.isAnnotated;
import static org.junit.platform.commons.util.ReflectionUtils.HierarchyTraversalMode.BOTTOM_UP;
import static org.junit.platform.commons.util.ReflectionUtils.HierarchyTraversalMode.TOP_DOWN;
import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.platform.commons.PreconditionViolationException;
import org.junit.platform.commons.util.pkg1.ClassLevelDir;
import org.junit.platform.commons.util.pkg1.InstanceLevelDir;
import org.junit.platform.commons.util.pkg1.SuperclassWithStaticPackagePrivateBeforeMethod;
import org.junit.platform.commons.util.pkg1.SuperclassWithStaticPackagePrivateTempDirField;
import org.junit.platform.commons.util.pkg1.subpkg.SubclassWithNonStaticPackagePrivateBeforeMethod;
import org.junit.platform.commons.util.pkg1.subpkg.SubclassWithNonStaticPackagePrivateTempDirField;
/**
* Unit tests for {@link AnnotationUtils}.
*
* @since 1.0
*/
class AnnotationUtilsTests {
private static final Predicate<Field> isStringField = field -> field.getType() == String.class;
@Test
void findAnnotationForNullOptional() {
assertThat(findAnnotation((Optional<AnnotatedElement>) null, Annotation1.class)).isEmpty();
}
@Test
void findAnnotationForEmptyOptional() {
assertThat(findAnnotation(Optional.empty(), Annotation1.class)).isEmpty();
}
@Test
void findAnnotationForNullAnnotatedElement() {
assertThat(findAnnotation((AnnotatedElement) null, Annotation1.class)).isEmpty();
}
@Test
void findAnnotationOnClassWithoutAnnotation() {
assertThat(findAnnotation(Annotation1Class.class, Annotation2.class)).isNotPresent();
}
@Test
void findAnnotationIndirectlyPresentOnOptionalClass() {
Optional<Class<?>> optional = Optional.of(SubInheritedAnnotationClass.class);
assertThat(findAnnotation(optional, InheritedAnnotation.class)).isPresent();
}
@Test
void findAnnotationIndirectlyPresentOnClass() {
assertThat(findAnnotation(SubInheritedAnnotationClass.class, InheritedAnnotation.class)).isPresent();
}
/**
* Test for https://github.com/junit-team/junit5/issues/1133
*/
@Test
void findInheritedAnnotationMetaPresentOnNonInheritedComposedAnnotationPresentOnSuperclass() {
assertThat(findAnnotation(SubNonInheritedCompositionOfInheritedAnnotationClass.class,
InheritedAnnotation.class)).isPresent();
}
@Test
void findAnnotationDirectlyPresentOnClass() {
assertThat(findAnnotation(Annotation1Class.class, Annotation1.class)).isPresent();
}
@Test
void findAnnotationMetaPresentOnClass() {
assertThat(findAnnotation(ComposedAnnotationClass.class, Annotation1.class)).isPresent();
}
/**
* <b>Note:</b> there is no findAnnotationIndirectlyMetaPresentOnMethod
* counterpart because the {@code @Inherited} annotation has no effect if
* the annotation type is used to annotate anything other than a class.
*
* @see Inherited
*/
@Test
void findAnnotationIndirectlyMetaPresentOnClass() {
assertThat(findAnnotation(SubInheritedComposedAnnotationClass.class, Annotation1.class)).isPresent();
}
@Test
void findAnnotationDirectlyPresentOnImplementedInterface() {
assertThat(findAnnotation(TestingTraitClass.class, Annotation1.class)).isPresent();
}
@Test
void findAnnotationMetaPresentOnImplementedInterface() {
assertThat(findAnnotation(ComposedTestingTraitClass.class, Annotation1.class)).isPresent();
}
@Test
void findAnnotationDirectlyPresentOnMethod() throws Exception {
var method = Annotation2Class.class.getDeclaredMethod("method");
assertThat(findAnnotation(method, Annotation1.class)).isPresent();
}
@Test
void findAnnotationMetaPresentOnMethod() throws Exception {
var method = ComposedAnnotationClass.class.getDeclaredMethod("method");
assertThat(findAnnotation(method, Annotation1.class)).isPresent();
}
@Test
void findAnnotationMetaPresentOnOptionalMethod() throws Exception {
var method = ComposedAnnotationClass.class.getDeclaredMethod("method");
assertThat(findAnnotation(Optional.of(method), Annotation1.class)).isPresent();
}
@Test
void findAnnotationDirectlyPresentOnEnclosingClass() throws Exception {
Class<?> clazz = Annotation1Class.InnerClass.class;
assertThat(findAnnotation(clazz, Annotation1.class, false)).isNotPresent();
assertThat(findAnnotation(clazz, Annotation1.class, true)).isPresent();
clazz = Annotation1Class.InnerClass.InnerInnerClass.class;
assertThat(findAnnotation(clazz, Annotation1.class, false)).isNotPresent();
assertThat(findAnnotation(clazz, Annotation1.class, true)).isPresent();
clazz = Annotation1Class.NestedClass.class;
assertThat(findAnnotation(clazz, Annotation1.class, false)).isNotPresent();
assertThat(findAnnotation(clazz, Annotation1.class, true)).isNotPresent();
}
@Test
void findAnnotationMetaPresentOnEnclosingClass() throws Exception {
Class<?> clazz = ComposedAnnotationClass.InnerClass.class;
assertThat(findAnnotation(clazz, Annotation1.class, false)).isNotPresent();
assertThat(findAnnotation(clazz, Annotation1.class, true)).isPresent();
clazz = ComposedAnnotationClass.InnerClass.InnerInnerClass.class;
assertThat(findAnnotation(clazz, Annotation1.class, false)).isNotPresent();
assertThat(findAnnotation(clazz, Annotation1.class, true)).isPresent();
}
@Test
void isAnnotatedForClassWithoutAnnotation() {
assertFalse(isAnnotated(Annotation1Class.class, Annotation2.class));
}
@Test
void isAnnotatedWhenIndirectlyPresentOnClass() {
assertTrue(isAnnotated(SubInheritedAnnotationClass.class, InheritedAnnotation.class));
}
@Test
void isAnnotatedWhenDirectlyPresentOnClass() {
assertTrue(isAnnotated(Annotation1Class.class, Annotation1.class));
}
@Test
void isAnnotatedWhenMetaPresentOnClass() {
assertTrue(isAnnotated(ComposedAnnotationClass.class, Annotation1.class));
}
@Test
void isAnnotatedWhenDirectlyPresentOnMethod() throws Exception {
assertTrue(isAnnotated(Annotation2Class.class.getDeclaredMethod("method"), Annotation1.class));
}
@Test
void isAnnotatedWhenMetaPresentOnMethod() throws Exception {
assertTrue(isAnnotated(ComposedAnnotationClass.class.getDeclaredMethod("method"), Annotation1.class));
}
@Test
void findRepeatableAnnotationsForNotRepeatableAnnotation() {
var exception = assertThrows(PreconditionViolationException.class,
() -> findRepeatableAnnotations(getClass(), Inherited.class));
assertThat(exception.getMessage()).isEqualTo(Inherited.class.getName() + " must be @Repeatable");
}
@Test
void findRepeatableAnnotationsForNullOptionalAnnotatedElement() {
assertThat(findRepeatableAnnotations((Optional<AnnotatedElement>) null, Tag.class)).isEmpty();
}
@Test
void findRepeatableAnnotationsForEmptyOptionalAnnotatedElement() {
assertThat(findRepeatableAnnotations(Optional.empty(), Tag.class)).isEmpty();
}
@Test
void findRepeatableAnnotationsForNullAnnotatedElement() {
assertThat(findRepeatableAnnotations((AnnotatedElement) null, Tag.class)).isEmpty();
}
@Test
void findRepeatableAnnotationsWithSingleTag() {
assertTagsFound(SingleTaggedClass.class, "a");
}
@Test
void findRepeatableAnnotationsWithSingleComposedTag() {
assertTagsFound(SingleComposedTaggedClass.class, "fast");
}
@Test
void findRepeatableAnnotationsWithSingleComposedTagOnImplementedInterface() {
assertTagsFound(TaggedInterfaceClass.class, "fast");
}
@Test
void findRepeatableAnnotationsWithLocalComposedTagAndComposedTagOnImplementedInterface() {
assertTagsFound(LocalTagOnTaggedInterfaceClass.class, "fast", "smoke");
}
@Test
void findRepeatableAnnotationsWithMultipleTags() {
assertTagsFound(MultiTaggedClass.class, "a", "b", "c");
}
@Test
void findRepeatableAnnotationsWithMultipleComposedTags() {
assertTagsFound(MultiComposedTaggedClass.class, "fast", "smoke");
assertTagsFound(FastAndSmokyTaggedClass.class, "fast", "smoke");
}
@Test
void findRepeatableAnnotationsWithContainer() {
assertTagsFound(ContainerTaggedClass.class, "a", "b", "c", "d");
}
@Test
void findRepeatableAnnotationsWithComposedTagBeforeContainer() {
assertTagsFound(ContainerAfterComposedTaggedClass.class, "fast", "a", "b", "c");
}
private void assertTagsFound(Class<?> clazz, String... tags) {
assertEquals(List.of(tags),
findRepeatableAnnotations(clazz, Tag.class).stream().map(Tag::value).collect(toList()),
() -> "Tags found for class " + clazz.getName());
}
@Test
void findInheritedRepeatableAnnotationsWithSingleAnnotationOnSuperclass() {
assertExtensionsFound(SingleExtensionClass.class, "a");
assertExtensionsFound(SubSingleExtensionClass.class, "a");
}
@Test
void findInheritedRepeatableAnnotationsWithMultipleAnnotationsOnSuperclass() {
assertExtensionsFound(MultiExtensionClass.class, "a", "b", "c");
assertExtensionsFound(SubMultiExtensionClass.class, "a", "b", "c", "x", "y", "z");
}
@Test
void findInheritedRepeatableAnnotationsWithContainerAnnotationOnSuperclass() {
assertExtensionsFound(ContainerExtensionClass.class, "a", "b", "c");
assertExtensionsFound(SubContainerExtensionClass.class, "a", "b", "c", "x");
}
@Test
void findInheritedRepeatableAnnotationsWithSingleComposedAnnotation() {
assertExtensionsFound(SingleComposedExtensionClass.class, "foo");
}
/**
* @since 1.5
*/
@Test
void findInheritedRepeatableAnnotationsWithComposedAnnotationsInNestedContainer() {
assertExtensionsFound(MultipleFoos1.class, "foo");
assertExtensionsFound(MultipleFoos2.class, "foo");
}
@Test
void findInheritedRepeatableAnnotationsWithSingleComposedAnnotationOnSuperclass() {
assertExtensionsFound(SubSingleComposedExtensionClass.class, "foo");
}
@Test
void findInheritedRepeatableAnnotationsWithMultipleComposedAnnotations() {
assertExtensionsFound(MultiComposedExtensionClass.class, "foo", "bar");
}
@Test
void findInheritedRepeatableAnnotationsWithMultipleComposedAnnotationsOnSuperclass() {
assertExtensionsFound(SubMultiComposedExtensionClass.class, "foo", "bar");
}
@Test
void findInheritedRepeatableAnnotationsWithMultipleComposedAnnotationsOnSuperclassAndLocalContainerAndComposed() {
assertExtensionsFound(ContainerPlusSubMultiComposedExtensionClass.class, "foo", "bar", "x", "y", "z");
}
private void assertExtensionsFound(Class<?> clazz, String... tags) {
assertEquals(List.of(tags),
findRepeatableAnnotations(clazz, ExtendWith.class).stream().map(ExtendWith::value).collect(toList()),
() -> "Extensions found for class " + clazz.getName());
}
@Test
void findAnnotatedMethodsForNullClass() {
assertThrows(PreconditionViolationException.class,
() -> findAnnotatedMethods(null, Annotation1.class, TOP_DOWN));
}
@Test
void findAnnotatedMethodsForNullAnnotationType() {
assertThrows(PreconditionViolationException.class,
() -> findAnnotatedMethods(ClassWithAnnotatedMethods.class, null, TOP_DOWN));
}
@Test
void findAnnotatedMethodsForAnnotationThatIsNotPresent() {
assertThat(findAnnotatedMethods(ClassWithAnnotatedMethods.class, Fast.class, TOP_DOWN)).isEmpty();
}
@Test
void findAnnotatedMethodsForAnnotationOnMethodsInClassUsingHierarchyDownMode() throws Exception {
var method2 = ClassWithAnnotatedMethods.class.getDeclaredMethod("method2");
var method3 = ClassWithAnnotatedMethods.class.getDeclaredMethod("method3");
var methods = findAnnotatedMethods(ClassWithAnnotatedMethods.class, Annotation2.class, TOP_DOWN);
assertThat(methods).containsOnly(method2, method3);
}
@Test
void findAnnotatedMethodsForAnnotationOnMethodsInClassHierarchyUsingHierarchyUpMode() throws Exception {
var method1 = ClassWithAnnotatedMethods.class.getDeclaredMethod("method1");
var method3 = ClassWithAnnotatedMethods.class.getDeclaredMethod("method3");
var superMethod = SuperclassWithAnnotatedMethod.class.getDeclaredMethod("superMethod");
var methods = findAnnotatedMethods(ClassWithAnnotatedMethods.class, Annotation1.class, BOTTOM_UP);
assertEquals(3, methods.size());
assertThat(methods.subList(0, 2)).containsOnly(method1, method3);
assertEquals(superMethod, methods.get(2));
}
@Test
void findAnnotatedMethodsForAnnotationUsedInClassAndSuperclassHierarchyDown() throws Exception {
var method1 = ClassWithAnnotatedMethods.class.getDeclaredMethod("method1");
var method3 = ClassWithAnnotatedMethods.class.getDeclaredMethod("method3");
var superMethod = SuperclassWithAnnotatedMethod.class.getDeclaredMethod("superMethod");
var methods = findAnnotatedMethods(ClassWithAnnotatedMethods.class, Annotation1.class, TOP_DOWN);
assertEquals(3, methods.size());
assertEquals(superMethod, methods.get(0));
assertThat(methods.subList(1, 3)).containsOnly(method1, method3);
}
/**
* @see https://github.com/junit-team/junit5/issues/3553
*/
@Disabled("Until #3553 is resolved")
@Test
void findAnnotatedMethodsDoesNotAllowInstanceMethodToHideStaticMethod() throws Exception {
final String BEFORE = "before";
Class<?> superclass = SuperclassWithStaticPackagePrivateBeforeMethod.class;
Method beforeAllMethod = superclass.getDeclaredMethod(BEFORE);
Class<?> subclass = SubclassWithNonStaticPackagePrivateBeforeMethod.class;
Method beforeEachMethod = subclass.getDeclaredMethod(BEFORE);
// Prerequisite
var methods = findAnnotatedMethods(superclass, BeforeAll.class, TOP_DOWN);
assertThat(methods).containsExactly(beforeAllMethod);
// Actual use cases for this test
methods = findAnnotatedMethods(subclass, BeforeAll.class, TOP_DOWN);
assertThat(methods).containsExactly(beforeAllMethod);
methods = findAnnotatedMethods(subclass, BeforeEach.class, TOP_DOWN);
assertThat(methods).containsExactly(beforeEachMethod);
}
@Test
void findAnnotatedMethodsForAnnotationUsedInInterface() throws Exception {
var interfaceMethod = InterfaceWithAnnotatedDefaultMethod.class.getDeclaredMethod("interfaceMethod");
var methods = findAnnotatedMethods(ClassWithAnnotatedMethods.class, Annotation3.class, BOTTOM_UP);
assertThat(methods).containsExactly(interfaceMethod);
}
// === findAnnotatedFields() ===============================================
@Test
void findAnnotatedFieldsForNullClass() {
assertThrows(PreconditionViolationException.class,
() -> findAnnotatedFields(null, Annotation1.class, isStringField, TOP_DOWN));
}
@Test
void findAnnotatedFieldsForNullAnnotationType() {
assertThrows(PreconditionViolationException.class,
() -> findAnnotatedFields(ClassWithAnnotatedFields.class, null, isStringField, TOP_DOWN));
}
@Test
void findAnnotatedFieldsForNullPredicate() {
assertThrows(PreconditionViolationException.class,
() -> findAnnotatedFields(ClassWithAnnotatedFields.class, Annotation1.class, null, TOP_DOWN));
}
@Test
void findAnnotatedFieldsForAnnotationThatIsNotPresent() {
assertThat(findAnnotatedFields(ClassWithAnnotatedFields.class, Fast.class, isStringField, TOP_DOWN)).isEmpty();
}
@Test
void findAnnotatedFieldsForAnnotationOnFieldsInClassUsingHierarchyDownMode() throws Exception {
var field2 = ClassWithAnnotatedFields.class.getDeclaredField("field2");
var field3 = ClassWithAnnotatedFields.class.getDeclaredField("field3");
var fields = findAnnotatedFields(ClassWithAnnotatedFields.class, Annotation2.class, isStringField, TOP_DOWN);
assertThat(fields).containsOnly(field2, field3);
}
@Test
void findAnnotatedFieldsForAnnotationOnFieldsInClassHierarchyUsingHierarchyUpMode() throws Exception {
var field1 = ClassWithAnnotatedFields.class.getDeclaredField("field1");
var field3 = ClassWithAnnotatedFields.class.getDeclaredField("field3");
var superField = SuperclassWithAnnotatedField.class.getDeclaredField("superField");
var fields = findAnnotatedFields(ClassWithAnnotatedFields.class, Annotation1.class, isStringField, BOTTOM_UP);
assertEquals(3, fields.size());
assertThat(fields.subList(0, 2)).containsOnly(field1, field3);
assertEquals(superField, fields.get(2));
}
@Test
void findAnnotatedFieldsForAnnotationUsedInClassAndSuperclassHierarchyDown() throws Exception {
var field1 = ClassWithAnnotatedFields.class.getDeclaredField("field1");
var field3 = ClassWithAnnotatedFields.class.getDeclaredField("field3");
var superField = SuperclassWithAnnotatedField.class.getDeclaredField("superField");
var fields = findAnnotatedFields(ClassWithAnnotatedFields.class, Annotation1.class, isStringField, TOP_DOWN);
assertEquals(3, fields.size());
assertEquals(superField, fields.get(0));
assertThat(fields.subList(1, 3)).containsOnly(field1, field3);
}
@Test
void findAnnotatedFieldsForAnnotationUsedInInterface() throws Exception {
var interfaceField = InterfaceWithAnnotatedField.class.getDeclaredField("interfaceField");
var fields = findAnnotatedFields(ClassWithAnnotatedFields.class, Annotation3.class, isStringField, BOTTOM_UP);
assertThat(fields).containsExactly(interfaceField);
}
@Test
void findAnnotatedFieldsForShadowedFields() throws Exception {
Class<?> clazz = ClassWithShadowedAnnotatedFields.class;
var interfaceField = clazz.getDeclaredField("interfaceField");
var superField = clazz.getDeclaredField("superField");
var field1 = clazz.getDeclaredField("field1");
var field2 = clazz.getDeclaredField("field2");
var field3 = clazz.getDeclaredField("field3");
assertThat(findShadowingAnnotatedFields(Annotation1.class)).containsExactly(superField, field1, field3);
assertThat(findShadowingAnnotatedFields(Annotation2.class)).containsExactly(field2, field3);
assertThat(findShadowingAnnotatedFields(Annotation3.class)).containsExactly(interfaceField);
}
private List<Field> findShadowingAnnotatedFields(Class<? extends Annotation> annotationType) {
return findAnnotatedFields(ClassWithShadowedAnnotatedFields.class, annotationType, isStringField);
}
/**
* @see https://github.com/junit-team/junit5/issues/3553
*/
@Disabled("Until #3553 is resolved")
@Test
void findAnnotatedFieldsDoesNotAllowInstanceFieldToHideStaticField() throws Exception {
final String TEMP_DIR = "tempDir";
Class<?> superclass = SuperclassWithStaticPackagePrivateTempDirField.class;
Field staticField = superclass.getDeclaredField(TEMP_DIR);
Class<?> subclass = SubclassWithNonStaticPackagePrivateTempDirField.class;
Field nonStaticField = subclass.getDeclaredField(TEMP_DIR);
// Prerequisite
var fields = findAnnotatedFields(superclass, ClassLevelDir.class, field -> true);
assertThat(fields).containsExactly(staticField);
// Actual use cases for this test
fields = findAnnotatedFields(subclass, ClassLevelDir.class, field -> true);
assertThat(fields).containsExactly(staticField);
fields = findAnnotatedFields(subclass, InstanceLevelDir.class, field -> true);
assertThat(fields).containsExactly(nonStaticField);
}
// === findPublicAnnotatedFields() =========================================
@Test
void findPublicAnnotatedFieldsForNullClass() {
assertThrows(PreconditionViolationException.class,
() -> findPublicAnnotatedFields(null, String.class, Annotation1.class));
}
@Test
void findPublicAnnotatedFieldsForNullFieldType() {
assertThrows(PreconditionViolationException.class,
() -> findPublicAnnotatedFields(getClass(), null, Annotation1.class));
}
@Test
void findPublicAnnotatedFieldsForNullAnnotationType() {
assertThrows(PreconditionViolationException.class,
() -> findPublicAnnotatedFields(getClass(), String.class, null));
}
@Test
void findPublicAnnotatedFieldsForPrivateField() {
var fields = findPublicAnnotatedFields(getClass(), Boolean.class, Annotation1.class);
assertNotNull(fields);
assertEquals(0, fields.size());
}
@Test
void findPublicAnnotatedFieldsForDirectlyAnnotatedFieldOfWrongFieldType() {
var fields = findPublicAnnotatedFields(getClass(), BigDecimal.class, Annotation1.class);
assertNotNull(fields);
assertEquals(0, fields.size());
}
@Test
void findPublicAnnotatedFieldsForDirectlyAnnotatedField() {
var fields = findPublicAnnotatedFields(getClass(), String.class, Annotation1.class);
assertNotNull(fields);
assertIterableEquals(List.of("directlyAnnotatedField"), asNames(fields));
}
@Test
void findPublicAnnotatedFieldsForMetaAnnotatedField() {
var fields = findPublicAnnotatedFields(getClass(), Number.class, Annotation1.class);
assertNotNull(fields);
assertEquals(1, fields.size());
assertIterableEquals(List.of("metaAnnotatedField"), asNames(fields));
}
@Test
void findPublicAnnotatedFieldsForDirectlyAnnotatedFieldInInterface() {
var fields = findPublicAnnotatedFields(InterfaceWithAnnotatedFields.class, String.class, Annotation1.class);
assertNotNull(fields);
assertIterableEquals(List.of("foo"), asNames(fields));
}
@Test
void findPublicAnnotatedFieldsForDirectlyAnnotatedFieldsInClassAndInterface() {
var fields = findPublicAnnotatedFields(ClassWithPublicAnnotatedFieldsFromInterface.class, String.class,
Annotation1.class);
assertNotNull(fields);
assertThat(asNames(fields)).containsExactlyInAnyOrder("foo", "bar");
}
private List<String> asNames(List<Field> fields) {
return fields.stream().map(Field::getName).collect(toList());
}
// -------------------------------------------------------------------------
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface AnnotationWithDefaultValue {
String value() default "default";
}
@Target({ ElementType.TYPE, ElementType.METHOD, ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
@interface Annotation1 {
}
@Target({ ElementType.TYPE, ElementType.METHOD, ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
@interface Annotation2 {
}
@Target({ ElementType.TYPE, ElementType.METHOD, ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
@interface Annotation3 {
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@interface InheritedAnnotation {
}
@Target({ ElementType.TYPE, ElementType.METHOD, ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
@Annotation1
@interface ComposedAnnotation {
}
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Annotation1
@Inherited
@interface InheritedComposedAnnotation {
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@InheritedAnnotation
// DO NOT make this @Inherited.
@interface NonInheritedCompositionOfInheritedAnnotation {
}
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
// DO NOT make this @Inherited.
@interface Tags {
Tag[] value();
}
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(Tags.class)
// DO NOT make this @Inherited.
@interface Tag {
String value();
}
@Retention(RetentionPolicy.RUNTIME)
@Tag("fast")
@interface Fast {
}
@Retention(RetentionPolicy.RUNTIME)
@Tag("smoke")
@interface Smoke {
}
@Retention(RetentionPolicy.RUNTIME)
@Fast
@Smoke
@interface FastAndSmoky {
}
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@interface Extensions {
ExtendWith[] value();
}
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Repeatable(Extensions.class)
@interface ExtendWith {
String value();
}
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@ExtendWith("foo")
@Repeatable(FooExtensions.class)
@interface ExtendWithFoo {
String info() default "";
}
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@interface FooExtensions {
ExtendWithFoo[] value();
}
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
// Intentionally NOT @Inherited in order to ensure that the algorithm for
// findRepeatableAnnotations() in fact lives up to the claims in the
// Javadoc regarding searching for repeatable annotations with implicit
// "inheritance" if the repeatable annotation is @Inherited but the
// custom composed annotation is not @Inherited.
// @Inherited
@ExtendWith("bar")
@interface ExtendWithBar {
}
@AnnotationWithDefaultValue
static class AnnotationWithDefaultValueClass {
}
@Annotation1
static class Annotation1Class {
class InnerClass {
class InnerInnerClass {
}
}
static class NestedClass {
}
}
@Annotation2
static class Annotation2Class {
@Annotation1
void method() {
}
}
@InheritedAnnotation
static class InheritedAnnotationClass {
}
static class SubInheritedAnnotationClass extends InheritedAnnotationClass {
}
@ComposedAnnotation
static class ComposedAnnotationClass {
@ComposedAnnotation
void method() {
}
class InnerClass {
class InnerInnerClass {
}
}
}
@InheritedComposedAnnotation
static class InheritedComposedAnnotationClass {
@InheritedComposedAnnotation
void method() {
}
}
static class SubInheritedComposedAnnotationClass extends InheritedComposedAnnotationClass {
}
@NonInheritedCompositionOfInheritedAnnotation
static class NonInheritedCompositionOfInheritedAnnotationClass {
}
static class SubNonInheritedCompositionOfInheritedAnnotationClass
extends NonInheritedCompositionOfInheritedAnnotationClass {
}
@Annotation1
interface TestingTrait {
}
static class TestingTraitClass implements TestingTrait {
}
@ComposedAnnotation
interface ComposedTestingTrait {
}
static class ComposedTestingTraitClass implements ComposedTestingTrait {
}
@Tag("a")
static class SingleTaggedClass {
}
@Fast
static class SingleComposedTaggedClass {
}
@Tag("a")
@Tag("b")
@Tag("c")
static class MultiTaggedClass {
}
@Fast
@Smoke
static class MultiComposedTaggedClass {
}
@FastAndSmoky
static class FastAndSmokyTaggedClass {
}
@Fast
interface TaggedInterface {
}
static class TaggedInterfaceClass implements TaggedInterface {
}
@Smoke
static class LocalTagOnTaggedInterfaceClass implements TaggedInterface {
}
@Tags({ @Tag("a"), @Tag("b"), @Tag("c") })
@Tag("d")
static class ContainerTaggedClass {
}
@Fast
@Tags({ @Tag("a"), @Tag("b"), @Tag("c") })
static class ContainerAfterComposedTaggedClass {
}
@ExtendWith("a")
static class SingleExtensionClass {
}
static class SubSingleExtensionClass extends SingleExtensionClass {
}
@ExtendWith("a")
@ExtendWith("b")
@ExtendWith("c")
static class MultiExtensionClass {
}
@ExtendWith("x")
@ExtendWith("y")
@ExtendWith("b") // duplicates parent
@ExtendWith("z")
@ExtendWith("a") // duplicates parent
static class SubMultiExtensionClass extends MultiExtensionClass {
}
@Extensions({ @ExtendWith("a"), @ExtendWith("b"), @ExtendWith("c"), @ExtendWith("a") })
static class ContainerExtensionClass {
}
@ExtendWith("x")
static class SubContainerExtensionClass extends ContainerExtensionClass {
}
@ExtendWithFoo
static class SingleComposedExtensionClass {
}
@ExtendWithFoo(info = "A")
@ExtendWithFoo(info = "B")
static class MultipleFoos1 {
}
@FooExtensions({ @ExtendWithFoo(info = "A"), @ExtendWithFoo(info = "B") })
static class MultipleFoos2 {
}
static class SubSingleComposedExtensionClass extends SingleComposedExtensionClass {
}
@ExtendWithFoo
@ExtendWithBar
static class MultiComposedExtensionClass {
}
static class SubMultiComposedExtensionClass extends MultiComposedExtensionClass {
}
@ExtendWith("x")
@Extensions({ @ExtendWith("y"), @ExtendWith("z") })
@ExtendWithBar
static class ContainerPlusSubMultiComposedExtensionClass extends MultiComposedExtensionClass {
}
interface InterfaceWithAnnotatedDefaultMethod {
@Annotation3
default void interfaceMethod() {
}
}
static class SuperclassWithAnnotatedMethod {
@Annotation1
void superMethod() {
}
}
static class ClassWithAnnotatedMethods extends SuperclassWithAnnotatedMethod
implements InterfaceWithAnnotatedDefaultMethod {
@Annotation1
void method1() {
}
@Annotation2
void method2() {
}
@Annotation1
@Annotation2
void method3() {
}
}
// -------------------------------------------------------------------------
interface InterfaceWithAnnotatedField {
@Annotation3
String interfaceField = "interface";
}
static class SuperclassWithAnnotatedField {
@Annotation1
String superField = "super";
}
static class ClassWithAnnotatedFields extends SuperclassWithAnnotatedField implements InterfaceWithAnnotatedField {
@Annotation1
protected Object field0 = "?";
@Annotation1
protected String field1 = "foo";
@Annotation2
protected String field2 = "bar";
@Annotation1
@Annotation2
protected String field3 = "baz";
}
static class ClassWithShadowedAnnotatedFields extends ClassWithAnnotatedFields {
@Annotation3
String interfaceField = "interface-shadow";
@Annotation1
String superField = "super-shadow";
@Annotation1
protected String field1 = "foo-shadow";
@Annotation2
protected String field2 = "bar-shadow";
@Annotation1
@Annotation2
protected String field3 = "baz-shadow";
}
// -------------------------------------------------------------------------
@Annotation1
private Boolean privateDirectlyAnnotatedField;
@Annotation1
public String directlyAnnotatedField;