Skip to content

Commit

Permalink
AbstractApplicationContext allows for re-refresh and re-close
Browse files Browse the repository at this point in the history
Issue: SPR-13425
  • Loading branch information
jhoeller committed Sep 4, 2015
1 parent c663fd5 commit 811de8e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
Expand Up @@ -567,6 +567,7 @@ public void refresh() throws BeansException, IllegalStateException {
*/
protected void prepareRefresh() {
this.startupDate = System.currentTimeMillis();
this.closed.set(false);
this.active.set(true);

if (logger.isInfoEnabled()) {
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -49,7 +49,7 @@
* @author Juergen Hoeller
* @author Chris Beams
*/
public final class ClassPathXmlApplicationContextTests {
public class ClassPathXmlApplicationContextTests {

private static final String PATH = "/org/springframework/context/support/";
private static final String RESOURCE_CONTEXT = PATH + "ClassPathXmlApplicationContextTests-resource.xml";
Expand Down Expand Up @@ -82,13 +82,23 @@ public void testSingleConfigLocation() {
@Test
public void testMultipleConfigLocations() {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
new String[] { FQ_CONTEXT_B, FQ_CONTEXT_C, FQ_CONTEXT_A});
FQ_CONTEXT_B, FQ_CONTEXT_C, FQ_CONTEXT_A);
assertTrue(ctx.containsBean("service"));
assertTrue(ctx.containsBean("logicOne"));
assertTrue(ctx.containsBean("logicTwo"));

// re-refresh (after construction refresh)
Service service = (Service) ctx.getBean("service");
ctx.refresh();
assertTrue(service.isProperlyDestroyed());

// regular close call
service = (Service) ctx.getBean("service");
ctx.close();
assertTrue(service.isProperlyDestroyed());

// re-activating and re-closing the context (SPR-13425)
ctx.refresh();
service = (Service) ctx.getBean("service");
ctx.close();
assertTrue(service.isProperlyDestroyed());
Expand All @@ -107,16 +117,15 @@ public void testConfigLocationPattern() {

@Test
public void testSingleConfigLocationWithClass() {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
SIMPLE_CONTEXT, getClass());
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(SIMPLE_CONTEXT, getClass());
assertTrue(ctx.containsBean("someMessageSource"));
ctx.close();
}

@Test
public void testAliasWithPlaceholder() {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
new String[] { FQ_CONTEXT_B, FQ_ALIASED_CONTEXT_C, FQ_CONTEXT_A});
FQ_CONTEXT_B, FQ_ALIASED_CONTEXT_C, FQ_CONTEXT_A);
assertTrue(ctx.containsBean("service"));
assertTrue(ctx.containsBean("logicOne"));
assertTrue(ctx.containsBean("logicTwo"));
Expand Down Expand Up @@ -144,16 +153,14 @@ public void testContextWithInvalidValueType() throws IOException {
private void checkExceptionFromInvalidValueType(Throwable ex) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ex.printStackTrace(new PrintStream(baos));
String dump = FileCopyUtils.copyToString(
new InputStreamReader(new ByteArrayInputStream(baos.toByteArray())));
String dump = FileCopyUtils.copyToString(new InputStreamReader(new ByteArrayInputStream(baos.toByteArray())));
assertTrue(dump.contains("someMessageSource"));
assertTrue(dump.contains("useCodeAsDefaultMessage"));
}

@Test
public void testContextWithInvalidLazyClass() {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
INVALID_CLASS_CONTEXT, getClass());
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(INVALID_CLASS_CONTEXT, getClass());
assertTrue(ctx.containsBean("someMessageSource"));
try {
ctx.getBean("someMessageSource");
Expand All @@ -167,8 +174,7 @@ public void testContextWithInvalidLazyClass() {

@Test
public void testContextWithClassNameThatContainsPlaceholder() {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
CLASS_WITH_PLACEHOLDER_CONTEXT, getClass());
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(CLASS_WITH_PLACEHOLDER_CONTEXT, getClass());
assertTrue(ctx.containsBean("someMessageSource"));
assertTrue(ctx.getBean("someMessageSource") instanceof StaticMessageSource);
ctx.close();
Expand Down Expand Up @@ -282,7 +288,7 @@ public void testAliasThatOverridesParent() {
@Test
public void testAliasThatOverridesEarlierBean() {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
new String[] {FQ_SIMPLE_CONTEXT, ALIAS_THAT_OVERRIDES_PARENT_CONTEXT});
FQ_SIMPLE_CONTEXT, ALIAS_THAT_OVERRIDES_PARENT_CONTEXT);
Object myMs = ctx.getBean("myMessageSource");
Object someMs2 = ctx.getBean("someMessageSource");
assertSame(myMs, someMs2);
Expand Down

0 comments on commit 811de8e

Please sign in to comment.