use of org.springframework.mock.web.test.MockServletContext in project spring-framework by spring-projects.
the class ViewResolverTests method doTestUrlBasedViewResolverWithoutPrefixes.
private void doTestUrlBasedViewResolverWithoutPrefixes(UrlBasedViewResolver vr) throws Exception {
StaticWebApplicationContext wac = new StaticWebApplicationContext();
wac.setServletContext(new MockServletContext());
wac.refresh();
vr.setApplicationContext(wac);
vr.setContentType("myContentType");
vr.setRequestContextAttribute("rc");
View view = vr.resolveViewName("example1", Locale.getDefault());
assertEquals("Correct view class", JstlView.class, view.getClass());
assertEquals("Correct URL", "example1", ((InternalResourceView) view).getUrl());
assertEquals("Correct textContentType", "myContentType", ((InternalResourceView) view).getContentType());
view = vr.resolveViewName("example2", Locale.getDefault());
assertEquals("Correct view class", JstlView.class, view.getClass());
assertEquals("Correct URL", "example2", ((InternalResourceView) view).getUrl());
assertEquals("Correct textContentType", "myContentType", ((InternalResourceView) view).getContentType());
HttpServletRequest request = new MockHttpServletRequest(wac.getServletContext());
HttpServletResponse response = new MockHttpServletResponse();
request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver());
Map model = new HashMap();
TestBean tb = new TestBean();
model.put("tb", tb);
view.render(model, request, response);
assertTrue("Correct tb attribute", tb.equals(request.getAttribute("tb")));
assertTrue("Correct rc attribute", request.getAttribute("rc") instanceof RequestContext);
view = vr.resolveViewName("redirect:myUrl", Locale.getDefault());
assertEquals("Correct view class", RedirectView.class, view.getClass());
assertEquals("Correct URL", "myUrl", ((RedirectView) view).getUrl());
assertSame("View not initialized as bean", wac, ((RedirectView) view).getApplicationContext());
view = vr.resolveViewName("forward:myUrl", Locale.getDefault());
assertEquals("Correct view class", InternalResourceView.class, view.getClass());
assertEquals("Correct URL", "myUrl", ((InternalResourceView) view).getUrl());
}
use of org.springframework.mock.web.test.MockServletContext in project spring-framework by spring-projects.
the class ViewResolverTests method testInternalResourceViewResolverWithAttributes.
@Test
public void testInternalResourceViewResolverWithAttributes() throws Exception {
MockServletContext sc = new MockServletContext();
StaticWebApplicationContext wac = new StaticWebApplicationContext();
wac.setServletContext(sc);
wac.refresh();
InternalResourceViewResolver vr = new InternalResourceViewResolver();
Properties props = new Properties();
props.setProperty("key1", "value1");
vr.setAttributes(props);
Map map = new HashMap();
map.put("key2", new Integer(2));
vr.setAttributesMap(map);
vr.setApplicationContext(wac);
View view = vr.resolveViewName("example1", Locale.getDefault());
assertEquals("Correct view class", JstlView.class, view.getClass());
assertEquals("Correct URL", "example1", ((InternalResourceView) view).getUrl());
Map attributes = ((InternalResourceView) view).getStaticAttributes();
assertEquals("value1", attributes.get("key1"));
assertEquals(new Integer(2), attributes.get("key2"));
view = vr.resolveViewName("example2", Locale.getDefault());
assertEquals("Correct view class", JstlView.class, view.getClass());
assertEquals("Correct URL", "example2", ((InternalResourceView) view).getUrl());
attributes = ((InternalResourceView) view).getStaticAttributes();
assertEquals("value1", attributes.get("key1"));
assertEquals(new Integer(2), attributes.get("key2"));
MockHttpServletRequest request = new MockHttpServletRequest(sc);
HttpServletResponse response = new MockHttpServletResponse();
request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
Map model = new HashMap();
TestBean tb = new TestBean();
model.put("tb", tb);
view.render(model, request, response);
assertTrue("Correct tb attribute", tb.equals(request.getAttribute("tb")));
assertTrue("Correct rc attribute", request.getAttribute("rc") == null);
assertEquals("value1", request.getAttribute("key1"));
assertEquals(new Integer(2), request.getAttribute("key2"));
}
use of org.springframework.mock.web.test.MockServletContext in project spring-framework by spring-projects.
the class ViewResolverTests method testXmlViewResolverWithoutCache.
@Test
public void testXmlViewResolverWithoutCache() throws Exception {
StaticWebApplicationContext wac = new StaticWebApplicationContext() {
@Override
protected Resource getResourceByPath(String path) {
assertTrue("Correct default location", XmlViewResolver.DEFAULT_LOCATION.equals(path));
return super.getResourceByPath(path);
}
};
wac.setServletContext(new MockServletContext());
wac.refresh();
XmlViewResolver vr = new XmlViewResolver();
vr.setCache(false);
try {
vr.setApplicationContext(wac);
} catch (ApplicationContextException ex) {
fail("Should not have thrown ApplicationContextException: " + ex.getMessage());
}
try {
vr.resolveViewName("example1", Locale.getDefault());
fail("Should have thrown BeanDefinitionStoreException");
} catch (BeanDefinitionStoreException ex) {
// expected
}
}
use of org.springframework.mock.web.test.MockServletContext in project spring-framework by spring-projects.
the class ViewResolverTests method testXmlViewResolverDefaultLocation.
@Test
public void testXmlViewResolverDefaultLocation() {
StaticWebApplicationContext wac = new StaticWebApplicationContext() {
@Override
protected Resource getResourceByPath(String path) {
assertTrue("Correct default location", XmlViewResolver.DEFAULT_LOCATION.equals(path));
return super.getResourceByPath(path);
}
};
wac.setServletContext(new MockServletContext());
wac.refresh();
XmlViewResolver vr = new XmlViewResolver();
try {
vr.setApplicationContext(wac);
vr.afterPropertiesSet();
fail("Should have thrown BeanDefinitionStoreException");
} catch (BeanDefinitionStoreException ex) {
// expected
}
}
use of org.springframework.mock.web.test.MockServletContext in project spring-framework by spring-projects.
the class ViewResolverTests method doTestUrlBasedViewResolverWithPrefixes.
private void doTestUrlBasedViewResolverWithPrefixes(UrlBasedViewResolver vr) throws Exception {
StaticWebApplicationContext wac = new StaticWebApplicationContext();
wac.setServletContext(new MockServletContext());
wac.refresh();
vr.setPrefix("/WEB-INF/");
vr.setSuffix(".jsp");
vr.setApplicationContext(wac);
View view = vr.resolveViewName("example1", Locale.getDefault());
assertEquals("Correct view class", JstlView.class, view.getClass());
assertEquals("Correct URL", "/WEB-INF/example1.jsp", ((InternalResourceView) view).getUrl());
view = vr.resolveViewName("example2", Locale.getDefault());
assertEquals("Correct view class", JstlView.class, view.getClass());
assertEquals("Correct URL", "/WEB-INF/example2.jsp", ((InternalResourceView) view).getUrl());
view = vr.resolveViewName("redirect:myUrl", Locale.getDefault());
assertEquals("Correct view class", RedirectView.class, view.getClass());
assertEquals("Correct URL", "myUrl", ((RedirectView) view).getUrl());
view = vr.resolveViewName("forward:myUrl", Locale.getDefault());
assertEquals("Correct view class", InternalResourceView.class, view.getClass());
assertEquals("Correct URL", "myUrl", ((InternalResourceView) view).getUrl());
}
Aggregations