Search in sources :

Example 56 with MockServletContext

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

the class ContextLoaderTests method contextLoaderListenerWithDefaultContext.

@Test
void contextLoaderListenerWithDefaultContext() {
    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);
    String contextAttr = WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE;
    WebApplicationContext context = (WebApplicationContext) sc.getAttribute(contextAttr);
    boolean condition1 = context instanceof XmlWebApplicationContext;
    assertThat(condition1).as("Correct WebApplicationContext exposed in ServletContext").isTrue();
    assertThat(WebApplicationContextUtils.getRequiredWebApplicationContext(sc) instanceof XmlWebApplicationContext).isTrue();
    LifecycleBean lb = (LifecycleBean) context.getBean("lifecycle");
    assertThat(context.containsBean("father")).as("Has father").isTrue();
    assertThat(context.containsBean("rod")).as("Has rod").isTrue();
    assertThat(context.containsBean("kerry")).as("Has kerry").isTrue();
    boolean condition = !lb.isDestroyed();
    assertThat(condition).as("Not destroyed").isTrue();
    assertThat(context.containsBean("beans1.bean1")).isFalse();
    assertThat(context.containsBean("beans1.bean2")).isFalse();
    listener.contextDestroyed(event);
    assertThat(lb.isDestroyed()).as("Destroyed").isTrue();
    assertThat(sc.getAttribute(contextAttr)).isNull();
    assertThat(WebApplicationContextUtils.getWebApplicationContext(sc)).isNull();
}
Also used : XmlWebApplicationContext(org.springframework.web.context.support.XmlWebApplicationContext) ServletContextListener(jakarta.servlet.ServletContextListener) LifecycleBean(org.springframework.beans.testfixture.beans.LifecycleBean) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) ServletContextEvent(jakarta.servlet.ServletContextEvent) SimpleWebApplicationContext(org.springframework.web.servlet.SimpleWebApplicationContext) XmlWebApplicationContext(org.springframework.web.context.support.XmlWebApplicationContext) Test(org.junit.jupiter.api.Test)

Example 57 with MockServletContext

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

the class ContextLoaderTests method contextLoaderListenerWithMixedContextInitializers.

@Test
void contextLoaderListenerWithMixedContextInitializers() {
    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()).isEqualTo("testName");
    assertThat(wac.getServletContext().getAttribute("initialized")).isNotNull();
}
Also used : TestBean(org.springframework.beans.testfixture.beans.TestBean) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) ServletContextEvent(jakarta.servlet.ServletContextEvent) SimpleWebApplicationContext(org.springframework.web.servlet.SimpleWebApplicationContext) XmlWebApplicationContext(org.springframework.web.context.support.XmlWebApplicationContext) Test(org.junit.jupiter.api.Test)

Example 58 with MockServletContext

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

the class ResourceHttpRequestHandlerTests method getResourceWithMediaTypeResolvedThroughServletContext.

// SPR-14368
@Test
public void getResourceWithMediaTypeResolvedThroughServletContext() throws Exception {
    MockServletContext servletContext = new MockServletContext() {

        @Override
        public String getMimeType(String filePath) {
            return "foo/bar";
        }
    };
    List<Resource> paths = Collections.singletonList(new ClassPathResource("test/", getClass()));
    ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler();
    handler.setServletContext(servletContext);
    handler.setLocations(paths);
    handler.afterPropertiesSet();
    MockHttpServletRequest request = new MockHttpServletRequest(servletContext, "GET", "");
    request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css");
    handler.handleRequest(request, this.response);
    assertThat(this.response.getContentType()).isEqualTo("foo/bar");
    assertThat(this.response.getContentAsString()).isEqualTo("h1 { color:red; }");
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) UrlResource(org.springframework.core.io.UrlResource) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) FileSystemResource(org.springframework.core.io.FileSystemResource) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.jupiter.api.Test)

Example 59 with MockServletContext

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

the class ResourceUrlProviderTests method initializeOnCurrentContext.

@Test
@SuppressWarnings("resource")
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();
    assertThat(parentUrlProvider.isAutodetect()).isTrue();
    ResourceUrlProvider childUrlProvider = childContext.getBean(ResourceUrlProvider.class);
    assertThat(childUrlProvider.getHandlerMap()).containsOnlyKeys("/resources/**");
    assertThat(childUrlProvider.isAutodetect()).isFalse();
}
Also used : AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) Test(org.junit.jupiter.api.Test)

Example 60 with MockServletContext

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

the class AbstractTagTests method createPageContext.

protected MockPageContext createPageContext() {
    MockServletContext sc = new MockServletContext();
    SimpleWebApplicationContext wac = new SimpleWebApplicationContext();
    wac.setServletContext(sc);
    wac.setNamespace("test");
    wac.refresh();
    MockHttpServletRequest request = new MockHttpServletRequest(sc);
    MockHttpServletResponse response = new MockHttpServletResponse();
    if (inDispatcherServlet()) {
        request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
        LocaleResolver lr = new AcceptHeaderLocaleResolver();
        request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, lr);
        ThemeResolver tr = new FixedThemeResolver();
        request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, tr);
        request.setAttribute(DispatcherServlet.THEME_SOURCE_ATTRIBUTE, wac);
    } else {
        sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    }
    return new MockPageContext(sc, request, response);
}
Also used : LocaleResolver(org.springframework.web.servlet.LocaleResolver) AcceptHeaderLocaleResolver(org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver) FixedThemeResolver(org.springframework.web.servlet.theme.FixedThemeResolver) ThemeResolver(org.springframework.web.servlet.ThemeResolver) FixedThemeResolver(org.springframework.web.servlet.theme.FixedThemeResolver) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) SimpleWebApplicationContext(org.springframework.web.servlet.SimpleWebApplicationContext) MockPageContext(org.springframework.web.testfixture.servlet.MockPageContext) AcceptHeaderLocaleResolver(org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse)

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