AnnotationUtilsTests.java 18.6 KB
Newer Older
A
Arjen Poutsma 已提交
1
/*
2
 * Copyright 2002-2013 the original author or authors.
A
Arjen Poutsma 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * 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 org.springframework.core.annotation;

19
import java.lang.annotation.Annotation;
20
import java.lang.annotation.Inherited;
21
import java.lang.annotation.Repeatable;
22 23
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
A
Arjen Poutsma 已提交
24
import java.lang.reflect.Method;
25
import java.util.Arrays;
26
import java.util.HashSet;
27
import java.util.List;
28
import java.util.Set;
29

30
import org.junit.Test;
A
Arjen Poutsma 已提交
31
import org.springframework.core.Ordered;
32
import org.springframework.core.annotation.subpackage.NonPublicAnnotatedClass;
33
import org.springframework.stereotype.Component;
A
Arjen Poutsma 已提交
34

35
import static org.hamcrest.Matchers.*;
36 37 38
import static org.junit.Assert.*;
import static org.springframework.core.annotation.AnnotationUtils.*;

A
Arjen Poutsma 已提交
39 40 41 42
/**
 * @author Rod Johnson
 * @author Juergen Hoeller
 * @author Sam Brannen
43
 * @author Chris Beams
44
 * @author Phillip Webb
A
Arjen Poutsma 已提交
45
 */
46
public class AnnotationUtilsTests {
A
Arjen Poutsma 已提交
47

48
	@Test
49
	public void findMethodAnnotationOnLeaf() throws Exception {
50
		Method m = Leaf.class.getMethod("annotatedOnLeaf", (Class[]) null);
A
Arjen Poutsma 已提交
51 52 53 54 55
		assertNotNull(m.getAnnotation(Order.class));
		assertNotNull(getAnnotation(m, Order.class));
		assertNotNull(findAnnotation(m, Order.class));
	}

56
	@Test
57
	public void findMethodAnnotationOnRoot() throws Exception {
58
		Method m = Leaf.class.getMethod("annotatedOnRoot", (Class[]) null);
A
Arjen Poutsma 已提交
59 60 61 62 63
		assertNotNull(m.getAnnotation(Order.class));
		assertNotNull(getAnnotation(m, Order.class));
		assertNotNull(findAnnotation(m, Order.class));
	}

64
	@Test
65
	public void findMethodAnnotationOnRootButOverridden() throws Exception {
66
		Method m = Leaf.class.getMethod("overrideWithoutNewAnnotation", (Class[]) null);
A
Arjen Poutsma 已提交
67 68 69 70 71
		assertNull(m.getAnnotation(Order.class));
		assertNull(getAnnotation(m, Order.class));
		assertNotNull(findAnnotation(m, Order.class));
	}

72
	@Test
73
	public void findMethodAnnotationNotAnnotated() throws Exception {
74
		Method m = Leaf.class.getMethod("notAnnotated", (Class[]) null);
A
Arjen Poutsma 已提交
75 76 77
		assertNull(findAnnotation(m, Order.class));
	}

78
	@Test
79
	public void findMethodAnnotationOnBridgeMethod() throws Exception {
80
		Method m = SimpleFoo.class.getMethod("something", Object.class);
A
Arjen Poutsma 已提交
81 82 83 84
		assertTrue(m.isBridge());
		assertNull(m.getAnnotation(Order.class));
		assertNull(getAnnotation(m, Order.class));
		assertNotNull(findAnnotation(m, Order.class));
85
		// TODO: actually found on OpenJDK 8 b99! assertNull(m.getAnnotation(Transactional.class));
A
Arjen Poutsma 已提交
86 87 88 89 90
		assertNotNull(getAnnotation(m, Transactional.class));
		assertNotNull(findAnnotation(m, Transactional.class));
	}

