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

19 20 21 22 23 24 25 26 27 28 29 30 31 32
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.springframework.core.annotation.AnnotationUtils.findAnnotation;
import static org.springframework.core.annotation.AnnotationUtils.findAnnotationDeclaringClass;
import static org.springframework.core.annotation.AnnotationUtils.getAnnotation;
import static org.springframework.core.annotation.AnnotationUtils.isAnnotationDeclaredLocally;
import static org.springframework.core.annotation.AnnotationUtils.isAnnotationInherited;

import java.io.IOException;
33 34 35
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
A
Arjen Poutsma 已提交
36
import java.lang.reflect.Method;
37 38
import java.util.List;
import java.util.Map;
A
Arjen Poutsma 已提交
39

40
import org.junit.Test;
A
Arjen Poutsma 已提交
41
import org.springframework.core.Ordered;
42
import org.springframework.core.type.classreading.SimpleMetadataReaderFactory;
43
import org.springframework.stereotype.Component;
A
Arjen Poutsma 已提交
44 45 46 47 48

/**
 * @author Rod Johnson
 * @author Juergen Hoeller
 * @author Sam Brannen
49
 * @author Chris Beams
A
Arjen Poutsma 已提交
50
 */
51
public class AnnotationUtilsTests {
A
Arjen Poutsma 已提交
52

53
	@Test
A
Arjen Poutsma 已提交
54 55 56 57 58 59 60 61
	public void testFindMethodAnnotationOnLeaf() throws SecurityException, NoSuchMethodException {

		final Method m = Leaf.class.getMethod("annotatedOnLeaf", (Class[]) null);
		assertNotNull(m.getAnnotation(Order.class));
		assertNotNull(getAnnotation(m, Order.class));
		assertNotNull(findAnnotation(m, Order.class));
	}

62
	@Test
A
Arjen Poutsma 已提交
63 64 65 66 67 68 69 70
	public void testFindMethodAnnotationOnRoot() throws SecurityException, NoSuchMethodException {

		final Method m = Leaf.class.getMethod("annotatedOnRoot", (Class[]) null);
		assertNotNull(m.getAnnotation(Order.class));
		assertNotNull(getAnnotation(m, Order.class));
		assertNotNull(findAnnotation(m, Order.class));
	}

71
	@Test
A
Arjen Poutsma 已提交
72 73 74 75 76 77 78 79
	public void testFindMethodAnnotationOnRootButOverridden() throws SecurityException, NoSuchMethodException {

		final Method m = Leaf.class.getMethod("overrideWithoutNewAnnotation", (Class[]) null);
		assertNull(m.getAnnotation(Order.class));
		assertNull(getAnnotation(m, Order.class));
		assertNotNull(findAnnotation(m, Order.class));
	}

80
	@Test
A
Arjen Poutsma 已提交
81 82 83 84 85 86
	public void testFindMethodAnnotationNotAnnotated() throws SecurityException, NoSuchMethodException {

		final Method m = Leaf.class.getMethod("notAnnotated", (Class[]) null);
		assertNull(findAnnotation(m, Order.class));
	}

87
	@Test
A
Arjen Poutsma 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
	public void testFindMethodAnnotationOnBridgeMethod() throws Exception {

		final Method m = SimpleFoo.class.getMethod("something", Object.class);
		assertTrue(m.isBridge());
		assertNull(m.getAnnotation(Order.class));
		assertNull(getAnnotation(m, Order.class));
		assertNotNull(findAnnotation(m, Order.class));
		assertNull(m.getAnnotation(Transactional.class));
		assertNotNull(getAnnotation(m, Transactional.class));
		assertNotNull(findAnnotation(m, Transactional.class));
	}

	// TODO consider whether we want this to handle annotations on interfaces
	// public void testFindMethodAnnotationFromInterfaceImplementedByRoot()
	// throws Exception {
	// Method m = Leaf.class.getMethod("fromInterfaceImplementedByRoot",
	// (Class[]) null);
	// Order o = findAnnotation(Order.class, m, Leaf.class);
	// assertNotNull(o);
	// }

109
	@Test
A
Arjen Poutsma 已提交
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
	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
		assertEquals(InheritedAnnotationInterface.class, findAnnotationDeclaringClass(Transactional.class,
				InheritedAnnotationInterface.class));
		assertNull(findAnnotationDeclaringClass(Transactional.class, SubInheritedAnnotationInterface.class));
		assertEquals(InheritedAnnotationClass.class, findAnnotationDeclaringClass(Transactional.class,
				InheritedAnnotationClass.class));
		assertEquals(InheritedAnnotationClass.class, findAnnotationDeclaringClass(Transactional.class,
				SubInheritedAnnotationClass.class));

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

136
	@Test
A
Arjen Poutsma 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
	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));
	}

156
	@Test
A
Arjen Poutsma 已提交
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
	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));
	}

179
	@Test
A
Arjen Poutsma 已提交
180 181 182 183 184 185 186 187 188
	public void testGetValueFromAnnotation() throws Exception {

		final Method method = SimpleFoo.class.getMethod("something", Object.class);
		final Order order = findAnnotation(method, Order.class);

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

189
	@Test
A
Arjen Poutsma 已提交
190 191 192 193 194 195 196 197 198
	public void testGetDefaultValueFromAnnotation() throws Exception {

		final Method method = SimpleFoo.class.getMethod("something", Object.class);
		final Order order = findAnnotation(method, Order.class);

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

199
	@Test
A
Arjen Poutsma 已提交
200 201 202 203 204 205
	public void testGetDefaultValueFromAnnotationType() throws Exception {

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

206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
	@Test
	public void testFindAnnotationFromInterface() throws Exception {

		Method method = ImplementsInterfaceWithAnnotatedMethod.class.getMethod("foo");
		Order order = findAnnotation(method, Order.class);
		assertNotNull(order);
	}

	@Test
	public void testFindAnnotationFromInterfaceOnSuper() throws Exception {

		Method method = SubOfImplementsInterfaceWithAnnotatedMethod.class.getMethod("foo");
		Order order = findAnnotation(method, Order.class);
		assertNotNull(order);
	}

222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238

	@Component(value="meta1")
	@Retention(RetentionPolicy.RUNTIME)
	@interface Meta1 {
	}

	@Component(value="meta2")
	@Retention(RetentionPolicy.RUNTIME)
	@interface Meta2 {
	}

	@Meta1
	@Component(value="local")
	@Meta2
	static class HasLocalAndMetaComponentAnnotation {
	}

A
Arjen Poutsma 已提交
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
	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() {

		}

		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> {

		@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 {
	}

337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355

	public static interface InterfaceWithAnnotatedMethod {

		@Order
		void foo();
	}

	public static class ImplementsInterfaceWithAnnotatedMethod implements InterfaceWithAnnotatedMethod {

		public void foo() {
		}
	}

	public static class SubOfImplementsInterfaceWithAnnotatedMethod extends ImplementsInterfaceWithAnnotatedMethod {

		public void foo() {
		}
	}

A
Arjen Poutsma 已提交
356
}
357 358 359 360

@Retention(RetentionPolicy.RUNTIME)
@Inherited
@interface Transactional {
O
Oliver Gierke 已提交
361
	
362
}