Search in sources :

Example 1 with LifecycleBean

use of org.springframework.tests.sample.beans.LifecycleBean in project spring-framework by spring-projects.

the class XmlListableBeanFactoryTests method lifecycleMethods.

@Test
public void lifecycleMethods() throws Exception {
    LifecycleBean bean = (LifecycleBean) getBeanFactory().getBean("lifecycle");
    bean.businessMethod();
}
Also used : LifecycleBean(org.springframework.tests.sample.beans.LifecycleBean) Test(org.junit.Test)

Example 2 with LifecycleBean

use of org.springframework.tests.sample.beans.LifecycleBean in project spring-framework by spring-projects.

the class AbstractBeanFactoryTests method lifecycleCallbacks.

/**
	 * Test that InitializingBean/BeanFactoryAware/DisposableBean objects receive the
	 * afterPropertiesSet() callback before BeanFactoryAware callbacks
	 */
@Test
public void lifecycleCallbacks() {
    LifecycleBean lb = (LifecycleBean) getBeanFactory().getBean("lifecycle");
    assertEquals("lifecycle", lb.getBeanName());
    // The dummy business method will throw an exception if the
    // necessary callbacks weren't invoked in the right order.
    lb.businessMethod();
    assertTrue("Not destroyed", !lb.isDestroyed());
}
Also used : LifecycleBean(org.springframework.tests.sample.beans.LifecycleBean) Test(org.junit.Test)

Example 3 with LifecycleBean

use of org.springframework.tests.sample.beans.LifecycleBean in project spring-framework by spring-projects.

the class AbstractApplicationContextTests method closeTriggersDestroy.

@Test
public void closeTriggersDestroy() {
    LifecycleBean lb = (LifecycleBean) applicationContext.getBean("lifecycle");
    assertTrue("Not destroyed", !lb.isDestroyed());
    applicationContext.close();
    if (applicationContext.getParent() != null) {
        ((ConfigurableApplicationContext) applicationContext.getParent()).close();
    }
    assertTrue("Destroyed", lb.isDestroyed());
    applicationContext.close();
    if (applicationContext.getParent() != null) {
        ((ConfigurableApplicationContext) applicationContext.getParent()).close();
    }
    assertTrue("Destroyed", lb.isDestroyed());
}
Also used : LifecycleBean(org.springframework.tests.sample.beans.LifecycleBean) Test(org.junit.Test)

Example 4 with LifecycleBean

use of org.springframework.tests.sample.beans.LifecycleBean in project spring-framework by spring-projects.

the class ContextLoaderTests method testContextLoaderListenerWithDefaultContext.

@Test
public void testContextLoaderListenerWithDefaultContext() {
    MockServletContext sc = new MockServletContext("");
    sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "/org/springframework/web/context/WEB-INF/applicationContext.xml " + "/org/springframework/web/context/WEB-INF/context-addition.xml");
    ServletContextListener listener = new ContextLoaderListener();
    ServletContextEvent event = new ServletContextEvent(sc);
    listener.contextInitialized(event);
    WebApplicationContext context = (WebApplicationContext) sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
    assertTrue("Correct WebApplicationContext exposed in ServletContext", context instanceof XmlWebApplicationContext);
    assertTrue(WebApplicationContextUtils.getRequiredWebApplicationContext(sc) instanceof XmlWebApplicationContext);
    LifecycleBean lb = (LifecycleBean) context.getBean("lifecycle");
    assertTrue("Has father", context.containsBean("father"));
    assertTrue("Has rod", context.containsBean("rod"));
    assertTrue("Has kerry", context.containsBean("kerry"));
    assertTrue("Not destroyed", !lb.isDestroyed());
    assertFalse(context.containsBean("beans1.bean1"));
    assertFalse(context.containsBean("beans1.bean2"));
    listener.contextDestroyed(event);
    assertTrue("Destroyed", lb.isDestroyed());
    assertNull(sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE));
    assertNull(WebApplicationContextUtils.getWebApplicationContext(sc));
}
Also used : XmlWebApplicationContext(org.springframework.web.context.support.XmlWebApplicationContext) ServletContextListener(javax.servlet.ServletContextListener) LifecycleBean(org.springframework.tests.sample.beans.LifecycleBean) MockServletContext(org.springframework.mock.web.test.MockServletContext) ServletContextEvent(javax.servlet.ServletContextEvent) SimpleWebApplicationContext(org.springframework.web.servlet.SimpleWebApplicationContext) XmlWebApplicationContext(org.springframework.web.context.support.XmlWebApplicationContext) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)4 LifecycleBean (org.springframework.tests.sample.beans.LifecycleBean)4 ServletContextEvent (javax.servlet.ServletContextEvent)1 ServletContextListener (javax.servlet.ServletContextListener)1 MockServletContext (org.springframework.mock.web.test.MockServletContext)1 XmlWebApplicationContext (org.springframework.web.context.support.XmlWebApplicationContext)1 SimpleWebApplicationContext (org.springframework.web.servlet.SimpleWebApplicationContext)1