Search in sources :

Example 1 with ContextConfiguration

use of org.springframework.test.context.ContextConfiguration in project spring-framework by spring-projects.

the class GenericXmlContextLoaderResourceLocationsTests method assertContextConfigurationLocations.

@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);
}
Also used : ContextLoader(org.springframework.test.context.ContextLoader) ContextConfiguration(org.springframework.test.context.ContextConfiguration) Test(org.junit.Test)

Example 2 with ContextConfiguration

use of org.springframework.test.context.ContextConfiguration in project spring-framework by spring-projects.

the class MetaAnnotationUtilsTests method findAnnotationDescriptorForTypesWithMetaAnnotationWithDefaultAttributes.

@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());
}
Also used : ContextConfiguration(org.springframework.test.context.ContextConfiguration) UntypedAnnotationDescriptor(org.springframework.test.util.MetaAnnotationUtils.UntypedAnnotationDescriptor) Test(org.junit.Test)

Example 3 with ContextConfiguration

use of org.springframework.test.context.ContextConfiguration in project spring-framework by spring-projects.

the class OverriddenMetaAnnotationAttributesTests method overriddenContextConfigurationLocationsAndInheritLocations.

@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"));
}
Also used : AnnotationAttributes(org.springframework.core.annotation.AnnotationAttributes) ContextConfiguration(org.springframework.test.context.ContextConfiguration) Test(org.junit.Test)

Example 4 with ContextConfiguration

use of org.springframework.test.context.ContextConfiguration in project spring-framework by spring-projects.

the class OverriddenMetaAnnotationAttributesTests method overriddenContextConfigurationValue.

@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"));
}
Also used : AnnotationAttributes(org.springframework.core.annotation.AnnotationAttributes) ContextConfiguration(org.springframework.test.context.ContextConfiguration) Test(org.junit.Test)

Example 5 with ContextConfiguration

use of org.springframework.test.context.ContextConfiguration in project spring-framework by spring-projects.

the class ContextLoaderUtils method resolveContextHierarchyAttributes.

/**
	 * Resolve the list of lists of {@linkplain ContextConfigurationAttributes context
	 * configuration attributes} for the supplied {@linkplain Class test class} and its
	 * superclasses, taking into account context hierarchies declared via
	 * {@link ContextHierarchy @ContextHierarchy} and
	 * {@link ContextConfiguration @ContextConfiguration}.
	 * <p>The outer list represents a top-down ordering of context configuration
	 * attributes, where each element in the list represents the context configuration
	 * declared on a given test class in the class hierarchy. Each nested list
	 * contains the context configuration attributes declared either via a single
	 * instance of {@code @ContextConfiguration} on the particular class or via
	 * multiple instances of {@code @ContextConfiguration} declared within a
	 * single {@code @ContextHierarchy} instance on the particular class.
	 * Furthermore, each nested list maintains the order in which
	 * {@code @ContextConfiguration} instances are declared.
	 * <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 nested lists returned by this method.
	 * @param testClass the class for which to resolve the context hierarchy attributes
	 * (must not be {@code null})
	 * @return the list of lists of configuration attributes for the specified class;
	 * never {@code null}
	 * @throws IllegalArgumentException if the supplied class is {@code null}; or if
	 * neither {@code @ContextConfiguration} nor {@code @ContextHierarchy} is
	 * <em>present</em> on the supplied class
	 * @throws IllegalStateException if a test class or composed annotation
	 * in the class hierarchy declares both {@code @ContextConfiguration} and
	 * {@code @ContextHierarchy} as top-level annotations.
	 * @since 3.2.2
	 * @see #buildContextHierarchyMap(Class)
	 * @see #resolveContextConfigurationAttributes(Class)
	 */
@SuppressWarnings("unchecked")
static List<List<ContextConfigurationAttributes>> resolveContextHierarchyAttributes(Class<?> testClass) {
    Assert.notNull(testClass, "Class must not be null");
    Class<ContextConfiguration> contextConfigType = ContextConfiguration.class;
    Class<ContextHierarchy> contextHierarchyType = ContextHierarchy.class;
    List<List<ContextConfigurationAttributes>> hierarchyAttributes = new ArrayList<>();
    UntypedAnnotationDescriptor desc = findAnnotationDescriptorForTypes(testClass, contextConfigType, contextHierarchyType);
    Assert.notNull(desc, () -> String.format("Could not find an 'annotation declaring class' for annotation type [%s] or [%s] and test class [%s]", contextConfigType.getName(), contextHierarchyType.getName(), testClass.getName()));
    while (desc != null) {
        Class<?> rootDeclaringClass = desc.getRootDeclaringClass();
        Class<?> declaringClass = desc.getDeclaringClass();
        boolean contextConfigDeclaredLocally = isAnnotationDeclaredLocally(contextConfigType, declaringClass);
        boolean contextHierarchyDeclaredLocally = isAnnotationDeclaredLocally(contextHierarchyType, declaringClass);
        if (contextConfigDeclaredLocally && contextHierarchyDeclaredLocally) {
            String msg = String.format("Class [%s] has been configured with both @ContextConfiguration " + "and @ContextHierarchy. Only one of these annotations may be declared on a test class " + "or composed annotation.", declaringClass.getName());
            logger.error(msg);
            throw new IllegalStateException(msg);
        }
        List<ContextConfigurationAttributes> configAttributesList = new ArrayList<>();
        if (contextConfigDeclaredLocally) {
            ContextConfiguration contextConfiguration = AnnotationUtils.synthesizeAnnotation(desc.getAnnotationAttributes(), ContextConfiguration.class, desc.getRootDeclaringClass());
            convertContextConfigToConfigAttributesAndAddToList(contextConfiguration, rootDeclaringClass, configAttributesList);
        } else if (contextHierarchyDeclaredLocally) {
            ContextHierarchy contextHierarchy = getAnnotation(declaringClass, contextHierarchyType);
            for (ContextConfiguration contextConfiguration : contextHierarchy.value()) {
                convertContextConfigToConfigAttributesAndAddToList(contextConfiguration, rootDeclaringClass, configAttributesList);
            }
        } else {
            // This should theoretically never happen...
            String msg = String.format("Test class [%s] has been configured with neither @ContextConfiguration " + "nor @ContextHierarchy as a class-level annotation.", rootDeclaringClass.getName());
            logger.error(msg);
            throw new IllegalStateException(msg);
        }
        hierarchyAttributes.add(0, configAttributesList);
        desc = findAnnotationDescriptorForTypes(rootDeclaringClass.getSuperclass(), contextConfigType, contextHierarchyType);
    }
    return hierarchyAttributes;
}
Also used : ContextConfigurationAttributes(org.springframework.test.context.ContextConfigurationAttributes) ArrayList(java.util.ArrayList) ContextHierarchy(org.springframework.test.context.ContextHierarchy) ArrayList(java.util.ArrayList) List(java.util.List) ContextConfiguration(org.springframework.test.context.ContextConfiguration)

Aggregations

ContextConfiguration (org.springframework.test.context.ContextConfiguration)7 Test (org.junit.Test)5 AnnotationAttributes (org.springframework.core.annotation.AnnotationAttributes)2 UntypedAnnotationDescriptor (org.springframework.test.util.MetaAnnotationUtils.UntypedAnnotationDescriptor)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ProcessEngine (org.activiti.engine.ProcessEngine)1 ContextConfigurationAttributes (org.springframework.test.context.ContextConfigurationAttributes)1 ContextHierarchy (org.springframework.test.context.ContextHierarchy)1 ContextLoader (org.springframework.test.context.ContextLoader)1