org.springframework.test.context.MergedContextConfiguration Java Examples

The following examples show how to use org.springframework.test.context.MergedContextConfiguration. 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: DefaultContextCache.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private void remove(List<MergedContextConfiguration> removedContexts, MergedContextConfiguration key) {
	Assert.notNull(key, "Key must not be null");

	Set<MergedContextConfiguration> children = this.hierarchyMap.get(key);
	if (children != null) {
		for (MergedContextConfiguration child : children) {
			// Recurse through lower levels
			remove(removedContexts, child);
		}
		// Remove the set of children for the current context from the hierarchy map.
		this.hierarchyMap.remove(key);
	}

	// Physically remove and close leaf nodes first (i.e., on the way back up the
	// stack as opposed to prior to the recursive call).
	ApplicationContext context = this.contextMap.remove(key);
	if (context instanceof ConfigurableApplicationContext) {
		((ConfigurableApplicationContext) context).close();
	}
	removedContexts.add(key);
}
 
Example #2
Source File: BootstrapTestUtilsMergedConfigTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Introduced to investigate claims made in a discussion on
 * <a href="http://stackoverflow.com/questions/24725438/what-could-cause-a-class-implementing-applicationlistenercontextrefreshedevent">Stack Overflow</a>.
 */
@Test
public void buildMergedConfigWithAtWebAppConfigurationWithAnnotationAndClassesOnSuperclass() {
	Class<?> webTestClass = WebClassesFoo.class;
	Class<?> standardTestClass = ClassesFoo.class;
	WebMergedContextConfiguration webMergedConfig = (WebMergedContextConfiguration) buildMergedContextConfiguration(webTestClass);
	MergedContextConfiguration standardMergedConfig = buildMergedContextConfiguration(standardTestClass);

	assertEquals(webMergedConfig, webMergedConfig);
	assertEquals(standardMergedConfig, standardMergedConfig);
	assertNotEquals(standardMergedConfig, webMergedConfig);
	assertNotEquals(webMergedConfig, standardMergedConfig);

	assertMergedConfig(webMergedConfig, webTestClass, EMPTY_STRING_ARRAY, array(FooConfig.class),
		WebDelegatingSmartContextLoader.class);
	assertMergedConfig(standardMergedConfig, standardTestClass, EMPTY_STRING_ARRAY,
		array(FooConfig.class), DelegatingSmartContextLoader.class);
}
 
Example #3
Source File: AbstractContextConfigurationUtilsTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
void assertMergedConfig(
		MergedContextConfiguration mergedConfig,
		Class<?> expectedTestClass,
		String[] expectedLocations,
		Class<?>[] expectedClasses,
		Set<Class<? extends ApplicationContextInitializer<?>>> expectedInitializerClasses,
		Class<? extends ContextLoader> expectedContextLoaderClass) {

	assertNotNull(mergedConfig);
	assertEquals(expectedTestClass, mergedConfig.getTestClass());
	assertNotNull(mergedConfig.getLocations());
	assertArrayEquals(expectedLocations, mergedConfig.getLocations());
	assertNotNull(mergedConfig.getClasses());
	assertArrayEquals(expectedClasses, mergedConfig.getClasses());
	assertNotNull(mergedConfig.getActiveProfiles());
	if (expectedContextLoaderClass == null) {
		assertNull(mergedConfig.getContextLoader());
	}
	else {
		assertEquals(expectedContextLoaderClass, mergedConfig.getContextLoader().getClass());
	}
	assertNotNull(mergedConfig.getContextInitializerClasses());
	assertEquals(expectedInitializerClasses, mergedConfig.getContextInitializerClasses());
}
 
Example #4
Source File: DefaultContextCache.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
private void remove(List<MergedContextConfiguration> removedContexts, MergedContextConfiguration key) {
	Assert.notNull(key, "Key must not be null");

	Set<MergedContextConfiguration> children = this.hierarchyMap.get(key);
	if (children != null) {
		for (MergedContextConfiguration child : children) {
			// Recurse through lower levels
			remove(removedContexts, child);
		}
		// Remove the set of children for the current context from the hierarchy map.
		this.hierarchyMap.remove(key);
	}

	// Physically remove and close leaf nodes first (i.e., on the way back up the
	// stack as opposed to prior to the recursive call).
	ApplicationContext context = this.contextMap.remove(key);
	if (context instanceof ConfigurableApplicationContext) {
		((ConfigurableApplicationContext) context).close();
	}
	removedContexts.add(key);
}
 
