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

The following examples show how to use org.springframework.test.context.ContextConfigurationAttributes. 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
@Override
@Nullable
public ContextCustomizer createContextCustomizer(Class<?> testClass,
		List<ContextConfigurationAttributes> configAttributes) {

	if (webSocketPresent && isAnnotatedWithWebAppConfiguration(testClass)) {
		try {
			Class<?> clazz = ClassUtils.forName(MOCK_SERVER_CONTAINER_CONTEXT_CUSTOMIZER_CLASS_NAME,
					getClass().getClassLoader());
			return (ContextCustomizer) BeanUtils.instantiateClass(clazz);
		}
		catch (Throwable ex) {
			throw new IllegalStateException("Failed to enable WebSocket test support; could not load class: " +
					MOCK_SERVER_CONTAINER_CONTEXT_CUSTOMIZER_CLASS_NAME, ex);
		}
	}

	// Else, nothing to customize
	return null;
}
 
Example 2
@Test
public void buildContextHierarchyMapForTestClassHierarchyWithMultiLevelContextHierarchies() {
	Map<String, List<ContextConfigurationAttributes>> map = buildContextHierarchyMap(TestClass3WithMultiLevelContextHierarchy.class);

	assertThat(map.size(), is(3));
	assertThat(map.keySet(), hasItems("alpha", "beta", "gamma"));

	List<ContextConfigurationAttributes> alphaConfig = map.get("alpha");
	assertThat(alphaConfig.size(), is(3));
	assertThat(alphaConfig.get(0).getLocations()[0], is("1-A.xml"));
	assertThat(alphaConfig.get(1).getLocations()[0], is("2-A.xml"));
	assertThat(alphaConfig.get(2).getLocations()[0], is("3-A.xml"));

	List<ContextConfigurationAttributes> betaConfig = map.get("beta");
	assertThat(betaConfig.size(), is(3));
	assertThat(betaConfig.get(0).getLocations()[0], is("1-B.xml"));
	assertThat(betaConfig.get(1).getLocations()[0], is("2-B.xml"));
	assertThat(betaConfig.get(2).getLocations()[0], is("3-B.xml"));

	List<ContextConfigurationAttributes> gammaConfig = map.get("gamma");
	assertThat(gammaConfig.size(), is(1));
	assertThat(gammaConfig.get(0).getLocations()[0], is("3-C.xml"));
}
 
Example 3
@Override
public ContextCustomizer createContextCustomizer(Class<?> testClass, List<ContextConfigurationAttributes> configAttributes) {
    Set<AutoConfigureEmbeddedDatabase> databaseAnnotations = AnnotatedElementUtils.getMergedRepeatableAnnotations(
            testClass, AutoConfigureEmbeddedDatabase.class, AutoConfigureEmbeddedDatabases.class);

    databaseAnnotations = databaseAnnotations.stream()
            .filter(distinctByKey(AutoConfigureEmbeddedDatabase::beanName))
            .filter(databaseAnnotation -> databaseAnnotation.type() == EmbeddedDatabaseType.POSTGRES)
            .filter(databaseAnnotation -> databaseAnnotation.replace() != Replace.NONE)
            .collect(Collectors.toCollection(LinkedHashSet::new));

    if (!databaseAnnotations.isEmpty()) {
        return new PreloadableEmbeddedPostgresContextCustomizer(databaseAnnotations);
    }

    return null;
}
 
Example 4
@Test
public void buildContextHierarchyMapForTestClassHierarchyWithMultiLevelContextHierarchies() {
	Map<String, List<ContextConfigurationAttributes>> map = buildContextHierarchyMap(TestClass3WithMultiLevelContextHierarchy.class);

	assertThat(map.size(), is(3));
	assertThat(map.keySet(), hasItems("alpha", "beta", "gamma"));

	List<ContextConfigurationAttributes> alphaConfig = map.get("alpha");
	assertThat(alphaConfig.size(), is(3));
	assertThat(alphaConfig.get(0).getLocations()[0], is("1-A.xml"));
	assertThat(alphaConfig.get(1).getLocations()[0], is("2-A.xml"));
	assertThat(alphaConfig.get(2).getLocations()[0], is("3-A.xml"));

	List<ContextConfigurationAttributes> betaConfig = map.get("beta");
	assertThat(betaConfig.size(), is(3));
	assertThat(betaConfig.get(0).getLocations()[0], is("1-B.xml"));
	assertThat(betaConfig.get(1).getLocations()[0], is("2-B.xml"));
	assertThat(betaConfig.get(2).getLocations()[0], is("3-B.xml"));

	List<ContextConfigurationAttributes> gammaConfig = map.get("gamma");
	assertThat(gammaConfig.size(), is(1));
	assertThat(gammaConfig.get(0).getLocations()[0], is("3-C.xml"));
}
 
