FreemarkerRenderer implements Renderer { @Override public void render(String path, Request request) throws IOException { if (path == null) { throw new CannotRenderException("Cannot dispatch a null path"); } ServletRequest servletRequest = org.apache.tiles.request.servlet.ServletUtil.getServletRequest(request); HttpServletRequest httpRequest = servletRequest.getRequest(); HttpServletResponse httpResponse = servletRequest.getResponse(); servlet.setValue(path); try { servlet.doGet(httpRequest, new ExternalWriterHttpServletResponse(httpResponse, request.getPrintWriter())); } catch (ServletException e) { throw new FreemarkerRequestException("Exception when rendering a FreeMarker attribute", e); } } FreemarkerRenderer(AttributeValueFreemarkerServlet servlet); @Override void render(String path, Request request); boolean isRenderable(String path, Request request);  }
@Test public void testWrite() throws IOException, ServletException { ApplicationContext applicationContext = createMock(ServletApplicationContext.class); ServletContext servletContext = createMock(ServletContext.class); GenericServlet servlet = createMockBuilder(GenericServlet.class).createMock(); ServletConfig servletConfig = createMock(ServletConfig.class); ObjectWrapper objectWrapper = createMock(ObjectWrapper.class); expect(servletConfig.getServletContext()).andReturn(servletContext); replay(servlet, servletConfig); servlet.init(servletConfig); ServletContextHashModel servletContextHashModel = new ServletContextHashModel(servlet, objectWrapper); expect(applicationContext.getContext()).andReturn(servletContext).anyTimes(); expect(servletContext.getRealPath(isA(String.class))).andReturn(null).anyTimes(); URL resource = getClass().getResource("/test.ftl"); expect(servletContext.getResource(isA(String.class))).andReturn(resource).anyTimes(); expect(servletContext.getAttribute(ATTR_APPLICATION_MODEL)).andReturn(servletContextHashModel); expect(servletContext.getAttribute(ATTR_JSP_TAGLIBS_MODEL)).andReturn(null); replay(applicationContext, servletContext, objectWrapper); FreemarkerRenderer renderer = FreemarkerRendererBuilder .createInstance().setApplicationContext(applicationContext) .setParameter("TemplatePath", "/") .setParameter("NoCache", "true") .setParameter("ContentType", "text/html") .setParameter("template_update_delay", "0") .setParameter("default_encoding", "ISO-8859-1") .setParameter("number_format", "0.##########").build(); ServletRequest request = createMock(ServletRequest.class); HttpServletRequest httpRequest = createMock(HttpServletRequest.class); HttpServletResponse response = createMock(HttpServletResponse.class); StringWriter stringWriter = new StringWriter(); PrintWriter printWriter = new PrintWriter(stringWriter); HttpRequestHashModel requestModel = new HttpRequestHashModel(httpRequest, response, objectWrapper); HttpRequestParametersHashModel parametersModel = new HttpRequestParametersHashModel(httpRequest); expect(request.getRequest()).andReturn(httpRequest); expect(request.getResponse()).andReturn(response); expect(request.getPrintWriter()).andReturn(printWriter); expect(httpRequest.getSession(false)).andReturn(null); expect(httpRequest.getAttribute(ATTR_REQUEST_MODEL)).andReturn(requestModel); expect(httpRequest.getAttribute(ATTR_REQUEST_PARAMETERS_MODEL)).andReturn(parametersModel); response.setContentType("text/html; charset=ISO-8859-1"); response.setHeader(eq("Cache-Control"), isA(String.class)); response.setHeader("Pragma", "no-cache"); response.setHeader(eq("Expires"), isA(String.class)); replay(request, httpRequest, response); renderer.render("hello", request); stringWriter.close(); assertTrue(stringWriter.toString().startsWith("Hello!")); verify(applicationContext, servletContext, request, httpRequest, response, servlet, servletConfig, objectWrapper); }
@Test(expected = CannotRenderException.class) public void testRenderException1() throws IOException, ServletException { ApplicationContext applicationContext = createMock(ServletApplicationContext.class); ServletContext servletContext = createMock(ServletContext.class); GenericServlet servlet = createMockBuilder(GenericServlet.class).createMock(); ServletConfig servletConfig = createMock(ServletConfig.class); ObjectWrapper objectWrapper = createMock(ObjectWrapper.class); replay(servlet, servletConfig); servlet.init(servletConfig); expect(applicationContext.getContext()).andReturn(servletContext).anyTimes(); expect(servletContext.getRealPath(isA(String.class))).andReturn(null).anyTimes(); URL resource = getClass().getResource("/test.ftl"); expect(servletContext.getResource(isA(String.class))).andReturn(resource).anyTimes(); replay(applicationContext, servletContext, objectWrapper); FreemarkerRenderer renderer = FreemarkerRendererBuilder .createInstance().setApplicationContext(applicationContext) .setParameter("TemplatePath", "/") .setParameter("NoCache", "true") .setParameter("ContentType", "text/html") .setParameter("template_update_delay", "0") .setParameter("default_encoding", "ISO-8859-1") .setParameter("number_format", "0.##########").build(); ServletRequest request = createMock(ServletRequest.class); replay(request); try { renderer.render(null, request); } finally { verify(applicationContext, servletContext, request, servlet, servletConfig, objectWrapper); } }