	// TODO consider whether we want this to handle annotations on interfaces
91
	// public void findMethodAnnotationFromInterfaceImplementedByRoot()
A
Arjen Poutsma 已提交
92 93 94 95 96 97 98
	// throws Exception {
	// Method m = Leaf.class.getMethod("fromInterfaceImplementedByRoot",
	// (Class[]) null);
	// Order o = findAnnotation(Order.class, m, Leaf.class);
	// assertNotNull(o);
	// }

99 100 101 102 103 104 105 106 107 108 109 110
	@Test
	public void findAnnotationPrefersInteracesOverLocalMetaAnnotations() {
		Component component = AnnotationUtils.findAnnotation(
			ClassWithLocalMetaAnnotationAndMetaAnnotatedInterface.class, Component.class);

		// By inspecting ClassWithLocalMetaAnnotationAndMetaAnnotatedInterface, one
		// might expect that "meta2" should be found; however, with the current
		// implementation "meta1" will be found.
		assertNotNull(component);
		assertEquals("meta1", component.value());
	}

111
	@Test
A
Arjen Poutsma 已提交
112 113 114 115 116 117
	public void testFindAnnotationDeclaringClass() throws Exception {
		// no class-level annotation
		assertNull(findAnnotationDeclaringClass(Transactional.class, NonAnnotatedInterface.class));
		assertNull(findAnnotationDeclaringClass(Transactional.class, NonAnnotatedClass.class));

		// inherited class-level annotation; note: @Transactional is inherited
118 119
		assertEquals(InheritedAnnotationInterface.class,
			findAnnotationDeclaringClass(Transactional.class, InheritedAnnotationInterface.class));
A
Arjen Poutsma 已提交
120
		assertNull(findAnnotationDeclaringClass(Transactional.class, SubInheritedAnnotationInterface.class));
121 122 123 124
		assertEquals(InheritedAnnotationClass.class,
			findAnnotationDeclaringClass(Transactional.class, InheritedAnnotationClass.class));
		assertEquals(InheritedAnnotationClass.class,
			findAnnotationDeclaringClass(Transactional.class, SubInheritedAnnotationClass.class));
A
Arjen Poutsma 已提交
125 126

		// non-inherited class-level annotation; note: @Order is not inherited,
127 128 129
		// but findAnnotationDeclaringClass() should still find it on classes.
		assertEquals(NonInheritedAnnotationInterface.class,
			findAnnotationDeclaringClass(Order.class, NonInheritedAnnotationInterface.class));
A
Arjen Poutsma 已提交
130
		assertNull(findAnnotationDeclaringClass(Order.class, SubNonInheritedAnnotationInterface.class));
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
		assertEquals(NonInheritedAnnotationClass.class,
			findAnnotationDeclaringClass(Order.class, NonInheritedAnnotationClass.class));
		assertEquals(NonInheritedAnnotationClass.class,
			findAnnotationDeclaringClass(Order.class, SubNonInheritedAnnotationClass.class));
	}

	@Test
	public void findAnnotationDeclaringClassForTypesWithSingleCandidateType() {
		// no class-level annotation
		List<Class<? extends Annotation>> transactionalCandidateList = Arrays.<Class<? extends Annotation>> asList(Transactional.class);
		assertNull(findAnnotationDeclaringClassForTypes(transactionalCandidateList, NonAnnotatedInterface.class));
		assertNull(findAnnotationDeclaringClassForTypes(transactionalCandidateList, NonAnnotatedClass.class));

		// inherited class-level annotation; note: @Transactional is inherited
		assertEquals(InheritedAnnotationInterface.class,
			findAnnotationDeclaringClassForTypes(transactionalCandidateList, InheritedAnnotationInterface.class));
		assertNull(findAnnotationDeclaringClassForTypes(transactionalCandidateList,
148
				SubInheritedAnnotationInterface.class));
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
		assertEquals(InheritedAnnotationClass.class,
			findAnnotationDeclaringClassForTypes(transactionalCandidateList, InheritedAnnotationClass.class));
		assertEquals(InheritedAnnotationClass.class,
			findAnnotationDeclaringClassForTypes(transactionalCandidateList, SubInheritedAnnotationClass.class));

		// non-inherited class-level annotation; note: @Order is not inherited,
		// but findAnnotationDeclaringClassForTypes() should still find it on classes.
		List<Class<? extends Annotation>> orderCandidateList = Arrays.<Class<? extends Annotation>> asList(Order.class);
		assertEquals(NonInheritedAnnotationInterface.class,
			findAnnotationDeclaringClassForTypes(orderCandidateList, NonInheritedAnnotationInterface.class));
		assertNull(findAnnotationDeclaringClassForTypes(orderCandidateList, SubNonInheritedAnnotationInterface.class));
		assertEquals(NonInheritedAnnotationClass.class,
			findAnnotationDeclaringClassForTypes(orderCandidateList, NonInheritedAnnotationClass.class));
		assertEquals(NonInheritedAnnotationClass.class,
			findAnnotationDeclaringClassForTypes(orderCandidateList, SubNonInheritedAnnotationClass.class));
	}

