Search in sources :

Example 1 with MockServletContext

use of org.springframework.mock.web.test.MockServletContext in project spring-framework by spring-projects.

the class CommonsMultipartResolverTests method doTestWithApplicationContext.

private void doTestWithApplicationContext(boolean lazy) throws Exception {
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(new MockServletContext());
    wac.getServletContext().setAttribute(WebUtils.TEMP_DIR_CONTEXT_ATTRIBUTE, new File("mytemp"));
    wac.refresh();
    MockCommonsMultipartResolver resolver = new MockCommonsMultipartResolver();
    resolver.setMaxUploadSize(1000);
    resolver.setMaxInMemorySize(100);
    resolver.setDefaultEncoding("enc");
    if (lazy) {
        resolver.setResolveLazily(false);
    }
    resolver.setServletContext(wac.getServletContext());
    assertEquals(1000, resolver.getFileUpload().getSizeMax());
    assertEquals(100, resolver.getFileItemFactory().getSizeThreshold());
    assertEquals("enc", resolver.getFileUpload().getHeaderEncoding());
    assertTrue(resolver.getFileItemFactory().getRepository().getAbsolutePath().endsWith("mytemp"));
    MockHttpServletRequest originalRequest = new MockHttpServletRequest();
    originalRequest.setMethod("POST");
    originalRequest.setContentType("multipart/form-data");
    originalRequest.addHeader("Content-type", "multipart/form-data");
    originalRequest.addParameter("getField", "getValue");
    assertTrue(resolver.isMultipart(originalRequest));
    MultipartHttpServletRequest request = resolver.resolveMultipart(originalRequest);
    doTestParameters(request);
    doTestFiles(request);
    doTestBinding(resolver, originalRequest, request);
    wac.close();
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) MockServletContext(org.springframework.mock.web.test.MockServletContext) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest)

Example 2 with MockServletContext

use of org.springframework.mock.web.test.MockServletContext in project spring-framework by spring-projects.

the class ContextLoaderTests method testFrameworkServletWithCustomLocation.

@Test
public void testFrameworkServletWithCustomLocation() throws Exception {
    DispatcherServlet servlet = new DispatcherServlet();
    servlet.setContextConfigLocation("/org/springframework/web/context/WEB-INF/testNamespace.xml " + "/org/springframework/web/context/WEB-INF/context-addition.xml");
    servlet.init(new MockServletConfig(new MockServletContext(""), "test"));
    assertTrue(servlet.getWebApplicationContext().containsBean("kerry"));
    assertTrue(servlet.getWebApplicationContext().containsBean("kerryX"));
}
Also used : DispatcherServlet(org.springframework.web.servlet.DispatcherServlet) MockServletConfig(org.springframework.mock.web.test.MockServletConfig) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Example 3 with MockServletContext

use of org.springframework.mock.web.test.MockServletContext in project spring-framework by spring-projects.

the class ContextLoaderTests method testContextLoaderWithDefaultLocation.

@Test
public void testContextLoaderWithDefaultLocation() throws Exception {
    MockServletContext sc = new MockServletContext("");
    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 IOException);
        assertTrue(ex.getCause().getMessage().contains("/WEB-INF/applicationContext.xml"));
    }
}
Also used : ServletContextListener(javax.servlet.ServletContextListener) BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException) IOException(java.io.IOException) MockServletContext(org.springframework.mock.web.test.MockServletContext) ServletContextEvent(javax.servlet.ServletContextEvent) Test(org.junit.Test)

Example 4 with MockServletContext

use of org.springframework.mock.web.test.MockServletContext 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 5 with MockServletContext

use of org.springframework.mock.web.test.MockServletContext 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)

Aggregations

MockServletContext (org.springframework.mock.web.test.MockServletContext)41 Test (org.junit.Test)33 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)16 ServletContextEvent (javax.servlet.ServletContextEvent)14 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)14 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)14 TestBean (org.springframework.tests.sample.beans.TestBean)11 XmlWebApplicationContext (org.springframework.web.context.support.XmlWebApplicationContext)11 HttpServletResponse (javax.servlet.http.HttpServletResponse)8 SimpleWebApplicationContext (org.springframework.web.servlet.SimpleWebApplicationContext)8 View (org.springframework.web.servlet.View)8 HashMap (java.util.HashMap)6 Map (java.util.Map)6 ServletContextListener (javax.servlet.ServletContextListener)6 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 Before (org.junit.Before)5 BeanDefinitionStoreException (org.springframework.beans.factory.BeanDefinitionStoreException)5 AcceptHeaderLocaleResolver (org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver)5 ServletRequest (javax.servlet.ServletRequest)4 ServletResponse (javax.servlet.ServletResponse)4