Example 5
/**
 * Resolve the set of merged {@code ApplicationContextInitializer} classes for the
 * supplied list of {@code ContextConfigurationAttributes}.
 *
 * <p>Note that the {@link ContextConfiguration#inheritInitializers inheritInitializers}
 * flag of {@link ContextConfiguration @ContextConfiguration} will be taken into
 * consideration. Specifically, if the {@code inheritInitializers} flag is set to
 * {@code true} for a given level in the class hierarchy represented by the provided
 * configuration attributes, context initializer classes defined at the given level
 * will be merged with those defined in higher levels of the class hierarchy.
 *
 * @param configAttributesList the list of configuration attributes to process; must
 * not be {@code null} or <em>empty</em>; must be ordered <em>bottom-up</em>
 * (i.e., as if we were traversing up the class hierarchy)
 * @return the set of merged context initializer classes, including those from
 * superclasses if appropriate (never {@code null})
 * @since 3.2
 */
static Set<Class<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>>> resolveInitializerClasses(
		List<ContextConfigurationAttributes> configAttributesList) {
	Assert.notEmpty(configAttributesList, "ContextConfigurationAttributes list must not be empty");

	final Set<Class<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>>> initializerClasses = //
	new HashSet<Class<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>>>();

	for (ContextConfigurationAttributes configAttributes : configAttributesList) {
		if (logger.isTraceEnabled()) {
			logger.trace(String.format("Processing context initializers for context configuration attributes %s",
				configAttributes));
		}

		initializerClasses.addAll(Arrays.asList(configAttributes.getInitializers()));

		if (!configAttributes.isInheritInitializers()) {
			break;
		}
	}

	return initializerClasses;
}
 
Example 6
@Test
public void resolveContextHierarchyAttributesForTestClassHierarchyWithMultiLevelContextHierarchies() {
	List<List<ContextConfigurationAttributes>> hierarchyAttributes = resolveContextHierarchyAttributes(TestClass3WithMultiLevelContextHierarchy.class);
	assertEquals(3, hierarchyAttributes.size());

	List<ContextConfigurationAttributes> configAttributesListClassLevel1 = hierarchyAttributes.get(0);
	debugConfigAttributes(configAttributesListClassLevel1);
	assertEquals(2, configAttributesListClassLevel1.size());
	assertThat(configAttributesListClassLevel1.get(0).getLocations()[0], equalTo("1-A.xml"));
	assertThat(configAttributesListClassLevel1.get(1).getLocations()[0], equalTo("1-B.xml"));

	List<ContextConfigurationAttributes> configAttributesListClassLevel2 = hierarchyAttributes.get(1);
	debugConfigAttributes(configAttributesListClassLevel2);
	assertEquals(2, configAttributesListClassLevel2.size());
	assertThat(configAttributesListClassLevel2.get(0).getLocations()[0], equalTo("2-A.xml"));
	assertThat(configAttributesListClassLevel2.get(1).getLocations()[0], equalTo("2-B.xml"));

	List<ContextConfigurationAttributes> configAttributesListClassLevel3 = hierarchyAttributes.get(2);
	debugConfigAttributes(configAttributesListClassLevel3);
	assertEquals(3, configAttributesListClassLevel3.size());
	assertThat(configAttributesListClassLevel3.get(0).getLocations()[0], equalTo("3-A.xml"));
	assertThat(configAttributesListClassLevel3.get(1).getLocations()[0], equalTo("3-B.xml"));
	assertThat(configAttributesListClassLevel3.get(2).getLocations()[0], equalTo("3-C.xml"));
}
 
