use of org.springframework.web.context.support.StaticWebApplicationContext in project spring-boot by spring-projects.
the class MustacheViewResolverTests method init.
@Before
public void init() {
this.resolver.setApplicationContext(new StaticWebApplicationContext());
this.resolver.setServletContext(new MockServletContext());
this.resolver.setPrefix("classpath:/mustache-templates/");
this.resolver.setSuffix(".html");
}
use of org.springframework.web.context.support.StaticWebApplicationContext in project spring-boot by spring-projects.
the class MustacheViewResolverTests method templateResourceInputStreamIsClosed.
@Test
public void templateResourceInputStreamIsClosed() throws Exception {
final Resource resource = mock(Resource.class);
given(resource.exists()).willReturn(true);
InputStream inputStream = new ByteArrayInputStream(new byte[0]);
InputStream spyInputStream = spy(inputStream);
given(resource.getInputStream()).willReturn(spyInputStream);
this.resolver = new MustacheViewResolver();
this.resolver.setApplicationContext(new StaticWebApplicationContext() {
@Override
public Resource getResource(String location) {
return resource;
}
});
this.resolver.loadView("foo", null);
verify(spyInputStream).close();
}
use of org.springframework.web.context.support.StaticWebApplicationContext 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.web.context.support.StaticWebApplicationContext 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.web.context.support.StaticWebApplicationContext 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
}
}
Aggregations