Search in sources :

Example 6 with WebApplicationContext

use of org.springframework.web.context.WebApplicationContext in project redisson by redisson.

the class RedissonSessionManagerTest method testExpire.

@Test
public void testExpire() throws Exception {
    Initializer.CONFIG_CLASS = ConfigTimeout.class;
    // start the server at http://localhost:8080/myapp
    TomcatServer server = new TomcatServer("myapp", 8080, "src/test/");
    server.start();
    WebApplicationContext wa = WebApplicationContextUtils.getRequiredWebApplicationContext(server.getServletContext());
    SessionEventsListener listener = wa.getBean(SessionEventsListener.class);
    Executor executor = Executor.newInstance();
    BasicCookieStore cookieStore = new BasicCookieStore();
    executor.use(cookieStore);
    write(executor, "test", "1234");
    Cookie cookie = cookieStore.getCookies().get(0);
    Assert.assertEquals(1, listener.getSessionCreatedEvents());
    Assert.assertEquals(0, listener.getSessionExpiredEvents());
    Executor.closeIdleConnections();
    Thread.sleep(6000);
    Assert.assertEquals(1, listener.getSessionCreatedEvents());
    Assert.assertEquals(1, listener.getSessionExpiredEvents());
    executor = Executor.newInstance();
    cookieStore = new BasicCookieStore();
    cookieStore.addCookie(cookie);
    executor.use(cookieStore);
    read(executor, "test", "null");
    Assert.assertEquals(2, listener.getSessionCreatedEvents());
    write(executor, "test", "1234");
    Thread.sleep(3000);
    read(executor, "test", "1234");
    Thread.sleep(3000);
    Assert.assertEquals(1, listener.getSessionExpiredEvents());
    Thread.sleep(1000);
    Assert.assertEquals(1, listener.getSessionExpiredEvents());
    Thread.sleep(3000);
    Assert.assertEquals(2, listener.getSessionExpiredEvents());
    Executor.closeIdleConnections();
    server.stop();
}
Also used : Cookie(org.apache.http.cookie.Cookie) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) Executor(org.apache.http.client.fluent.Executor) WebApplicationContext(org.springframework.web.context.WebApplicationContext) Test(org.junit.Test)

Example 7 with WebApplicationContext

use of org.springframework.web.context.WebApplicationContext in project redisson by redisson.

the class RedissonSessionManagerTest method testInvalidate.

@Test
public void testInvalidate() throws Exception {
    // start the server at http://localhost:8080/myapp
    TomcatServer server = new TomcatServer("myapp", 8080, "src/test/");
    server.start();
    WebApplicationContext wa = WebApplicationContextUtils.getRequiredWebApplicationContext(server.getServletContext());
    SessionEventsListener listener = wa.getBean(SessionEventsListener.class);
    Executor executor = Executor.newInstance();
    BasicCookieStore cookieStore = new BasicCookieStore();
    executor.use(cookieStore);
    write(executor, "test", "1234");
    Cookie cookie = cookieStore.getCookies().get(0);
    Assert.assertEquals(1, listener.getSessionCreatedEvents());
    Assert.assertEquals(0, listener.getSessionDeletedEvents());
    invalidate(executor);
    Assert.assertEquals(1, listener.getSessionCreatedEvents());
    Assert.assertEquals(1, listener.getSessionDeletedEvents());
    Executor.closeIdleConnections();
    executor = Executor.newInstance();
    cookieStore = new BasicCookieStore();
    cookieStore.addCookie(cookie);
    executor.use(cookieStore);
    read(executor, "test", "null");
    Executor.closeIdleConnections();
    server.stop();
}
Also used : Cookie(org.apache.http.cookie.Cookie) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) Executor(org.apache.http.client.fluent.Executor) WebApplicationContext(org.springframework.web.context.WebApplicationContext) Test(org.junit.Test)

Example 8 with WebApplicationContext

use of org.springframework.web.context.WebApplicationContext in project spring-boot by spring-projects.

