Search in sources :

Example 81 with MockServletContext

use of org.springframework.web.testfixture.servlet.MockServletContext in project spring-framework by spring-projects.

the class ResourceTests method testServletContextResource.

@Test
public void testServletContextResource() throws IOException {
    MockServletContext sc = new MockServletContext();
    Resource resource = new ServletContextResource(sc, "org/springframework/core/io/Resource.class");
    doTestResource(resource);
    assertThat(new ServletContextResource(sc, "org/springframework/core/../core/io/./Resource.class")).isEqualTo(resource);
}
Also used : Resource(org.springframework.core.io.Resource) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) Test(org.junit.jupiter.api.Test)

Example 82 with MockServletContext

use of org.springframework.web.testfixture.servlet.MockServletContext in project spring-framework by spring-projects.

the class SpringBeanAutowiringSupportTests method testProcessInjectionBasedOnServletContext.

@Test
public void testProcessInjectionBasedOnServletContext() {
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    AnnotationConfigUtils.registerAnnotationConfigProcessors(wac);
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("name", "tb");
    wac.registerSingleton("testBean", TestBean.class, pvs);
    MockServletContext sc = new MockServletContext();
    wac.setServletContext(sc);
    wac.refresh();
    sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    InjectionTarget target = new InjectionTarget();
    SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(target, sc);
    boolean condition = target.testBean instanceof TestBean;
    assertThat(condition).isTrue();
    assertThat(target.name).isEqualTo("tb");
}
Also used : TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) Test(org.junit.jupiter.api.Test)

Example 83 with MockServletContext

use of org.springframework.web.testfixture.servlet.MockServletContext in project spring-framework by spring-projects.

the class ServletAnnotationControllerHandlerMethodTests method parameterDispatchingController.

@PathPatternsParameterizedTest
void parameterDispatchingController(boolean usePathPatterns) throws Exception {
    final MockServletContext servletContext = new MockServletContext();
    final MockServletConfig servletConfig = new MockServletConfig(servletContext);
    WebApplicationContext webAppContext = initDispatcherServlet(MyParameterDispatchingController.class, usePathPatterns, wac -> {
        wac.setServletContext(servletContext);
        AnnotationConfigUtils.registerAnnotationConfigProcessors(wac);
        wac.getBeanFactory().registerResolvableDependency(ServletConfig.class, servletConfig);
    });
    MockHttpServletRequest request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
    MockHttpServletResponse response = new MockHttpServletResponse();
    HttpSession session = request.getSession();
    assertThat(session).isNotNull();
    getServlet().service(request, response);
    assertThat(response.getContentAsString()).isEqualTo("myView");
    assertThat(request.getAttribute("servletContext")).isSameAs(servletContext);
    assertThat(request.getAttribute("servletConfig")).isSameAs(servletConfig);
    assertThat(request.getAttribute("sessionId")).isSameAs(session.getId());
    assertThat(request.getAttribute("requestUri")).isSameAs(request.getRequestURI());
    assertThat(request.getAttribute("locale")).isSameAs(request.getLocale());
    request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
    response = new MockHttpServletResponse();
    session = request.getSession();
    assertThat(session).isNotNull();
    getServlet().service(request, response);
    assertThat(response.getContentAsString()).isEqualTo("myView");
    assertThat(request.getAttribute("servletContext")).isSameAs(servletContext);
    assertThat(request.getAttribute("servletConfig")).isSameAs(servletConfig);
    assertThat(request.getAttribute("sessionId")).isSameAs(session.getId());
    assertThat(request.getAttribute("requestUri")).isSameAs(request.getRequestURI());
    request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
    request.addParameter("view", "other");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getContentAsString()).isEqualTo("myOtherView");
    request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
    request.addParameter("view", "my");
    request.addParameter("lang", "de");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getContentAsString()).isEqualTo("myLangView");
    request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
    request.addParameter("surprise", "!");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getContentAsString()).isEqualTo("mySurpriseView");
    MyParameterDispatchingController deserialized = (MyParameterDispatchingController) SerializationTestUtils.serializeAndDeserialize(webAppContext.getBean(MyParameterDispatchingController.class.getSimpleName()));
    assertThat(deserialized.request).isNotNull();
    assertThat(deserialized.session).isNotNull();
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) HttpSession(jakarta.servlet.http.HttpSession) MockServletConfig(org.springframework.web.testfixture.servlet.MockServletConfig) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

Example 84 with MockServletContext

use of org.springframework.web.testfixture.servlet.MockServletContext in project spring-framework by spring-projects.

the class ResourceUrlProviderTests method initializeOnce.

// SPR-12592
@Test
@SuppressWarnings("resource")
void initializeOnce() {
    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
    context.setServletContext(new MockServletContext());
    context.register(HandlerMappingConfiguration.class);
    context.refresh();
    assertThat(context.getBean(ResourceUrlProvider.class).getHandlerMap()).hasKeySatisfying(pathPatternStringOf("/resources/**"));
}
Also used : AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) Test(org.junit.jupiter.api.Test)

Example 85 with MockServletContext

use of org.springframework.web.testfixture.servlet.MockServletContext in project spring-framework by spring-projects.

the class ResourceUrlProviderTests method initializeOnCurrentContext.

@Test
void initializeOnCurrentContext() {
    AnnotationConfigWebApplicationContext parentContext = new AnnotationConfigWebApplicationContext();
    parentContext.setServletContext(new MockServletContext());
    parentContext.register(ParentHandlerMappingConfiguration.class);
    AnnotationConfigWebApplicationContext childContext = new AnnotationConfigWebApplicationContext();
    childContext.setParent(parentContext);
    childContext.setServletContext(new MockServletContext());
    childContext.register(HandlerMappingConfiguration.class);
    parentContext.refresh();
    childContext.refresh();
    ResourceUrlProvider parentUrlProvider = parentContext.getBean(ResourceUrlProvider.class);
    assertThat(parentUrlProvider.getHandlerMap()).isEmpty();
    ResourceUrlProvider childUrlProvider = childContext.getBean(ResourceUrlProvider.class);
    assertThat(childUrlProvider.getHandlerMap()).hasKeySatisfying(pathPatternStringOf("/resources/**"));
    childContext.close();
    parentContext.close();
}
Also used : AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) Test(org.junit.jupiter.api.Test)

Aggregations

MockServletContext (org.springframework.web.testfixture.servlet.MockServletContext)138 Test (org.junit.jupiter.api.Test)112 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)44 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)38 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)24 ServletContext (jakarta.servlet.ServletContext)22 ServletContextEvent (jakarta.servlet.ServletContextEvent)21 BeforeEach (org.junit.jupiter.api.BeforeEach)17 WebApplicationContext (org.springframework.web.context.WebApplicationContext)17 MockFilterConfig (org.springframework.web.testfixture.servlet.MockFilterConfig)14 MockServletConfig (org.springframework.web.testfixture.servlet.MockServletConfig)13 Resource (org.springframework.core.io.Resource)12 XmlWebApplicationContext (org.springframework.web.context.support.XmlWebApplicationContext)12 HashMap (java.util.HashMap)11 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)11 ServletContextAwareProcessor (org.springframework.web.context.support.ServletContextAwareProcessor)10 TestBean (org.springframework.beans.testfixture.beans.TestBean)9 ClassPathResource (org.springframework.core.io.ClassPathResource)9 SimpleWebApplicationContext (org.springframework.web.servlet.SimpleWebApplicationContext)9 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)7