### Question:
MustacheTemplatePreProcessor extends AbstractTemplatePreProcessor { @SuppressWarnings("PMD.PrematureDeclaration") @Override public String preProcess(String template) throws TemplateProcessingException { String newTemplate = super.preProcess(template); if (inSectionSymbol) { throw new TemplateProcessingException("The template is invalid since a section has not been closed"); } newTemplate = newTemplate.replaceAll(DOUBLE_OPEN_BRACE_PATTERN, MUSTACHE_OPEN_DELIMITER); newTemplate = newTemplate.replaceAll(DOUBLE_CLOSE_BRACE_PATTERN, MUSTACHE_CLOSE_DELIMITER); return newTemplate; } MustacheTemplatePreProcessor(); @SuppressWarnings("PMD.PrematureDeclaration") @Override String preProcess(String template); @Override boolean isMySymbol(String literal); @Override void reset(); @Override Map<String, Object> getUriParams();  }### Answer:
@Test public void testBasicTemplate() throws Exception { String template = "{{text}}"; String newTemplate = processor.preProcess(template); assertNotEquals(template, newTemplate); assertEquals("[[body.text]]", newTemplate); }
@Test public void testTemplate() throws Exception { String template = EMPTY_STRING + "At {{time}}, {{name}}" + NEW_LINE + "submitted the following message:" + NEW_LINE + "{{text}}"; String expected = EMPTY_STRING + "At [[body.time]], [[body.name]]" + NEW_LINE + "submitted the following message:" + NEW_LINE + "[[body.text]]"; String newTemplate = processor.preProcess(template); assertNotEquals(template, newTemplate); assertEquals(expected, newTemplate); }
@Test public void test2SymbolsTogether() throws Exception { String template = "{{name}}{{description}}"; String newTemplate = processor.preProcess(template); assertNotEquals(template, newTemplate); assertEquals("[[body.name]][[body.description]]", newTemplate); }
@Test public void testSectionTemplate() throws Exception { String template = EMPTY_STRING + "{{name}} passed the following courses:" + NEW_LINE + "{{#course}}" + NEW_LINE + "\t* {{name}}" + NEW_LINE + "{{/course}}" + NEW_LINE + "{{text}}"; String expected = EMPTY_STRING + "[[body.name]] passed the following courses:" + NEW_LINE + "[[#body.course]]" + NEW_LINE + "\t* [[name]]" + NEW_LINE + "[[/body.course]]" + NEW_LINE + "[[body.text]]"; String newTemplate = processor.preProcess(template); assertNotEquals(template, newTemplate); assertEquals(expected, newTemplate); }
@Test public void testDanglingSectionTemplate() { String template = EMPTY_STRING + "{{name}} passed the following courses:" + NEW_LINE + "{{#course}}" + NEW_LINE + "\t* {{name}}" + NEW_LINE + "{{text}}"; assertThatThrownBy(() -> processor.preProcess(template)) .isInstanceOf(TemplateProcessingException.class) .hasMessageContaining("section has not been closed"); }
@Test public void testTemplateSymbolsWithSpaces() throws Exception { String template = EMPTY_STRING + "At {{the time}}, {{the name}}" + NEW_LINE + "submitted the following message:" + NEW_LINE + "{{the text}}"; String expected = EMPTY_STRING + "At [[body.the time]], [[body.the name]]" + NEW_LINE + "submitted the following message:" + NEW_LINE + "[[body.the text]]"; String newTemplate = processor.preProcess(template); assertNotEquals(template, newTemplate); assertEquals(expected, newTemplate); }
@Test public void testInvalidTemplateWrongSyntax() { String template = EMPTY_STRING + "At ${time}, ${name}" + NEW_LINE + "submitted the following message:" + NEW_LINE + "${text}"; assertThatThrownBy(() -> processor.preProcess(template)) .isInstanceOf(TemplateProcessingException.class) .hasMessageContaining("wrong language"); }
@Test public void testInvalidTemplateNoEndTag() { String template = EMPTY_STRING + "At {{time}"; assertThatThrownBy(() -> processor.preProcess(template)) .isInstanceOf(TemplateProcessingException.class) .hasMessageContaining("incomplete symbol"); }