@Test public void testSpringMacroRequestContextAttributeUsed() { final String helperTool = "wrongType"; FreeMarkerView fv = new FreeMarkerView() { @Override protected void processTemplate(Template template, SimpleHash model, HttpServletResponse response) { fail(); } }; fv.setUrl(TEMPLATE_FILE); fv.setApplicationContext(wac); fv.setExposeSpringMacroHelpers(true); Map<String, Object> model = new HashMap<>(); model.put(FreeMarkerView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE, helperTool); try { fv.render(model, request, response); } catch (Exception ex) { assertTrue(ex instanceof ServletException); assertTrue(ex.getMessage().contains(FreeMarkerView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE)); } }
@Test public void testExposeSpringMacroHelpers() throws Exception { FreeMarkerView fv = new FreeMarkerView() { @Override @SuppressWarnings("rawtypes") protected void processTemplate(Template template, SimpleHash fmModel, HttpServletResponse response) throws TemplateException { Map model = fmModel.toMap(); assertTrue(model.get(FreeMarkerView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE) instanceof RequestContext); RequestContext rc = (RequestContext) model.get(FreeMarkerView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE); BindStatus status = rc.getBindStatus("tb.name"); assertEquals("name", status.getExpression()); assertEquals("juergen", status.getValue()); } }; fv.setUrl(TEMPLATE_FILE); fv.setApplicationContext(wac); fv.setExposeSpringMacroHelpers(true); Map<String, Object> model = new HashMap<>(); model.put("tb", new TestBean("juergen", 99)); fv.render(model, request, response); }
view.setUrl("tmp.ftl"); view.setExposeSpringMacroHelpers(false); view.setConfiguration(config);
@Test public void noFreeMarkerConfig() throws Exception { FreeMarkerView fv = new FreeMarkerView(); WebApplicationContext wac = mock(WebApplicationContext.class); given(wac.getBeansOfType(FreeMarkerConfig.class, true, false)).willReturn(new HashMap<>()); given(wac.getServletContext()).willReturn(new MockServletContext()); fv.setUrl("anythingButNull"); exception.expect(ApplicationContextException.class); exception.expectMessage(containsString("FreeMarkerConfig")); fv.setApplicationContext(wac); }
@Test public void keepExistingContentType() throws Exception { FreeMarkerView fv = new FreeMarkerView(); WebApplicationContext wac = mock(WebApplicationContext.class); MockServletContext sc = new MockServletContext(); Map<String, FreeMarkerConfig> configs = new HashMap<>(); FreeMarkerConfigurer configurer = new FreeMarkerConfigurer(); configurer.setConfiguration(new TestConfiguration()); configurer.setServletContext(sc); 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(); response.setContentType("myContentType"); Map<String, Object> model = new HashMap<>(); model.put("myattr", "myvalue"); fv.render(model, request, response); assertEquals("myContentType", response.getContentType()); }
@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<>(); FreeMarkerConfigurer configurer = new FreeMarkerConfigurer(); configurer.setConfiguration(new TestConfiguration()); configurer.setServletContext(sc); 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<>(); model.put("myattr", "myvalue"); fv.render(model, request, response); assertEquals(AbstractView.DEFAULT_CONTENT_TYPE, response.getContentType()); }
public View getObject() throws Exception { if (!FREEMARKER_AVAILABLE) { return null; } FreeMarkerView view = new FreeMarkerView(); view.setUrl(template); view.setBeanName(beanName); view.setApplicationContext(applicationContext); view.setServletContext(servletContext); return new InsightOperationView(view, applicationContext); }