Example 7
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 8
@Test
public void resolveContextHierarchyAttributesForTestClassHierarchyWithSingleLevelContextHierarchies() {
	List<List<ContextConfigurationAttributes>> hierarchyAttributes = resolveContextHierarchyAttributes(TestClass3WithSingleLevelContextHierarchy.class);
	assertEquals(3, hierarchyAttributes.size());

	List<ContextConfigurationAttributes> configAttributesListClassLevel1 = hierarchyAttributes.get(0);
	debugConfigAttributes(configAttributesListClassLevel1);
	assertEquals(1, configAttributesListClassLevel1.size());
	assertThat(configAttributesListClassLevel1.get(0).getLocations()[0], equalTo("one.xml"));

	List<ContextConfigurationAttributes> configAttributesListClassLevel2 = hierarchyAttributes.get(1);
	debugConfigAttributes(configAttributesListClassLevel2);
	assertEquals(1, configAttributesListClassLevel2.size());
	assertArrayEquals(new String[] { "two-A.xml", "two-B.xml" },
		configAttributesListClassLevel2.get(0).getLocations());

	List<ContextConfigurationAttributes> configAttributesListClassLevel3 = hierarchyAttributes.get(2);
	debugConfigAttributes(configAttributesListClassLevel3);
	assertEquals(1, configAttributesListClassLevel3.size());
	assertThat(configAttributesListClassLevel3.get(0).getLocations()[0], equalTo("three.xml"));
}
 
Example 9
@Test
public void resolveContextHierarchyAttributesForTestClassHierarchyWithMultiLevelContextHierarchies() {
	List<List<ContextConfigurationAttributes>> hierarchyAttributes = resolveContextHierarchyAttributes(TestClass3WithMultiLevelContextHierarchy.class);
	assertEquals(3, hierarchyAttributes.size());

	List<ContextConfigurationAttributes> configAttributesListClassLevel1 = hierarchyAttributes.get(0);
	debugConfigAttributes(configAttributesListClassLevel1);
	assertEquals(2, configAttributesListClassLevel1.size());
	assertThat(configAttributesListClassLevel1.get(0).getLocations()[0], equalTo("1-A.xml"));
	assertThat(configAttributesListClassLevel1.get(1).getLocations()[0], equalTo("1-B.xml"));

	List<ContextConfigurationAttributes> configAttributesListClassLevel2 = hierarchyAttributes.get(1);
	debugConfigAttributes(configAttributesListClassLevel2);
	assertEquals(2, configAttributesListClassLevel2.size());
	assertThat(configAttributesListClassLevel2.get(0).getLocations()[0], equalTo("2-A.xml"));
	assertThat(configAttributesListClassLevel2.get(1).getLocations()[0], equalTo("2-B.xml"));

	List<ContextConfigurationAttributes> configAttributesListClassLevel3 = hierarchyAttributes.get(2);
	debugConfigAttributes(configAttributesListClassLevel3);
	assertEquals(3, configAttributesListClassLevel3.size());
	assertThat(configAttributesListClassLevel3.get(0).getLocations()[0], equalTo("3-A.xml"));
	assertThat(configAttributesListClassLevel3.get(1).getLocations()[0], equalTo("3-B.xml"));
	assertThat(configAttributesListClassLevel3.get(2).getLocations()[0], equalTo("3-C.xml"));
}
 
Example 10
@Test
public void buildContextHierarchyMapForTestClassHierarchyWithMultiLevelContextHierarchies() {
	Map<String, List<ContextConfigurationAttributes>> map = buildContextHierarchyMap(TestClass3WithMultiLevelContextHierarchy.class);

	assertThat(map.size(), is(3));
	assertThat(map.keySet(), hasItems("alpha", "beta", "gamma"));

	List<ContextConfigurationAttributes> alphaConfig = map.get("alpha");
	assertThat(alphaConfig.size(), is(3));
	assertThat(alphaConfig.get(0).getLocations()[0], is("1-A.xml"));
	assertThat(alphaConfig.get(1).getLocations()[0], is("2-A.xml"));
	assertThat(alphaConfig.get(2).getLocations()[0], is("3-A.xml"));

	List<ContextConfigurationAttributes> betaConfig = map.get("beta");
	assertThat(betaConfig.size(), is(3));
	assertThat(betaConfig.get(0).getLocations()[0], is("1-B.xml"));
	assertThat(betaConfig.get(1).getLocations()[0], is("2-B.xml"));
	assertThat(betaConfig.get(2).getLocations()[0], is("3-B.xml"));

	List<ContextConfigurationAttributes> gammaConfig = map.get("gamma");
	assertThat(gammaConfig.size(), is(1));
	assertThat(gammaConfig.get(0).getLocations()[0], is("3-C.xml"));
}
 
