Consistent overriding for all variants of init/destroy method inheritance

Issue: SPR-15532
This commit is contained in:
Juergen Hoeller
2017-08-18 00:13:49 +02:00
parent b94302b5bd
commit ac5e2599f7
5 changed files with 45 additions and 19 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2017 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.
@@ -26,6 +26,7 @@ import static org.junit.Assert.*;
/**
* @author Rob Harrop
* @author Juergen Hoeller
*/
public class DefaultLifecycleMethodsTests {
@@ -33,26 +34,31 @@ public class DefaultLifecycleMethodsTests {
@Before
public void setUp() throws Exception {
new XmlBeanDefinitionReader(this.beanFactory).loadBeanDefinitions(new ClassPathResource(
"defaultLifecycleMethods.xml", getClass()));
public void setup() throws Exception {
new XmlBeanDefinitionReader(this.beanFactory).loadBeanDefinitions(
new ClassPathResource("defaultLifecycleMethods.xml", getClass()));
}
@Test
public void lifecycleMethodsInvoked() {
LifecycleAwareBean bean = (LifecycleAwareBean) this.beanFactory.getBean("lifecycleAware");
assertTrue("Bean not initialized", bean.isInitCalled());
assertFalse("Custom init method called incorrectly", bean.isCustomInitCalled());
assertFalse("Bean destroyed too early", bean.isDestroyCalled());
this.beanFactory.destroySingletons();
assertTrue("Bean not destroyed", bean.isDestroyCalled());
assertFalse("Custom destroy method called incorrectly", bean.isCustomDestroyCalled());
}
@Test
public void lifecycleMethodsDisabled() throws Exception {
LifecycleAwareBean bean = (LifecycleAwareBean) this.beanFactory.getBean("lifecycleMethodsDisabled");
assertFalse("Bean init method called incorrectly", bean.isInitCalled());
assertFalse("Custom init method called incorrectly", bean.isCustomInitCalled());
this.beanFactory.destroySingletons();
assertFalse("Bean destroy method called incorrectly", bean.isDestroyCalled());
assertFalse("Custom destroy method called incorrectly", bean.isCustomDestroyCalled());
}
@Test
@@ -67,11 +73,32 @@ public class DefaultLifecycleMethodsTests {
@Test
public void overrideDefaultLifecycleMethods() throws Exception {
LifecycleAwareBean bean = (LifecycleAwareBean) this.beanFactory.getBean("overrideLifecycleMethods");
assertFalse("Default init method called incorrectly.", bean.isInitCalled());
assertTrue("Custom init method not called.", bean.isCustomInitCalled());
assertFalse("Default init method called incorrectly", bean.isInitCalled());
assertTrue("Custom init method not called", bean.isCustomInitCalled());
this.beanFactory.destroySingletons();
assertFalse("Default destory method called incorrectly.", bean.isDestroyCalled());
assertTrue("Custom destory method not called.", bean.isCustomDestroyCalled());
assertFalse("Default destroy method called incorrectly", bean.isDestroyCalled());
assertTrue("Custom destroy method not called", bean.isCustomDestroyCalled());
}
@Test
public void childWithDefaultLifecycleMethods() throws Exception {
LifecycleAwareBean bean = (LifecycleAwareBean) this.beanFactory.getBean("childWithDefaultLifecycleMethods");
assertTrue("Bean not initialized", bean.isInitCalled());
assertFalse("Custom init method called incorrectly", bean.isCustomInitCalled());
assertFalse("Bean destroyed too early", bean.isDestroyCalled());
this.beanFactory.destroySingletons();
assertTrue("Bean not destroyed", bean.isDestroyCalled());
assertFalse("Custom destroy method called incorrectly", bean.isCustomDestroyCalled());
}
@Test
public void childWithLifecycleMethodsDisabled() throws Exception {
LifecycleAwareBean bean = (LifecycleAwareBean) this.beanFactory.getBean("childWithLifecycleMethodsDisabled");
assertFalse("Bean init method called incorrectly", bean.isInitCalled());
assertFalse("Custom init method called incorrectly", bean.isCustomInitCalled());
this.beanFactory.destroySingletons();
assertFalse("Bean destroy method called incorrectly", bean.isDestroyCalled());
assertFalse("Custom destroy method called incorrectly", bean.isCustomDestroyCalled());
}