org.springframework.test.context.ContextConfigurationAttributes Java Examples

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

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

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

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

	return attributesList;
}
 
Example #7
Source File: ContextLoaderUtilsContextHierarchyTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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 #8
Source File: ContextLoaderUtilsContextHierarchyTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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 #9
Source File: AbstractTestContextBootstrapper.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * 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 #10
Source File: ApplicationContextInitializerUtils.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * 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 #11
Source File: ContextLoaderUtilsContextHierarchyTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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 #12
Source File: EmbeddedPostgresContextCustomizerFactory.java    From embedded-database-spring-test with Apache License 2.0 6 votes vote down vote up
@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 #13
Source File: ContextLoaderUtilsContextHierarchyTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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 #14
Source File: ContextLoaderUtils.java    From spring4-understanding with 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
Source File: ContextLoaderUtilsContextHierarchyTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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 #16
Source File: ContextLoaderUtilsContextHierarchyTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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 #17
Source File: ContextLoaderUtilsConfigurationAttributesTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void resolveConfigAttributesWithMetaAnnotationAndLocationsAndOverriddenAttributes() {
	Class<MetaLocationsFooWithOverriddenAttributes> testClass = MetaLocationsFooWithOverriddenAttributes.class;
	List<ContextConfigurationAttributes> attributesList = resolveContextConfigurationAttributes(testClass);
	assertNotNull(attributesList);
	assertEquals(1, attributesList.size());
	assertAttributes(attributesList.get(0),
			testClass, new String[] {"foo1.xml", "foo2.xml"}, EMPTY_CLASS_ARRAY, ContextLoader.class, true);
}
 
Example #18
Source File: ContextLoaderUtilsConfigurationAttributesTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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 #19
Source File: ContextLoaderUtilsConfigurationAttributesTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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 #20
Source File: ContextLoaderUtilsConfigurationAttributesTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void resolveConfigAttributesWithMetaAnnotationAndLocations() {
	Class<MetaLocationsFoo> testClass = MetaLocationsFoo.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 #21
Source File: AbstractTestContextBootstrapper.java    From java-technology-stack with MIT License 5 votes vote down vote up
private Set<ContextCustomizer> getContextCustomizers(Class<?> testClass,
		List<ContextConfigurationAttributes> configAttributes) {

	List<ContextCustomizerFactory> factories = getContextCustomizerFactories();
	Set<ContextCustomizer> customizers = new LinkedHashSet<>(factories.size());
	for (ContextCustomizerFactory factory : factories) {
		ContextCustomizer customizer = factory.createContextCustomizer(testClass, configAttributes);
		if (customizer != null) {
			customizers.add(customizer);
		}
	}
	return customizers;
}
 
Example #22
Source File: ContextLoaderUtilsConfigurationAttributesTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void resolveConfigAttributesWithLocalAndInheritedAnnotationsAndClasses() {
	List<ContextConfigurationAttributes> attributesList = resolveContextConfigurationAttributes(ClassesBar.class);
	assertNotNull(attributesList);
	assertEquals(2, attributesList.size());
	assertClassesBarAttributes(attributesList.get(0));
	assertClassesFooAttributes(attributesList.get(1));
}
 
Example #23
Source File: ContextLoaderUtilsConfigurationAttributesTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void resolveConfigAttributesWithLocalAndInheritedAnnotationsAndClasses() {
	List<ContextConfigurationAttributes> attributesList = resolveContextConfigurationAttributes(ClassesBar.class);
	assertNotNull(attributesList);
	assertEquals(2, attributesList.size());
	assertClassesBarAttributes(attributesList.get(0));
	assertClassesFooAttributes(attributesList.get(1));
}
 
Example #24
Source File: ContextLoaderUtilsConfigurationAttributesTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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 #25
Source File: DelegatingSmartContextLoaderTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void processContextConfigurationWithDefaultXmlConfigAndConfigurationClassGeneration() {
	expectedException.expect(IllegalStateException.class);
	expectedException.expectMessage(containsString("both default locations AND default configuration classes were detected"));

	ContextConfigurationAttributes configAttributes = new ContextConfigurationAttributes(
			ImproperDuplicateDefaultXmlAndConfigClassTestCase.class, EMPTY_STRING_ARRAY, EMPTY_CLASS_ARRAY,
			true, null, true, ContextLoader.class);
	loader.processContextConfiguration(configAttributes);
}
 
Example #26
Source File: ContextLoaderUtilsConfigurationAttributesTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void resolveConfigAttributesWithBareAnnotations() {
	Class<BareAnnotations> testClass = BareAnnotations.class;
	List<ContextConfigurationAttributes> attributesList = resolveContextConfigurationAttributes(testClass);
	assertNotNull(attributesList);
	assertEquals(1, attributesList.size());
	assertAttributes(attributesList.get(0),
			testClass, EMPTY_STRING_ARRAY, EMPTY_CLASS_ARRAY, ContextLoader.class, true);
}
 
Example #27
Source File: ContextLoaderUtilsContextHierarchyTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void resolveContextHierarchyAttributesForSingleTestClassWithSingleLevelContextHierarchy() {
	List<List<ContextConfigurationAttributes>> hierarchyAttributes = resolveContextHierarchyAttributes(SingleTestClassWithSingleLevelContextHierarchy.class);
	assertEquals(1, hierarchyAttributes.size());
	List<ContextConfigurationAttributes> configAttributesList = hierarchyAttributes.get(0);
	assertEquals(1, configAttributesList.size());
	debugConfigAttributes(configAttributesList);
}
 
Example #28
Source File: ContextLoaderUtilsContextHierarchyTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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 #29
Source File: ContextLoaderUtilsContextHierarchyTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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 #30
Source File: EmbeddedCassandraContextCustomizerFactory.java    From embedded-cassandra with Apache License 2.0 5 votes vote down vote up
@Override
@Nullable
public ContextCustomizer createContextCustomizer(Class<?> testClass,
		List<ContextConfigurationAttributes> configAttributes) {
	EmbeddedCassandra annotation = AnnotatedElementUtils.findMergedAnnotation(testClass, EmbeddedCassandra.class);
	if (annotation != null) {
		return new EmbeddedCassandraContextCustomizer(annotation);
	}
	return null;
}