MustacheRenderer implements Renderer { @Override public void render(String path, Request request) throws IOException { if (path == null) { throw new CannotRenderException("Cannot dispatch a null path"); } try { REQUEST_HOLDER.set(request); factory .compile(path) .execute(request.getWriter(), buildScope(request)); REQUEST_HOLDER.remove(); } catch(MustacheException ex) { throw new IOException("failed to MustacheRenderer.render(" + path + ",request)", ex); } } MustacheRenderer();  MustacheRenderer(MustacheFactory factory); @Override void render(String path, Request request); static Request getThreadLocalRequest(); @Override boolean isRenderable(String path, Request request); final void setAcceptPattern(Pattern acceptPattern);  }
@Test public void testRender() throws IOException { Request request = createMock(Request.class); StringWriter writer = new StringWriter(); ApplicationContext applicationContext = createMock(ApplicationContext.class); ApplicationResource applicationResource = createMock(ApplicationResource.class); Map<String,Object> context = Collections.singletonMap("testKey", (Object)"test value"); expect(applicationContext.getResource(isA(String.class))).andReturn(applicationResource).anyTimes(); expect(request.getAvailableScopes()).andReturn(Arrays.asList(Request.REQUEST_SCOPE, "session", Request.APPLICATION_SCOPE)); expect(request.getContext(Request.REQUEST_SCOPE)).andReturn(context); expect(request.getContext("session")).andReturn(Collections.<String,Object>emptyMap()); expect(request.getContext(Request.APPLICATION_SCOPE)).andReturn(Collections.<String,Object>emptyMap()); expect(request.getWriter()).andReturn(writer).anyTimes(); replay(request, applicationContext, applicationResource); Renderer renderer = new MustacheRenderer(new DefaultMustacheFactory() { @Override public Reader getReader(String path) { return new InputStreamReader(getClass().getResourceAsStream(path), Charset.forName("utf-8")); } }); renderer.render("/test.html", request); verify(request, applicationContext, applicationResource); assertEquals("test template with test value", writer.toString()); }
@Test(expected = CannotRenderException.class) public void testRenderException() throws IOException { Request request = createMock(Request.class); replay(request); Renderer renderer = new MustacheRenderer(); try { renderer.render(null, request); } finally { verify(request); } }