org.springframework.test.context.ContextConfiguration Java Examples

The following examples show how to use org.springframework.test.context.ContextConfiguration. 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 Project: spring-analysis-note   Author: Vip-Augus   File: ContextLoaderUtils.java    License: 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 Project: spring-analysis-note   Author: Vip-Augus   File: GenericXmlContextLoaderResourceLocationsTests.java    License: MIT License 6 votes vote down vote up
@Test
public void assertContextConfigurationLocations() throws Exception {

	final ContextConfiguration contextConfig = this.testClass.getAnnotation(ContextConfiguration.class);
	final ContextLoader contextLoader = new GenericXmlContextLoader();
	final String[] configuredLocations = (String[]) AnnotationUtils.getValue(contextConfig);
	final String[] processedLocations = contextLoader.processLocations(this.testClass, configuredLocations);

	if (logger.isDebugEnabled()) {
		logger.debug("----------------------------------------------------------------------");
		logger.debug("Configured locations: " + ObjectUtils.nullSafeToString(configuredLocations));
		logger.debug("Expected   locations: " + ObjectUtils.nullSafeToString(this.expectedLocations));
		logger.debug("Processed  locations: " + ObjectUtils.nullSafeToString(processedLocations));
	}

	assertArrayEquals("Verifying locations for test [" + this.testClass + "].", this.expectedLocations,
		processedLocations);
}
 
Example #3
Source Project: spring-analysis-note   Author: Vip-Augus   File: MetaAnnotationUtilsTests.java    License: MIT License 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void findAnnotationDescriptorForTypesWithMetaAnnotationWithDefaultAttributes() {
	Class<?> startClass = MetaConfigWithDefaultAttributesTestCase.class;
	Class<ContextConfiguration> annotationType = ContextConfiguration.class;

	UntypedAnnotationDescriptor descriptor = findAnnotationDescriptorForTypes(startClass,
			Service.class, ContextConfiguration.class, Order.class, Transactional.class);

	assertNotNull(descriptor);
	assertEquals(startClass, descriptor.getRootDeclaringClass());
	assertEquals(annotationType, descriptor.getAnnotationType());
	assertArrayEquals(new Class<?>[] {}, ((ContextConfiguration) descriptor.getAnnotation()).value());
	assertArrayEquals(new Class<?>[] {MetaConfig.DevConfig.class, MetaConfig.ProductionConfig.class},
			descriptor.getAnnotationAttributes().getClassArray("classes"));
	assertNotNull(descriptor.getComposedAnnotation());
	assertEquals(MetaConfig.class, descriptor.getComposedAnnotationType());
}
 
Example #4
Source Project: spring-analysis-note   Author: Vip-Augus   File: OverriddenMetaAnnotationAttributesTests.java    License: 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 #5
Source Project: spring-analysis-note   Author: Vip-Augus   File: OverriddenMetaAnnotationAttributesTests.java    License: 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 #6
Source Project: spring-analysis-note   Author: Vip-Augus   File: OverriddenMetaAnnotationAttributesTests.java    License: 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 #7
Source Project: java-technology-stack   Author: codeEngraver   File: ContextLoaderUtils.java    License: 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 #8
Source Project: java-technology-stack   Author: codeEngraver   File: GenericXmlContextLoaderResourceLocationsTests.java    License: MIT License 6 votes vote down vote up
@Test
public void assertContextConfigurationLocations() throws Exception {

	final ContextConfiguration contextConfig = this.testClass.getAnnotation(ContextConfiguration.class);
	final ContextLoader contextLoader = new GenericXmlContextLoader();
	final String[] configuredLocations = (String[]) AnnotationUtils.getValue(contextConfig);
	final String[] processedLocations = contextLoader.processLocations(this.testClass, configuredLocations);

	if (logger.isDebugEnabled()) {
		logger.debug("----------------------------------------------------------------------");
		logger.debug("Configured locations: " + ObjectUtils.nullSafeToString(configuredLocations));
		logger.debug("Expected   locations: " + ObjectUtils.nullSafeToString(this.expectedLocations));
		logger.debug("Processed  locations: " + ObjectUtils.nullSafeToString(processedLocations));
	}

	assertArrayEquals("Verifying locations for test [" + this.testClass + "].", this.expectedLocations,
		processedLocations);
}
 
