29
29
30
30
import org .junit .Test ;
31
31
import org .springframework .core .Ordered ;
32
+ import org .springframework .core .annotation .subpackage .NonPublicAnnotatedClass ;
32
33
import org .springframework .stereotype .Component ;
33
34
34
35
import static org .hamcrest .Matchers .*;
35
-
36
36
import static org .junit .Assert .*;
37
37
import static org .springframework .core .annotation .AnnotationUtils .*;
38
38
46
46
public class AnnotationUtilsTests {
47
47
48
48
@ Test
49
- public void testFindMethodAnnotationOnLeaf () throws SecurityException , NoSuchMethodException {
49
+ public void findMethodAnnotationOnLeaf () throws Exception {
50
50
Method m = Leaf .class .getMethod ("annotatedOnLeaf" , (Class []) null );
51
51
assertNotNull (m .getAnnotation (Order .class ));
52
52
assertNotNull (getAnnotation (m , Order .class ));
53
53
assertNotNull (findAnnotation (m , Order .class ));
54
54
}
55
55
56
56
@ Test
57
- public void testFindMethodAnnotationOnRoot () throws SecurityException , NoSuchMethodException {
57
+ public void findMethodAnnotationOnRoot () throws Exception {
58
58
Method m = Leaf .class .getMethod ("annotatedOnRoot" , (Class []) null );
59
59
assertNotNull (m .getAnnotation (Order .class ));
60
60
assertNotNull (getAnnotation (m , Order .class ));
61
61
assertNotNull (findAnnotation (m , Order .class ));
62
62
}
63
63
64
64
@ Test
65
- public void testFindMethodAnnotationOnRootButOverridden () throws SecurityException , NoSuchMethodException {
65
+ public void findMethodAnnotationOnRootButOverridden () throws Exception {
66
66
Method m = Leaf .class .getMethod ("overrideWithoutNewAnnotation" , (Class []) null );
67
67
assertNull (m .getAnnotation (Order .class ));
68
68
assertNull (getAnnotation (m , Order .class ));
69
69
assertNotNull (findAnnotation (m , Order .class ));
70
70
}
71
71
72
72
@ Test
73
- public void testFindMethodAnnotationNotAnnotated () throws SecurityException , NoSuchMethodException {
73
+ public void findMethodAnnotationNotAnnotated () throws Exception {
74
74
Method m = Leaf .class .getMethod ("notAnnotated" , (Class []) null );
75
75
assertNull (findAnnotation (m , Order .class ));
76
76
}
77
77
78
78
@ Test
79
- public void testFindMethodAnnotationOnBridgeMethod () throws Exception {
79
+ public void findMethodAnnotationOnBridgeMethod () throws Exception {
80
80
Method m = SimpleFoo .class .getMethod ("something" , Object .class );
81
81
assertTrue (m .isBridge ());
82
82
assertNull (m .getAnnotation (Order .class ));
@@ -88,7 +88,7 @@ public void testFindMethodAnnotationOnBridgeMethod() throws Exception {
88
88
}
89
89
90
90
// TODO consider whether we want this to handle annotations on interfaces
91
- // public void testFindMethodAnnotationFromInterfaceImplementedByRoot ()
91
+ // public void findMethodAnnotationFromInterfaceImplementedByRoot ()
92
92
// throws Exception {
93
93
// Method m = Leaf.class.getMethod("fromInterfaceImplementedByRoot",
94
94
// (Class[]) null);
@@ -241,7 +241,7 @@ public void testIsAnnotationInherited() throws Exception {
241
241
}
242
242
243
243
@ Test
244
- public void testGetValueFromAnnotation () throws Exception {
244
+ public void getValueFromAnnotation () throws Exception {
245
245
Method method = SimpleFoo .class .getMethod ("something" , Object .class );
246
246
Order order = findAnnotation (method , Order .class );
247
247
@@ -250,7 +250,18 @@ public void testGetValueFromAnnotation() throws Exception {
250
250
}
251
251
252
252
@ Test
253
- public void testGetDefaultValueFromAnnotation () throws Exception {
253
+ public void getValueFromNonPublicAnnotation () throws Exception {
254
+ Annotation [] declaredAnnotations = NonPublicAnnotatedClass .class .getDeclaredAnnotations ();
255
+ assertEquals (1 , declaredAnnotations .length );
256
+ Annotation annotation = declaredAnnotations [0 ];
257
+ assertNotNull (annotation );
258
+ assertEquals ("NonPublicAnnotation" , annotation .annotationType ().getSimpleName ());
259
+ assertEquals (42 , AnnotationUtils .getValue (annotation , AnnotationUtils .VALUE ));
260
+ assertEquals (42 , AnnotationUtils .getValue (annotation ));
261
+ }
262
+
263
+ @ Test
264
+ public void getDefaultValueFromAnnotation () throws Exception {
254
265
Method method = SimpleFoo .class .getMethod ("something" , Object .class );
255
266
Order order = findAnnotation (method , Order .class );
256
267
@@ -259,34 +270,46 @@ public void testGetDefaultValueFromAnnotation() throws Exception {
259
270
}
260
271
261
272
@ Test
262
- public void testGetDefaultValueFromAnnotationType () throws Exception {
273
+ public void getDefaultValueFromNonPublicAnnotation () throws Exception {
274
+ Annotation [] declaredAnnotations = NonPublicAnnotatedClass .class .getDeclaredAnnotations ();
275
+ assertEquals (1 , declaredAnnotations .length );
276
+ Annotation annotation = declaredAnnotations [0 ];
277
+ assertNotNull (annotation );
278
+ assertEquals ("NonPublicAnnotation" , annotation .annotationType ().getSimpleName ());
279
+ assertEquals (-1 , AnnotationUtils .getDefaultValue (annotation , AnnotationUtils .VALUE ));
280
+ assertEquals (-1 , AnnotationUtils .getDefaultValue (annotation ));
281
+ }
282
+
283
+ @ Test
284
+ public void getDefaultValueFromAnnotationType () throws Exception {
263
285
assertEquals (Ordered .LOWEST_PRECEDENCE , AnnotationUtils .getDefaultValue (Order .class , AnnotationUtils .VALUE ));
264
286
assertEquals (Ordered .LOWEST_PRECEDENCE , AnnotationUtils .getDefaultValue (Order .class ));
265
287
}
266
288
267
289
@ Test
268
- public void testFindAnnotationFromInterface () throws Exception {
290
+ public void findAnnotationFromInterface () throws Exception {
269
291
Method method = ImplementsInterfaceWithAnnotatedMethod .class .getMethod ("foo" );
270
292
Order order = findAnnotation (method , Order .class );
271
293
assertNotNull (order );
272
294
}
273
295
274
296
@ Test
275
- public void testFindAnnotationFromInterfaceOnSuper () throws Exception {
297
+ public void findAnnotationFromInterfaceOnSuper () throws Exception {
276
298
Method method = SubOfImplementsInterfaceWithAnnotatedMethod .class .getMethod ("foo" );
277
299
Order order = findAnnotation (method , Order .class );
278
300
assertNotNull (order );
279
301
}
280
302
281
303
@ Test
282
- public void testFindAnnotationFromInterfaceWhenSuperDoesNotImplementMethod () throws Exception {
304
+ public void findAnnotationFromInterfaceWhenSuperDoesNotImplementMethod ()
305
+ throws Exception {
283
306
Method method = SubOfAbstractImplementsInterfaceWithAnnotatedMethod .class .getMethod ("foo" );
284
307
Order order = findAnnotation (method , Order .class );
285
308
assertNotNull (order );
286
309
}
287
310
288
311
@ Test
289
- public void testGetRepeatableFromMethod () throws Exception {
312
+ public void getRepeatableFromMethod () throws Exception {
290
313
Method method = InterfaceWithRepeated .class .getMethod ("foo" );
291
314
Set <MyRepeatable > annotions = AnnotationUtils .getRepeatableAnnotation (method ,
292
315
MyRepeatableContainer .class , MyRepeatable .class );
@@ -319,6 +342,7 @@ static interface InterfaceWithMetaAnnotation {
319
342
static class ClassWithLocalMetaAnnotationAndMetaAnnotatedInterface implements InterfaceWithMetaAnnotation {
320
343
}
321
344
345
+
322
346
public static interface AnnotatedInterface {
323
347
324
348
@ Order (0 )
0 commit comments