	@Test
	public void findAnnotationDeclaringClassForTypesWithMultipleCandidateTypes() {
168
		List<Class<? extends Annotation>> candidates = Arrays.<Class<? extends Annotation>> asList(Transactional.class, Order.class);
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

		// no class-level annotation
		assertNull(findAnnotationDeclaringClassForTypes(candidates, NonAnnotatedInterface.class));
		assertNull(findAnnotationDeclaringClassForTypes(candidates, NonAnnotatedClass.class));

		// inherited class-level annotation; note: @Transactional is inherited
		assertEquals(InheritedAnnotationInterface.class,
			findAnnotationDeclaringClassForTypes(candidates, InheritedAnnotationInterface.class));
		assertNull(findAnnotationDeclaringClassForTypes(candidates, SubInheritedAnnotationInterface.class));
		assertEquals(InheritedAnnotationClass.class,
			findAnnotationDeclaringClassForTypes(candidates, InheritedAnnotationClass.class));
		assertEquals(InheritedAnnotationClass.class,
			findAnnotationDeclaringClassForTypes(candidates, SubInheritedAnnotationClass.class));

		// non-inherited class-level annotation; note: @Order is not inherited,
		// but findAnnotationDeclaringClassForTypes() should still find it on classes.
		assertEquals(NonInheritedAnnotationInterface.class,
			findAnnotationDeclaringClassForTypes(candidates, NonInheritedAnnotationInterface.class));
		assertNull(findAnnotationDeclaringClassForTypes(candidates, SubNonInheritedAnnotationInterface.class));
		assertEquals(NonInheritedAnnotationClass.class,
			findAnnotationDeclaringClassForTypes(candidates, NonInheritedAnnotationClass.class));
		assertEquals(NonInheritedAnnotationClass.class,
			findAnnotationDeclaringClassForTypes(candidates, SubNonInheritedAnnotationClass.class));

		// class hierarchy mixed with @Transactional and @Order declarations
		assertEquals(TransactionalClass.class,
			findAnnotationDeclaringClassForTypes(candidates, TransactionalClass.class));
		assertEquals(TransactionalAndOrderedClass.class,
			findAnnotationDeclaringClassForTypes(candidates, TransactionalAndOrderedClass.class));
		assertEquals(TransactionalAndOrderedClass.class,
			findAnnotationDeclaringClassForTypes(candidates, SubTransactionalAndOrderedClass.class));
A
Arjen Poutsma 已提交
200 201
	}

202
	@Test
A
Arjen Poutsma 已提交
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
	public void testIsAnnotationDeclaredLocally() throws Exception {
		// no class-level annotation
		assertFalse(isAnnotationDeclaredLocally(Transactional.class, NonAnnotatedInterface.class));
		assertFalse(isAnnotationDeclaredLocally(Transactional.class, NonAnnotatedClass.class));

		// inherited class-level annotation; note: @Transactional is inherited
		assertTrue(isAnnotationDeclaredLocally(Transactional.class, InheritedAnnotationInterface.class));
		assertFalse(isAnnotationDeclaredLocally(Transactional.class, SubInheritedAnnotationInterface.class));
		assertTrue(isAnnotationDeclaredLocally(Transactional.class, InheritedAnnotationClass.class));
		assertFalse(isAnnotationDeclaredLocally(Transactional.class, SubInheritedAnnotationClass.class));

		// non-inherited class-level annotation; note: @Order is not inherited
		assertTrue(isAnnotationDeclaredLocally(Order.class, NonInheritedAnnotationInterface.class));
		assertFalse(isAnnotationDeclaredLocally(Order.class, SubNonInheritedAnnotationInterface.class));
		assertTrue(isAnnotationDeclaredLocally(Order.class, NonInheritedAnnotationClass.class));
		assertFalse(isAnnotationDeclaredLocally(Order.class, SubNonInheritedAnnotationClass.class));
	}

221
	@Test
A
Arjen Poutsma 已提交
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
	public void testIsAnnotationInherited() throws Exception {
		// no class-level annotation
		assertFalse(isAnnotationInherited(Transactional.class, NonAnnotatedInterface.class));
		assertFalse(isAnnotationInherited(Transactional.class, NonAnnotatedClass.class));

		// inherited class-level annotation; note: @Transactional is inherited
		assertFalse(isAnnotationInherited(Transactional.class, InheritedAnnotationInterface.class));
		// isAnnotationInherited() does not currently traverse interface
		// hierarchies. Thus the following, though perhaps counter intuitive,
		// must be false:
		assertFalse(isAnnotationInherited(Transactional.class, SubInheritedAnnotationInterface.class));
		assertFalse(isAnnotationInherited(Transactional.class, InheritedAnnotationClass.class));
		assertTrue(isAnnotationInherited(Transactional.class, SubInheritedAnnotationClass.class));

		// non-inherited class-level annotation; note: @Order is not inherited
		assertFalse(isAnnotationInherited(Order.class, NonInheritedAnnotationInterface.class));
		assertFalse(isAnnotationInherited(Order.class, SubNonInheritedAnnotationInterface.class));
		assertFalse(isAnnotationInherited(Order.class, NonInheritedAnnotationClass.class));
		assertFalse(isAnnotationInherited(Order.class, SubNonInheritedAnnotationClass.class));
	}

243
	@Test
244
	public void getValueFromAnnotation() throws Exception {
245 246
		Method method = SimpleFoo.class.getMethod("something", Object.class);
		Order order = findAnnotation(method, Order.class);
A
Arjen Poutsma 已提交
247 248 249 250 251

		assertEquals(1, AnnotationUtils.getValue(order, AnnotationUtils.VALUE));
		assertEquals(1, AnnotationUtils.getValue(order));
	}

252
	@Test
253 254 255 256 257 258 259 260 261 262 263 264
	public void getValueFromNonPublicAnnotation() throws Exception {
		Annotation[] declaredAnnotations = NonPublicAnnotatedClass.class.getDeclaredAnnotations();
		assertEquals(1, declaredAnnotations.length);
		Annotation annotation = declaredAnnotations[0];
		assertNotNull(annotation);
		assertEquals("NonPublicAnnotation", annotation.annotationType().getSimpleName());
		assertEquals(42, AnnotationUtils.getValue(annotation, AnnotationUtils.VALUE));
		assertEquals(42, AnnotationUtils.getValue(annotation));
	}