Example #9
Source Project: java-technology-stack   Author: codeEngraver   File: MetaAnnotationUtilsTests.java    License: MIT License 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void findAnnotationDescriptorForTypesWithMetaAnnotationWithDefaultAttributes() {
	Class<?> startClass = MetaConfigWithDefaultAttributesTestCase.class;
	Class<ContextConfiguration> annotationType = ContextConfiguration.class;

	UntypedAnnotationDescriptor descriptor = findAnnotationDescriptorForTypes(startClass,
			Service.class, ContextConfiguration.class, Order.class, Transactional.class);

	assertNotNull(descriptor);
	assertEquals(startClass, descriptor.getRootDeclaringClass());
	assertEquals(annotationType, descriptor.getAnnotationType());
	assertArrayEquals(new Class<?>[] {}, ((ContextConfiguration) descriptor.getAnnotation()).value());
	assertArrayEquals(new Class<?>[] {MetaConfig.DevConfig.class, MetaConfig.ProductionConfig.class},
			descriptor.getAnnotationAttributes().getClassArray("classes"));
	assertNotNull(descriptor.getComposedAnnotation());
	assertEquals(MetaConfig.class, descriptor.getComposedAnnotationType());
}
 
Example #10
Source Project: java-technology-stack   Author: codeEngraver   File: MetaAnnotationUtilsTests.java    License: MIT License 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void findAnnotationDescriptorForTypesWithMetaAnnotationWithOverriddenAttributes() {
	Class<?> startClass = MetaConfigWithOverriddenAttributesTestCase.class;
	Class<ContextConfiguration> annotationType = ContextConfiguration.class;

	UntypedAnnotationDescriptor descriptor = findAnnotationDescriptorForTypes(
			startClass, Service.class, ContextConfiguration.class, Order.class, Transactional.class);

	assertNotNull(descriptor);
	assertEquals(startClass, descriptor.getRootDeclaringClass());
	assertEquals(annotationType, descriptor.getAnnotationType());
	assertArrayEquals(new Class<?>[] {}, ((ContextConfiguration) descriptor.getAnnotation()).value());
	assertArrayEquals(new Class<?>[] {MetaAnnotationUtilsTests.class},
			descriptor.getAnnotationAttributes().getClassArray("classes"));
	assertNotNull(descriptor.getComposedAnnotation());
	assertEquals(MetaConfig.class, descriptor.getComposedAnnotationType());
}
 