Example #5
Source File: BootstrapTestUtilsMergedConfigTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Introduced to investigate claims made in a discussion on
 * <a href="http://stackoverflow.com/questions/24725438/what-could-cause-a-class-implementing-applicationlistenercontextrefreshedevent">Stack Overflow</a>.
 */
@Test
public void buildMergedConfigWithAtWebAppConfigurationWithAnnotationAndClassesOnSuperclass() {
	Class<?> webTestClass = WebClassesFoo.class;
	Class<?> standardTestClass = ClassesFoo.class;
	WebMergedContextConfiguration webMergedConfig = (WebMergedContextConfiguration) buildMergedContextConfiguration(webTestClass);
	MergedContextConfiguration standardMergedConfig = buildMergedContextConfiguration(standardTestClass);

	assertEquals(webMergedConfig, webMergedConfig);
	assertEquals(standardMergedConfig, standardMergedConfig);
	assertNotEquals(standardMergedConfig, webMergedConfig);
	assertNotEquals(webMergedConfig, standardMergedConfig);

	assertMergedConfig(webMergedConfig, webTestClass, EMPTY_STRING_ARRAY, new Class<?>[] { FooConfig.class },
		WebDelegatingSmartContextLoader.class);
	assertMergedConfig(standardMergedConfig, standardTestClass, EMPTY_STRING_ARRAY,
		new Class<?>[] { FooConfig.class }, DelegatingSmartContextLoader.class);
}
 
Example #6
Source File: DefaultContextCache.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void put(MergedContextConfiguration key, ApplicationContext context) {
	Assert.notNull(key, "Key must not be null");
	Assert.notNull(context, "ApplicationContext must not be null");

	this.contextMap.put(key, context);
	MergedContextConfiguration child = key;
	MergedContextConfiguration parent = child.getParent();
	while (parent != null) {
		Set<MergedContextConfiguration> list = this.hierarchyMap.computeIfAbsent(parent, k -> new HashSet<>());
		list.add(child);
		child = parent;
		parent = child.getParent();
	}
}
 
Example #7
Source File: BootstrapTestUtilsMergedConfigTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Introduced to investigate claims made in a discussion on
 * <a href="https://stackoverflow.com/questions/24725438/what-could-cause-a-class-implementing-applicationlistenercontextrefreshedevent">Stack Overflow</a>.
 */
@Test
public void buildMergedConfigWithAtWebAppConfigurationWithAnnotationAndClassesOnSuperclass() {
	Class<?> webTestClass = WebClassesFoo.class;
	Class<?> standardTestClass = ClassesFoo.class;
	WebMergedContextConfiguration webMergedConfig = (WebMergedContextConfiguration) buildMergedContextConfiguration(webTestClass);
	MergedContextConfiguration standardMergedConfig = buildMergedContextConfiguration(standardTestClass);

	assertEquals(webMergedConfig, webMergedConfig);
	assertEquals(standardMergedConfig, standardMergedConfig);
	assertNotEquals(standardMergedConfig, webMergedConfig);
	assertNotEquals(webMergedConfig, standardMergedConfig);

	assertMergedConfig(webMergedConfig, webTestClass, EMPTY_STRING_ARRAY, array(FooConfig.class),
		WebDelegatingSmartContextLoader.class);
	assertMergedConfig(standardMergedConfig, standardTestClass, EMPTY_STRING_ARRAY,
		array(FooConfig.class), DelegatingSmartContextLoader.class);
}
 
Example #8
Source File: DefaultContextCache.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
protected boolean removeEldestEntry(Map.Entry<MergedContextConfiguration, ApplicationContext> eldest) {
	if (this.size() > DefaultContextCache.this.getMaxSize()) {
		// Do NOT delete "DefaultContextCache.this."; otherwise, we accidentally
		// invoke java.util.Map.remove(Object, Object).
		DefaultContextCache.this.remove(eldest.getKey(), HierarchyMode.CURRENT_LEVEL);
	}

	// Return false since we invoke a custom eviction algorithm.
	return false;
}
 
