org.springframework.test.util.MetaAnnotationUtils.AnnotationDescriptor Java Examples

The following examples show how to use org.springframework.test.util.MetaAnnotationUtils.AnnotationDescriptor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ContextLoaderUtils.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Resolve the list of {@linkplain ContextConfigurationAttributes context
 * configuration attributes} for the supplied {@linkplain Class test class} and its
 * superclasses.
 * <p>Note that the {@link ContextConfiguration#inheritLocations inheritLocations} and
 * {@link ContextConfiguration#inheritInitializers() inheritInitializers} flags of
 * {@link ContextConfiguration @ContextConfiguration} will <strong>not</strong>
 * be taken into consideration. If these flags need to be honored, that must be
 * handled manually when traversing the list returned by this method.
 * @param testClass the class for which to resolve the configuration attributes
 * (must not be {@code null})
 * @return the list of configuration attributes for the specified class, ordered
 * <em>bottom-up</em> (i.e., as if we were traversing up the class hierarchy);
 * never {@code null}
 * @throws IllegalArgumentException if the supplied class is {@code null} or if
 * {@code @ContextConfiguration} is not <em>present</em> on the supplied class
 */
static List<ContextConfigurationAttributes> resolveContextConfigurationAttributes(Class<?> testClass) {
	Assert.notNull(testClass, "Class must not be null");

	List<ContextConfigurationAttributes> attributesList = new ArrayList<>();
	Class<ContextConfiguration> annotationType = ContextConfiguration.class;

	AnnotationDescriptor<ContextConfiguration> descriptor = findAnnotationDescriptor(testClass, annotationType);
	Assert.notNull(descriptor, () -> String.format(
				"Could not find an 'annotation declaring class' for annotation type [%s] and class [%s]",
				annotationType.getName(), testClass.getName()));

	while (descriptor != null) {
		convertContextConfigToConfigAttributesAndAddToList(descriptor.synthesizeAnnotation(),
				descriptor.getRootDeclaringClass(), attributesList);
		descriptor = findAnnotationDescriptor(descriptor.getRootDeclaringClass().getSuperclass(), annotationType);
	}

	return attributesList;
}
 
Example #2
Source File: MetaAnnotationUtilsTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void findAnnotationDescriptorWithInheritedAnnotationOnInterface() {
	// Note: @Transactional is inherited
	Transactional rawAnnotation = InheritedAnnotationInterface.class.getAnnotation(Transactional.class);

	AnnotationDescriptor<Transactional> descriptor =
			findAnnotationDescriptor(InheritedAnnotationInterface.class, Transactional.class);
	assertNotNull(descriptor);
	assertEquals(InheritedAnnotationInterface.class, descriptor.getRootDeclaringClass());
	assertEquals(InheritedAnnotationInterface.class, descriptor.getDeclaringClass());
	assertEquals(rawAnnotation, descriptor.getAnnotation());

	descriptor = findAnnotationDescriptor(SubInheritedAnnotationInterface.class, Transactional.class);
	assertNotNull(descriptor);
	assertEquals(SubInheritedAnnotationInterface.class, descriptor.getRootDeclaringClass());
	assertEquals(InheritedAnnotationInterface.class, descriptor.getDeclaringClass());
	assertEquals(rawAnnotation, descriptor.getAnnotation());

	descriptor = findAnnotationDescriptor(SubSubInheritedAnnotationInterface.class, Transactional.class);
	assertNotNull(descriptor);
	assertEquals(SubSubInheritedAnnotationInterface.class, descriptor.getRootDeclaringClass());
	assertEquals(InheritedAnnotationInterface.class, descriptor.getDeclaringClass());
	assertEquals(rawAnnotation, descriptor.getAnnotation());
}
 
Example #3
Source File: MetaAnnotationUtilsTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void findAnnotationDescriptorForNonInheritedAnnotationOnInterface() {
	// Note: @Order is not inherited.
	Order rawAnnotation = NonInheritedAnnotationInterface.class.getAnnotation(Order.class);

	AnnotationDescriptor<Order> descriptor =
			findAnnotationDescriptor(NonInheritedAnnotationInterface.class, Order.class);
	assertNotNull(descriptor);
	assertEquals(NonInheritedAnnotationInterface.class, descriptor.getRootDeclaringClass());
	assertEquals(NonInheritedAnnotationInterface.class, descriptor.getDeclaringClass());
	assertEquals(rawAnnotation, descriptor.getAnnotation());

	descriptor = findAnnotationDescriptor(SubNonInheritedAnnotationInterface.class, Order.class);
	assertNotNull(descriptor);
	assertEquals(SubNonInheritedAnnotationInterface.class, descriptor.getRootDeclaringClass());
	assertEquals(NonInheritedAnnotationInterface.class, descriptor.getDeclaringClass());
	assertEquals(rawAnnotation, descriptor.getAnnotation());
}
 
Example #4
Source File: ContextLoaderUtils.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Resolve the list of {@linkplain ContextConfigurationAttributes context
 * configuration attributes} for the supplied {@linkplain Class test class} and its
 * superclasses.
 * <p>Note that the {@link ContextConfiguration#inheritLocations inheritLocations} and
 * {@link ContextConfiguration#inheritInitializers() inheritInitializers} flags of
 * {@link ContextConfiguration @ContextConfiguration} will <strong>not</strong>
 * be taken into consideration. If these flags need to be honored, that must be
 * handled manually when traversing the list returned by this method.
 * @param testClass the class for which to resolve the configuration attributes
 * (must not be {@code null})
 * @return the list of configuration attributes for the specified class, ordered
 * <em>bottom-up</em> (i.e., as if we were traversing up the class hierarchy);
 * never {@code null}
 * @throws IllegalArgumentException if the supplied class is {@code null} or if
 * {@code @ContextConfiguration} is not <em>present</em> on the supplied class
 */
static List<ContextConfigurationAttributes> resolveContextConfigurationAttributes(Class<?> testClass) {
	Assert.notNull(testClass, "Class must not be null");

	List<ContextConfigurationAttributes> attributesList = new ArrayList<>();
	Class<ContextConfiguration> annotationType = ContextConfiguration.class;

	AnnotationDescriptor<ContextConfiguration> descriptor = findAnnotationDescriptor(testClass, annotationType);
	Assert.notNull(descriptor, () -> String.format(
				"Could not find an 'annotation declaring class' for annotation type [%s] and class [%s]",
				annotationType.getName(), testClass.getName()));

	while (descriptor != null) {
		convertContextConfigToConfigAttributesAndAddToList(descriptor.synthesizeAnnotation(),
				descriptor.getRootDeclaringClass(), attributesList);
		descriptor = findAnnotationDescriptor(descriptor.getRootDeclaringClass().getSuperclass(), annotationType);
	}

	return attributesList;
}
 
Example #5
Source File: OverriddenMetaAnnotationAttributesTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void overriddenContextConfigurationLocationsAndInheritLocations() throws Exception {
	Class<?> declaringClass = OverriddenMetaLocationsConfigTestCase.class;
	AnnotationDescriptor<ContextConfiguration> descriptor = findAnnotationDescriptor(declaringClass,
		ContextConfiguration.class);
	assertNotNull(descriptor);
	assertEquals(declaringClass, descriptor.getRootDeclaringClass());
	assertEquals(MetaLocationsConfig.class, descriptor.getComposedAnnotationType());
	assertEquals(ContextConfiguration.class, descriptor.getAnnotationType());
	assertNotNull(descriptor.getComposedAnnotation());
	assertEquals(MetaLocationsConfig.class, descriptor.getComposedAnnotationType());

	// direct access to annotation attributes:
	assertArrayEquals(new String[] { "foo.xml" }, descriptor.getAnnotation().locations());
	assertFalse(descriptor.getAnnotation().inheritLocations());

	// overridden attributes:
	AnnotationAttributes attributes = descriptor.getAnnotationAttributes();
	assertArrayEquals(new String[] { "bar.xml" }, attributes.getStringArray("locations"));
	assertTrue(attributes.getBoolean("inheritLocations"));
}
 
Example #6
Source File: OverriddenMetaAnnotationAttributesTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void contextConfigurationLocationsAndInheritLocations() throws Exception {
	Class<MetaLocationsConfigTestCase> declaringClass = MetaLocationsConfigTestCase.class;
	AnnotationDescriptor<ContextConfiguration> descriptor = findAnnotationDescriptor(declaringClass,
		ContextConfiguration.class);
	assertNotNull(descriptor);
	assertEquals(declaringClass, descriptor.getRootDeclaringClass());
	assertEquals(MetaLocationsConfig.class, descriptor.getComposedAnnotationType());
	assertEquals(ContextConfiguration.class, descriptor.getAnnotationType());
	assertNotNull(descriptor.getComposedAnnotation());
	assertEquals(MetaLocationsConfig.class, descriptor.getComposedAnnotationType());

	// direct access to annotation attributes:
	assertArrayEquals(new String[] { "foo.xml" }, descriptor.getAnnotation().locations());
	assertFalse(descriptor.getAnnotation().inheritLocations());
}
 
Example #7
Source File: OverriddenMetaAnnotationAttributesTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void overriddenContextConfigurationValue() throws Exception {
	Class<?> declaringClass = OverriddenMetaValueConfigTestCase.class;
	AnnotationDescriptor<ContextConfiguration> descriptor = findAnnotationDescriptor(declaringClass,
		ContextConfiguration.class);
	assertNotNull(descriptor);
	assertEquals(declaringClass, descriptor.getRootDeclaringClass());
	assertEquals(MetaValueConfig.class, descriptor.getComposedAnnotationType());
	assertEquals(ContextConfiguration.class, descriptor.getAnnotationType());
	assertNotNull(descriptor.getComposedAnnotation());
	assertEquals(MetaValueConfig.class, descriptor.getComposedAnnotationType());

	// direct access to annotation value:
	assertArrayEquals(new String[] { "foo.xml" }, descriptor.getAnnotation().value());

	// overridden attribute:
	AnnotationAttributes attributes = descriptor.getAnnotationAttributes();

	// NOTE: we would like to be able to override the 'value' attribute; however,
	// Spring currently does not allow overrides for the 'value' attribute.
	// See SPR-11393 for related discussions.
	assertArrayEquals(new String[] { "foo.xml" }, attributes.getStringArray("value"));
}
 
Example #8
Source File: MetaAnnotationUtilsTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * @since 4.0.3
 */
@Test
public void findAnnotationDescriptorOnMetaCycleAnnotatedClassWithMissingTargetMetaAnnotation() {
	AnnotationDescriptor<Component> descriptor = findAnnotationDescriptor(
			MetaCycleAnnotatedClass.class, Component.class);
	assertNull("Should not find @Component on MetaCycleAnnotatedClass", descriptor);
}
 
Example #9
Source File: DefaultActiveProfilesResolver.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Resolve the <em>bean definition profiles</em> for the given {@linkplain
 * Class test class} based on profiles configured declaratively via
 * {@link ActiveProfiles#profiles} or {@link ActiveProfiles#value}.
 * @param testClass the test class for which the profiles should be resolved;
 * never {@code null}
 * @return the list of bean definition profiles to use when loading the
 * {@code ApplicationContext}; never {@code null}
 */
@Override
public String[] resolve(Class<?> testClass) {
	Assert.notNull(testClass, "Class must not be null");

	final Set<String> activeProfiles = new LinkedHashSet<String>();

	Class<ActiveProfiles> annotationType = ActiveProfiles.class;
	AnnotationDescriptor<ActiveProfiles> descriptor = findAnnotationDescriptor(testClass, annotationType);

	if (descriptor == null) {
		if (logger.isDebugEnabled()) {
			logger.debug(String.format(
				"Could not find an 'annotation declaring class' for annotation type [%s] and class [%s]",
				annotationType.getName(), testClass.getName()));
		}
	}
	else {
		Class<?> declaringClass = descriptor.getDeclaringClass();
		ActiveProfiles annotation = descriptor.synthesizeAnnotation();

		if (logger.isTraceEnabled()) {
			logger.trace(String.format("Retrieved @ActiveProfiles [%s] for declaring class [%s].", annotation,
				declaringClass.getName()));
		}

		for (String profile : annotation.profiles()) {
			if (StringUtils.hasText(profile)) {
				activeProfiles.add(profile.trim());
			}
		}
	}

	return StringUtils.toStringArray(activeProfiles);
}
 
Example #10
Source File: TestPropertySourceUtils.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private static List<TestPropertySourceAttributes> resolveTestPropertySourceAttributes(Class<?> testClass) {
	Assert.notNull(testClass, "Class must not be null");

	final List<TestPropertySourceAttributes> attributesList = new ArrayList<TestPropertySourceAttributes>();
	final Class<TestPropertySource> annotationType = TestPropertySource.class;
	AnnotationDescriptor<TestPropertySource> descriptor = findAnnotationDescriptor(testClass, annotationType);
	Assert.notNull(descriptor, String.format(
		"Could not find an 'annotation declaring class' for annotation type [%s] and class [%s]",
		annotationType.getName(), testClass.getName()));

	while (descriptor != null) {
		TestPropertySource testPropertySource = descriptor.synthesizeAnnotation();
		Class<?> rootDeclaringClass = descriptor.getRootDeclaringClass();

		if (logger.isTraceEnabled()) {
			logger.trace(String.format("Retrieved @TestPropertySource [%s] for declaring class [%s].",
				testPropertySource, rootDeclaringClass.getName()));
		}

		TestPropertySourceAttributes attributes = new TestPropertySourceAttributes(rootDeclaringClass,
			testPropertySource);
		if (logger.isTraceEnabled()) {
			logger.trace("Resolved TestPropertySource attributes: " + attributes);
		}
		attributesList.add(attributes);

		descriptor = findAnnotationDescriptor(rootDeclaringClass.getSuperclass(), annotationType);
	}

	return attributesList;
}
 
Example #11
Source File: TestPropertySourceUtils.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
static MergedTestPropertySources buildMergedTestPropertySources(Class<?> testClass) {
	Class<TestPropertySource> annotationType = TestPropertySource.class;
	AnnotationDescriptor<TestPropertySource> descriptor = findAnnotationDescriptor(testClass, annotationType);
	if (descriptor == null) {
		return new MergedTestPropertySources();
	}

	// else...
	List<TestPropertySourceAttributes> attributesList = resolveTestPropertySourceAttributes(testClass);
	String[] locations = mergeLocations(attributesList);
	String[] properties = mergeProperties(attributesList);
	return new MergedTestPropertySources(locations, properties);
}
 
Example #12
Source File: DefaultActiveProfilesResolver.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Resolve the <em>bean definition profiles</em> for the given {@linkplain
 * Class test class} based on profiles configured declaratively via
 * {@link ActiveProfiles#profiles} or {@link ActiveProfiles#value}.
 * @param testClass the test class for which the profiles should be resolved;
 * never {@code null}
 * @return the list of bean definition profiles to use when loading the
 * {@code ApplicationContext}; never {@code null}
 */
@Override
public String[] resolve(Class<?> testClass) {
	Assert.notNull(testClass, "Class must not be null");

	final Set<String> activeProfiles = new LinkedHashSet<>();

	Class<ActiveProfiles> annotationType = ActiveProfiles.class;
	AnnotationDescriptor<ActiveProfiles> descriptor = findAnnotationDescriptor(testClass, annotationType);

	if (descriptor == null) {
		if (logger.isDebugEnabled()) {
			logger.debug(String.format(
				"Could not find an 'annotation declaring class' for annotation type [%s] and class [%s]",
				annotationType.getName(), testClass.getName()));
		}
	}
	else {
		Class<?> declaringClass = descriptor.getDeclaringClass();
		ActiveProfiles annotation = descriptor.synthesizeAnnotation();

		if (logger.isTraceEnabled()) {
			logger.trace(String.format("Retrieved @ActiveProfiles [%s] for declaring class [%s].", annotation,
				declaringClass.getName()));
		}

		for (String profile : annotation.profiles()) {
			if (StringUtils.hasText(profile)) {
				activeProfiles.add(profile.trim());
			}
		}
	}

	return StringUtils.toStringArray(activeProfiles);
}
 
Example #13
Source File: TestPropertySourceUtils.java    From java-technology-stack with MIT License 5 votes vote down vote up
private static List<TestPropertySourceAttributes> resolveTestPropertySourceAttributes(Class<?> testClass) {
	Assert.notNull(testClass, "Class must not be null");
	List<TestPropertySourceAttributes> attributesList = new ArrayList<>();
	Class<TestPropertySource> annotationType = TestPropertySource.class;

	AnnotationDescriptor<TestPropertySource> descriptor = findAnnotationDescriptor(testClass, annotationType);
	Assert.notNull(descriptor, String.format(
			"Could not find an 'annotation declaring class' for annotation type [%s] and class [%s]",
			annotationType.getName(), testClass.getName()));

	while (descriptor != null) {
		TestPropertySource testPropertySource = descriptor.synthesizeAnnotation();
		Class<?> rootDeclaringClass = descriptor.getRootDeclaringClass();
		if (logger.isTraceEnabled()) {
			logger.trace(String.format("Retrieved @TestPropertySource [%s] for declaring class [%s].",
				testPropertySource, rootDeclaringClass.getName()));
		}
		TestPropertySourceAttributes attributes =
				new TestPropertySourceAttributes(rootDeclaringClass, testPropertySource);
		if (logger.isTraceEnabled()) {
			logger.trace("Resolved TestPropertySource attributes: " + attributes);
		}
		attributesList.add(attributes);
		descriptor = findAnnotationDescriptor(rootDeclaringClass.getSuperclass(), annotationType);
	}

	return attributesList;
}
 
Example #14
Source File: TestPropertySourceUtils.java    From java-technology-stack with MIT License 5 votes vote down vote up
static MergedTestPropertySources buildMergedTestPropertySources(Class<?> testClass) {
	Class<TestPropertySource> annotationType = TestPropertySource.class;
	AnnotationDescriptor<TestPropertySource> descriptor = findAnnotationDescriptor(testClass, annotationType);
	if (descriptor == null) {
		return new MergedTestPropertySources();
	}

	List<TestPropertySourceAttributes> attributesList = resolveTestPropertySourceAttributes(testClass);
	String[] locations = mergeLocations(attributesList);
	String[] properties = mergeProperties(attributesList);
	return new MergedTestPropertySources(locations, properties);
}
 
Example #15
Source File: OverriddenMetaAnnotationAttributesTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void contextConfigurationValue() throws Exception {
	Class<MetaValueConfigTestCase> declaringClass = MetaValueConfigTestCase.class;
	AnnotationDescriptor<ContextConfiguration> descriptor = findAnnotationDescriptor(declaringClass,
		ContextConfiguration.class);
	assertNotNull(descriptor);
	assertEquals(declaringClass, descriptor.getRootDeclaringClass());
	assertEquals(MetaValueConfig.class, descriptor.getComposedAnnotationType());
	assertEquals(ContextConfiguration.class, descriptor.getAnnotationType());
	assertNotNull(descriptor.getComposedAnnotation());
	assertEquals(MetaValueConfig.class, descriptor.getComposedAnnotationType());

	// direct access to annotation value:
	assertArrayEquals(new String[] { "foo.xml" }, descriptor.getAnnotation().value());
}
 
Example #16
Source File: TestPropertySourceUtils.java    From spring-analysis-note with MIT License 5 votes vote down vote up
static MergedTestPropertySources buildMergedTestPropertySources(Class<?> testClass) {
	Class<TestPropertySource> annotationType = TestPropertySource.class;
	AnnotationDescriptor<TestPropertySource> descriptor = findAnnotationDescriptor(testClass, annotationType);
	if (descriptor == null) {
		return new MergedTestPropertySources();
	}

	List<TestPropertySourceAttributes> attributesList = resolveTestPropertySourceAttributes(testClass);
	String[] locations = mergeLocations(attributesList);
	String[] properties = mergeProperties(attributesList);
	return new MergedTestPropertySources(locations, properties);
}
 
Example #17
Source File: MetaAnnotationUtilsTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * @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);
}
 
Example #18
Source File: MetaAnnotationUtilsTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void findAnnotationDescriptorForClassWithLocalMetaAnnotationAndAnnotatedSuperclass() {
	AnnotationDescriptor<ContextConfiguration> descriptor = findAnnotationDescriptor(
			MetaAnnotatedAndSuperAnnotatedContextConfigClass.class, ContextConfiguration.class);

	assertNotNull("AnnotationDescriptor should not be null", descriptor);
	assertEquals("rootDeclaringClass", MetaAnnotatedAndSuperAnnotatedContextConfigClass.class, descriptor.getRootDeclaringClass());
	assertEquals("declaringClass", MetaConfig.class, descriptor.getDeclaringClass());
	assertEquals("annotationType", ContextConfiguration.class, descriptor.getAnnotationType());
	assertNotNull("composedAnnotation should not be null", descriptor.getComposedAnnotation());
	assertEquals("composedAnnotationType", MetaConfig.class, descriptor.getComposedAnnotationType());

	assertArrayEquals("configured classes", new Class<?>[] {String.class},
			descriptor.getAnnotationAttributes().getClassArray("classes"));
}
 
Example #19
Source File: MetaAnnotationUtilsTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void findAnnotationDescriptorForClassWithMetaAnnotatedInterface() {
	Component rawAnnotation = AnnotationUtils.findAnnotation(ClassWithMetaAnnotatedInterface.class, Component.class);
	AnnotationDescriptor<Component> descriptor =
			findAnnotationDescriptor(ClassWithMetaAnnotatedInterface.class, Component.class);

	assertNotNull(descriptor);
	assertEquals(ClassWithMetaAnnotatedInterface.class, descriptor.getRootDeclaringClass());
	assertEquals(Meta1.class, descriptor.getDeclaringClass());
	assertEquals(rawAnnotation, descriptor.getAnnotation());
	assertEquals(Meta1.class, descriptor.getComposedAnnotation().annotationType());
}
 
Example #20
Source File: MetaAnnotationUtilsTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void findAnnotationDescriptorWithLocalAndMetaComponentAnnotation() {
	Class<Component> annotationType = Component.class;
	AnnotationDescriptor<Component> descriptor = findAnnotationDescriptor(
			HasLocalAndMetaComponentAnnotation.class, annotationType);

	assertEquals(HasLocalAndMetaComponentAnnotation.class, descriptor.getRootDeclaringClass());
	assertEquals(annotationType, descriptor.getAnnotationType());
	assertNull(descriptor.getComposedAnnotation());
	assertNull(descriptor.getComposedAnnotationType());
}
 
Example #21
Source File: MetaAnnotationUtilsTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void assertAtComponentOnComposedAnnotation(Class<?> startClass, Class<?> rootDeclaringClass,
		Class<?> declaringClass, String name, Class<? extends Annotation> composedAnnotationType) {

	AnnotationDescriptor<Component> descriptor = findAnnotationDescriptor(startClass, Component.class);
	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());
}
 
Example #22
Source File: DefaultActiveProfilesResolver.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Resolve the <em>bean definition profiles</em> for the given {@linkplain
 * Class test class} based on profiles configured declaratively via
 * {@link ActiveProfiles#profiles} or {@link ActiveProfiles#value}.
 * @param testClass the test class for which the profiles should be resolved;
 * never {@code null}
 * @return the list of bean definition profiles to use when loading the
 * {@code ApplicationContext}; never {@code null}
 */
@Override
public String[] resolve(Class<?> testClass) {
	Assert.notNull(testClass, "Class must not be null");

	final Set<String> activeProfiles = new LinkedHashSet<>();

	Class<ActiveProfiles> annotationType = ActiveProfiles.class;
	AnnotationDescriptor<ActiveProfiles> descriptor = findAnnotationDescriptor(testClass, annotationType);

	if (descriptor == null) {
		if (logger.isDebugEnabled()) {
			logger.debug(String.format(
				"Could not find an 'annotation declaring class' for annotation type [%s] and class [%s]",
				annotationType.getName(), testClass.getName()));
		}
	}
	else {
		Class<?> declaringClass = descriptor.getDeclaringClass();
		ActiveProfiles annotation = descriptor.synthesizeAnnotation();

		if (logger.isTraceEnabled()) {
			logger.trace(String.format("Retrieved @ActiveProfiles [%s] for declaring class [%s].", annotation,
				declaringClass.getName()));
		}

		for (String profile : annotation.profiles()) {
			if (StringUtils.hasText(profile)) {
				activeProfiles.add(profile.trim());
			}
		}
	}

	return StringUtils.toStringArray(activeProfiles);
}
 
Example #23
Source File: TestPropertySourceUtils.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private static List<TestPropertySourceAttributes> resolveTestPropertySourceAttributes(Class<?> testClass) {
	Assert.notNull(testClass, "Class must not be null");
	List<TestPropertySourceAttributes> attributesList = new ArrayList<>();
	Class<TestPropertySource> annotationType = TestPropertySource.class;

	AnnotationDescriptor<TestPropertySource> descriptor = findAnnotationDescriptor(testClass, annotationType);
	Assert.notNull(descriptor, String.format(
			"Could not find an 'annotation declaring class' for annotation type [%s] and class [%s]",
			annotationType.getName(), testClass.getName()));

	while (descriptor != null) {
		TestPropertySource testPropertySource = descriptor.synthesizeAnnotation();
		Class<?> rootDeclaringClass = descriptor.getRootDeclaringClass();
		if (logger.isTraceEnabled()) {
			logger.trace(String.format("Retrieved @TestPropertySource [%s] for declaring class [%s].",
				testPropertySource, rootDeclaringClass.getName()));
		}
		TestPropertySourceAttributes attributes =
				new TestPropertySourceAttributes(rootDeclaringClass, testPropertySource);
		if (logger.isTraceEnabled()) {
			logger.trace("Resolved TestPropertySource attributes: " + attributes);
		}
		attributesList.add(attributes);
		descriptor = findAnnotationDescriptor(rootDeclaringClass.getSuperclass(), annotationType);
	}

	return attributesList;
}