Search in sources :

Example 96 with StaticWebApplicationContext

use of org.springframework.web.context.support.StaticWebApplicationContext in project spring-framework by spring-projects.

the class ScriptTemplateViewTests method setup.

@BeforeEach
public void setup() {
    this.configurer = new ScriptTemplateConfigurer();
    this.wac = new StaticWebApplicationContext();
    this.wac.getBeanFactory().registerSingleton("scriptTemplateConfigurer", this.configurer);
    this.view = new ScriptTemplateView();
}
Also used : StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 97 with StaticWebApplicationContext

use of org.springframework.web.context.support.StaticWebApplicationContext in project spring-framework by spring-projects.

the class DispatcherServletTests method detectHandlerMappingFromParent.

@Test
public void detectHandlerMappingFromParent() throws ServletException, IOException {
    // create a parent context that includes a mapping
    StaticWebApplicationContext parent = new StaticWebApplicationContext();
    parent.setServletContext(getServletContext());
    parent.registerSingleton("parentHandler", ControllerFromParent.class, new MutablePropertyValues());
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.addPropertyValue(new PropertyValue("mappings", URL_KNOWN_ONLY_PARENT + "=parentHandler"));
    parent.registerSingleton("parentMapping", SimpleUrlHandlerMapping.class, pvs);
    parent.refresh();
    DispatcherServlet complexDispatcherServlet = new DispatcherServlet();
    // will have parent
    complexDispatcherServlet.setContextClass(ComplexWebApplicationContext.class);
    complexDispatcherServlet.setNamespace("test");
    ServletConfig config = new MockServletConfig(getServletContext(), "complex");
    config.getServletContext().setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, parent);
    complexDispatcherServlet.init(config);
    MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", URL_KNOWN_ONLY_PARENT);
    MockHttpServletResponse response = new MockHttpServletResponse();
    complexDispatcherServlet.service(request, response);
    assertThat(response.getStatus() == HttpServletResponse.SC_NOT_FOUND).as("Matched through parent controller/handler pair: not response=" + response.getStatus()).isFalse();
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) MockServletConfig(org.springframework.web.testfixture.servlet.MockServletConfig) ServletConfig(jakarta.servlet.ServletConfig) PropertyValue(org.springframework.beans.PropertyValue) MockServletConfig(org.springframework.web.testfixture.servlet.MockServletConfig) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 98 with StaticWebApplicationContext

use of org.springframework.web.context.support.StaticWebApplicationContext in project spring-framework by spring-projects.

the class DispatcherServletTests method cleanupAfterIncludeWithRestore.

@Test
public void cleanupAfterIncludeWithRestore() throws ServletException, IOException {
    MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/main.do");
    MockHttpServletResponse response = new MockHttpServletResponse();
    request.setAttribute("test1", "value1");
    request.setAttribute("test2", "value2");
    WebApplicationContext wac = new StaticWebApplicationContext();
    request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    TestBean command = new TestBean();
    request.setAttribute("command", command);
    request.setAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE, "/form.do");
    simpleDispatcherServlet.service(request, response);
    assertThat(request.getAttribute("test1")).isEqualTo("value1");
    assertThat(request.getAttribute("test2")).isEqualTo("value2");
    assertThat(request.getAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE)).isSameAs(wac);
}
Also used : TestBean(org.springframework.beans.testfixture.beans.TestBean) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) ConfigurableWebApplicationContext(org.springframework.web.context.ConfigurableWebApplicationContext) Test(org.junit.jupiter.api.Test)

Example 99 with StaticWebApplicationContext

use of org.springframework.web.context.support.StaticWebApplicationContext in project spring-framework by spring-projects.

the class WebMvcConfigurationSupportExtensionTests method setUp.

@BeforeEach
public void setUp() {
    this.context = new StaticWebApplicationContext();
    this.context.setServletContext(new MockServletContext(new FileSystemResourceLoader()));
    this.context.registerSingleton("controller", TestController.class);
    this.context.registerSingleton("userController", UserController.class);
    this.config = new TestWebMvcConfigurationSupport();
    this.config.setApplicationContext(this.context);
    this.config.setServletContext(this.context.getServletContext());
}
Also used : FileSystemResourceLoader(org.springframework.core.io.FileSystemResourceLoader) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 100 with StaticWebApplicationContext

use of org.springframework.web.context.support.StaticWebApplicationContext in project spring-security by spring-projects.

the class HttpSessionEventPublisherTests method publishedEventIsReceivedbyListener.

/**
 * It's not that complicated so we'll just run it straight through here.
 */
@Test
public void publishedEventIsReceivedbyListener() {
    HttpSessionEventPublisher publisher = new HttpSessionEventPublisher();
    StaticWebApplicationContext context = new StaticWebApplicationContext();
    MockServletContext servletContext = new MockServletContext();
    servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, context);
    context.setServletContext(servletContext);
    context.registerSingleton("listener", MockApplicationListener.class, null);
    context.refresh();
    MockHttpSession session = new MockHttpSession(servletContext);
    MockApplicationListener listener = (MockApplicationListener) context.getBean("listener");
    HttpSessionEvent event = new HttpSessionEvent(session);
    publisher.sessionCreated(event);
    assertThat(listener.getCreatedEvent()).isNotNull();
    assertThat(listener.getDestroyedEvent()).isNull();
    assertThat(listener.getCreatedEvent().getSession()).isEqualTo(session);
    listener.setCreatedEvent(null);
    listener.setDestroyedEvent(null);
    publisher.sessionDestroyed(event);
    assertThat(listener.getDestroyedEvent()).isNotNull();
    assertThat(listener.getCreatedEvent()).isNull();
    assertThat(listener.getDestroyedEvent().getSession()).isEqualTo(session);
    publisher.sessionIdChanged(event, "oldSessionId");
    assertThat(listener.getSessionIdChangedEvent()).isNotNull();
    assertThat(listener.getSessionIdChangedEvent().getOldSessionId()).isEqualTo("oldSessionId");
    listener.setSessionIdChangedEvent(null);
}
Also used : HttpSessionEvent(jakarta.servlet.http.HttpSessionEvent) MockHttpSession(org.springframework.mock.web.MockHttpSession) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.jupiter.api.Test)

Aggregations

StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)101 Test (org.junit.jupiter.api.Test)54 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)29 MockServletContext (org.springframework.web.testfixture.servlet.MockServletContext)21 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)20 MockServletContext (org.springframework.mock.web.test.MockServletContext)16 Test (org.junit.Test)15 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)14 ServletContext (jakarta.servlet.ServletContext)11 View (org.springframework.web.servlet.View)11 BeforeEach (org.junit.jupiter.api.BeforeEach)10 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)10 MockFilterConfig (org.springframework.web.testfixture.servlet.MockFilterConfig)10 HttpServletResponse (javax.servlet.http.HttpServletResponse)8 Before (org.junit.Before)8 WebApplicationContext (org.springframework.web.context.WebApplicationContext)8 HashMap (java.util.HashMap)6 Map (java.util.Map)6 AcceptHeaderLocaleResolver (org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver)6 Properties (java.util.Properties)5