Example #9
Source File: DelegatingSmartContextLoaderTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void loadContextWithXmlConfig() throws Exception {
	MergedContextConfiguration mergedConfig = new MergedContextConfiguration(
			XmlTestCase.class,
			new String[] {"classpath:/org/springframework/test/context/support/DelegatingSmartContextLoaderTests$XmlTestCase-context.xml"},
			EMPTY_CLASS_ARRAY, EMPTY_STRING_ARRAY, loader);
	assertApplicationContextLoadsAndContainsFooString(mergedConfig);
}
 
Example #10
Source File: BootstrapTestUtilsMergedConfigTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void buildMergedConfigWithLocalAnnotationAndOverriddenContextLoaderAndLocations() {
	Class<?> testClass = PropertiesLocationsFoo.class;
	Class<? extends ContextLoader> expectedContextLoaderClass = GenericPropertiesContextLoader.class;
	MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass);

	assertMergedConfig(mergedConfig, testClass, new String[] { "classpath:/foo.properties" }, EMPTY_CLASS_ARRAY,
		expectedContextLoaderClass);
}
 
Example #11
Source File: DelegatingSmartContextLoaderTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * @since 4.1
 */
@Test
public void loadContextWithLocationsAndConfigurationClasses() throws Exception {
	MergedContextConfiguration mergedConfig = new MergedContextConfiguration(getClass(),
			new String[] {"test.xml"}, new Class<?>[] {getClass()}, EMPTY_STRING_ARRAY, loader);
	assertThatIllegalStateException().isThrownBy(() ->
			loader.loadContext(mergedConfig))
		.withMessageStartingWith("Neither")
		.withMessageContaining("declare either 'locations' or 'classes' but not both.");
}
 
Example #12
Source File: DelegatingSmartContextLoaderTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void loadContextWithoutLocationsAndConfigurationClasses() throws Exception {
	MergedContextConfiguration mergedConfig = new MergedContextConfiguration(
			getClass(), EMPTY_STRING_ARRAY, EMPTY_CLASS_ARRAY, EMPTY_STRING_ARRAY, loader);
	assertThatIllegalStateException().isThrownBy(() ->
			loader.loadContext(mergedConfig))
		.withMessageStartingWith("Neither")
		.withMessageContaining("was able to load an ApplicationContext from");
}
 
Example #13
Source File: GenericXmlContextLoader.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Ensure that the supplied {@link MergedContextConfiguration} does not
 * contain {@link MergedContextConfiguration#getClasses() classes}.
 * @since 4.0.4
 * @see AbstractGenericContextLoader#validateMergedContextConfiguration
 */
@Override
protected void validateMergedContextConfiguration(MergedContextConfiguration mergedConfig) {
	if (mergedConfig.hasClasses()) {
		String msg = String.format(
			"Test class [%s] has been configured with @ContextConfiguration's 'classes' attribute %s, "
					+ "but %s does not support annotated classes.", mergedConfig.getTestClass().getName(),
			ObjectUtils.nullSafeToString(mergedConfig.getClasses()), getClass().getSimpleName());
		logger.error(msg);
		throw new IllegalStateException(msg);
	}
}
 
Example #14
Source File: GenericPropertiesContextLoaderTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void configMustNotContainAnnotatedClasses() throws Exception {
	expectedException.expect(IllegalStateException.class);
	expectedException.expectMessage(containsString("does not support annotated classes"));

	GenericPropertiesContextLoader loader = new GenericPropertiesContextLoader();
	MergedContextConfiguration mergedConfig = new MergedContextConfiguration(getClass(), EMPTY_STRING_ARRAY,
		new Class<?>[] { getClass() }, EMPTY_STRING_ARRAY, loader);
	loader.loadContext(mergedConfig);
}
 
Example #15
Source File: BootstrapTestUtilsContextInitializerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void buildMergedConfigWithOverriddenInitializers() {
	Class<?> testClass = OverriddenInitializersBar.class;
	Class<?>[] expectedClasses = new Class<?>[] { FooConfig.class, BarConfig.class };
	Set<Class<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>>> expectedInitializerClasses//
	= new HashSet<Class<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>>>();
	expectedInitializerClasses.add(BarInitializer.class);

	MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass);

	assertMergedConfig(mergedConfig, testClass, EMPTY_STRING_ARRAY, expectedClasses, expectedInitializerClasses,
		DelegatingSmartContextLoader.class);
}
 