Example 11
/**
 * Used to reproduce bug reported in https://jira.spring.io/browse/SPR-10997
 */
@Test
public void buildContextHierarchyMapForTestClassHierarchyWithMultiLevelContextHierarchiesAndOverriddenInitializers() {
	Map<String, List<ContextConfigurationAttributes>> map = buildContextHierarchyMap(TestClass2WithMultiLevelContextHierarchyWithOverriddenInitializers.class);

	assertThat(map.size(), is(2));
	assertThat(map.keySet(), hasItems("alpha", "beta"));

	List<ContextConfigurationAttributes> alphaConfig = map.get("alpha");
	assertThat(alphaConfig.size(), is(2));
	assertThat(alphaConfig.get(0).getLocations().length, is(1));
	assertThat(alphaConfig.get(0).getLocations()[0], is("1-A.xml"));
	assertThat(alphaConfig.get(0).getInitializers().length, is(0));
	assertThat(alphaConfig.get(1).getLocations().length, is(0));
	assertThat(alphaConfig.get(1).getInitializers().length, is(1));
	assertEquals(DummyApplicationContextInitializer.class, alphaConfig.get(1).getInitializers()[0]);

	List<ContextConfigurationAttributes> betaConfig = map.get("beta");
	assertThat(betaConfig.size(), is(2));
	assertThat(betaConfig.get(0).getLocations().length, is(1));
	assertThat(betaConfig.get(0).getLocations()[0], is("1-B.xml"));
	assertThat(betaConfig.get(0).getInitializers().length, is(0));
	assertThat(betaConfig.get(1).getLocations().length, is(0));
	assertThat(betaConfig.get(1).getInitializers().length, is(1));
	assertEquals(DummyApplicationContextInitializer.class, betaConfig.get(1).getInitializers()[0]);
}
 
Example 12
@Override
@Nullable
public ContextCustomizer createContextCustomizer(Class<?> testClass,
		List<ContextConfigurationAttributes> configAttributes) {

	if (webSocketPresent && isAnnotatedWithWebAppConfiguration(testClass)) {
		try {
			Class<?> clazz = ClassUtils.forName(MOCK_SERVER_CONTAINER_CONTEXT_CUSTOMIZER_CLASS_NAME,
					getClass().getClassLoader());
			return (ContextCustomizer) BeanUtils.instantiateClass(clazz);
		}
		catch (Throwable ex) {
			throw new IllegalStateException("Failed to enable WebSocket test support; could not load class: " +
					MOCK_SERVER_CONTAINER_CONTEXT_CUSTOMIZER_CLASS_NAME, ex);
		}
	}

	// Else, nothing to customize
	return null;
}
 
Example 13
/**
 * Resolve the set of merged {@code ApplicationContextInitializer} classes for the
 * supplied list of {@code ContextConfigurationAttributes}.
 * <p>Note that the {@link ContextConfiguration#inheritInitializers inheritInitializers}
 * flag of {@link ContextConfiguration @ContextConfiguration} will be taken into
 * consideration. Specifically, if the {@code inheritInitializers} flag is set to
 * {@code true} for a given level in the class hierarchy represented by the provided
 * configuration attributes, context initializer classes defined at the given level
 * will be merged with those defined in higher levels of the class hierarchy.
 * @param configAttributesList the list of configuration attributes to process; must
 * not be {@code null} or <em>empty</em>; must be ordered <em>bottom-up</em>
 * (i.e., as if we were traversing up the class hierarchy)
 * @return the set of merged context initializer classes, including those from
 * superclasses if appropriate (never {@code null})
 * @since 3.2
 */
static Set<Class<? extends ApplicationContextInitializer<?>>> resolveInitializerClasses(
		List<ContextConfigurationAttributes> configAttributesList) {

	Assert.notEmpty(configAttributesList, "ContextConfigurationAttributes List must not be empty");
	Set<Class<? extends ApplicationContextInitializer<?>>> initializerClasses = new LinkedHashSet<>();

	for (ContextConfigurationAttributes configAttributes : configAttributesList) {
		if (logger.isTraceEnabled()) {
			logger.trace("Processing context initializers for configuration attributes " + configAttributes);
		}
		Collections.addAll(initializerClasses, configAttributes.getInitializers());
		if (!configAttributes.isInheritInitializers()) {
			break;
		}
	}

	return initializerClasses;
}
 
