Support arbitrary meta-annotation levels in the TCF

Prior to this commit, the findAnnotationDescriptor() and
findAnnotationDescriptorForTypes() methods in MetaAnnotationUtils only
supported a single level of meta-annotations. In particular, this kept
the following annotations from being used as meta-annotations on
meta-annotations:

- @ContextConfiguration
- @ContextHierarchy
- @ActiveProfiles
- @TestExecutionListeners

This commit alters the search algorithms used in MetaAnnotationUtils so
that arbitrary levels of meta-annotations are now supported for the
aforementioned test-related annotations.

Issue: SPR-11470
This commit is contained in:
Sam Brannen
2014-02-23 01:05:15 +01:00
parent 2c8f25a14c
commit f8950960f2
9 changed files with 542 additions and 70 deletions

View File

@@ -47,28 +47,48 @@ public class MetaAnnotationUtilsTests {
private void assertAtComponentOnComposedAnnotation(Class<?> startClass, Class<?> rootDeclaringClass, String name,
Class<? extends Annotation> composedAnnotationType) {
assertAtComponentOnComposedAnnotation(rootDeclaringClass, rootDeclaringClass, composedAnnotationType, name,
composedAnnotationType);
}
private void assertAtComponentOnComposedAnnotation(Class<?> startClass, Class<?> rootDeclaringClass,
Class<?> declaringClass, String name, Class<? extends Annotation> composedAnnotationType) {
AnnotationDescriptor<Component> descriptor = findAnnotationDescriptor(startClass, Component.class);
assertNotNull(descriptor);
assertEquals(rootDeclaringClass, descriptor.getRootDeclaringClass());
assertEquals(composedAnnotationType, descriptor.getDeclaringClass());
assertEquals(Component.class, descriptor.getAnnotationType());
assertEquals(name, descriptor.getAnnotation().value());
assertNotNull(descriptor.getComposedAnnotation());
assertEquals(composedAnnotationType, descriptor.getComposedAnnotationType());
assertNotNull("AnnotationDescriptor should not be null", descriptor);
assertEquals("rootDeclaringClass", rootDeclaringClass, descriptor.getRootDeclaringClass());
assertEquals("declaringClass", declaringClass, descriptor.getDeclaringClass());
assertEquals("annotationType", Component.class, descriptor.getAnnotationType());
assertEquals("component name", name, descriptor.getAnnotation().value());
assertNotNull("composedAnnotation should not be null", descriptor.getComposedAnnotation());
assertEquals("composedAnnotationType", composedAnnotationType, descriptor.getComposedAnnotationType());
}
private void assertAtComponentOnComposedAnnotationForMultipleCandidateTypes(Class<?> startClass, String name,
Class<? extends Annotation> composedAnnotationType) {
assertAtComponentOnComposedAnnotationForMultipleCandidateTypes(startClass, startClass, name,
composedAnnotationType);
}
private void assertAtComponentOnComposedAnnotationForMultipleCandidateTypes(Class<?> startClass,
Class<?> rootDeclaringClass, String name, Class<? extends Annotation> composedAnnotationType) {
assertAtComponentOnComposedAnnotationForMultipleCandidateTypes(startClass, rootDeclaringClass,
composedAnnotationType, name, composedAnnotationType);
}
@SuppressWarnings("unchecked")
private void assertAtComponentOnComposedAnnotationForMultipleCandidateTypes(Class<?> startClass,
Class<?> declaringClass, String name, Class<? extends Annotation> composedAnnotationType) {
Class<?> rootDeclaringClass, Class<?> declaringClass, String name,
Class<? extends Annotation> composedAnnotationType) {
Class<Component> annotationType = Component.class;
UntypedAnnotationDescriptor descriptor = findAnnotationDescriptorForTypes(startClass, Service.class,
annotationType, Order.class, Transactional.class);
assertNotNull(descriptor);
assertEquals(declaringClass, descriptor.getRootDeclaringClass());
assertEquals(annotationType, descriptor.getAnnotationType());
assertEquals(name, ((Component) descriptor.getAnnotation()).value());
assertNotNull(descriptor.getComposedAnnotation());
assertEquals(composedAnnotationType, descriptor.getComposedAnnotationType());
assertNotNull("UntypedAnnotationDescriptor should not be null", descriptor);
assertEquals("rootDeclaringClass", rootDeclaringClass, descriptor.getRootDeclaringClass());
assertEquals("declaringClass", declaringClass, descriptor.getDeclaringClass());
assertEquals("annotationType", annotationType, descriptor.getAnnotationType());
assertEquals("component name", name, ((Component) descriptor.getAnnotation()).value());
assertNotNull("composedAnnotation should not be null", descriptor.getComposedAnnotation());
assertEquals("composedAnnotationType", composedAnnotationType, descriptor.getComposedAnnotationType());
}
@Test
@@ -150,6 +170,45 @@ public class MetaAnnotationUtilsTests {
ClassWithLocalMetaAnnotationAndMetaAnnotatedInterface.class, "meta2", Meta2.class);
}
/**
* @since 4.0.3
*/
@Test
public void findAnnotationDescriptorOnMetaMetaAnnotatedClass() {
Class<MetaMetaAnnotatedClass> startClass = MetaMetaAnnotatedClass.class;
assertAtComponentOnComposedAnnotation(startClass, startClass, Meta2.class, "meta2", MetaMeta.class);
}
/**
* @since 4.0.3
*/
@Test
public void findAnnotationDescriptorOnMetaMetaMetaAnnotatedClass() {
Class<MetaMetaMetaAnnotatedClass> startClass = MetaMetaMetaAnnotatedClass.class;
assertAtComponentOnComposedAnnotation(startClass, startClass, Meta2.class, "meta2", MetaMetaMeta.class);
}
/**
* @since 4.0.3
*/
@Test
public void findAnnotationDescriptorOnAnnotatedClassWithMissingTargetMetaAnnotation() {
// InheritedAnnotationClass is NOT annotated or meta-annotated with @Component
AnnotationDescriptor<Component> descriptor = findAnnotationDescriptor(InheritedAnnotationClass.class,
Component.class);
assertNull("Should not find @Component on InheritedAnnotationClass", descriptor);
}
/**
* @since 4.0.3
*/
@Test
public void findAnnotationDescriptorOnMetaCycleAnnotatedClassWithMissingTargetMetaAnnotation() {
AnnotationDescriptor<Component> descriptor = findAnnotationDescriptor(MetaCycleAnnotatedClass.class,
Component.class);
assertNull("Should not find @Component on MetaCycleAnnotatedClass", descriptor);
}
// -------------------------------------------------------------------------
@Test
@@ -217,7 +276,7 @@ public class MetaAnnotationUtilsTests {
@Test
public void findAnnotationDescriptorForTypesWithMetaComponentAnnotation() throws Exception {
Class<HasMetaComponentAnnotation> startClass = HasMetaComponentAnnotation.class;
assertAtComponentOnComposedAnnotationForMultipleCandidateTypes(startClass, startClass, "meta1", Meta1.class);
assertAtComponentOnComposedAnnotationForMultipleCandidateTypes(startClass, "meta1", Meta1.class);
}
@Test
@@ -261,7 +320,7 @@ public class MetaAnnotationUtilsTests {
@Test
public void findAnnotationDescriptorForTypesForInterfaceWithMetaAnnotation() {
Class<InterfaceWithMetaAnnotation> startClass = InterfaceWithMetaAnnotation.class;
assertAtComponentOnComposedAnnotationForMultipleCandidateTypes(startClass, startClass, "meta1", Meta1.class);
assertAtComponentOnComposedAnnotationForMultipleCandidateTypes(startClass, "meta1", Meta1.class);
}
@Test
@@ -274,7 +333,7 @@ public class MetaAnnotationUtilsTests {
@Test
public void findAnnotationDescriptorForTypesForClassWithLocalMetaAnnotationAndMetaAnnotatedInterface() {
Class<ClassWithLocalMetaAnnotationAndMetaAnnotatedInterface> startClass = ClassWithLocalMetaAnnotationAndMetaAnnotatedInterface.class;
assertAtComponentOnComposedAnnotationForMultipleCandidateTypes(startClass, startClass, "meta2", Meta2.class);
assertAtComponentOnComposedAnnotationForMultipleCandidateTypes(startClass, "meta2", Meta2.class);
}
@Test
@@ -284,6 +343,50 @@ public class MetaAnnotationUtilsTests {
ClassWithLocalMetaAnnotationAndMetaAnnotatedInterface.class, "meta2", Meta2.class);
}
/**
* @since 4.0.3
*/
@Test
public void findAnnotationDescriptorForTypesOnMetaMetaAnnotatedClass() {
Class<MetaMetaAnnotatedClass> startClass = MetaMetaAnnotatedClass.class;
assertAtComponentOnComposedAnnotationForMultipleCandidateTypes(startClass, startClass, Meta2.class, "meta2",
MetaMeta.class);
}
/**
* @since 4.0.3
*/
@Test
public void findAnnotationDescriptorForTypesOnMetaMetaMetaAnnotatedClass() {
Class<MetaMetaMetaAnnotatedClass> startClass = MetaMetaMetaAnnotatedClass.class;
assertAtComponentOnComposedAnnotationForMultipleCandidateTypes(startClass, startClass, Meta2.class, "meta2",
MetaMetaMeta.class);
}
/**
* @since 4.0.3
*/
@Test
@SuppressWarnings("unchecked")
public void findAnnotationDescriptorForTypesOnAnnotatedClassWithMissingTargetMetaAnnotation() {
// InheritedAnnotationClass is NOT annotated or meta-annotated with @Component,
// @Service, or @Order, but it is annotated with @Transactional.
UntypedAnnotationDescriptor descriptor = findAnnotationDescriptorForTypes(InheritedAnnotationClass.class,
Service.class, Component.class, Order.class);
assertNull("Should not find @Component on InheritedAnnotationClass", descriptor);
}
/**
* @since 4.0.3
*/
@Test
@SuppressWarnings("unchecked")
public void findAnnotationDescriptorForTypesOnMetaCycleAnnotatedClassWithMissingTargetMetaAnnotation() {
UntypedAnnotationDescriptor descriptor = findAnnotationDescriptorForTypes(MetaCycleAnnotatedClass.class,
Service.class, Component.class, Order.class);
assertNull("Should not find @Component on MetaCycleAnnotatedClass", descriptor);
}
// -------------------------------------------------------------------------
@@ -299,6 +402,31 @@ public class MetaAnnotationUtilsTests {
static @interface Meta2 {
}
@Meta2
@Retention(RetentionPolicy.RUNTIME)
@interface MetaMeta {
}
@MetaMeta
@Retention(RetentionPolicy.RUNTIME)
@interface MetaMetaMeta {
}
@MetaCycle3
@Retention(RetentionPolicy.RUNTIME)
@interface MetaCycle1 {
}
@MetaCycle1
@Retention(RetentionPolicy.RUNTIME)
@interface MetaCycle2 {
}
@MetaCycle2
@Retention(RetentionPolicy.RUNTIME)
@interface MetaCycle3 {
}
@ContextConfiguration
@Retention(RetentionPolicy.RUNTIME)
static @interface MetaConfig {
@@ -340,6 +468,18 @@ public class MetaAnnotationUtilsTests {
ClassWithLocalMetaAnnotationAndMetaAnnotatedInterface {
}
@MetaMeta
static class MetaMetaAnnotatedClass {
}
@MetaMetaMeta
static class MetaMetaMetaAnnotatedClass {
}
@MetaCycle3
static class MetaCycleAnnotatedClass {
}
@MetaConfig
public class MetaConfigWithDefaultAttributesTestCase {
}

View File

@@ -0,0 +1,62 @@
/*
* Copyright 2002-2014 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.test.context.hierarchies.meta;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.ContextHierarchy;
/**
* Custom context hierarchy configuration annotation.
*
* @author Sam Brannen
* @since 4.0.3
*/
@ContextHierarchy(@ContextConfiguration(classes = { DevConfig.class, ProductionConfig.class }))
@ActiveProfiles("dev")
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface MetaContextHierarchyConfig {
}
@Configuration
@Profile("dev")
class DevConfig {
@Bean
public String foo() {
return "Dev Foo";
}
}
@Configuration
@Profile("prod")
class ProductionConfig {
@Bean
public String foo() {
return "Production Foo";
}
}

View File

@@ -0,0 +1,43 @@
/*
* Copyright 2002-2014 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.test.context.hierarchies.meta;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.*;
/**
* @author Sam Brannen
* @since 4.0.3
*/
@RunWith(SpringJUnit4ClassRunner.class)
@MetaMetaContextHierarchyConfig
public class MetaHierarchyLevelOneTests {
@Autowired
private String foo;
@Test
public void foo() {
assertEquals("Dev Foo", foo);
}
}

View File

@@ -0,0 +1,68 @@
/*
* Copyright 2002-2014 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.test.context.hierarchies.meta;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import static org.junit.Assert.*;
/**
* @author Sam Brannen
* @since 4.0.3
*/
@ContextConfiguration
@ActiveProfiles("prod")
public class MetaHierarchyLevelTwoTests extends MetaHierarchyLevelOneTests {
@Configuration
@Profile("prod")
static class Config {
@Bean
public String bar() {
return "Prod Bar";
}
}
@Autowired
protected ApplicationContext context;
@Autowired
private String bar;
@Test
public void bar() {
assertEquals("Prod Bar", bar);
}
@Test
public void contextHierarchy() {
assertNotNull("child ApplicationContext", context);
assertNotNull("parent ApplicationContext", context.getParent());
assertNull("grandparent ApplicationContext", context.getParent().getParent());
}
}

View File

@@ -0,0 +1,36 @@
/*
* Copyright 2002-2014 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.test.context.hierarchies.meta;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Custom context hierarchy configuration annotation that is itself meta-annotated
* with {@link MetaContextHierarchyConfig @MetaContextHierarchyConfig}.
*
* @author Sam Brannen
* @since 4.0.3
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@MetaContextHierarchyConfig
public @interface MetaMetaContextHierarchyConfig {
}

View File

@@ -25,7 +25,7 @@ import static org.junit.Assert.*;
/**
* Integration tests for meta-annotation attribute override support, relying on
* default attribute values defined in {@link MetaConfig}.
* default attribute values defined in {@link MetaConfig @MetaConfig}.
*
* @author Sam Brannen
* @since 4.0

View File

@@ -0,0 +1,40 @@
/*
* Copyright 2002-2014 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.test.context.junit4.annotation.meta;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.test.context.ActiveProfiles;
/**
* Custom configuration annotation that is itself meta-annotated with
* {@link MetaConfig @MetaConfig} and {@link ActiveProfiles @ActiveProfiles}.
*
* @author Sam Brannen
* @since 4.0.3
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@MetaConfig
// Override default "dev" profile from @MetaConfig:
@ActiveProfiles("prod")
public @interface MetaMetaConfig {
}

View File

@@ -0,0 +1,46 @@
/*
* Copyright 2002-2014 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.test.context.junit4.annotation.meta;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.*;
/**
* Integration tests for meta-meta-annotation support, relying on
* default attribute values defined in {@link MetaConfig @MetaConfig} and
* overrides in {@link MetaMetaConfig @MetaMetaConfig}.
*
* @author Sam Brannen
* @since 4.0.3
*/
@RunWith(SpringJUnit4ClassRunner.class)
@MetaMetaConfig
public class MetaMetaConfigDefaultsTests {
@Autowired
private String foo;
@Test
public void foo() {
assertEquals("Production Foo", foo);
}
}