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();
}
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"));
}
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"));
}
}
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());
}
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());
}
Aggregations