	@Test
	public void getDefaultValueFromAnnotation() throws Exception {
265 266
		Method method = SimpleFoo.class.getMethod("something", Object.class);
		Order order = findAnnotation(method, Order.class);
A
Arjen Poutsma 已提交
267 268 269 270 271

		assertEquals(Ordered.LOWEST_PRECEDENCE, AnnotationUtils.getDefaultValue(order, AnnotationUtils.VALUE));
		assertEquals(Ordered.LOWEST_PRECEDENCE, AnnotationUtils.getDefaultValue(order));
	}

272
	@Test
273 274 275 276 277 278 279 280 281 282 283 284
	public void getDefaultValueFromNonPublicAnnotation() throws Exception {
		Annotation[] declaredAnnotations = NonPublicAnnotatedClass.class.getDeclaredAnnotations();
		assertEquals(1, declaredAnnotations.length);
		Annotation annotation = declaredAnnotations[0];
		assertNotNull(annotation);
		assertEquals("NonPublicAnnotation", annotation.annotationType().getSimpleName());
		assertEquals(-1, AnnotationUtils.getDefaultValue(annotation, AnnotationUtils.VALUE));
		assertEquals(-1, AnnotationUtils.getDefaultValue(annotation));
	}

	@Test
	public void getDefaultValueFromAnnotationType() throws Exception {
A
Arjen Poutsma 已提交
285 286 287 288
		assertEquals(Ordered.LOWEST_PRECEDENCE, AnnotationUtils.getDefaultValue(Order.class, AnnotationUtils.VALUE));
		assertEquals(Ordered.LOWEST_PRECEDENCE, AnnotationUtils.getDefaultValue(Order.class));
	}

289
	@Test
290
	public void findAnnotationFromInterface() throws Exception {
291 292 293 294 295 296
		Method method = ImplementsInterfaceWithAnnotatedMethod.class.getMethod("foo");
		Order order = findAnnotation(method, Order.class);
		assertNotNull(order);
	}

	@Test
297
	public void findAnnotationFromInterfaceOnSuper() throws Exception {
298 299 300 301 302
		Method method = SubOfImplementsInterfaceWithAnnotatedMethod.class.getMethod("foo");
		Order order = findAnnotation(method, Order.class);
		assertNotNull(order);
	}

303
	@Test
304 305
	public void findAnnotationFromInterfaceWhenSuperDoesNotImplementMethod()
			throws Exception {
306 307 308 309 310
		Method method = SubOfAbstractImplementsInterfaceWithAnnotatedMethod.class.getMethod("foo");
		Order order = findAnnotation(method, Order.class);
		assertNotNull(order);
	}

311
	@Test
312
	public void getRepeatableFromMethod() throws Exception {
313 314 315 316 317 318 319 320 321 322 323
		Method method = InterfaceWithRepeated.class.getMethod("foo");
		Set<MyRepeatable> annotions = AnnotationUtils.getRepeatableAnnotation(method,
				MyRepeatableContainer.class, MyRepeatable.class);
		Set<String> values = new HashSet<String>();
		for (MyRepeatable myRepeatable : annotions) {
			values.add(myRepeatable.value());
		}
		assertThat(values, equalTo((Set<String>) new HashSet<String>(
				Arrays.asList("a", "b", "c", "meta"))));
	}

324

325
	@Component(value = "meta1")
326
	@Order
327 328 329 330
	@Retention(RetentionPolicy.RUNTIME)
	@interface Meta1 {
	}

331
	@Component(value = "meta2")
332
	@Transactional
333 334 335 336 337
	@Retention(RetentionPolicy.RUNTIME)
	@interface Meta2 {
	}

