use of org.springframework.mock.web.test.MockServletConfig in project spring-framework by spring-projects.
the class ServletContextAwareProcessorTests method servletConfigAwareWithNullServletContextAndNonNullServletConfig.
@Test
public void servletConfigAwareWithNullServletContextAndNonNullServletConfig() {
ServletContext servletContext = new MockServletContext();
ServletConfig servletConfig = new MockServletConfig(servletContext);
ServletContextAwareProcessor processor = new ServletContextAwareProcessor(null, servletConfig);
ServletConfigAwareBean bean = new ServletConfigAwareBean();
assertNull(bean.getServletConfig());
processor.postProcessBeforeInitialization(bean, "testBean");
assertNotNull("ServletConfig should have been set", bean.getServletConfig());
assertEquals(servletConfig, bean.getServletConfig());
}
use of org.springframework.mock.web.test.MockServletConfig in project spring-framework by spring-projects.
the class DispatcherServletTests method dispatcherServletContextRefresh.
@Test
public void dispatcherServletContextRefresh() throws ServletException {
MockServletContext servletContext = new MockServletContext("org/springframework/web/context");
DispatcherServlet servlet = new DispatcherServlet();
servlet.init(new MockServletConfig(servletContext, "empty"));
ServletContextAwareBean contextBean = (ServletContextAwareBean) servlet.getWebApplicationContext().getBean("servletContextAwareBean");
ServletConfigAwareBean configBean = (ServletConfigAwareBean) servlet.getWebApplicationContext().getBean("servletConfigAwareBean");
assertSame(servletContext, contextBean.getServletContext());
assertSame(servlet.getServletConfig(), configBean.getServletConfig());
MultipartResolver multipartResolver = servlet.getMultipartResolver();
assertNotNull(multipartResolver);
((ConfigurableApplicationContext) servlet.getWebApplicationContext()).refresh();
ServletContextAwareBean contextBean2 = (ServletContextAwareBean) servlet.getWebApplicationContext().getBean("servletContextAwareBean");
ServletConfigAwareBean configBean2 = (ServletConfigAwareBean) servlet.getWebApplicationContext().getBean("servletConfigAwareBean");
assertSame(servletContext, contextBean2.getServletContext());
assertSame(servlet.getServletConfig(), configBean2.getServletConfig());
assertTrue(contextBean != contextBean2);
assertTrue(configBean != configBean2);
MultipartResolver multipartResolver2 = servlet.getMultipartResolver();
assertTrue(multipartResolver != multipartResolver2);
servlet.destroy();
}
use of org.springframework.mock.web.test.MockServletConfig in project spring-framework by spring-projects.
the class HttpRequestHandlerTests method testHttpRequestHandlerServletPassThrough.
@Test
public void testHttpRequestHandlerServletPassThrough() throws Exception {
MockServletContext servletContext = new MockServletContext();
final MockHttpServletRequest request = new MockHttpServletRequest();
final MockHttpServletResponse response = new MockHttpServletResponse();
StaticWebApplicationContext wac = new StaticWebApplicationContext();
wac.getBeanFactory().registerSingleton("myHandler", new HttpRequestHandler() {
@Override
public void handleRequest(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
assertSame(request, req);
assertSame(response, res);
String exception = request.getParameter("exception");
if ("ServletException".equals(exception)) {
throw new ServletException("test");
}
if ("IOException".equals(exception)) {
throw new IOException("test");
}
res.getWriter().write("myResponse");
}
});
wac.setServletContext(servletContext);
wac.refresh();
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
Servlet servlet = new HttpRequestHandlerServlet();
servlet.init(new MockServletConfig(servletContext, "myHandler"));
servlet.service(request, response);
assertEquals("myResponse", response.getContentAsString());
try {
request.setParameter("exception", "ServletException");
servlet.service(request, response);
fail("Should have thrown ServletException");
} catch (ServletException ex) {
assertEquals("test", ex.getMessage());
}
try {
request.setParameter("exception", "IOException");
servlet.service(request, response);
fail("Should have thrown IOException");
} catch (IOException ex) {
assertEquals("test", ex.getMessage());
}
}
use of org.springframework.mock.web.test.MockServletConfig in project spring-framework by spring-projects.
the class ViewResolutionIntegrationTests method runTest.
private MockHttpServletResponse runTest(Class<?> configClass) throws ServletException, IOException {
String basePath = "org/springframework/web/servlet/config/annotation";
MockServletContext servletContext = new MockServletContext(basePath);
MockServletConfig servletConfig = new MockServletConfig(servletContext);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
MockHttpServletResponse response = new MockHttpServletResponse();
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(configClass);
context.setServletContext(servletContext);
context.refresh();
DispatcherServlet servlet = new DispatcherServlet(context);
servlet.init(servletConfig);
servlet.service(request, response);
return response;
}
use of org.springframework.mock.web.test.MockServletConfig in project spring-framework by spring-projects.
the class CglibProxyControllerTests method initServlet.
@SuppressWarnings("serial")
private void initServlet(final Class<?> controllerClass) throws ServletException {
servlet = new DispatcherServlet() {
@Override
protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) {
GenericWebApplicationContext wac = new GenericWebApplicationContext();
wac.registerBeanDefinition("controller", new RootBeanDefinition(controllerClass));
DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator();
autoProxyCreator.setProxyTargetClass(true);
autoProxyCreator.setBeanFactory(wac.getBeanFactory());
wac.getBeanFactory().addBeanPostProcessor(autoProxyCreator);
wac.getBeanFactory().registerSingleton("advisor", new DefaultPointcutAdvisor(new SimpleTraceInterceptor(true)));
wac.refresh();
return wac;
}
};
servlet.init(new MockServletConfig());
}
Aggregations