Search in sources :

Example 16 with ServletContextEvent

use of javax.servlet.ServletContextEvent in project spring-framework by spring-projects.

the class ContextLoaderTests method testContextLoaderListenerWithMixedContextInitializers.

@Test
public void testContextLoaderListenerWithMixedContextInitializers() {
    MockServletContext sc = new MockServletContext("");
    sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "org/springframework/web/context/WEB-INF/ContextLoaderTests-acc-context.xml");
    sc.addInitParameter(ContextLoader.CONTEXT_INITIALIZER_CLASSES_PARAM, TestContextInitializer.class.getName());
    sc.addInitParameter(ContextLoader.GLOBAL_INITIALIZER_CLASSES_PARAM, TestWebContextInitializer.class.getName());
    ContextLoaderListener listener = new ContextLoaderListener();
    listener.contextInitialized(new ServletContextEvent(sc));
    WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
    TestBean testBean = wac.getBean(TestBean.class);
    assertThat(testBean.getName(), equalTo("testName"));
    assertThat(wac.getServletContext().getAttribute("initialized"), notNullValue());
}
Also used : TestBean(org.springframework.tests.sample.beans.TestBean) 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)

Example 17 with ServletContextEvent

use of javax.servlet.ServletContextEvent in project spring-framework by spring-projects.

the class ContextLoaderTests method testContextLoaderListenerWithProgrammaticAndGlobalInitializers.

@Test
public void testContextLoaderListenerWithProgrammaticAndGlobalInitializers() {
    MockServletContext sc = new MockServletContext("");
    sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "org/springframework/web/context/WEB-INF/ContextLoaderTests-acc-context.xml");
    sc.addInitParameter(ContextLoader.GLOBAL_INITIALIZER_CLASSES_PARAM, TestWebContextInitializer.class.getName());
    ContextLoaderListener listener = new ContextLoaderListener();
    listener.setContextInitializers(new TestContextInitializer());
    listener.contextInitialized(new ServletContextEvent(sc));
    WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
    TestBean testBean = wac.getBean(TestBean.class);
    assertThat(testBean.getName(), equalTo("testName"));
    assertThat(wac.getServletContext().getAttribute("initialized"), notNullValue());
}
Also used : TestBean(org.springframework.tests.sample.beans.TestBean) 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)

Example 18 with ServletContextEvent

use of javax.servlet.ServletContextEvent in project spring-framework by spring-projects.

the class ContextLoaderTests method testContextLoaderWithCustomContext.

@Test
public void testContextLoaderWithCustomContext() throws Exception {
    MockServletContext sc = new MockServletContext("");
    sc.addInitParameter(ContextLoader.CONTEXT_CLASS_PARAM, "org.springframework.web.servlet.SimpleWebApplicationContext");
    ServletContextListener listener = new ContextLoaderListener();
    ServletContextEvent event = new ServletContextEvent(sc);
    listener.contextInitialized(event);
    WebApplicationContext wc = (WebApplicationContext) sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
    assertTrue("Correct WebApplicationContext exposed in ServletContext", wc instanceof SimpleWebApplicationContext);
}
Also used : ServletContextListener(javax.servlet.ServletContextListener) SimpleWebApplicationContext(org.springframework.web.servlet.SimpleWebApplicationContext) 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)

Example 19 with ServletContextEvent

use of javax.servlet.ServletContextEvent in project spring-framework by spring-projects.

the class ContextLoaderTests method testContextLoaderWithInvalidLocation.

@Test
public void testContextLoaderWithInvalidLocation() throws Exception {
    MockServletContext sc = new MockServletContext("");
    sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "/WEB-INF/myContext.xml");
    ServletContextListener listener = new ContextLoaderListener();
    ServletContextEvent event = new ServletContextEvent(sc);
    try {
        listener.contextInitialized(event);
        fail("Should have thrown BeanDefinitionStoreException");
    } catch (BeanDefinitionStoreException ex) {
        // expected
        assertTrue(ex.getCause() instanceof FileNotFoundException);
    }
}
Also used : ServletContextListener(javax.servlet.ServletContextListener) BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException) FileNotFoundException(java.io.FileNotFoundException) MockServletContext(org.springframework.mock.web.test.MockServletContext) ServletContextEvent(javax.servlet.ServletContextEvent) Test(org.junit.Test)

Example 20 with ServletContextEvent

use of javax.servlet.ServletContextEvent in project gocd by gocd.

the class DelegatingServletTest method shouldDelegateToTheGivenServlet.

@Test
public void shouldDelegateToTheGivenServlet() throws IOException, ServletException {
    MockServletContext ctx = new MockServletContext();
    ctx.addInitParameter(DelegatingListener.DELEGATE_SERVLET, DummyServlet.class.getCanonicalName());
    ServletContextEvent evt = new ServletContextEvent(ctx);
    DelegatingListener listener = new DelegatingListener();
    listener.contextInitialized(evt);
    assertThat((DummyServlet) ctx.getAttribute(DelegatingListener.DELEGATE_SERVLET), isA(DummyServlet.class));
    DelegatingServlet servlet = new DelegatingServlet();
    servlet.init(new MockServletConfig(ctx));
    servlet.service(httpServletRequest, new MockHttpServletResponse());
    verify(servletRequestWrapper).setRequestURI("/go/stuff/action");
}
Also used : MockServletConfig(org.springframework.mock.web.MockServletConfig) MockServletContext(org.springframework.mock.web.MockServletContext) ServletContextEvent(javax.servlet.ServletContextEvent) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Aggregations

ServletContextEvent (javax.servlet.ServletContextEvent)70 Test (org.junit.Test)42 MockServletContext (org.springframework.mock.web.test.MockServletContext)21 ServletContext (javax.servlet.ServletContext)18 ServletContextListener (javax.servlet.ServletContextListener)17 Injector (com.google.inject.Injector)10 ContextLoaderListener (org.springframework.web.context.ContextLoaderListener)10 XmlWebApplicationContext (org.springframework.web.context.support.XmlWebApplicationContext)8 SimpleWebApplicationContext (org.springframework.web.servlet.SimpleWebApplicationContext)8 Test (org.testng.annotations.Test)8 BrokerService (org.apache.activemq.broker.BrokerService)7 IOException (java.io.IOException)6 Properties (java.util.Properties)6 TestBean (org.springframework.tests.sample.beans.TestBean)6 ArrayList (java.util.ArrayList)5 SystemPropertiesHelper.overrideSystemProperties (org.eclipse.che.commons.test.SystemPropertiesHelper.overrideSystemProperties)5 File (java.io.File)4 MalformedURLException (java.net.MalformedURLException)3 HashMap (java.util.HashMap)3 ServletException (javax.servlet.ServletException)3