	@Meta1
338 339 340
	static interface InterfaceWithMetaAnnotation {
	}

341
	@Meta2
342
	static class ClassWithLocalMetaAnnotationAndMetaAnnotatedInterface implements InterfaceWithMetaAnnotation {
343 344
	}

345

A
Arjen Poutsma 已提交
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
	public static interface AnnotatedInterface {

		@Order(0)
		void fromInterfaceImplementedByRoot();
	}

	public static class Root implements AnnotatedInterface {

		@Order(27)
		public void annotatedOnRoot() {

		}

		public void overrideToAnnotate() {

		}

		@Order(27)
		public void overrideWithoutNewAnnotation() {

		}

		public void notAnnotated() {

		}

372
		@Override
A
Arjen Poutsma 已提交
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
		public void fromInterfaceImplementedByRoot() {

		}
	}

	public static class Leaf extends Root {

		@Order(25)
		public void annotatedOnLeaf() {

		}

		@Override
		@Order(1)
		public void overrideToAnnotate() {

		}

		@Override
		public void overrideWithoutNewAnnotation() {

		}
	}

	public static abstract class Foo<T> {

		@Order(1)
		public abstract void something(T arg);
	}

	public static class SimpleFoo extends Foo<String> {

405
		@Override
A
Arjen Poutsma 已提交
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
		@Transactional
		public void something(final String arg) {

		}
	}

	@Transactional
	public static interface InheritedAnnotationInterface {
	}

	public static interface SubInheritedAnnotationInterface extends InheritedAnnotationInterface {
	}

	@Order
	public static interface NonInheritedAnnotationInterface {
	}

	public static interface SubNonInheritedAnnotationInterface extends NonInheritedAnnotationInterface {
	}

	public static class NonAnnotatedClass {
	}

	public static interface NonAnnotatedInterface {
	}

	@Transactional
	public static class InheritedAnnotationClass {
	}

	public static class SubInheritedAnnotationClass extends InheritedAnnotationClass {
	}

	@Order
	public static class NonInheritedAnnotationClass {
	}

	public static class SubNonInheritedAnnotationClass extends NonInheritedAnnotationClass {
	}

446 447 448 449 450 451 452 453 454 455
	@Transactional
	public static class TransactionalClass {
	}

	@Order
	public static class TransactionalAndOrderedClass {
	}

	public static class SubTransactionalAndOrderedClass extends TransactionalAndOrderedClass {
	}
456 457 458 459 460 461 462 463 464

	public static interface InterfaceWithAnnotatedMethod {

		@Order
		void foo();
	}

	public static class ImplementsInterfaceWithAnnotatedMethod implements InterfaceWithAnnotatedMethod {

465
		@Override
466 467 468 469 470 471
		public void foo() {
		}
	}

	public static class SubOfImplementsInterfaceWithAnnotatedMethod extends ImplementsInterfaceWithAnnotatedMethod {

472
		@Override
473 474 475 476
		public void foo() {
		}
	}

477 478
	public abstract static class AbstractDoesNotImplementInterfaceWithAnnotatedMethod implements
			InterfaceWithAnnotatedMethod {
479 480
	}

481 482
	public static class SubOfAbstractImplementsInterfaceWithAnnotatedMethod extends
			AbstractDoesNotImplementInterfaceWithAnnotatedMethod {
483

484
		@Override
485 486 487 488
		public void foo() {
		}
	}

489 490 491 492 493 494 495 496 497
	public static interface InterfaceWithRepeated {

		@MyRepeatable("a")
		@MyRepeatableContainer({ @MyRepeatable("b"), @MyRepeatable("c") })
		@MyRepeatableMeta
		void foo();

	}

A
Arjen Poutsma 已提交
498
}
499

500

501 502 503
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@interface Transactional {
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


@Retention(RetentionPolicy.RUNTIME)
@Inherited
@interface MyRepeatableContainer {

	MyRepeatable[] value();

}


@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Repeatable(MyRepeatableContainer.class)
@interface MyRepeatable {

	String value();

}


@Retention(RetentionPolicy.RUNTIME)
@Inherited
@MyRepeatable("meta")
@interface MyRepeatableMeta {
}