Compiler { public CompilerExecutor newExecutor() { return new CompilerExecutor(this); } Compiler(FormatterTable formatterTable, PredicateTable predicateTable); FormatterTable formatterTable(); PredicateTable predicateTable(); CompilerExecutor newExecutor(); CompiledTemplate compile(String template); CompiledTemplate compile(String template, boolean safeMode, boolean preprocess); ValidatedTemplate validate(String template);  }
@Test public void testExecuteNoTemplate() throws CodeException { Context ctx = COMPILER.newExecutor().execute(); assertEquals(ctx.buffer().toString(), ""); }
@Test public void testAppendBuffer() throws CodeException { StringBuilder buf = new StringBuilder(); COMPILER.newExecutor().template("{@}").json("1").buffer(buf).execute(); COMPILER.newExecutor().template("{@}").json("2").buffer(buf).execute(); COMPILER.newExecutor().template("{@}").json("3").buffer(buf).execute(); assertEquals(buf.toString(), "123"); }
@Test public void testExecuteTemplate() throws CodeException { Context ctx = COMPILER.newExecutor() .template("{@}") .json(JsonUtils.decode("123")) .execute(); assertEquals(ctx.buffer().toString(), "123"); }
@Test public void testExecutePartials() throws CodeException { ObjectNode partialsMap = (ObjectNode) JsonUtils.decode("{\n\"foo\"\n:\n\"{@}\"\n}\n"); Context ctx = COMPILER.newExecutor() .template("{@|apply foo}") .json("123") .partialsMap(partialsMap) .execute(); assertEquals(ctx.buffer().toString(), "123"); }
@Test public void testCodeLimiter() throws CodeException { CodeLimiter limiter = new NoopCodeLimiter(); Context ctx = COMPILER.newExecutor() .template("#{@}#") .json("123") .codeLimiter(limiter) .execute(); assertEquals(limiter.instructionCount(), 4); assertEquals(ctx.buffer().toString(), "#123#"); }
@Test public void testLocale() throws CodeException { Context ctx = COMPILER.newExecutor() .template("{@|date %B}") .json("{\"website\":{\"timeZone\":\"UTC\"},\"now\": 1}") .locale(Locale.GERMANY) .execute(); assertEquals(ctx.buffer().toString(), "Januar"); }
@Test public void testSafeMode() throws CodeException { Context ctx = COMPILER.newExecutor() .safeExecution(true) .template("{@|foobar}") .json("123") .execute(); assertEquals(ctx.getErrors().size(), 1); assertEquals(ctx.getErrors().get(0).getType(), SyntaxErrorType.FORMATTER_UNKNOWN); }
@Test public void testLoggingHook() throws CodeException { final AtomicInteger count = new AtomicInteger(); LoggingHook loggingHook = new LoggingHook() { @Override public void log(Exception e) { count.incrementAndGet(); assertTrue(e instanceof NullPointerException); } }; Context ctx = COMPILER.newExecutor() .template("{@|npe}") .json("123") .safeExecution(true) .loggingHook(loggingHook) .execute(); assertEquals(count.get(), 1); assertEquals(ctx.getErrors().size(), 1); assertEquals(ctx.getErrors().get(0).getType(), ExecuteErrorType.UNEXPECTED_ERROR); }