1
1
/*
2
- * Copyright 2002-2012 the original author or authors.
2
+ * Copyright 2002-2013 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
16
16
17
17
package org .springframework .core .annotation ;
18
18
19
+ import static org .junit .Assert .*;
20
+ import static org .springframework .core .annotation .AnnotationUtils .*;
21
+
22
+ import java .lang .annotation .Annotation ;
19
23
import java .lang .annotation .Inherited ;
20
24
import java .lang .annotation .Retention ;
21
25
import java .lang .annotation .RetentionPolicy ;
22
26
import java .lang .reflect .Method ;
23
27
28
+ import java .util .Arrays ;
29
+ import java .util .List ;
30
+
24
31
import org .junit .Test ;
25
32
26
33
import org .springframework .core .Ordered ;
27
34
import org .springframework .stereotype .Component ;
28
35
29
- import static org .junit .Assert .*;
30
-
31
- import static org .springframework .core .annotation .AnnotationUtils .*;
32
-
33
36
/**
37
+ * Unit tests for {@link AnnotationUtils}.
38
+ *
34
39
* @author Rod Johnson
35
40
* @author Juergen Hoeller
36
41
* @author Sam Brannen
@@ -102,23 +107,91 @@ public void testFindAnnotationDeclaringClass() throws Exception {
102
107
assertNull (findAnnotationDeclaringClass (Transactional .class , NonAnnotatedClass .class ));
103
108
104
109
// inherited class-level annotation; note: @Transactional is inherited
105
- assertEquals (InheritedAnnotationInterface .class , findAnnotationDeclaringClass ( Transactional . class ,
106
- InheritedAnnotationInterface .class ));
110
+ assertEquals (InheritedAnnotationInterface .class ,
111
+ findAnnotationDeclaringClass ( Transactional . class , InheritedAnnotationInterface .class ));
107
112
assertNull (findAnnotationDeclaringClass (Transactional .class , SubInheritedAnnotationInterface .class ));
108
- assertEquals (InheritedAnnotationClass .class , findAnnotationDeclaringClass ( Transactional . class ,
109
- InheritedAnnotationClass .class ));
110
- assertEquals (InheritedAnnotationClass .class , findAnnotationDeclaringClass ( Transactional . class ,
111
- SubInheritedAnnotationClass .class ));
113
+ assertEquals (InheritedAnnotationClass .class ,
114
+ findAnnotationDeclaringClass ( Transactional . class , InheritedAnnotationClass .class ));
115
+ assertEquals (InheritedAnnotationClass .class ,
116
+ findAnnotationDeclaringClass ( Transactional . class , SubInheritedAnnotationClass .class ));
112
117
113
118
// non-inherited class-level annotation; note: @Order is not inherited,
114
- // but findAnnotationDeclaringClass() should still find it.
115
- assertEquals (NonInheritedAnnotationInterface .class , findAnnotationDeclaringClass ( Order . class ,
116
- NonInheritedAnnotationInterface .class ));
119
+ // but findAnnotationDeclaringClass() should still find it on classes .
120
+ assertEquals (NonInheritedAnnotationInterface .class ,
121
+ findAnnotationDeclaringClass ( Order . class , NonInheritedAnnotationInterface .class ));
117
122
assertNull (findAnnotationDeclaringClass (Order .class , SubNonInheritedAnnotationInterface .class ));
118
- assertEquals (NonInheritedAnnotationClass .class , findAnnotationDeclaringClass (Order .class ,
119
- NonInheritedAnnotationClass .class ));
120
- assertEquals (NonInheritedAnnotationClass .class , findAnnotationDeclaringClass (Order .class ,
121
- SubNonInheritedAnnotationClass .class ));
123
+ assertEquals (NonInheritedAnnotationClass .class ,
124
+ findAnnotationDeclaringClass (Order .class , NonInheritedAnnotationClass .class ));
125
+ assertEquals (NonInheritedAnnotationClass .class ,
126
+ findAnnotationDeclaringClass (Order .class , SubNonInheritedAnnotationClass .class ));
127
+ }
128
+
129
+ @ Test
130
+ public void findAnnotationDeclaringClassForTypesWithSingleCandidateType () {
131
+
132
+ // no class-level annotation
133
+ List <Class <? extends Annotation >> transactionalCandidateList = Arrays .<Class <? extends Annotation >> asList (Transactional .class );
134
+ assertNull (findAnnotationDeclaringClassForTypes (transactionalCandidateList , NonAnnotatedInterface .class ));
135
+ assertNull (findAnnotationDeclaringClassForTypes (transactionalCandidateList , NonAnnotatedClass .class ));
136
+
137
+ // inherited class-level annotation; note: @Transactional is inherited
138
+ assertEquals (InheritedAnnotationInterface .class ,
139
+ findAnnotationDeclaringClassForTypes (transactionalCandidateList , InheritedAnnotationInterface .class ));
140
+ assertNull (findAnnotationDeclaringClassForTypes (transactionalCandidateList ,
141
+ SubInheritedAnnotationInterface .class ));
142
+ assertEquals (InheritedAnnotationClass .class ,
143
+ findAnnotationDeclaringClassForTypes (transactionalCandidateList , InheritedAnnotationClass .class ));
144
+ assertEquals (InheritedAnnotationClass .class ,
145
+ findAnnotationDeclaringClassForTypes (transactionalCandidateList , SubInheritedAnnotationClass .class ));
146
+
147
+ // non-inherited class-level annotation; note: @Order is not inherited,
148
+ // but findAnnotationDeclaringClassForTypes() should still find it on classes.
149
+ List <Class <? extends Annotation >> orderCandidateList = Arrays .<Class <? extends Annotation >> asList (Order .class );
150
+ assertEquals (NonInheritedAnnotationInterface .class ,
151
+ findAnnotationDeclaringClassForTypes (orderCandidateList , NonInheritedAnnotationInterface .class ));
152
+ assertNull (findAnnotationDeclaringClassForTypes (orderCandidateList , SubNonInheritedAnnotationInterface .class ));
153
+ assertEquals (NonInheritedAnnotationClass .class ,
154
+ findAnnotationDeclaringClassForTypes (orderCandidateList , NonInheritedAnnotationClass .class ));
155
+ assertEquals (NonInheritedAnnotationClass .class ,
156
+ findAnnotationDeclaringClassForTypes (orderCandidateList , SubNonInheritedAnnotationClass .class ));
157
+ }
158
+
159
+ @ Test
160
+ public void findAnnotationDeclaringClassForTypesWithMultipleCandidateTypes () {
161
+
162
+ List <Class <? extends Annotation >> candidates = Arrays .<Class <? extends Annotation >> asList (Transactional .class ,
163
+ Order .class );
164
+
165
+ // no class-level annotation
166
+ assertNull (findAnnotationDeclaringClassForTypes (candidates , NonAnnotatedInterface .class ));
167
+ assertNull (findAnnotationDeclaringClassForTypes (candidates , NonAnnotatedClass .class ));
168
+
169
+ // inherited class-level annotation; note: @Transactional is inherited
170
+ assertEquals (InheritedAnnotationInterface .class ,
171
+ findAnnotationDeclaringClassForTypes (candidates , InheritedAnnotationInterface .class ));
172
+ assertNull (findAnnotationDeclaringClassForTypes (candidates , SubInheritedAnnotationInterface .class ));
173
+ assertEquals (InheritedAnnotationClass .class ,
174
+ findAnnotationDeclaringClassForTypes (candidates , InheritedAnnotationClass .class ));
175
+ assertEquals (InheritedAnnotationClass .class ,
176
+ findAnnotationDeclaringClassForTypes (candidates , SubInheritedAnnotationClass .class ));
177
+
178
+ // non-inherited class-level annotation; note: @Order is not inherited,
179
+ // but findAnnotationDeclaringClassForTypes() should still find it on classes.
180
+ assertEquals (NonInheritedAnnotationInterface .class ,
181
+ findAnnotationDeclaringClassForTypes (candidates , NonInheritedAnnotationInterface .class ));
182
+ assertNull (findAnnotationDeclaringClassForTypes (candidates , SubNonInheritedAnnotationInterface .class ));
183
+ assertEquals (NonInheritedAnnotationClass .class ,
184
+ findAnnotationDeclaringClassForTypes (candidates , NonInheritedAnnotationClass .class ));
185
+ assertEquals (NonInheritedAnnotationClass .class ,
186
+ findAnnotationDeclaringClassForTypes (candidates , SubNonInheritedAnnotationClass .class ));
187
+
188
+ // class hierarchy mixed with @Transactional and @Order declarations
189
+ assertEquals (TransactionalClass .class ,
190
+ findAnnotationDeclaringClassForTypes (candidates , TransactionalClass .class ));
191
+ assertEquals (TransactionalAndOrderedClass .class ,
192
+ findAnnotationDeclaringClassForTypes (candidates , TransactionalAndOrderedClass .class ));
193
+ assertEquals (TransactionalAndOrderedClass .class ,
194
+ findAnnotationDeclaringClassForTypes (candidates , SubTransactionalAndOrderedClass .class ));
122
195
}
123
196
124
197
@ Test
@@ -216,18 +289,18 @@ public void testFindAnnotationFromInterfaceWhenSuperDoesNotImplementMethod() thr
216
289
}
217
290
218
291
219
- @ Component (value = "meta1" )
292
+ @ Component (value = "meta1" )
220
293
@ Retention (RetentionPolicy .RUNTIME )
221
294
@interface Meta1 {
222
295
}
223
296
224
- @ Component (value = "meta2" )
297
+ @ Component (value = "meta2" )
225
298
@ Retention (RetentionPolicy .RUNTIME )
226
299
@interface Meta2 {
227
300
}
228
301
229
302
@ Meta1
230
- @ Component (value = "local" )
303
+ @ Component (value = "local" )
231
304
@ Meta2
232
305
static class HasLocalAndMetaComponentAnnotation {
233
306
}
@@ -332,6 +405,16 @@ public static class NonInheritedAnnotationClass {
332
405
public static class SubNonInheritedAnnotationClass extends NonInheritedAnnotationClass {
333
406
}
334
407
408
+ @ Transactional
409
+ public static class TransactionalClass {
410
+ }
411
+
412
+ @ Order
413
+ public static class TransactionalAndOrderedClass {
414
+ }
415
+
416
+ public static class SubTransactionalAndOrderedClass extends TransactionalAndOrderedClass {
417
+ }
335
418
336
419
public static interface InterfaceWithAnnotatedMethod {
337
420
@@ -353,10 +436,12 @@ public void foo() {
353
436
}
354
437
}
355
438
356
- public abstract static class AbstractDoesNotImplementInterfaceWithAnnotatedMethod implements InterfaceWithAnnotatedMethod {
439
+ public abstract static class AbstractDoesNotImplementInterfaceWithAnnotatedMethod implements
440
+ InterfaceWithAnnotatedMethod {
357
441
}
358
442
359
- public static class SubOfAbstractImplementsInterfaceWithAnnotatedMethod extends AbstractDoesNotImplementInterfaceWithAnnotatedMethod {
443
+ public static class SubOfAbstractImplementsInterfaceWithAnnotatedMethod extends
444
+ AbstractDoesNotImplementInterfaceWithAnnotatedMethod {
360
445
361
446
@ Override
362
447
public void foo () {
0 commit comments