Example 14
/**
 * Resolve the {@link ContextLoader} {@linkplain Class class} to use for the supplied
 * list of {@link ContextConfigurationAttributes}.
 * <p>Beginning with the first level in the context configuration attributes hierarchy:
 * <ol>
 * <li>If the {@link ContextConfigurationAttributes#getContextLoaderClass()
 * contextLoaderClass} property of {@link ContextConfigurationAttributes} is
 * configured with an explicit class, that class will be returned.</li>
 * <li>If an explicit {@code ContextLoader} class is not specified at the current
 * level in the hierarchy, traverse to the next level in the hierarchy and return to
 * step #1.</li>
 * </ol>
 * @param configAttributesList the list of configuration attributes to process;
 * must not be {@code null}; must be ordered <em>bottom-up</em>
 * (i.e., as if we were traversing up the class hierarchy)
 * @return the {@code ContextLoader} class to use for the supplied configuration
 * attributes, or {@code null} if no explicit loader is found
 * @throws IllegalArgumentException if supplied configuration attributes are
 * {@code null} or <em>empty</em>
 */
@Nullable
protected Class<? extends ContextLoader> resolveExplicitContextLoaderClass(
		List<ContextConfigurationAttributes> configAttributesList) {

	Assert.notNull(configAttributesList, "ContextConfigurationAttributes list must not be null");

	for (ContextConfigurationAttributes configAttributes : configAttributesList) {
		if (logger.isTraceEnabled()) {
			logger.trace(String.format("Resolving ContextLoader for context configuration attributes %s",
					configAttributes));
		}
		Class<? extends ContextLoader> contextLoaderClass = configAttributes.getContextLoaderClass();
		if (ContextLoader.class != contextLoaderClass) {
			if (logger.isDebugEnabled()) {
				logger.debug(String.format(
						"Found explicit ContextLoader class [%s] for context configuration attributes %s",
						contextLoaderClass.getName(), configAttributes));
			}
			return contextLoaderClass;
		}
	}
	return null;
}
 
Example 15
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 16
/**
 * Resolve the {@link ContextLoader} {@linkplain Class class} to use for the
 * supplied list of {@link ContextConfigurationAttributes} and then instantiate
 * and return that {@code ContextLoader}.
 * <p>If the user has not explicitly declared which loader to use, the value
 * returned from {@link #getDefaultContextLoaderClass} will be used as the
 * default context loader class. For details on the class resolution process,
 * see {@link #resolveExplicitContextLoaderClass} and
 * {@link #getDefaultContextLoaderClass}.
 * @param testClass the test class for which the {@code ContextLoader} should be
 * resolved; must not be {@code null}
 * @param configAttributesList the list of configuration attributes to process; must
 * not be {@code null} or <em>empty</em>; must be ordered <em>bottom-up</em>
 * (i.e., as if we were traversing up the class hierarchy)
 * @return the resolved {@code ContextLoader} for the supplied {@code testClass}
 * (never {@code null})
 * @throws IllegalStateException if {@link #getDefaultContextLoaderClass(Class)}
 * returns {@code null}
 */
protected ContextLoader resolveContextLoader(Class<?> testClass,
		List<ContextConfigurationAttributes> configAttributesList) {

	Assert.notNull(testClass, "Class must not be null");
	Assert.notEmpty(configAttributesList, "ContextConfigurationAttributes list must not be empty");

	Class<? extends ContextLoader> contextLoaderClass = resolveExplicitContextLoaderClass(configAttributesList);
	if (contextLoaderClass == null) {
		contextLoaderClass = getDefaultContextLoaderClass(testClass);
		if (contextLoaderClass == null) {
			throw new IllegalStateException("getDefaultContextLoaderClass() must not return null");
		}
	}
	if (logger.isTraceEnabled()) {
		logger.trace(String.format("Using ContextLoader class [%s] for test class [%s]",
			contextLoaderClass.getName(), testClass.getName()));
	}
	return BeanUtils.instantiateClass(contextLoaderClass, ContextLoader.class);
}
 
Example 17
@Test
public void resolveContextHierarchyAttributesForSingleTestClassWithImplicitSingleLevelContextHierarchy() {
	List<List<ContextConfigurationAttributes>> hierarchyAttributes = resolveContextHierarchyAttributes(BareAnnotations.class);
	assertEquals(1, hierarchyAttributes.size());
	List<ContextConfigurationAttributes> configAttributesList = hierarchyAttributes.get(0);
	assertEquals(1, configAttributesList.size());
	debugConfigAttributes(configAttributesList);
}
 