Example #16
Source File: BootstrapTestUtilsMergedConfigTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void buildMergedConfigWithMetaAnnotationAndLocations() {
	Class<?> testClass = MetaLocationsFoo.class;
	MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass);

	assertMergedConfig(mergedConfig, testClass, array("classpath:/foo.xml"), EMPTY_CLASS_ARRAY,
		DelegatingSmartContextLoader.class);
}
 
Example #17
Source File: BootstrapTestUtilsMergedConfigTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void buildMergedConfigWithLocalAnnotationAndClasses() {
	Class<?> testClass = ClassesFoo.class;
	MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass);

	assertMergedConfig(mergedConfig, testClass, EMPTY_STRING_ARRAY, array(FooConfig.class),
		DelegatingSmartContextLoader.class);
}
 
Example #18
Source File: BootstrapTestUtilsMergedConfigTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void buildImplicitMergedConfigWithoutAnnotation() {
	Class<?> testClass = Enigma.class;
	MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass);

	assertMergedConfig(mergedConfig, testClass, EMPTY_STRING_ARRAY, EMPTY_CLASS_ARRAY, DelegatingSmartContextLoader.class);
}
 
Example #19
Source File: BootstrapTestUtilsContextInitializerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void buildMergedConfigWithLocalAndInheritedInitializer() {
	Class<?> testClass = InitializersBar.class;
	MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass);

	assertMergedConfig(mergedConfig, testClass, EMPTY_STRING_ARRAY, classes(FooConfig.class, BarConfig.class),
		initializers(FooInitializer.class, BarInitializer.class), DelegatingSmartContextLoader.class);
}
 
Example #20
Source File: BootstrapTestUtilsContextInitializerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void buildMergedConfigWithSingleLocalInitializer() {
	Class<?> testClass = SingleInitializer.class;
	MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass);

	assertMergedConfig(mergedConfig, testClass, EMPTY_STRING_ARRAY, EMPTY_CLASS_ARRAY,
		initializers(FooInitializer.class), DelegatingSmartContextLoader.class);
}
 
Example #21
Source File: AbstractContextConfigurationUtilsTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
void assertMergedConfig(MergedContextConfiguration mergedConfig, Class<?> expectedTestClass,
		String[] expectedLocations, Class<?>[] expectedClasses,
		Class<? extends ContextLoader> expectedContextLoaderClass) {

	assertMergedConfig(mergedConfig, expectedTestClass, expectedLocations, expectedClasses,
			EMPTY_INITIALIZER_CLASSES, expectedContextLoaderClass);
}
 
Example #22
Source File: BootstrapTestUtilsContextInitializerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void buildMergedConfigWithOverriddenInitializers() {
	Class<?> testClass = OverriddenInitializersBar.class;
	MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass);

	assertMergedConfig(mergedConfig, testClass, EMPTY_STRING_ARRAY, classes(FooConfig.class, BarConfig.class),
		initializers(BarInitializer.class), DelegatingSmartContextLoader.class);
}
 
Example #23
Source File: AbstractContextConfigurationUtilsTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
MergedContextConfiguration buildMergedContextConfiguration(Class<?> testClass) {
	CacheAwareContextLoaderDelegate cacheAwareContextLoaderDelegate = Mockito.mock(CacheAwareContextLoaderDelegate.class);
	BootstrapContext bootstrapContext = BootstrapTestUtils.buildBootstrapContext(testClass, cacheAwareContextLoaderDelegate);
	TestContextBootstrapper bootstrapper = BootstrapTestUtils.resolveTestContextBootstrapper(bootstrapContext);
	return bootstrapper.buildMergedContextConfiguration();
}
 