Example #11
Source Project: java-technology-stack   Author: codeEngraver   File: OverriddenMetaAnnotationAttributesTests.java    License: 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 #12
Source Project: java-technology-stack   Author: codeEngraver   File: OverriddenMetaAnnotationAttributesTests.java    License: 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 #13
Source Project: java-technology-stack   Author: codeEngraver   File: OverriddenMetaAnnotationAttributesTests.java    License: 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 #14
Source Project: spring4-understanding   Author: langtianya   File: ContextLoaderUtils.java    License: Apache License 2.0 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<ContextConfigurationAttributes>();
	Class<ContextConfiguration> annotationType = ContextConfiguration.class;

	AnnotationDescriptor<ContextConfiguration> descriptor = findAnnotationDescriptor(testClass, annotationType);
	if (descriptor == null) {
		throw new IllegalArgumentException(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 #15
@Test
public void assertContextConfigurationLocations() throws Exception {

	final ContextConfiguration contextConfig = this.testClass.getAnnotation(ContextConfiguration.class);
	final ContextLoader contextLoader = new GenericXmlContextLoader();
	final String[] configuredLocations = (String[]) AnnotationUtils.getValue(contextConfig);
	final String[] processedLocations = contextLoader.processLocations(this.testClass, configuredLocations);

	if (logger.isDebugEnabled()) {
		logger.debug("----------------------------------------------------------------------");
		logger.debug("Configured locations: " + ObjectUtils.nullSafeToString(configuredLocations));
		logger.debug("Expected   locations: " + ObjectUtils.nullSafeToString(this.expectedLocations));
		logger.debug("Processed  locations: " + ObjectUtils.nullSafeToString(processedLocations));
	}

	assertArrayEquals("Verifying locations for test [" + this.testClass + "].", this.expectedLocations,
		processedLocations);
}
 
Example #16
Source Project: spring4-understanding   Author: langtianya   File: MetaAnnotationUtilsTests.java    License: Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void findAnnotationDescriptorForTypesWithMetaAnnotationWithDefaultAttributes() throws Exception {
	Class<?> startClass = MetaConfigWithDefaultAttributesTestCase.class;
	Class<ContextConfiguration> annotationType = ContextConfiguration.class;

	UntypedAnnotationDescriptor descriptor = findAnnotationDescriptorForTypes(startClass, Service.class,
		ContextConfiguration.class, Order.class, Transactional.class);

	assertNotNull(descriptor);
	assertEquals(startClass, descriptor.getRootDeclaringClass());
	assertEquals(annotationType, descriptor.getAnnotationType());
	assertArrayEquals(new Class[] {}, ((ContextConfiguration) descriptor.getAnnotation()).value());
	assertArrayEquals(new Class[] { MetaConfig.DevConfig.class, MetaConfig.ProductionConfig.class },
		descriptor.getAnnotationAttributes().getClassArray("classes"));
	assertNotNull(descriptor.getComposedAnnotation());
	assertEquals(MetaConfig.class, descriptor.getComposedAnnotationType());
}
 
Example #17
Source Project: spring4-understanding   Author: langtianya   File: MetaAnnotationUtilsTests.java    License: Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void findAnnotationDescriptorForTypesWithMetaAnnotationWithOverriddenAttributes() throws Exception {
	Class<?> startClass = MetaConfigWithOverriddenAttributesTestCase.class;
	Class<ContextConfiguration> annotationType = ContextConfiguration.class;

	UntypedAnnotationDescriptor descriptor = findAnnotationDescriptorForTypes(startClass, Service.class,
		ContextConfiguration.class, Order.class, Transactional.class);

	assertNotNull(descriptor);
	assertEquals(startClass, descriptor.getRootDeclaringClass());
	assertEquals(annotationType, descriptor.getAnnotationType());
	assertArrayEquals(new Class[] {}, ((ContextConfiguration) descriptor.getAnnotation()).value());
	assertArrayEquals(new Class[] { MetaAnnotationUtilsTests.class },
		descriptor.getAnnotationAttributes().getClassArray("classes"));
	assertNotNull(descriptor.getComposedAnnotation());
	assertEquals(MetaConfig.class, descriptor.getComposedAnnotationType());
}
 
Example #18
Source Project: spring4-understanding   Author: langtianya   File: OverriddenMetaAnnotationAttributesTests.java    License: Apache License 2.0 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 #19
Source Project: spring4-understanding   Author: langtianya   File: OverriddenMetaAnnotationAttributesTests.java    License: Apache License 2.0 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 #20
Source Project: spring4-understanding   Author: langtianya   File: OverriddenMetaAnnotationAttributesTests.java    License: Apache License 2.0 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 #21
Source Project: spring-analysis-note   Author: Vip-Augus   File: ContextLoaderUtils.java    License: MIT License 5 votes vote down vote up
/**
 * Convenience method for creating a {@link ContextConfigurationAttributes}
 * instance from the supplied {@link ContextConfiguration} annotation and
 * declaring class and then adding the attributes to the supplied list.
 */
private static void convertContextConfigToConfigAttributesAndAddToList(ContextConfiguration contextConfiguration,
		Class<?> declaringClass, final List<ContextConfigurationAttributes> attributesList) {

	if (logger.isTraceEnabled()) {
		logger.trace(String.format("Retrieved @ContextConfiguration [%s] for declaring class [%s].",
				contextConfiguration, declaringClass.getName()));
	}
	ContextConfigurationAttributes attributes =
			new ContextConfigurationAttributes(declaringClass, contextConfiguration);
	if (logger.isTraceEnabled()) {
		logger.trace("Resolved context configuration attributes: " + attributes);
	}
	attributesList.add(attributes);
}
 
Example #22
Source Project: spring-analysis-note   Author: Vip-Augus   File: MetaAnnotationUtilsTests.java    License: 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 #23
Source Project: spring-analysis-note   Author: Vip-Augus   File: MetaAnnotationUtilsTests.java    License: MIT License 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void findAnnotationDescriptorForTypesWithMetaAnnotationWithOverriddenAttributes() {
	Class<?> startClass = MetaConfigWithOverriddenAttributesTestCase.class;
	Class<ContextConfiguration> annotationType = ContextConfiguration.class;

	UntypedAnnotationDescriptor descriptor = findAnnotationDescriptorForTypes(
			startClass, Service.class, ContextConfiguration.class, Order.class, Transactional.class);

	assertNotNull(descriptor);
	assertEquals(startClass, descriptor.getRootDeclaringClass());
	assertEquals(annotationType, descriptor.getAnnotationType());
	assertArrayEquals(new Class<?>[] {}, ((ContextConfiguration) descriptor.getAnnotation()).value());
	assertArrayEquals(new Class<?>[] {MetaAnnotationUtilsTests.class},
			descriptor.getAnnotationAttributes().getClassArray("classes"));
	assertNotNull(descriptor.getComposedAnnotation());
	assertEquals(MetaConfig.class, descriptor.getComposedAnnotationType());
}
 
Example #24
Source Project: spring-analysis-note   Author: Vip-Augus   File: OverriddenMetaAnnotationAttributesTests.java    License: 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 #25
Source Project: java-technology-stack   Author: codeEngraver   File: ContextLoaderUtils.java    License: MIT License 5 votes vote down vote up
/**
 * Convenience method for creating a {@link ContextConfigurationAttributes}
 * instance from the supplied {@link ContextConfiguration} annotation and
 * declaring class and then adding the attributes to the supplied list.
 */
private static void convertContextConfigToConfigAttributesAndAddToList(ContextConfiguration contextConfiguration,
		Class<?> declaringClass, final List<ContextConfigurationAttributes> attributesList) {

	if (logger.isTraceEnabled()) {
		logger.trace(String.format("Retrieved @ContextConfiguration [%s] for declaring class [%s].",
				contextConfiguration, declaringClass.getName()));
	}
	ContextConfigurationAttributes attributes =
			new ContextConfigurationAttributes(declaringClass, contextConfiguration);
	if (logger.isTraceEnabled()) {
		logger.trace("Resolved context configuration attributes: " + attributes);
	}
	attributesList.add(attributes);
}
 
Example #26
Source Project: java-technology-stack   Author: codeEngraver   File: MetaAnnotationUtilsTests.java    License: 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 #27
Source Project: java-technology-stack   Author: codeEngraver   File: OverriddenMetaAnnotationAttributesTests.java    License: 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 #28
Source Project: activiti6-boot2   Author: dingziyang   File: SpringActivitiTestCase.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void initializeProcessEngine() {
  ContextConfiguration contextConfiguration = getClass().getAnnotation(ContextConfiguration.class);
  String[] value = contextConfiguration.value();
  boolean hasOneArg = value != null && value.length == 1;
  String key = hasOneArg ? value[0] : ProcessEngine.class.getName();
  ProcessEngine engine = this.cachedProcessEngines.containsKey(key) ? this.cachedProcessEngines.get(key) : this.applicationContext.getBean(ProcessEngine.class);

  this.cachedProcessEngines.put(key, engine);
  this.processEngine = engine;
}
 
Example #29
Source Project: activiti6-boot2   Author: dingziyang   File: SpringActivitiTestCase.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void initializeProcessEngine() {
  ContextConfiguration contextConfiguration = getClass().getAnnotation(ContextConfiguration.class);
  String[] value = contextConfiguration.value();
  boolean hasOneArg = value != null && value.length == 1;
  String key = hasOneArg ? value[0] : ProcessEngine.class.getName();
  ProcessEngine engine = this.cachedProcessEngines.containsKey(key) ? this.cachedProcessEngines.get(key) : this.applicationContext.getBean(ProcessEngine.class);
  
  this.cachedProcessEngines.put(key, engine);
  this.processEngine = engine;
}
 
Example #30
Source Project: spring4-understanding   Author: langtianya   File: ContextLoaderUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Convenience method for creating a {@link ContextConfigurationAttributes}
 * instance from the supplied {@link ContextConfiguration} annotation and
 * declaring class and then adding the attributes to the supplied list.
 */
private static void convertContextConfigToConfigAttributesAndAddToList(ContextConfiguration contextConfiguration,
		Class<?> declaringClass, final List<ContextConfigurationAttributes> attributesList) {

	if (logger.isTraceEnabled()) {
		logger.trace(String.format("Retrieved @ContextConfiguration [%s] for declaring class [%s].",
				contextConfiguration, declaringClass.getName()));
	}
	ContextConfigurationAttributes attributes =
			new ContextConfigurationAttributes(declaringClass, contextConfiguration);
	if (logger.isTraceEnabled()) {
		logger.trace("Resolved context configuration attributes: " + attributes);
	}
	attributesList.add(attributes);
}