Example 18
/**
 * Verifies change requested in <a href="https://jira.spring.io/browse/SPR-11634">SPR-11634</a>.
 * @since 4.0.4
 */
@Test
public void resolveConfigAttributesWithLocationsAndClasses() {
	List<ContextConfigurationAttributes> attributesList = resolveContextConfigurationAttributes(LocationsAndClasses.class);
	assertNotNull(attributesList);
	assertEquals(1, attributesList.size());
}
 
Example 19
@Test
public void resolveContextHierarchyAttributesForTestClassHierarchyWithSingleLevelContextHierarchiesAndMetaAnnotations() {
	List<List<ContextConfigurationAttributes>> hierarchyAttributes = resolveContextHierarchyAttributes(TestClass3WithSingleLevelContextHierarchyFromMetaAnnotation.class);
	assertEquals(3, hierarchyAttributes.size());

	List<ContextConfigurationAttributes> configAttributesListClassLevel1 = hierarchyAttributes.get(0);
	debugConfigAttributes(configAttributesListClassLevel1);
	assertEquals(1, configAttributesListClassLevel1.size());
	assertThat(configAttributesListClassLevel1.get(0).getLocations()[0], equalTo("A.xml"));
	assertAttributes(configAttributesListClassLevel1.get(0),
		TestClass1WithSingleLevelContextHierarchyFromMetaAnnotation.class, new String[] { "A.xml" },
		EMPTY_CLASS_ARRAY, ContextLoader.class, true);

	List<ContextConfigurationAttributes> configAttributesListClassLevel2 = hierarchyAttributes.get(1);
	debugConfigAttributes(configAttributesListClassLevel2);
	assertEquals(1, configAttributesListClassLevel2.size());
	assertArrayEquals(new String[] { "B-one.xml", "B-two.xml" },
		configAttributesListClassLevel2.get(0).getLocations());
	assertAttributes(configAttributesListClassLevel2.get(0),
		TestClass2WithSingleLevelContextHierarchyFromMetaAnnotation.class,
		new String[] { "B-one.xml",
		"B-two.xml" }, EMPTY_CLASS_ARRAY, ContextLoader.class, true);

	List<ContextConfigurationAttributes> configAttributesListClassLevel3 = hierarchyAttributes.get(2);
	debugConfigAttributes(configAttributesListClassLevel3);
	assertEquals(1, configAttributesListClassLevel3.size());
	assertThat(configAttributesListClassLevel3.get(0).getLocations()[0], equalTo("C.xml"));
	assertAttributes(configAttributesListClassLevel3.get(0),
		TestClass3WithSingleLevelContextHierarchyFromMetaAnnotation.class, new String[] { "C.xml" },
		EMPTY_CLASS_ARRAY, ContextLoader.class, true);
}
 
Example 20
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 21
Source Project: spring-analysis-note   Source File: HybridContextLoader.java    License: MIT License 5 votes vote down vote up
@Override
public void processContextConfiguration(ContextConfigurationAttributes configAttributes) {
	// Detect default XML configuration files:
	super.processContextConfiguration(configAttributes);

	// Detect default configuration classes:
	if (!configAttributes.hasClasses() && isGenerateDefaultLocations()) {
		configAttributes.setClasses(detectDefaultConfigurationClasses(configAttributes.getDeclaringClass()));
	}
}
 
Example 22
@Test
public void buildContextHierarchyMapForTestClassHierarchyWithMultiLevelContextHierarchiesAndPartiallyNamedConfig() {
	Map<String, List<ContextConfigurationAttributes>> map = buildContextHierarchyMap(TestClass2WithMultiLevelContextHierarchyAndPartiallyNamedConfig.class);

	String level1 = "parent";
	String level2 = GENERATED_CONTEXT_HIERARCHY_LEVEL_PREFIX + 2;
	String level3 = GENERATED_CONTEXT_HIERARCHY_LEVEL_PREFIX + 3;

	assertThat(map.size(), is(3));
	assertThat(map.keySet(), hasItems(level1, level2, level3));
	Iterator<String> levels = map.keySet().iterator();
	assertThat(levels.next(), is(level1));
	assertThat(levels.next(), is(level2));
	assertThat(levels.next(), is(level3));

	List<ContextConfigurationAttributes> level1Config = map.get(level1);
	assertThat(level1Config.size(), is(2));
	assertThat(level1Config.get(0).getLocations()[0], is("1-A.xml"));
	assertThat(level1Config.get(1).getLocations()[0], is("2-A.xml"));

	List<ContextConfigurationAttributes> level2Config = map.get(level2);
	assertThat(level2Config.size(), is(1));
	assertThat(level2Config.get(0).getLocations()[0], is("1-B.xml"));

	List<ContextConfigurationAttributes> level3Config = map.get(level3);
	assertThat(level3Config.size(), is(1));
	assertThat(level3Config.get(0).getLocations()[0], is("2-C.xml"));
}
 
