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()); }
public void testConcurrency() throws IOException { DefaultMustacheFactory c = createMustacheFactory(); c.setExecutorService(Executors.newCachedThreadPool()); Mustache m = c.compile(new StringReader("{{a}} {{#caps}}{{b}}{{/caps}} {{c}}"), "concurrency"); StringWriter sw = new StringWriter(); long start = System.currentTimeMillis(); Writer execute = m.execute(sw, new Object() { Callable<Object> a = () -> { Thread.sleep(300); return "How"; }; Callable<Object> b = () -> { Thread.sleep(200); return "are"; }; Callable<Object> c = () -> { Thread.sleep(100); return "you?"; }; Callable<Function> caps = () -> (Function) o -> o.toString().toUpperCase(); }); execute.close(); assertTrue("Time < 600ms", System.currentTimeMillis() - start < 600); assertEquals("How ARE you?", sw.toString()); }
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); }
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 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); }
}).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) {