TemplateManager { public String render(String templateName, Map<String, Object> parameters) { StringWriter out = new StringWriter(); try { Template template = cfg.getTemplate(templateName); template.process(parameters, out); } catch (Exception e) { throw new IllegalArgumentException("Template " + templateName + " not found", e); } return out.toString(); } protected  TemplateManager(); static TemplateManager get(); static synchronized TemplateManager reset(); String render(String templateName, Map<String, Object> parameters); void registerTemplate(String id, Object template); void unregisterTemplate(String id); void close();  }
@Test(timeout=30000) public void testLoadNewTemplateFile() throws Exception { CountDownLatch loadTemplate = new CountDownLatch(1); configureTemplateManager(loadTemplate, null); String expected = "<html><body>Hello John</body></html>"; Map<String, Object> parameters = new HashMap<>(); parameters.put("Name", "John"); String rendered = templateManager.render("basic-email", parameters); assertEquals(expected, rendered); String advancedTemplate = "<html><body>Hello ${Name}, welcome!</body></html>"; String expectedAdvanced = "<html><body>Hello John, welcome!</body></html>"; Thread.sleep(3000); Files.write(new File(templateDir, "advanced-email.html").toPath(), advancedTemplate.getBytes()); loadTemplate.await(); rendered = templateManager.render("advanced-email", parameters); assertEquals(expectedAdvanced, rendered); }
@Test(timeout=30000) public void testLoadUpdatedTemplateFile() throws Exception { CountDownLatch loadTemplate = new CountDownLatch(1); configureTemplateManager(loadTemplate, null); String expected = "<html><body>Hello John</body></html>"; Map<String, Object> parameters = new HashMap<>(); parameters.put("Name", "John"); String rendered = templateManager.render("basic-email", parameters); assertEquals(expected, rendered); String updatedTemplate = "<html><body>Hello ${Name}, welcome!</body></html>"; Files.write(new File(templateDir, "basic-email.html").toPath(), updatedTemplate.getBytes()); loadTemplate.await(); assertThat(templateManager.getCreatedOrUpdated()).hasSize(1).contains("basic-email"); }
@Test(timeout=30000) public void testDeletedTemplateFile() throws Exception { CountDownLatch removeTemplate = new CountDownLatch(1); configureTemplateManager(null, removeTemplate); String expected = "<html><body>Hello John</body></html>"; Map<String, Object> parameters = new HashMap<>(); parameters.put("Name", "John"); String rendered = templateManager.render("basic-email", parameters); assertEquals(expected, rendered); Files.delete(new File(templateDir, "basic-email.html").toPath()); removeTemplate.await(); assertThat(templateManager.getRemoved()).hasSize(1).contains("basic-email"); }