Java Code Examples for org.springframework.test.context.ContextConfiguration

The following examples show how to use org.springframework.test.context.ContextConfiguration. These examples are extracted from open source projects. 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   Source 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
@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   Source 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   Source 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 5
@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 6
@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
@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 8
Source Project: java-technology-stack   Source 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 9
@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 10
Source Project: java-technology-stack   Source 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 11
Source Project: java-technology-stack   Source 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 12
@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 13
@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 14
@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 15
Source Project: spring4-understanding   Source 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 16
@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 17
@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 18
@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 19
@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 20
@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 21
@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 22
Source Project: spring-analysis-note   Source 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 23
Source Project: spring-analysis-note   Source 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 24
@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   Source 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   Source 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
@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   Source 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   Source 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   Source 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);
}