public void testObjectArray() throws IOException { MustacheFactory mf = createMustacheFactory(); Mustache m = mf.compile(new StringReader("{{#values}}{{.}}{{/values}}{{^values}}Test2{{/values}}"), "testObjectArray"); StringWriter sw = new StringWriter(); m.execute(sw, new Object() { Integer[] values = new Integer[]{1, 2, 3}; }).close(); assertEquals("123", sw.toString()); }
public void testReadmeSerial() throws MustacheException, IOException { MustacheFactory c = createMustacheFactory(); Mustache m = c.compile("items2.html"); StringWriter sw = new StringWriter(); long start = System.currentTimeMillis(); m.execute(sw, new Context()); long diff = System.currentTimeMillis() - start; assertEquals(getContents(root, "items.txt"), sw.toString()); assertTrue("Should be a little bit more than 4 seconds: " + diff, diff > 3999 && diff < 6000); }
public void testRecursionWithInheritance() throws IOException { StringWriter sw = execute("recursion_with_inheritance.html", new Object() { Object value = new Object() { boolean value = false; }; }); assertEquals(getContents(root, "recursion.txt"), sw.toString()); }
public void testNestedAccessWithSimpleObjectHandler() throws IOException { assertEquals(getOutput(false), getOutput(true)); }
public void testRelativePathFail() throws IOException { MustacheFactory mf = createMustacheFactory(); try { Mustache compile = mf.compile("relative/pathfail.html"); fail("Should have failed to compile"); } catch (MustacheException e) { // Success } }
public void testLimitedDepthRecursion() { try { StringWriter sw = execute("infiniteparent.html", new Context()); fail("Should have failed"); } catch (StackOverflowError soe) { fail("Should not have overflowed the stack"); } catch (MustacheException e) { assertEquals("Maximum partial recursion limit reached: 100 @[infiniteparent.html:1]", e.getMessage()); } }
public void testAccessTracker() throws IOException { { Map<String, String> accessTrackingMap = createBaseMap(); DefaultMustacheFactory mf = createMustacheFactory(); Mustache test = mf.compile(new StringReader("{{first}} {{last}}"), "test"); StringWriter sw = new StringWriter(); test.execute(sw, accessTrackingMap).close(); assertEquals("Sam Pullara", sw.toString()); } { AccessTrackingMap accessTrackingMap = createBaseMap(); accessTrackingMap.put("notused", "shouldcauseanerror"); DefaultMustacheFactory mf = createMustacheFactory(); Mustache test = mf.compile(new StringReader("{{first}} {{last}}"), "test"); StringWriter sw = new StringWriter(); test.execute(sw, accessTrackingMap).close(); assertEquals("Sam Pullara", sw.toString()); try { accessTrackingMap.check(); fail("Should have thrown an exception"); } catch (MustacheException me) { // Succcess } } }
public void testImplicitIteratorNoScope() throws IOException { Mustache test = new DefaultMustacheFactory().compile(new StringReader("{{.}}"), "test"); StringWriter sw = new StringWriter(); test.execute(sw, "").close(); assertEquals("", sw.toString()); StringWriter sw2 = new StringWriter(); test.execute(sw2, new Object[0]).close(); assertEquals("", sw2.toString()); }
public void testClosingReader() { final AtomicBoolean closed = new AtomicBoolean(); StringReader reader = new StringReader("{{test") { @Override public void close() { closed.set(true); } }; MustacheFactory mf = new DefaultMustacheFactory(); try { mf.compile(reader, "test"); fail("Should have thrown an exception"); } catch (MustacheException me) { // The reader should be closed now assertEquals(true, closed.get()); } }
public void testReadmeParallel() throws MustacheException, IOException { MustacheFactory c = initParallel(); Mustache m = c.compile("items2.html"); StringWriter sw = new StringWriter(); long start = System.currentTimeMillis(); m.execute(sw, new Context()).close(); long diff = System.currentTimeMillis() - start; assertEquals(getContents(root, "items.txt"), sw.toString()); assertTrue("Should be a little bit more than 1 second: " + diff, diff > 999 && diff < 2000); }
m.execute(sw, new Object() { }); fail("Should throw an exception"); } catch (MustacheException e) { }).close(); ps.flush(); assertTrue(new String(out.toByteArray()).contains("Variable is null")); } catch (MustacheException e) { }).close(); ps.flush(); assertTrue(new String(out.toByteArray()).contains("Variable is empty string")); } catch (MustacheException e) { }).close(); ps.flush(); assertEquals("", new String(out.toByteArray())); assertEquals("value", sw.toString()); } catch (MustacheException e) {
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")); } }
public void testComplexParallel() throws MustacheException, IOException { MustacheFactory c = initParallel(); Mustache m = c.compile("complex.html"); StringWriter sw = new StringWriter(); m.execute(sw, new ParallelComplexObject()).close(); assertEquals(getContents(root, "complex.txt"), sw.toString()); }
public void testPragma() throws IOException { final AtomicBoolean found = new AtomicBoolean(); DefaultMustacheFactory mf = new DefaultMustacheFactory() { @Override public MustacheVisitor createMustacheVisitor() { DefaultMustacheVisitor visitor = new DefaultMustacheVisitor(this); visitor.addPragmaHandler("pragma", (tc, pragma, args) -> { if (pragma.equals("pragma") && args.equals("1 2 3")) { found.set(true); } return null; }); return visitor; } }; Mustache m = mf.compile(new StringReader("Pragma: {{% pragma 1 2 3 }}"), "testPragma"); StringWriter sw = new StringWriter(); m.execute(sw, "").close(); // Values ignored as if it didn't exist at all assertEquals("Pragma: ", sw.toString()); assertTrue(found.get()); }
private DefaultMustacheFactory initParallel() { DefaultMustacheFactory cf = createMustacheFactory(); cf.setExecutorService(Executors.newCachedThreadPool()); return cf; }
public void testRootCheck() throws MustacheException, IOException, ExecutionException, InterruptedException { MustacheFactory c = createMustacheFactory(); try { Mustache m = c.compile("../../../pom.xml"); fail("Should have failed to compile"); } catch (MustacheException e) { // Success } }
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 testImproperlyClosedVariable() throws IOException { try { new DefaultMustacheFactory().compile(new StringReader("{{{#containers}} {{/containers}}"), "example"); fail("Should have throw MustacheException"); } catch (MustacheException actual) { assertEquals("Improperly closed variable in example:1 @[example:1]", actual.getMessage()); } }
private StringWriter execute(String name, Object object) { MustacheFactory c = createMustacheFactory(); Mustache m = c.compile(name); StringWriter sw = new StringWriter(); m.execute(sw, object); return sw; }
public void testSuperField() throws IOException { MustacheFactory mf = createMustacheFactory(); Mustache m = mf.compile(new StringReader("{{#values}}Test1{{/values}}{{^values}}Test2{{/values}}"), "testIterator"); StringWriter sw = new StringWriter(); m.execute(sw, new SuperClass() { }).close(); // Values ignored as if it didn't exist at all assertEquals("Test1", sw.toString()); }