Example 23
@Test
public void resolveConfigAttributesWithLocalAndInheritedAnnotationsAndLocations() {
	List<ContextConfigurationAttributes> attributesList = resolveContextConfigurationAttributes(LocationsBar.class);
	assertNotNull(attributesList);
	assertEquals(2, attributesList.size());
	assertLocationsBarAttributes(attributesList.get(0));
	assertLocationsFooAttributes(attributesList.get(1));
}
 
Example 24
@Test
public void resolveConfigAttributesWithLocalAnnotationAndLocations() {
	List<ContextConfigurationAttributes> attributesList = resolveContextConfigurationAttributes(LocationsFoo.class);
	assertNotNull(attributesList);
	assertEquals(1, attributesList.size());
	assertLocationsFooAttributes(attributesList.get(0));
}
 
Example 25
@Test
public void resolveConfigAttributesWithLocalAnnotationAndClasses() {
	List<ContextConfigurationAttributes> attributesList = resolveContextConfigurationAttributes(ClassesFoo.class);
	assertNotNull(attributesList);
	assertEquals(1, attributesList.size());
	assertClassesFooAttributes(attributesList.get(0));
}
 
Example 26
@Test
public void resolveConfigAttributesWithMetaAnnotationAndLocationsAndOverrides() {
	Class<MetaLocationsFooWithOverrides> testClass = MetaLocationsFooWithOverrides.class;
	List<ContextConfigurationAttributes> attributesList = resolveContextConfigurationAttributes(testClass);
	assertNotNull(attributesList);
	assertEquals(1, attributesList.size());
	assertAttributes(attributesList.get(0),
			testClass, new String[] {"/foo.xml"}, EMPTY_CLASS_ARRAY, ContextLoader.class, true);
}
 
Example 27
private static void delegateProcessing(SmartContextLoader loader, ContextConfigurationAttributes configAttributes) {
	if (logger.isDebugEnabled()) {
		logger.debug(String.format("Delegating to %s to process context configuration %s.", name(loader),
			configAttributes));
	}
	loader.processContextConfiguration(configAttributes);
}
 
Example 28
@Test
public void resolveConfigAttributesWithMetaAnnotationAndLocationsInClassHierarchy() {
	Class<MetaLocationsBar> testClass = MetaLocationsBar.class;
	List<ContextConfigurationAttributes> attributesList = resolveContextConfigurationAttributes(testClass);
	assertNotNull(attributesList);
	assertEquals(2, attributesList.size());
	assertAttributes(attributesList.get(0),
			testClass, new String[] {"/bar.xml"}, EMPTY_CLASS_ARRAY, ContextLoader.class, true);
	assertAttributes(attributesList.get(1),
			MetaLocationsFoo.class, new String[] {"/foo.xml"}, EMPTY_CLASS_ARRAY, ContextLoader.class, true);
}
 
Example 29
@Test
public void resolveConfigAttributesWithLocalAnnotationAndClasses() {
	List<ContextConfigurationAttributes> attributesList = resolveContextConfigurationAttributes(ClassesFoo.class);
	assertNotNull(attributesList);
	assertEquals(1, attributesList.size());
	assertClassesFooAttributes(attributesList.get(0));
}
 
Example 30
@Test
public void resolveConfigAttributesWithLocalAndInheritedAnnotationsAndLocations() {
	List<ContextConfigurationAttributes> attributesList = resolveContextConfigurationAttributes(LocationsBar.class);
	assertNotNull(attributesList);
	assertEquals(2, attributesList.size());
	assertLocationsBarAttributes(attributesList.get(0));
	assertLocationsFooAttributes(attributesList.get(1));
}