public void testPropertyWithDot() throws IOException { DefaultMustacheFactory mustacheFactory = new DefaultMustacheFactory(); Reader reader = new StringReader("value=${some.value}"); Mustache mustache = mustacheFactory.compile(reader, "maven", "${", "}"); Map<String, String> properties = new HashMap<>(); properties.put("some.value", "some.value"); StringWriter writer = new StringWriter(); mustache.execute(writer, new Object[]{properties}).close(); Assert.assertEquals("value=some.value", writer.toString()); }
public void testImplicitIteratorWithScope() throws IOException { Mustache test = new DefaultMustacheFactory().compile(new StringReader("{{#test}}_{{.}}_{{/test}}"), "test"); StringWriter sw = new StringWriter(); test.execute(sw, new Object() { List<String> test = Arrays.asList("a", "b", "c"); }).close(); assertEquals("_a__b__c_", sw.toString()); }
public void testContextPrecedence() throws IOException { Mustache m = new DefaultMustacheFactory().compile(new StringReader("{{#a}}{{b.c}}{{/a}}"), "test"); Map map = new ObjectMapper().readValue("{\"a\": {\"b\": {}}, \"b\": {\"c\": \"ERROR\"}}", Map.class); StringWriter sw = new StringWriter(); m.execute(sw, map).close(); assertEquals("", sw.toString()); }
@Override public View getView(String viewName, ResourceLoader resourceLoader) throws Exception { String filename = filename(viewName); if (resourceLoader.load(filename) == null) return null; DefaultMustacheFactory mf = getViewFactory(resourceLoader); Mustache mustache = mf.compile(filename); return view(mustache); }
public void testVariableInhertiance() throws IOException { DefaultMustacheFactory mf = createMustacheFactory(); Mustache m = mf.compile("issue_201/chat.html"); StringWriter sw = new StringWriter(); m.execute(sw, new Object()).close(); assertEquals("<script src=\"test\"></script>", sw.toString()); }
public void testOutputDelimiters() { String template = "{{=## ##=}}{{##={{ }}=####"; Mustache mustache = new DefaultMustacheFactory().compile(new StringReader(template), "test"); StringWriter sw = new StringWriter(); mustache.execute(sw, new Object[0]); assertEquals("{{##", sw.toString()); }
@Test public void testWithTemplateFunction2() throws IOException { DefaultMustacheFactory mf = new DefaultMustacheFactory(); Mustache maven = mf.compile(new StringReader("Hello, ${foo}."), "maven", "${", "}"); StringWriter sw = new StringWriter(); maven.execute(sw, new Object() { TemplateFunction foo = (s) -> "${name}"; String name = "Jason"; }).close(); assertEquals("Hello, ${name}.", sw.toString()); }
@Test public void testWithTemplateFunction() throws IOException { DefaultMustacheFactory mf = new DefaultMustacheFactory(); Mustache maven = mf.compile(new StringReader("Hello, ${#f}${foo}${/f}."), "maven", "${", "}"); StringWriter sw = new StringWriter(); maven.execute(sw, new Object() { TemplateFunction f = (s) -> s; String foo = "Jason"; }).close(); assertEquals("Hello, Jason.", sw.toString()); }
@Test public void testStrSubstitutor() throws IOException { DefaultMustacheFactory mf = new DefaultMustacheFactory(); Mustache maven = mf.compile(new StringReader("Hello, $<foo>."), "maven", "$<", ">"); StringWriter sw = new StringWriter(); maven.execute(sw, new Object() { String foo = "Jason"; }).close(); assertEquals("Hello, Jason.", sw.toString()); }
@Test public void testStrSubstitutor2() throws IOException { DefaultMustacheFactory mf = new DefaultMustacheFactory(); Mustache maven = mf.compile(new StringReader("{{=$< >=}}Hello, $<foo>."), "maven"); StringWriter sw = new StringWriter(); maven.execute(sw, new Object() { String foo = "Jason"; }).close(); assertEquals("Hello, Jason.", sw.toString()); }
@Test public void testConcurrentExecution() throws InterruptedException { if (skip()) return; String template = "{{aa}}:{{bb}}:{{cc}}"; final Mustache test = new DefaultMustacheFactory().compile(new StringReader(template), "test"); ExecutorService es = Executors.newCachedThreadPool(); final AtomicInteger total = render(test, es); assertEquals(0, total.intValue()); }
@Test public void testToJson4() throws IOException { DefaultMustacheFactory dmf = new DefaultMustacheFactory(); Mustache compile = dmf.compile("fdbcli2.mustache"); Path file = getPath("src/test/resources/fdbcli3.txt"); String txt = new String(Files.readAllBytes(file), "UTF-8"); System.out.println("Input text:["); System.out.print(txt); System.out.println("]"); Node invert = compile.invert(txt); output(invert); }
@Test public void testConcurrentExecutionWithConcurrentTemplate() throws InterruptedException { if (skip()) return; String template = "{{calla}}:{{callb}}:{{callc}}"; ExecutorService es = Executors.newCachedThreadPool(); DefaultMustacheFactory dmf = new DefaultMustacheFactory(); dmf.setExecutorService(es); final Mustache test = dmf.compile(new StringReader(template), "test"); final AtomicInteger total = render(test, es); assertEquals(0, total.intValue()); } }
@Test public void testXMLtoMap() throws IOException, SAXException, ParserConfigurationException { Map<String, Object> map = convertXMLtoMap(xml); DefaultMustacheFactory dmf = new DefaultMustacheFactory(); Mustache response = dmf.compile(new StringReader(template), "response"); Writer execute = response.execute(new StringWriter(), map); assertEquals(correct, execute.toString()); }
public void testEmptyMustache() { try { new DefaultMustacheFactory().compile(new StringReader("{{}}"), "test"); fail("Didn't throw an exception"); } catch (MustacheException e) { assertTrue(e.getMessage().startsWith("Empty mustache")); } }
@Test public void testWrapperCaching() { factory.setObjectHandler(new MapMethodReflectionHandler()); Reader reader = new StringReader(TEMPLATE); Mustache mustache = factory.compile(reader, "template"); Map<String, String> model = new HashMap<>(); verifyOutput("true", model, mustache); model.put("empty", "data"); verifyOutput("data", model, mustache); }
@Test public void testToJson2() throws IOException { DefaultMustacheFactory dmf = new DefaultMustacheFactory(); Mustache compile = dmf.compile("fdbcli2.mustache"); Path file = getPath("src/test/resources/fdbcli2.txt"); String txt = new String(Files.readAllBytes(file), "UTF-8"); Node invert = compile.invert(txt); output(invert); }
public void testMustacheNotFoundException() { String nonExistingMustache = "404"; try { new DefaultMustacheFactory().compile(nonExistingMustache); fail("Didn't throw an exception"); } catch (MustacheNotFoundException e) { assertEquals(nonExistingMustache, e.getName()); } }
@Test public void testKeyValues() { Map<String, String> model = new TreeMap<String, String>() {{ put("test", "testvalue"); put("key", "keyvalue"); }}; Reader reader = new StringReader(TEMPLATE2); factory.setObjectHandler(new MapMethodReflectionHandler()); Mustache mustache = factory.compile(reader, "template"); verifyOutput("key=keyvalue\ntest=testvalue\n", model, mustache); }
@Test public void testSimple() throws IOException { DefaultMustacheFactory dmf = new DefaultMustacheFactory(); Mustache test = dmf.compile(new StringReader("test {{value}} test"), "test"); Node invert = test.invert("test value test"); Node node = new Node(); node.put("value", value("value")); assertEquals(node, invert); }