@Override public View resolveViewName(final String viewName, Locale locale) throws Exception { return new AbstractView () { @Override public String getContentType() { return null; } @Override protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception { for (String key : attrsToValidate.keySet()) { assertTrue("Model should contain attribute named " + key, model.containsKey(key)); assertEquals(attrsToValidate.get(key), model.get(key)); validatedAttrCount++; } } }; }
protected <V extends AbstractView> V initialize (V view) { if (attributes != null && !attributes.isEmpty ()) view.setAttributesMap (attributes); if (applicationContext != null) view.setApplicationContext (applicationContext); if (beanName != null) view.setBeanName (beanName); if (contentType != null) view.setContentType (contentType); view.setExposePathVariables (exposePathVariables); if (requestContextAttribute != null) view.setRequestContextAttribute (requestContextAttribute); if (servletContext != null) view.setServletContext (servletContext); return view; }
@Test public void validTemplateName() throws Exception { FreeMarkerView fv = new FreeMarkerView(); WebApplicationContext wac = mock(WebApplicationContext.class); MockServletContext sc = new MockServletContext(); Map<String, FreeMarkerConfig> configs = new HashMap<String, FreeMarkerConfig>(); FreeMarkerConfigurer configurer = new FreeMarkerConfigurer(); configurer.setConfiguration(new TestConfiguration()); configs.put("configurer", configurer); given(wac.getBeansOfType(FreeMarkerConfig.class, true, false)).willReturn(configs); given(wac.getServletContext()).willReturn(sc); fv.setUrl("templateName"); fv.setApplicationContext(wac); MockHttpServletRequest request = new MockHttpServletRequest(); request.addPreferredLocale(Locale.US); request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver()); HttpServletResponse response = new MockHttpServletResponse(); Map<String, Object> model = new HashMap<String, Object>(); model.put("myattr", "myvalue"); fv.render(model, request, response); assertEquals(AbstractView.DEFAULT_CONTENT_TYPE, response.getContentType()); }
@Bean public View error() { return new AbstractView() { @Override protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception { response.getWriter().write("ERROR_BEAN"); } }; }
@Bean public View jsonView() { return new AbstractView() { @Override protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception { response.getOutputStream().write("Hello World".getBytes()); } }; }
@Override public View resolveViewName(String viewName, Locale locale) throws Exception { switch (viewName) { case "owner/index": AbstractView view = new OwnerPdfView(); view.setBeanName(viewName); return view; } return null; }
@Override public View resolveViewName(String viewName, Locale locale) throws Exception { switch (viewName) { case "owner/index": AbstractView view = new OwnerAtomFeedView(); view.setBeanName(viewName); return view; } return null; }
@Override public View resolveViewName(String viewName, Locale locale) throws Exception { switch (viewName) { case "owner/index": AbstractView view = new OwnerExcelView(); view.setBeanName(viewName); return view; } return null; }
@Test public void testValidTemplateName() throws Exception { FreeMarkerView fv = new FreeMarkerView(); WebApplicationContext wac = mock(WebApplicationContext.class); MockServletContext sc = new MockServletContext(); Map configs = new HashMap(); FreeMarkerConfigurer configurer = new FreeMarkerConfigurer(); configurer.setConfiguration(new TestConfiguration()); configs.put("configurer", configurer); given(wac.getBeansOfType(FreeMarkerConfig.class, true, false)).willReturn(configs); given(wac.getServletContext()).willReturn(sc); fv.setUrl("templateName"); fv.setApplicationContext(wac); MockHttpServletRequest request = new MockHttpServletRequest(); request.addPreferredLocale(Locale.US); request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver()); HttpServletResponse response = new MockHttpServletResponse(); Map model = new HashMap(); model.put("myattr", "myvalue"); fv.render(model, request, response); assertEquals(AbstractView.DEFAULT_CONTENT_TYPE, response.getContentType()); }
@Test public void testExposeHelpers() throws Exception { final String templateName = "test.vm"; WebApplicationContext wac = mock(WebApplicationContext.class); given(wac.getServletContext()).willReturn(new MockServletContext()); final Template expectedTemplate = new Template(); VelocityConfig vc = new VelocityConfig() { @Override public VelocityEngine getVelocityEngine() { return new TestVelocityEngine(templateName, expectedTemplate); } }; Map<String, VelocityConfig> configurers = new HashMap<String, VelocityConfig>(); configurers.put("velocityConfigurer", vc); given(wac.getBeansOfType(VelocityConfig.class, true, false)).willReturn(configurers); // let it ask for locale HttpServletRequest req = mock(HttpServletRequest.class); given(req.getAttribute(View.PATH_VARIABLES)).willReturn(null); given(req.getAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE)).willReturn(new AcceptHeaderLocaleResolver()); given(req.getLocale()).willReturn(Locale.CANADA); final HttpServletResponse expectedResponse = new MockHttpServletResponse(); VelocityView vv = new VelocityView() { @Override protected void mergeTemplate(Template template, Context context, HttpServletResponse response) throws Exception { assertTrue(template == expectedTemplate); assertTrue(response == expectedResponse); assertEquals("myValue", context.get("myHelper")); assertTrue(context.get("math") instanceof MathTool); assertTrue(context.get("dateTool") instanceof DateTool); DateTool dateTool = (DateTool) context.get("dateTool"); assertTrue(dateTool.getLocale().equals(Locale.CANADA)); assertTrue(context.get("numberTool") instanceof NumberTool); NumberTool numberTool = (NumberTool) context.get("numberTool"); assertTrue(numberTool.getLocale().equals(Locale.CANADA)); } @Override protected void exposeHelpers(Map<String, Object> model, HttpServletRequest request) throws Exception { model.put("myHelper", "myValue"); } }; vv.setUrl(templateName); vv.setApplicationContext(wac); Map<String, Class<?>> toolAttributes = new HashMap<String, Class<?>>(); toolAttributes.put("math", MathTool.class); vv.setToolAttributes(toolAttributes); vv.setDateToolAttribute("dateTool"); vv.setNumberToolAttribute("numberTool"); vv.setExposeSpringMacroHelpers(false); vv.render(new HashMap<String, Object>(), req, expectedResponse); assertEquals(AbstractView.DEFAULT_CONTENT_TYPE, expectedResponse.getContentType()); }
@Test public void testExposeHelpers() throws Exception { final String templateName = "test.vm"; WebApplicationContext wac = mock(WebApplicationContext.class); given(wac.getServletContext()).willReturn(new MockServletContext()); final Template expectedTemplate = new Template(); VelocityConfig vc = new VelocityConfig() { @Override public VelocityEngine getVelocityEngine() { return new TestVelocityEngine(templateName, expectedTemplate); } }; Map<String, VelocityConfig> configurers = new HashMap<String, VelocityConfig>(); configurers.put("velocityConfigurer", vc); given(wac.getBeansOfType(VelocityConfig.class, true, false)).willReturn(configurers); // let it ask for locale HttpServletRequest req = mock(HttpServletRequest.class); given(req.getAttribute(View.PATH_VARIABLES)).willReturn(null); given(req.getAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE)).willReturn(new AcceptHeaderLocaleResolver()); given(req.getLocale()).willReturn(Locale.CANADA); final HttpServletResponse expectedResponse = new MockHttpServletResponse(); VelocityView vv = new VelocityView() { @Override protected void mergeTemplate(Template template, Context context, HttpServletResponse response) throws Exception { assertTrue(template == expectedTemplate); assertTrue(response == expectedResponse); assertEquals("myValue", context.get("myHelper")); assertTrue(context.get("math") instanceof MathTool); assertTrue(context.get("dateTool") instanceof DateTool); DateTool dateTool = (DateTool) context.get("dateTool"); assertTrue(dateTool.getLocale().equals(Locale.CANADA)); assertTrue(context.get("numberTool") instanceof NumberTool); NumberTool numberTool = (NumberTool) context.get("numberTool"); assertTrue(numberTool.getLocale().equals(Locale.CANADA)); } @Override protected void exposeHelpers(Map<String, Object> model, HttpServletRequest request) throws Exception { model.put("myHelper", "myValue"); } }; vv.setUrl(templateName); vv.setApplicationContext(wac); Map<String, Class> toolAttributes = new HashMap<String, Class>(); toolAttributes.put("math", MathTool.class); vv.setToolAttributes(toolAttributes); vv.setDateToolAttribute("dateTool"); vv.setNumberToolAttribute("numberTool"); vv.setExposeSpringMacroHelpers(false); vv.render(new HashMap<String, Object>(), req, expectedResponse); assertEquals(AbstractView.DEFAULT_CONTENT_TYPE, expectedResponse.getContentType()); }
private View applyLifecycleMethods(String viewName, AbstractView view) { return (View) getApplicationContext().getAutowireCapableBeanFactory().initializeBean(view, viewName); }