Example #24
Source File: BootstrapTestUtilsContextInitializerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void buildMergedConfigWithLocalInitializer() {
	Class<?> testClass = InitializersFoo.class;
	Class<?>[] expectedClasses = new Class<?>[] { FooConfig.class };
	Set<Class<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>>> expectedInitializerClasses//
	= new HashSet<Class<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>>>();
	expectedInitializerClasses.add(FooInitializer.class);

	MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass);

	assertMergedConfig(mergedConfig, testClass, EMPTY_STRING_ARRAY, expectedClasses, expectedInitializerClasses,
		DelegatingSmartContextLoader.class);
}
 
Example #25
Source File: BootstrapTestUtilsMergedConfigTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void buildMergedConfigWithLocalAndInheritedAnnotationsAndLocations() {
	Class<?> testClass = LocationsBar.class;
	String[] expectedLocations = array("/foo.xml", "/bar.xml");
	MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass);

	assertMergedConfig(mergedConfig, testClass, expectedLocations, EMPTY_CLASS_ARRAY,
		AnnotationConfigContextLoader.class);
}
 
Example #26
Source File: DelegatingSmartContextLoaderTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private void assertApplicationContextLoadsAndContainsFooString(MergedContextConfiguration mergedConfig)
		throws Exception {
	ApplicationContext applicationContext = loader.loadContext(mergedConfig);
	assertNotNull(applicationContext);
	assertEquals("foo", applicationContext.getBean(String.class));
	assertTrue(applicationContext instanceof ConfigurableApplicationContext);
	((ConfigurableApplicationContext) applicationContext).close();
}
 
Example #27
Source File: DefaultCacheAwareContextLoaderDelegate.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public ApplicationContext loadContext(MergedContextConfiguration mergedContextConfiguration) {
	synchronized (this.contextCache) {
		ApplicationContext context = this.contextCache.get(mergedContextConfiguration);
		if (context == null) {
			try {
				context = loadContextInternal(mergedContextConfiguration);
				if (logger.isDebugEnabled()) {
					logger.debug(String.format("Storing ApplicationContext [%s] in cache under key [%s]",
							System.identityHashCode(context), mergedContextConfiguration));
				}
				this.contextCache.put(mergedContextConfiguration, context);
			}
			catch (Exception ex) {
				throw new IllegalStateException("Failed to load ApplicationContext", ex);
			}
		}
		else {
			if (logger.isDebugEnabled()) {
				logger.debug(String.format("Retrieved ApplicationContext [%s] from cache with key [%s]",
						System.identityHashCode(context), mergedContextConfiguration));
			}
		}

		this.contextCache.logStatistics();

		return context;
	}
}
 
Example #28
Source File: BootstrapTestUtilsMergedConfigTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void buildMergedConfigWithLocalAndInheritedAnnotationsAndClasses() {
	Class<?> testClass = ClassesBar.class;
	Class<?>[] expectedClasses = new Class<?>[] { FooConfig.class, BarConfig.class };
	MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass);

	assertMergedConfig(mergedConfig, testClass, EMPTY_STRING_ARRAY, expectedClasses,
		AnnotationConfigContextLoader.class);
}
 
Example #29
Source File: BootstrapTestUtilsContextInitializerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void buildMergedConfigWithOverriddenInitializersAndClasses() {
	Class<?> testClass = OverriddenInitializersAndClassesBar.class;
	MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass);

	assertMergedConfig(mergedConfig, testClass, EMPTY_STRING_ARRAY, classes(BarConfig.class),
		initializers(BarInitializer.class), DelegatingSmartContextLoader.class);
}
 
Example #30
Source File: BootstrapTestUtilsContextInitializerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void buildMergedConfigWithOverriddenInitializersAndClasses() {
	Class<?> testClass = OverriddenInitializersAndClassesBar.class;
	MergedContextConfiguration mergedConfig = buildMergedContextConfiguration(testClass);

	assertMergedConfig(mergedConfig, testClass, EMPTY_STRING_ARRAY, classes(BarConfig.class),
		initializers(BarInitializer.class), DelegatingSmartContextLoader.class);
}