the class SpringBootTestWebEnvironmentMockTests method validateWebApplicationContextIsSet.

@Test
public void validateWebApplicationContextIsSet() {
    WebApplicationContext fromServletContext = WebApplicationContextUtils.getWebApplicationContext(this.servletContext);
    assertThat(fromServletContext).isSameAs(this.context);
}
Also used : WebApplicationContext(org.springframework.web.context.WebApplicationContext) Test(org.junit.Test)

Example 9 with WebApplicationContext

use of org.springframework.web.context.WebApplicationContext in project spring-boot by spring-projects.

the class SpringBootServletInitializer method createRootApplicationContext.

protected WebApplicationContext createRootApplicationContext(ServletContext servletContext) {
    SpringApplicationBuilder builder = createSpringApplicationBuilder();
    builder.main(getClass());
    ApplicationContext parent = getExistingRootWebApplicationContext(servletContext);
    if (parent != null) {
        this.logger.info("Root context already created (using as parent).");
        servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, null);
        builder.initializers(new ParentContextApplicationContextInitializer(parent));
    }
    builder.initializers(new ServletContextApplicationContextInitializer(servletContext));
    builder.listeners(new ServletContextApplicationListener(servletContext));
    builder.contextClass(AnnotationConfigServletWebServerApplicationContext.class);
    builder = configure(builder);
    SpringApplication application = builder.build();
    if (application.getSources().isEmpty() && AnnotationUtils.findAnnotation(getClass(), Configuration.class) != null) {
        application.getSources().add(getClass());
    }
    Assert.state(!application.getSources().isEmpty(), "No SpringApplication sources have been defined. Either override the " + "configure method or add an @Configuration annotation");
    // Ensure error pages are registered
    if (this.registerErrorPageFilter) {
        application.getSources().add(ErrorPageFilterConfiguration.class);
    }
    return run(application);
}
Also used : AnnotationConfigServletWebServerApplicationContext(org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) SpringApplication(org.springframework.boot.SpringApplication) SpringApplicationBuilder(org.springframework.boot.builder.SpringApplicationBuilder) ParentContextApplicationContextInitializer(org.springframework.boot.builder.ParentContextApplicationContextInitializer)

Example 10 with WebApplicationContext

use of org.springframework.web.context.WebApplicationContext in project spring-boot by spring-projects.

the class SpringBootServletInitializer method onStartup.

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    // Logger initialization is deferred in case a ordered
    // LogServletContextInitializer is being used
    this.logger = LogFactory.getLog(getClass());
    WebApplicationContext rootAppContext = createRootApplicationContext(servletContext);
    if (rootAppContext != null) {
        servletContext.addListener(new ContextLoaderListener(rootAppContext) {

            @Override
            public void contextInitialized(ServletContextEvent event) {
            // no-op because the application context is already initialized
            }
        });
    } else {
        this.logger.debug("No ContextLoaderListener registered, as " + "createRootApplicationContext() did not " + "return an application context");
    }
}
Also used : ContextLoaderListener(org.springframework.web.context.ContextLoaderListener) ServletContextEvent(javax.servlet.ServletContextEvent) WebApplicationContext(org.springframework.web.context.WebApplicationContext)

Aggregations

WebApplicationContext (org.springframework.web.context.WebApplicationContext)100 Test (org.junit.Test)32 ServletContext (javax.servlet.ServletContext)17 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)15 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)13 ApplicationContext (org.springframework.context.ApplicationContext)11 GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)11 HashMap (java.util.HashMap)9 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)9 MockServletContext (org.springframework.mock.web.test.MockServletContext)8 ConfigurableWebApplicationContext (org.springframework.web.context.ConfigurableWebApplicationContext)7 Filter (javax.servlet.Filter)6 HttpServletResponse (javax.servlet.http.HttpServletResponse)6 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)6 Binding (groovy.lang.Binding)5 BeanBuilder (hudson.util.spring.BeanBuilder)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 DispatcherServlet (org.springframework.web.servlet.DispatcherServlet)5 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)4 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)4