use of org.springframework.mock.web.test.MockServletContext in project spring-framework by spring-projects.
the class ViewResolverTests method testCacheRemoval.
@Test
public void testCacheRemoval() throws Exception {
StaticWebApplicationContext wac = new StaticWebApplicationContext();
wac.setServletContext(new MockServletContext());
wac.refresh();
InternalResourceViewResolver vr = new InternalResourceViewResolver();
vr.setViewClass(JstlView.class);
vr.setApplicationContext(wac);
View view = vr.resolveViewName("example1", Locale.getDefault());
View cached = vr.resolveViewName("example1", Locale.getDefault());
if (view != cached) {
fail("Caching doesn't work");
}
vr.removeFromCache("example1", Locale.getDefault());
cached = vr.resolveViewName("example1", Locale.getDefault());
if (view == cached) {
// the chance of having the same reference (hashCode) twice if negligible).
fail("View wasn't removed from cache");
}
}
use of org.springframework.mock.web.test.MockServletContext in project spring-framework by spring-projects.
the class ViewResolverTests method testBeanNameViewResolver.
@Test
public void testBeanNameViewResolver() throws ServletException {
StaticWebApplicationContext wac = new StaticWebApplicationContext();
wac.setServletContext(new MockServletContext());
MutablePropertyValues pvs1 = new MutablePropertyValues();
pvs1.addPropertyValue(new PropertyValue("url", "/example1.jsp"));
wac.registerSingleton("example1", InternalResourceView.class, pvs1);
MutablePropertyValues pvs2 = new MutablePropertyValues();
pvs2.addPropertyValue(new PropertyValue("url", "/example2.jsp"));
wac.registerSingleton("example2", JstlView.class, pvs2);
BeanNameViewResolver vr = new BeanNameViewResolver();
vr.setApplicationContext(wac);
wac.refresh();
View view = vr.resolveViewName("example1", Locale.getDefault());
assertEquals("Correct view class", InternalResourceView.class, view.getClass());
assertEquals("Correct URL", "/example1.jsp", ((InternalResourceView) view).getUrl());
view = vr.resolveViewName("example2", Locale.getDefault());
assertEquals("Correct view class", JstlView.class, view.getClass());
assertEquals("Correct URL", "/example2.jsp", ((JstlView) view).getUrl());
}
use of org.springframework.mock.web.test.MockServletContext in project spring-framework by spring-projects.
the class ViewResolverTests method testXmlViewResolver.
@Test
public void testXmlViewResolver() throws Exception {
StaticWebApplicationContext wac = new StaticWebApplicationContext();
wac.registerSingleton("testBean", TestBean.class);
wac.setServletContext(new MockServletContext());
wac.refresh();
TestBean testBean = (TestBean) wac.getBean("testBean");
XmlViewResolver vr = new XmlViewResolver();
vr.setLocation(new ClassPathResource("org/springframework/web/servlet/view/views.xml"));
vr.setApplicationContext(wac);
View view1 = vr.resolveViewName("example1", Locale.getDefault());
assertTrue("Correct view class", TestView.class.equals(view1.getClass()));
assertTrue("Correct URL", "/example1.jsp".equals(((InternalResourceView) view1).getUrl()));
View view2 = vr.resolveViewName("example2", Locale.getDefault());
assertTrue("Correct view class", JstlView.class.equals(view2.getClass()));
assertTrue("Correct URL", "/example2new.jsp".equals(((InternalResourceView) view2).getUrl()));
ServletContext sc = new MockServletContext();
Map model = new HashMap();
TestBean tb = new TestBean();
model.put("tb", tb);
HttpServletRequest request = new MockHttpServletRequest(sc);
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());
view1.render(model, request, response);
assertTrue("Correct tb attribute", tb.equals(request.getAttribute("tb")));
assertTrue("Correct test1 attribute", "testvalue1".equals(request.getAttribute("test1")));
assertTrue("Correct test2 attribute", testBean.equals(request.getAttribute("test2")));
request = new MockHttpServletRequest(sc);
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());
view2.render(model, request, response);
assertTrue("Correct tb attribute", tb.equals(request.getAttribute("tb")));
assertTrue("Correct test1 attribute", "testvalue1".equals(request.getAttribute("test1")));
assertTrue("Correct test2 attribute", "testvalue2".equals(request.getAttribute("test2")));
}
use of org.springframework.mock.web.test.MockServletContext in project spring-framework by spring-projects.
the class ViewResolverTests method testInternalResourceViewResolverWithJstlAndContextParam.
@Test
public void testInternalResourceViewResolverWithJstlAndContextParam() throws Exception {
Locale locale = !Locale.GERMAN.equals(Locale.getDefault()) ? Locale.GERMAN : Locale.FRENCH;
MockServletContext sc = new MockServletContext();
sc.addInitParameter(Config.FMT_LOCALIZATION_CONTEXT, "org/springframework/web/context/WEB-INF/context-messages");
StaticWebApplicationContext wac = new StaticWebApplicationContext();
wac.setServletContext(sc);
wac.addMessage("code1", locale, "messageX");
wac.refresh();
InternalResourceViewResolver vr = new InternalResourceViewResolver();
vr.setViewClass(JstlView.class);
vr.setApplicationContext(wac);
View view = vr.resolveViewName("example1", Locale.getDefault());
assertEquals("Correct view class", JstlView.class, view.getClass());
assertEquals("Correct URL", "example1", ((JstlView) view).getUrl());
view = vr.resolveViewName("example2", Locale.getDefault());
assertEquals("Correct view class", JstlView.class, view.getClass());
assertEquals("Correct URL", "example2", ((JstlView) view).getUrl());
MockHttpServletRequest request = new MockHttpServletRequest(sc);
HttpServletResponse response = new MockHttpServletResponse();
request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new FixedLocaleResolver(locale));
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(locale, Config.get(request, Config.FMT_LOCALE));
LocalizationContext lc = (LocalizationContext) Config.get(request, Config.FMT_LOCALIZATION_CONTEXT);
assertEquals("message1", lc.getResourceBundle().getString("code1"));
assertEquals("message2", lc.getResourceBundle().getString("code2"));
}
use of org.springframework.mock.web.test.MockServletContext in project spring-framework by spring-projects.
the class FreeMarkerMacroTests method setUp.
@Before
public void setUp() throws Exception {
wac = new StaticWebApplicationContext();
wac.setServletContext(new MockServletContext());
// final Template expectedTemplate = new Template();
fc = new FreeMarkerConfigurer();
fc.setTemplateLoaderPaths("classpath:/", "file://" + System.getProperty("java.io.tmpdir"));
fc.afterPropertiesSet();
wac.getDefaultListableBeanFactory().registerSingleton("freeMarkerConfigurer", fc);
wac.refresh();
request = new MockHttpServletRequest();
request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver());
response = new MockHttpServletResponse();
}
Aggregations