diff --git a/stencil/src/net/stencilproject/template/GatingTemplateLexer.java b/stencil/src/net/stencilproject/template/GatingTemplateLexer.java index 8f29d07..65c8a83 100644 --- a/stencil/src/net/stencilproject/template/GatingTemplateLexer.java +++ b/stencil/src/net/stencilproject/template/GatingTemplateLexer.java @@ -1,5 +1,6 @@ package net.stencilproject.template; + import org.antlr.runtime.CharStream; import org.antlr.runtime.RecognitionException; import org.antlr.runtime.RecognizerSharedState; diff --git a/stencil/src/net/stencilproject/template/InternalParserTestSupport.java b/stencil/src/net/stencilproject/template/InternalParserTestSupport.java index 05683d9..5cb51d4 100644 --- a/stencil/src/net/stencilproject/template/InternalParserTestSupport.java +++ b/stencil/src/net/stencilproject/template/InternalParserTestSupport.java @@ -1,5 +1,6 @@ package net.stencilproject.template; + import org.antlr.runtime.ANTLRStringStream; import org.antlr.runtime.CommonTokenStream; import org.antlr.runtime.RecognitionException; diff --git a/stencil/src/net/stencilproject/template/IteratorUtils.java b/stencil/src/net/stencilproject/template/IteratorUtils.java index c7bec15..3f6a057 100644 --- a/stencil/src/net/stencilproject/template/IteratorUtils.java +++ b/stencil/src/net/stencilproject/template/IteratorUtils.java @@ -7,7 +7,7 @@ class IteratorUtils { /** - * Returns the null iterator - one that has no values. + * Represents the null iterator - one that has no values. */ public static final Iterator NULL_ITERATOR = new Iterator() { @Override diff --git a/stencil/src/net/stencilproject/template/TemplateParserBuilder.java b/stencil/src/net/stencilproject/template/TemplateBuilder.java similarity index 95% rename from stencil/src/net/stencilproject/template/TemplateParserBuilder.java rename to stencil/src/net/stencilproject/template/TemplateBuilder.java index dddd4c5..c5aba4a 100644 --- a/stencil/src/net/stencilproject/template/TemplateParserBuilder.java +++ b/stencil/src/net/stencilproject/template/TemplateBuilder.java @@ -6,22 +6,26 @@ import java.io.Reader; import java.net.MalformedURLException; import java.net.URL; -import java.nio.CharBuffer; import java.util.Date; import java.util.List; import java.util.Stack; import net.stencilproject.template.ProgramBuilder.Label; import net.stencilproject.template.TemplateParserException.TemplateError; -import net.stencilproject.template.TemplateTreeBuilder.EventHandler; import org.antlr.runtime.tree.Tree; import org.xml.sax.InputSource; import com.google.common.base.Charsets; import com.google.common.collect.Lists; - -public class TemplateParserBuilder extends BaseParser implements EventHandler { +import com.google.common.io.CharStreams; + +/** + * Builds a template from a given template file, invoking + * {@link TemplateTreeBuilder} and handling {@link TemplateBuilderEvents} callbacks as + * needed. + */ +public class TemplateBuilder extends BaseParser implements TemplateBuilderEvents { private ProgramBuilder program = new ProgramBuilder(); private Stack blockStack = new Stack(); private final TemplateRootScope rootScope; @@ -65,7 +69,7 @@ class ContentBlockContext extends BlockContext { } - private TemplateParserBuilder(TemplateRootScope rootScope, TemplateFile templateFile, TemplateMode mode, TemplateOptions options) { + private TemplateBuilder(TemplateRootScope rootScope, TemplateFile templateFile, TemplateMode mode, TemplateOptions options) { super(new TemplateFileSourceInfo(templateFile, null)); this.rootScope = rootScope; @@ -74,9 +78,9 @@ private TemplateParserBuilder(TemplateRootScope rootScope, TemplateFile template program.setEscapingMode(mode == TemplateMode.TEXT ? EscapingMode.TEXT : EscapingMode.XML); } - public static TemplateParserBuilder parse(TemplateRootScope rootScope, TemplateFile templateFile, TemplateMode mode, - TemplateOptions options) throws TemplateParserException, IOException { - TemplateParserBuilder templateParserBuilder = new TemplateParserBuilder(rootScope, templateFile, mode, options); + public static TemplateBuilder parse(TemplateRootScope rootScope, TemplateFile templateFile, TemplateMode mode, TemplateOptions options) + throws TemplateParserException, IOException { + TemplateBuilder templateParserBuilder = new TemplateBuilder(rootScope, templateFile, mode, options); templateParserBuilder.parse(); return templateParserBuilder; } @@ -91,8 +95,6 @@ public void parse() throws TemplateParserException, IOException { } protected String toString(InputSource source) throws TemplateParserException, IOException { - StringBuilder builder = new StringBuilder(); - CharBuffer buffer = CharBuffer.allocate(4096); Reader reader = source.getCharacterStream(); if (reader == null) { final InputStream inputStream = source.getByteStream(); @@ -104,14 +106,7 @@ protected String toString(InputSource source) throws TemplateParserException, IO reader = new InputStreamReader(inputStream, Charsets.UTF_8); } - while (reader.read(buffer) > 0) { - buffer.flip(); - builder.append(buffer); - } - - reader.close(); - - return builder.toString(); + return CharStreams.toString(reader); } @Override diff --git a/stencil/src/net/stencilproject/template/TemplateBuilderEvents.java b/stencil/src/net/stencilproject/template/TemplateBuilderEvents.java new file mode 100644 index 0000000..87de87e --- /dev/null +++ b/stencil/src/net/stencilproject/template/TemplateBuilderEvents.java @@ -0,0 +1,14 @@ +package net.stencilproject.template; + +import org.antlr.runtime.tree.Tree; + +/** + * Events that {@link TemplateBuilder} handles. + */ +interface TemplateBuilderEvents { + void text(TemplateFileSourceInfo source, String text) throws TemplateParserException; + + void tree(TemplateFileSourceInfo source, Tree tree) throws TemplateParserException; + + void done() throws TemplateParserException; +} \ No newline at end of file diff --git a/stencil/src/net/stencilproject/template/TemplateFactory.java b/stencil/src/net/stencilproject/template/TemplateFactory.java index 721d479..c01b9cb 100644 --- a/stencil/src/net/stencilproject/template/TemplateFactory.java +++ b/stencil/src/net/stencilproject/template/TemplateFactory.java @@ -4,9 +4,11 @@ import java.io.IOException; import java.net.URL; +import net.stencilproject.template.TemplateParserException.TemplateError; + /** - * Creates templates from various sources, allowing them to be re-read without restarting the application in development - * mode. + * Creates templates from various sources, allowing them to be re-read without + * restarting the application in development mode. */ public class TemplateFactory { private final TemplateOptions options; @@ -15,6 +17,15 @@ public TemplateFactory(TemplateOptions options) { this.options = options.clone(); } + public TemplateSource getTemplateSourceFromString(final TemplateMode mode, final String string) { + return new TemplateSource() { + @Override + public Template readTemplate(final TemplateRootScope rootScope) throws TemplateParserException, IOException { + return readTemplateInternal(mode, rootScope, TemplateFile.fromString(string)); + } + }; + } + public TemplateSource getTemplateSource(final TemplateMode mode, final File file) { if (mode == null) { throw new NullPointerException("mode is null"); @@ -65,6 +76,15 @@ public TemplateSource getTemplateSource(TemplateMode mode, Class clazz, Strin return getTemplateSource(mode, resource); } + public Template parseString(TemplateMode mode, TemplateRootScope rootScope, String template) throws TemplateParserException { + try { + return parse(getTemplateSourceFromString(mode, template), rootScope); + } catch (IOException e) { + // This shouldn't happen + throw new TemplateParserException(TemplateError.UNEXPECTED_ERROR, e, null); + } + } + public Template parse(TemplateMode mode, TemplateRootScope rootScope, Class clazz, String resourceName) throws TemplateParserException, IOException { return parse(getTemplateSource(mode, clazz, resourceName), rootScope); @@ -86,12 +106,14 @@ public Template parse(TemplateSource source, TemplateRootScope rootScope) throws private Template readTemplateInternal(TemplateMode mode, TemplateRootScope rootScope, TemplateFile templateFile) throws TemplateParserException, IOException { - Templater templater = new Templater(rootScope, options); - if (mode == TemplateMode.TEXT) { - return templater.parseText(templateFile); + TemplateBuilder templateParser = TemplateBuilder.parse(rootScope, templateFile, TemplateMode.TEXT, options); + return templateParser.toTemplate(); } else { - return templater.parseXml(mode == TemplateMode.HTML, templateFile); + TemplateBuilder templateParser = TemplateBuilder.parse(rootScope, templateFile, mode == TemplateMode.HTML ? TemplateMode.HTML + : TemplateMode.XML, options); + + return templateParser.toTemplate(); } } } diff --git a/stencil/src/net/stencilproject/template/TemplateParser.java b/stencil/src/net/stencilproject/template/TemplateParser.java index 489eab8..6fd312e 100644 --- a/stencil/src/net/stencilproject/template/TemplateParser.java +++ b/stencil/src/net/stencilproject/template/TemplateParser.java @@ -1,6 +1,7 @@ // $ANTLR 3.2 Sep 23, 2009 12:02:23 /Users/matthew/Documents/twitrants/stencil/src/com/dotspots/templating/Template.g 2010-12-06 14:34:39 package net.stencilproject.template; + import org.antlr.runtime.*; import java.util.Stack; import java.util.List; diff --git a/stencil/src/net/stencilproject/template/TemplateTreeBuilder.java b/stencil/src/net/stencilproject/template/TemplateTreeBuilder.java index b61349a..57a175a 100644 --- a/stencil/src/net/stencilproject/template/TemplateTreeBuilder.java +++ b/stencil/src/net/stencilproject/template/TemplateTreeBuilder.java @@ -17,13 +17,12 @@ import org.antlr.runtime.TokenStream; import org.antlr.runtime.tree.Tree; - /** * Parses a template, passing back the structure to an EventHandler. */ class TemplateTreeBuilder extends BaseParser { private StringBuilder builder = new StringBuilder(); - private final EventHandler eventHandler; + private final TemplateBuilderEvents eventHandler; private final TemplateMode mode; private TextState textState = TextState.TEXT; @@ -36,21 +35,13 @@ enum TextState { ESCAPE, } - TemplateTreeBuilder(TemplateFileSourceInfo source, TemplateMode mode, EventHandler eventHandler) { + TemplateTreeBuilder(TemplateFileSourceInfo source, TemplateMode mode, TemplateBuilderEvents eventHandler) { super(source); this.mode = mode; this.eventHandler = eventHandler; } - interface EventHandler { - void text(TemplateFileSourceInfo source, String text) throws TemplateParserException; - - void tree(TemplateFileSourceInfo source, Tree tree) throws TemplateParserException; - - void done() throws TemplateParserException; - } - - private class TextEventHandler implements EventHandler { + private class TextEventHandler implements TemplateBuilderEvents { @Override public void text(TemplateFileSourceInfo source, String text) throws TemplateParserException { emit(text); @@ -66,7 +57,7 @@ public void done() { } } - private class XmlEventHandler implements EventHandler, XmlTokenHandler { + private class XmlEventHandler implements TemplateBuilderEvents, XmlTokenHandler { private XmlPushLexer pushLexer = new XmlPushLexer(this); // TODO: Use something more efficient here private ArrayList trees = new ArrayList(); @@ -165,7 +156,7 @@ public void done() throws TemplateParserException { }; } - private void processTree(EventHandler eventHandler, Tree templateTree) throws TemplateParserException { + private void processTree(TemplateBuilderEvents eventHandler, Tree templateTree) throws TemplateParserException { for (int i = 0; i < templateTree.getChildCount(); i++) { Tree tree = templateTree.getChild(i); switch (tree.getType()) { @@ -196,7 +187,7 @@ private void parseDirectives(Tree tree) throws TemplateParserException { eventHandler.tree(source, tree); } - private Tree parseTree(String string, EventHandler eventHandler) throws TemplateParserException { + private Tree parseTree(String string, TemplateBuilderEvents eventHandler) throws TemplateParserException { final Exception[] stashed = new Exception[1]; final ANTLRStringStream stringStream = new ANTLRStringStream(string); @@ -238,7 +229,7 @@ public void reportError(RecognitionException e) { } public void parse() throws TemplateParserException, IOException { - EventHandler eventHandler = mode == TemplateMode.TEXT ? new TextEventHandler() : new XmlEventHandler(); + TemplateBuilderEvents eventHandler = mode == TemplateMode.TEXT ? new TextEventHandler() : new XmlEventHandler(); Tree tree = parseTree(templateFile.read(), eventHandler); processTree(eventHandler, tree); flushText(); diff --git a/stencil/src/net/stencilproject/template/Templater.java b/stencil/src/net/stencilproject/template/Templater.java deleted file mode 100644 index 5974d00..0000000 --- a/stencil/src/net/stencilproject/template/Templater.java +++ /dev/null @@ -1,64 +0,0 @@ -package net.stencilproject.template; - -import java.io.IOException; - -import net.stencilproject.template.TemplateParserException.TemplateError; - - -public class Templater { - private final TemplateRootScope rootScope; - private final TemplateOptions options; - - public Templater() { - this(null, new TemplateOptions()); - } - - public Templater(TemplateRootScope rootScope) { - this(rootScope, new TemplateOptions()); - } - - public Templater(TemplateRootScope rootScope, TemplateOptions options) { - this.rootScope = rootScope; - this.options = options; - } - - public Template parse(TemplateMode mode, TemplateFile templateFile) throws TemplateParserException, IOException { - TemplateParserBuilder baseTemplateParser = TemplateParserBuilder.parse(rootScope, templateFile, mode, options); - - return baseTemplateParser.toTemplate(); - } - - public Template parse(String input) throws TemplateParserException { - TemplateParserBuilder baseTemplateParser; - try { - baseTemplateParser = TemplateParserBuilder.parse(rootScope, TemplateFile.fromString(input), TemplateMode.TEXT, options); - } catch (IOException e) { - // Impossible - throw new TemplateParserException(TemplateError.UNEXPECTED_ERROR, e, null); - } - - return baseTemplateParser.toTemplate(); - } - - public Template parseText(TemplateFile templateFile) throws TemplateParserException, IOException { - TemplateParserBuilder templateParser = TemplateParserBuilder.parse(rootScope, templateFile, TemplateMode.TEXT, options); - - return templateParser.toTemplate(); - } - - public Template parseXml(boolean asHtml, String input) throws TemplateParserException { - try { - return parseXml(asHtml, TemplateFile.fromString(input)); - } catch (IOException e) { - // Impossible - throw new TemplateParserException(TemplateError.UNEXPECTED_ERROR, e, null); - } - } - - TemplateImpl parseXml(boolean asHtml, TemplateFile templateFile) throws TemplateParserException, IOException { - TemplateParserBuilder templateParser = TemplateParserBuilder.parse(rootScope, templateFile, asHtml ? TemplateMode.HTML - : TemplateMode.XML, options); - - return templateParser.toTemplate(); - } -} diff --git a/stencil/test/net/stencilproject/template/AbstractTemplateTest.java b/stencil/test/net/stencilproject/template/AbstractTemplateTest.java new file mode 100644 index 0000000..df4a168 --- /dev/null +++ b/stencil/test/net/stencilproject/template/AbstractTemplateTest.java @@ -0,0 +1,30 @@ +package net.stencilproject.template; + +import java.io.IOException; + +public abstract class AbstractTemplateTest { + protected Template parse(String text) throws TemplateParserException { + return new TemplateFactory(new TemplateOptions()).parseString(TemplateMode.TEXT, null, text); + } + + protected Template parseResource(String filename) throws TemplateParserException, IOException { + return new TemplateFactory(new TemplateOptions()).parse(TemplateMode.TEXT, null, getClass(), filename); + } + + protected Template parse(TemplateRootScope rootScope, String text) throws TemplateParserException { + return new TemplateFactory(new TemplateOptions()).parseString(TemplateMode.TEXT, rootScope, text); + } + + protected Template parseHtml(String text) throws TemplateParserException { + return new TemplateFactory(new TemplateOptions()).parseString(TemplateMode.HTML, null, text); + } + + protected Template parseXml(String text) throws TemplateParserException { + return new TemplateFactory(new TemplateOptions()).parseString(TemplateMode.XML, null, text); + } + + protected Template parseResourceXml(String filename) throws TemplateParserException, IOException { + return new TemplateFactory(new TemplateOptions()).parse(TemplateMode.XML, null, getClass(), filename); + } + +} diff --git a/stencil/test/net/stencilproject/template/TestErrorHandling.java b/stencil/test/net/stencilproject/template/TestErrorHandling.java index 04ca9df..8b92e24 100644 --- a/stencil/test/net/stencilproject/template/TestErrorHandling.java +++ b/stencil/test/net/stencilproject/template/TestErrorHandling.java @@ -11,8 +11,8 @@ public class TestErrorHandling { @Test public void testNestedError() throws TemplateParserException, IOException { - Templater templater = createTemplater(); - Template template = templater.parse(TemplateMode.TEXT, TemplateFile.fromResource(TestErrorHandling.class, "nestedErrorOuter.txt")); + TemplateFactory templater = createTemplater(); + Template template = templater.parse(TemplateMode.TEXT, null, TestErrorHandling.class, "nestedErrorOuter.txt"); ByteArrayOutputStream errStream = new ByteArrayOutputStream(); @@ -39,10 +39,10 @@ public void testNestedError() throws TemplateParserException, IOException { } } - private Templater createTemplater() { + private TemplateFactory createTemplater() { TemplateOptions options = new TemplateOptions(); options.setDumpTemplate(true); options.setTraceExecution(true); - return new Templater(null, options); + return new TemplateFactory(options); } } diff --git a/stencil/test/net/stencilproject/template/TestExceptions.java b/stencil/test/net/stencilproject/template/TestExceptions.java index e679d59..1c3b476 100644 --- a/stencil/test/net/stencilproject/template/TestExceptions.java +++ b/stencil/test/net/stencilproject/template/TestExceptions.java @@ -8,17 +8,9 @@ import com.google.common.collect.Maps; -public class TestExceptions { - private Templater createTemplater() { - TemplateOptions options = new TemplateOptions(); - options.setDumpTemplate(true); - options.setTraceExecution(true); - return new Templater(null, options); - } - +public class TestExceptions extends AbstractTemplateTest { @Test public void testExceptionInCalledMethod() throws TemplateParserException { - Templater templater = createTemplater(); Runnable runnable = new Runnable() { @Override public void run() { @@ -26,7 +18,7 @@ public void run() { } }; HashMap mapModel = Maps.newHashMap(); - Template template = templater.parse("{{ runnable.run() }}"); + Template template = parse("{{ runnable.run() }}"); mapModel.put("runnable", runnable); assertEquals("", template.process(mapModel)); } diff --git a/stencil/test/net/stencilproject/template/TestFor.java b/stencil/test/net/stencilproject/template/TestFor.java index e5e20bb..2842e0e 100644 --- a/stencil/test/net/stencilproject/template/TestFor.java +++ b/stencil/test/net/stencilproject/template/TestFor.java @@ -13,18 +13,10 @@ import com.google.common.collect.Maps; -public class TestFor { - private Templater createTemplater() { - TemplateOptions options = new TemplateOptions(); - options.setDumpTemplate(true); - options.setTraceExecution(true); - return new Templater(null, options); - } - +public class TestFor extends AbstractTemplateTest { @Test public void testFor() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater.parse("Check out this list: {% for x in data %}{{ x }}{% end %}."); + Template template = parse("Check out this list: {% for x in data %}{{ x }}{% end %}."); HashMap> mapModel = Maps.newHashMap(); mapModel.put("data", Arrays.asList("a", "b", "c", "d")); @@ -33,8 +25,7 @@ public void testFor() throws TemplateParserException { @Test public void testFor2() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater.parse("Check out this list: {% for x in data %}{{ x }}{% end %}"); + Template template = parse("Check out this list: {% for x in data %}{{ x }}{% end %}"); HashMap> mapModel = Maps.newHashMap(); mapModel.put("data", Arrays.asList("a", "b", "c", "d")); @@ -43,8 +34,7 @@ public void testFor2() throws TemplateParserException { @Test public void testForOverMap() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater.parse("Check out this map: {% for x in data %}{{ x[0] }} = {{ x[1] }},{% end %}."); + Template template = parse("Check out this map: {% for x in data %}{{ x[0] }} = {{ x[1] }},{% end %}."); HashMap mapModel = Maps.newHashMap(); TreeMap map = Maps.newTreeMap(); @@ -56,8 +46,7 @@ public void testForOverMap() throws TemplateParserException { @Test public void testForOverMapOrder() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater.parse("Check out this map: {% for x in data order by x[0] %}{{ x[0] }} = {{ x[1] }},{% end %}."); + Template template = parse("Check out this map: {% for x in data order by x[0] %}{{ x[0] }} = {{ x[1] }},{% end %}."); HashMap mapModel = Maps.newHashMap(); TreeMap map = Maps.newTreeMap(); @@ -69,8 +58,7 @@ public void testForOverMapOrder() throws TemplateParserException { @Test public void testForOrder() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater.parse("Check out this list: {% for x in data order by x %}{{ x }}{% end %}"); + Template template = parse("Check out this list: {% for x in data order by x %}{{ x }}{% end %}"); HashMap> mapModel = Maps.newHashMap(); mapModel.put("data", Arrays.asList("d", "b", "c", "a")); @@ -79,8 +67,7 @@ public void testForOrder() throws TemplateParserException { @Test public void testForOrder2() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater.parse("Check out this list: {% for x in data order by -x %}{{ x }}{% end %}"); + Template template = parse("Check out this list: {% for x in data order by -x %}{{ x }}{% end %}"); HashMap mapModel = Maps.newHashMap(); mapModel.put("data", Arrays.asList(4, 2, 1, 3)); @@ -89,8 +76,7 @@ public void testForOrder2() throws TemplateParserException { @Test public void testForOrderAndLimit() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater.parse("Check out this list: {% for x in data order by x limit 2 %}{{ x }}{% end %}"); + Template template = parse("Check out this list: {% for x in data order by x limit 2 %}{{ x }}{% end %}"); HashMap> mapModel = Maps.newHashMap(); mapModel.put("data", Arrays.asList("d", "b", "c", "a")); @@ -99,8 +85,7 @@ public void testForOrderAndLimit() throws TemplateParserException { @Test public void testForOrderAndLimit2() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater.parse("Check out this list: {% for x in data order by x limit 6 %}{{ x }}{% end %}"); + Template template = parse("Check out this list: {% for x in data order by x limit 6 %}{{ x }}{% end %}"); HashMap> mapModel = Maps.newHashMap(); mapModel.put("data", Arrays.asList("d", "b", "c", "a")); @@ -109,8 +94,7 @@ public void testForOrderAndLimit2() throws TemplateParserException { @Test public void testForString() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater.parse("String chars: {% for x in data %}{{ x }},{% end %}"); + Template template = parse("String chars: {% for x in data %}{{ x }},{% end %}"); HashMap mapModel = Maps.newHashMap(); mapModel.put("data", "abcd"); @@ -119,8 +103,7 @@ public void testForString() throws TemplateParserException { @Test public void testForArray() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater.parse("Integers: {% for x in data %}{{ x }},{% end %}"); + Template template = parse("Integers: {% for x in data %}{{ x }},{% end %}"); HashMap mapModel = Maps.newHashMap(); mapModel.put("data", new int[] { 1, 2, 3, 4 }); @@ -129,8 +112,7 @@ public void testForArray() throws TemplateParserException { @Test public void testForMap() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater.parse("Map: {% for x in data %}{{ x[0] }} == {{ x[1] }}, {% end %}"); + Template template = parse("Map: {% for x in data %}{{ x[0] }} == {{ x[1] }}, {% end %}"); Map mapModel = Maps.newHashMap(); Map map = Maps.newTreeMap(); @@ -142,8 +124,7 @@ public void testForMap() throws TemplateParserException { @Test public void testForLimit() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater.parse("Check out this list: {% for x in data limit lim %}{{x}}{% end %}"); + Template template = parse("Check out this list: {% for x in data limit lim %}{{x}}{% end %}"); HashMap mapModel = Maps.newHashMap(); mapModel.put("data", Arrays.asList("a", "b", "c", "d")); @@ -156,8 +137,7 @@ public void testForLimit() throws TemplateParserException { @Test public void testForLimitNull() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater.parse("Check out this list: {% for x in data limit null %}{{x}}{% end %}"); + Template template = parse("Check out this list: {% for x in data limit null %}{{x}}{% end %}"); HashMap mapModel = Maps.newHashMap(); mapModel.put("data", Arrays.asList("a", "b", "c", "d")); @@ -166,8 +146,7 @@ public void testForLimitNull() throws TemplateParserException { @Test public void testForScope() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater.parse("Check out this list: {{x}}{% for x in data %}{{x}}{% end %}{{x}}"); + Template template = parse("Check out this list: {{x}}{% for x in data %}{{x}}{% end %}{{x}}"); HashMap mapModel = Maps.newHashMap(); mapModel.put("data", Arrays.asList("a", "b", "c", "d")); @@ -178,9 +157,7 @@ public void testForScope() throws TemplateParserException { @SuppressWarnings("unchecked") @Test public void testNestedFor() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater - .parse("{% for item in x %}Whoa: {% for y in item.data %}{{ y }}{% else %}no data{% end %}. {% end %}"); + Template template = parse("{% for item in x %}Whoa: {% for y in item.data %}{{ y }}{% else %}no data{% end %}. {% end %}"); HashMap innerMap = Maps.newHashMap(); innerMap.put("data", Arrays.asList("a", "b", "c")); @@ -197,9 +174,7 @@ public void testNestedFor() throws TemplateParserException { @SuppressWarnings("unchecked") @Test public void testNestedForContinue() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater - .parse("{% for item in x %}Whoa: {% for y in item.data %}{% if y == 'b' %}{% continue %}{% end %}{{y}}{% else %}no data{% end %}. {% end %}"); + Template template = parse("{% for item in x %}Whoa: {% for y in item.data %}{% if y == 'b' %}{% continue %}{% end %}{{y}}{% else %}no data{% end %}. {% end %}"); HashMap innerMap = Maps.newHashMap(); innerMap.put("data", Arrays.asList("a", "b", "c")); @@ -220,9 +195,7 @@ public void testNestedForContinue() throws TemplateParserException { @SuppressWarnings("unchecked") @Test public void testNestedForContinue2() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater - .parse("{% for item in x %}Whoa: {% for y in item.data %}>{% if y == 'b' %}{% continue %}{% end %}{{y}}<{% else %}no data{% end %}. {% end %}"); + Template template = parse("{% for item in x %}Whoa: {% for y in item.data %}>{% if y == 'b' %}{% continue %}{% end %}{{y}}<{% else %}no data{% end %}. {% end %}"); HashMap innerMap = Maps.newHashMap(); innerMap.put("data", Arrays.asList("a", "b", "c")); @@ -239,8 +212,7 @@ public void testNestedForContinue2() throws TemplateParserException { @SuppressWarnings("unchecked") @Test public void testNestedFor2() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater.parse("{% for item in x %}Whoa: {% for y in item.data %}{{y}}{% else %}no data{% end %}. {% end %}"); + Template template = parse("{% for item in x %}Whoa: {% for y in item.data %}{{y}}{% else %}no data{% end %}. {% end %}"); HashMap innerMap = Maps.newHashMap(); innerMap.put("data", Arrays.asList("a", "b", "c")); @@ -256,8 +228,7 @@ public void testNestedFor2() throws TemplateParserException { @Test public void testForElse1() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater.parse("Check out this list: {% for x in data %}{{x}}{% else %}empty{% end %}"); + Template template = parse("Check out this list: {% for x in data %}{{x}}{% else %}empty{% end %}"); HashMap mapModel = Maps.newHashMap(); mapModel.put("data", Collections.emptyList()); @@ -266,8 +237,7 @@ public void testForElse1() throws TemplateParserException { @Test public void testForElse2() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater.parse("Check out this list: {% for x in data %}{{x}}{% else %}empty{% end %}"); + Template template = parse("Check out this list: {% for x in data %}{{x}}{% else %}empty{% end %}"); HashMap> mapModel = Maps.newHashMap(); mapModel.put("data", Arrays.asList("a", "b", "c", "d")); diff --git a/stencil/test/net/stencilproject/template/TestHtmlOutput.java b/stencil/test/net/stencilproject/template/TestHtmlOutput.java index 7bf830e..ec3aecf 100644 --- a/stencil/test/net/stencilproject/template/TestHtmlOutput.java +++ b/stencil/test/net/stencilproject/template/TestHtmlOutput.java @@ -2,23 +2,20 @@ import static org.junit.Assert.assertEquals; -import net.stencilproject.template.Template; -import net.stencilproject.template.TemplateParserException; -import net.stencilproject.template.Templater; +import java.io.IOException; import org.junit.Test; /** * Test some specific HTML mode issues. */ -public class TestHtmlOutput { +public class TestHtmlOutput extends AbstractTemplateTest { /** * br tags should be written using the compatible syntax: <br /> */ @Test - public void testBrTags() throws TemplateParserException { - Templater templater = new Templater(); - Template template = templater.parseXml(true, "Break tags rock
Just kidding"); + public void testBrTags() throws TemplateParserException, IOException { + Template template = parseHtml("Break tags rock
Just kidding"); System.out.println(template); assertEquals("Break tags rock
Just kidding", template.process(null)); @@ -29,8 +26,7 @@ public void testBrTags() throws TemplateParserException { */ @Test public void testScriptCDATAInHtml() throws TemplateParserException { - Templater templater = new Templater(); - Template template = templater.parseXml(true, ""); + Template template = parseHtml(""); System.out.println(template); @@ -42,8 +38,7 @@ public void testScriptCDATAInHtml() throws TemplateParserException { */ @Test public void testCDATAInHtml() throws TemplateParserException { - Templater templater = new Templater(); - Template template = templater.parseXml(true, "

"); + Template template = parseHtml("

"); System.out.println(template); diff --git a/stencil/test/net/stencilproject/template/TestIf.java b/stencil/test/net/stencilproject/template/TestIf.java index e8c1cb8..da76213 100644 --- a/stencil/test/net/stencilproject/template/TestIf.java +++ b/stencil/test/net/stencilproject/template/TestIf.java @@ -4,11 +4,6 @@ import java.util.HashMap; -import net.stencilproject.template.Template; -import net.stencilproject.template.TemplateOptions; -import net.stencilproject.template.TemplateParserException; -import net.stencilproject.template.Templater; - import org.junit.Test; import com.google.common.collect.Maps; @@ -16,18 +11,10 @@ /** * Tests the IF construct. */ -public class TestIf { - private Templater createTemplater() { - TemplateOptions options = new TemplateOptions(); - options.setDumpTemplate(true); - options.setTraceExecution(true); - return new Templater(null, options); - } - +public class TestIf extends AbstractTemplateTest { @Test public void testIf1a() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater.parse("x{% if x %} = true{% end %}, y{% if y %} = true{% end %}"); + Template template = parse("x{% if x %} = true{% end %}, y{% if y %} = true{% end %}"); HashMap mapModel = Maps.newHashMap(); mapModel.put("x", true); @@ -37,8 +24,7 @@ public void testIf1a() throws TemplateParserException { @Test public void testIf1b() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater.parse("x{% if x %} = true{% end %}, y{% if y %} = true{% end %}"); + Template template = parse("x{% if x %} = true{% end %}, y{% if y %} = true{% end %}"); HashMap mapModel = Maps.newHashMap(); mapModel.put("x", false); @@ -48,8 +34,7 @@ public void testIf1b() throws TemplateParserException { @Test public void testIf2() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater.parse("x{% if data.x %} = true{% end %}, y{% if data.y %} = true{% end %}"); + Template template = parse("x{% if data.x %} = true{% end %}, y{% if data.y %} = true{% end %}"); HashMap mapModelInner = Maps.newHashMap(); mapModelInner.put("x", true); @@ -61,8 +46,7 @@ public void testIf2() throws TemplateParserException { @Test public void testIfElse1() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater.parse("x{% if data.x %} = true{% else %} = false{% end %}, y{% if data.y %} = true{% else %} = false{% end %}"); + Template template = parse("x{% if data.x %} = true{% else %} = false{% end %}, y{% if data.y %} = true{% else %} = false{% end %}"); HashMap mapModelInner = Maps.newHashMap(); mapModelInner.put("x", true); @@ -74,8 +58,7 @@ public void testIfElse1() throws TemplateParserException { @Test public void testIfElse2() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater.parse("x{% if data.x %} = true{% else %} = false{% end %}, y{% if data.y %} = true{% else %} = false{% end %}"); + Template template = parse("x{% if data.x %} = true{% else %} = false{% end %}, y{% if data.y %} = true{% else %} = false{% end %}"); HashMap mapModelInner = Maps.newHashMap(); mapModelInner.put("x", false); @@ -87,8 +70,7 @@ public void testIfElse2() throws TemplateParserException { @Test public void testIfElIf1() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater.parse("{% if x %}x = true{% elif y %}y = true{% else %}neither{% end %}"); + Template template = parse("{% if x %}x = true{% elif y %}y = true{% else %}neither{% end %}"); HashMap mapModel = Maps.newHashMap(); @@ -111,8 +93,7 @@ public void testIfElIf1() throws TemplateParserException { @Test public void testIfElIf2() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater.parse("{% if x and y %}x&&y{% elif x %}x{% elif y %}y{% else %}!x&&!y{% end %}"); + Template template = parse("{% if x and y %}x&&y{% elif x %}x{% elif y %}y{% else %}!x&&!y{% end %}"); HashMap mapModel = Maps.newHashMap(); @@ -135,8 +116,7 @@ public void testIfElIf2() throws TemplateParserException { @Test public void testIfElIf3() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater.parse("{% if x and y %}x&&y{% elif x or y %}x||y{% else %}!x&&!y{% end %}"); + Template template = parse("{% if x and y %}x&&y{% elif x or y %}x||y{% else %}!x&&!y{% end %}"); HashMap mapModel = Maps.newHashMap(); diff --git a/stencil/test/net/stencilproject/template/TestIteratorUtils.java b/stencil/test/net/stencilproject/template/TestIteratorUtils.java index 923fc78..9bfd372 100644 --- a/stencil/test/net/stencilproject/template/TestIteratorUtils.java +++ b/stencil/test/net/stencilproject/template/TestIteratorUtils.java @@ -11,13 +11,6 @@ import java.util.Iterator; import java.util.Map; -import net.stencilproject.template.IteratorUtils; -import net.stencilproject.template.RunStatistics; -import net.stencilproject.template.Template; -import net.stencilproject.template.TemplateOptions; -import net.stencilproject.template.TemplateParserException; -import net.stencilproject.template.Templater; - import org.junit.Test; import com.google.common.collect.Maps; @@ -25,7 +18,7 @@ /** * This tests {@link ConcurrentModificationException} in templates. */ -public class TestIteratorUtils { +public class TestIteratorUtils extends AbstractTemplateTest { @Test public void testConcurrentModification() { HashMap map = Maps.newHashMap(); @@ -48,12 +41,7 @@ public void testConcurrentModification() { @Test public void testConcurrentModificationInTemplate() throws TemplateParserException, IOException { - TemplateOptions options = new TemplateOptions(); - options.setDumpTemplate(true); - options.setTraceExecution(true); - Templater templater = new Templater(null, options); - - Template template = templater.parse("{% for x in y %}{{ x }}{% end %}"); + Template template = parse("{% for x in y %}{{ x }}{% end %}"); StringWriter writer = new StringWriter(); final HashMap map = Maps.newHashMap(); diff --git a/stencil/test/net/stencilproject/template/TestParseExceptions.java b/stencil/test/net/stencilproject/template/TestParseExceptions.java index 30d2143..373dd8f 100644 --- a/stencil/test/net/stencilproject/template/TestParseExceptions.java +++ b/stencil/test/net/stencilproject/template/TestParseExceptions.java @@ -9,19 +9,11 @@ /** * Tests for various parsing exceptions. */ -public class TestParseExceptions { - private Templater createTemplater() { - TemplateOptions options = new TemplateOptions(); - options.setDumpTemplate(true); - options.setTraceExecution(true); - return new Templater(null, options); - } - +public class TestParseExceptions extends AbstractTemplateTest { @Test public void testErrorInvalidChar() { - Templater templater = createTemplater(); try { - templater.parse("{{ ! }} <- bad token"); + parse("{{ ! }} <- bad token"); fail("Should have thrown exception"); } catch (TemplateParserException e) { assertEquals(TemplateError.PARSE_ERROR, e.getError()); @@ -32,9 +24,8 @@ public void testErrorInvalidChar() { @Test public void testErrorReservedWord() { - Templater templater = createTemplater(); try { - templater.parse("{{ foo.limit }} <- limit is a reserved word"); + parse("{{ foo.limit }} <- limit is a reserved word"); fail("Should have thrown exception"); } catch (TemplateParserException e) { assertEquals(TemplateError.PARSE_ERROR, e.getError()); @@ -46,16 +37,15 @@ public void testErrorReservedWord() { @Test public void testErrorIfWithoutEnd() { - Templater templater = createTemplater(); try { - templater.parse("{% if x %} <- missing end"); + parse("{% if x %} <- missing end"); fail("Should have thrown exception"); } catch (TemplateParserException e) { assertEquals(TemplateError.IF_MISSING_END, e.getError()); } try { - templater.parse("{% if x %} <- missing end {% else %}"); + parse("{% if x %} <- missing end {% else %}"); fail("Should have thrown exception"); } catch (TemplateParserException e) { assertEquals(TemplateError.IF_MISSING_END, e.getError()); @@ -64,16 +54,15 @@ public void testErrorIfWithoutEnd() { @Test public void testErrorForWithoutEnd() { - Templater templater = createTemplater(); try { - templater.parse("{% for x in y %} <- missing end"); + parse("{% for x in y %} <- missing end"); fail("Should have thrown exception"); } catch (TemplateParserException e) { assertEquals(TemplateError.FOR_MISSING_END, e.getError()); } try { - templater.parse("{% for x in y %} <- missing end {% else %}"); + parse("{% for x in y %} <- missing end {% else %}"); fail("Should have thrown exception"); } catch (TemplateParserException e) { assertEquals(TemplateError.FOR_MISSING_END, e.getError()); @@ -82,9 +71,8 @@ public void testErrorForWithoutEnd() { @Test public void testErrorForDuplicateElse() { - Templater templater = createTemplater(); try { - templater.parse("{% for x in y %}{% else %}{% else %} <- duplicate else {% end %}"); + parse("{% for x in y %}{% else %}{% else %} <- duplicate else {% end %}"); fail("Should have thrown exception"); } catch (TemplateParserException e) { assertEquals(TemplateError.FOR_DUPLICATE_ELSE, e.getError()); @@ -93,9 +81,8 @@ public void testErrorForDuplicateElse() { @Test public void testErrorIfDuplicateElse() { - Templater templater = createTemplater(); try { - templater.parse("{% if x %}{% else %}{% else %} <- duplicate else {% end %}"); + parse("{% if x %}{% else %}{% else %} <- duplicate else {% end %}"); fail("Should have thrown exception"); } catch (TemplateParserException e) { assertEquals(TemplateError.IF_DUPLICATE_ELSE, e.getError()); @@ -104,9 +91,8 @@ public void testErrorIfDuplicateElse() { @Test public void testErrorElifWithoutBlock() { - Templater templater = createTemplater(); try { - templater.parse("{% elif x %} <- missing a block"); + parse("{% elif x %} <- missing a block"); fail("Should have thrown exception"); } catch (TemplateParserException e) { assertEquals(TemplateError.ELIF_WITHOUT_IF, e.getError()); @@ -115,9 +101,8 @@ public void testErrorElifWithoutBlock() { @Test public void testErrorElifWithWrongBlock() { - Templater templater = createTemplater(); try { - templater.parse("{% for x in y %}{% elif x %} <- doesn't match for"); + parse("{% for x in y %}{% elif x %} <- doesn't match for"); fail("Should have thrown exception"); } catch (TemplateParserException e) { assertEquals(TemplateError.ELIF_WITH_WRONG_BLOCK, e.getError()); @@ -126,9 +111,8 @@ public void testErrorElifWithWrongBlock() { @Test public void testErrorInvalidExpression() { - Templater templater = createTemplater(); try { - templater.parse("{{ 1 xor 1 }}"); + parse("{{ 1 xor 1 }}"); fail("Should have thrown exception"); } catch (TemplateParserException e) { assertEquals(TemplateError.PARSE_ERROR, e.getError()); @@ -137,9 +121,8 @@ public void testErrorInvalidExpression() { @Test public void testEndDefaultBlock() { - Templater templater = createTemplater(); try { - templater.parse("{% end %}"); + parse("{% end %}"); fail("Should have thrown exception"); } catch (TemplateParserException e) { assertEquals(TemplateError.END_WITHOUT_BLOCK, e.getError()); @@ -148,9 +131,8 @@ public void testEndDefaultBlock() { @Test public void testElseDefaultBlock() { - Templater templater = createTemplater(); try { - templater.parse("{% else %}"); + parse("{% else %}"); fail("Should have thrown exception"); } catch (TemplateParserException e) { assertEquals(TemplateError.ELSE_WITHOUT_BLOCK, e.getError()); diff --git a/stencil/test/net/stencilproject/template/TestReflection.java b/stencil/test/net/stencilproject/template/TestReflection.java index 597683a..c1ca31a 100644 --- a/stencil/test/net/stencilproject/template/TestReflection.java +++ b/stencil/test/net/stencilproject/template/TestReflection.java @@ -4,29 +4,16 @@ import java.util.HashMap; -import net.stencilproject.template.Template; -import net.stencilproject.template.TemplateOptions; -import net.stencilproject.template.TemplateParserException; -import net.stencilproject.template.TemplateRootScope; -import net.stencilproject.template.Templater; import net.stencilproject.template.otherpackage.NonPublicObjectFactory; import org.junit.Test; import com.google.common.collect.Maps; -public class TestReflection { - private Templater createTemplater() { - TemplateOptions options = new TemplateOptions(); - options.setDumpTemplate(true); - options.setTraceExecution(true); - return new Templater(null, options); - } - +public class TestReflection extends AbstractTemplateTest { @Test public void testPackageProtectedObject() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater.parse("{{ x.value }} {{ x.field }} {{ x.privateField }}"); + Template template = parse("{{ x.value }} {{ x.field }} {{ x.privateField }}"); HashMap mapModel = Maps.newHashMap(); mapModel.put("x", new NonPublicObjectFactory().getPackageProtectedObject()); @@ -35,8 +22,7 @@ public void testPackageProtectedObject() throws TemplateParserException { @Test public void testPrivateObject() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater.parse("{{ x.value }} {{ x.field }} {{ x.privateField }}"); + Template template = parse("{{ x.value }} {{ x.field }} {{ x.privateField }}"); HashMap mapModel = Maps.newHashMap(); mapModel.put("x", new NonPublicObjectFactory().getPrivateObject()); @@ -48,8 +34,9 @@ public void testInstanceHelperMethods() throws TemplateParserException { TemplateRootScope rootScope = new TemplateRootScope(); rootScope.put("x", new InstanceTestMethods()); - Templater templater = new Templater(rootScope); - Template template = templater.parse("{{ x.add_x('foo') }} {{x.add_x(1)}} {{x.sub_1(10)}} {{x.sub_1('10')}} {{x.self().sub_1(10)}} {{x.self().sub_1('10')}} {{x.add_together(1, 2)}}"); + Template template = parse( + rootScope, + "{{ x.add_x('foo') }} {{x.add_x(1)}} {{x.sub_1(10)}} {{x.sub_1('10')}} {{x.self().sub_1(10)}} {{x.self().sub_1('10')}} {{x.add_together(1, 2)}}"); assertEquals("foox 1x 9 9 9 9 3", template.process(null)); } @@ -57,16 +44,15 @@ public void testInstanceHelperMethods() throws TemplateParserException { public void testHelperEnum() throws TemplateParserException { TemplateRootScope rootScope = new TemplateRootScope(); rootScope.addEnum(EnumValuesToImport.class); - Templater templater = new Templater(rootScope); - Template template = templater.parse("{{ EnumValuesToImport.ENUM_A }} = {{ +EnumValuesToImport.ENUM_A }} {{ EnumValuesToImport.ENUM_B }} = {{ +EnumValuesToImport.ENUM_B }}"); + Template template = parse(rootScope, + "{{ EnumValuesToImport.ENUM_A }} = {{ +EnumValuesToImport.ENUM_A }} {{ EnumValuesToImport.ENUM_B }} = {{ +EnumValuesToImport.ENUM_B }}"); assertEquals("ENUM_A = 0 ENUM_B = 1", template.process(null)); } @Test public void testPropertyOverridesField() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater.parse("{{ x.x }}"); + Template template = parse("{{ x.x }}"); HashMap mapModel = Maps.newHashMap(); mapModel.put("x", new OverrideFieldWithProperty()); diff --git a/stencil/test/net/stencilproject/template/TestTemplater.java b/stencil/test/net/stencilproject/template/TestTemplater.java index 864ad87..312145d 100644 --- a/stencil/test/net/stencilproject/template/TestTemplater.java +++ b/stencil/test/net/stencilproject/template/TestTemplater.java @@ -19,22 +19,14 @@ import com.google.common.collect.Maps; -public class TestTemplater { +public class TestTemplater extends AbstractTemplateTest { enum EnumValues { BLAH, FOO } - private Templater createTemplater() { - TemplateOptions options = new TemplateOptions(); - options.setDumpTemplate(true); - options.setTraceExecution(true); - return new Templater(null, options); - } - @Test public void testTemplateEscapes() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater.parse("{{1}} {{{2}}} {{{{3}}}} {%%%raw {{4}}%%%}"); + Template template = parse("{{1}} {{{2}}} {{{{3}}}} {%%%raw {{4}}%%%}"); // ${}: Normal template // $${}: Literal '${}' @@ -47,24 +39,21 @@ public void testTemplateEscapes() throws TemplateParserException { @Test public void testStringyLookingTextOutsideTemplate() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater.parse("'{{ 1 }}'"); + Template template = parse("'{{ 1 }}'"); assertEquals("'1'", template.process(null)); } @Test public void testCommentyLookingTextOutsideTemplate() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater.parse("/*{{ 1 }}*/"); + Template template = parse("/*{{ 1 }}*/"); assertEquals("/*1*/", template.process(null)); } @Test public void testSimpleExpression() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater.parse("{{ x }}"); + Template template = parse("{{ x }}"); HashMap mapModel = new HashMap(); mapModel.put("x", 1); @@ -73,8 +62,7 @@ public void testSimpleExpression() throws TemplateParserException { @Test public void testMapVsMethod() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater.parse("{{x.name}}, {{y.name}}"); + Template template = parse("{{x.name}}, {{y.name}}"); HashMap mapModel = Maps.newHashMap(); HashMap innerMap = Maps.newHashMap(); @@ -86,16 +74,14 @@ public void testMapVsMethod() throws TemplateParserException { @Test public void testBasicRange() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater.parse("{% for i in 1 to 10 %}{{ i }},{% end %}"); + Template template = parse("{% for i in 1 to 10 %}{{ i }},{% end %}"); assertEquals("1,2,3,4,5,6,7,8,9,10,", template.process(null)); } @Test public void testRangeWithConditional() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater.parse("{% for i in false ? 3 : 1 to false ? 5 : 10 %}{{ i }},{% end %}"); + Template template = parse("{% for i in false ? 3 : 1 to false ? 5 : 10 %}{{ i }},{% end %}"); assertEquals("1,2,3,4,5,6,7,8,9,10,", template.process(null)); } @@ -111,8 +97,8 @@ public void testLongRange() throws TemplateParserException { templateString += "{% for i in " + test[0] + "%}{{ i }},{% end %}|"; resultString += test[1] + (test[1].length() == 0 ? "" : ",") + "|"; } - Templater templater = createTemplater(); - Template template = templater.parse(templateString); + + Template template = parse(templateString); HashMap mapModel = Maps.newHashMap(); assertEquals(resultString, template.process(mapModel)); @@ -130,8 +116,8 @@ public void testDoubleRange() throws TemplateParserException { templateString += "{% for i in " + test[0] + "%}{{ i }},{% end %}|"; resultString += test[1] + (test[1].length() == 0 ? "" : ",") + "|"; } - Templater templater = createTemplater(); - Template template = templater.parse(templateString); + + Template template = parse(templateString); HashMap mapModel = Maps.newHashMap(); assertEquals(resultString, template.process(mapModel)); @@ -148,8 +134,8 @@ public void testIn() throws TemplateParserException { templateString += "{{ " + test[0] + " }}|"; resultString += test[1] + "|"; } - Templater templater = createTemplater(); - Template template = templater.parse(templateString); + + Template template = parse(templateString); HashMap mapModel = Maps.newHashMap(); assertEquals(resultString, template.process(mapModel)); @@ -168,8 +154,8 @@ public void testStringSublist() throws TemplateParserException { templateString += "{{x[" + test[0] + "]}},"; resultString += test[1] + ","; } - Templater templater = createTemplater(); - Template template = templater.parse(templateString); + + Template template = parse(templateString); HashMap mapModel = Maps.newHashMap(); mapModel.put("x", "0123456789"); @@ -178,9 +164,7 @@ public void testStringSublist() throws TemplateParserException { @Test public void testListIndex() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater - .parse("{{ x.data[1] }} {{ x.data[-1] }} {{ x.data[-2] }} {{ x.data[-3] }} [{{ x.data[-4] }}] [{{ x.data[4] }}]"); + Template template = parse("{{ x.data[1] }} {{ x.data[-1] }} {{ x.data[-2] }} {{ x.data[-3] }} [{{ x.data[-4] }}] [{{ x.data[4] }}]"); HashMap mapModelInner = Maps.newHashMap(); mapModelInner.put("data", Arrays.asList("a", "b", "c")); @@ -202,8 +186,8 @@ public void testArraySlice() throws TemplateParserException { templateString += "{% for y in x[" + test[0] + "] %}{{ y }}{% end %} "; resultString += test[1] + " "; } - Templater templater = createTemplater(); - Template template = templater.parse(templateString); + + Template template = parse(templateString); HashMap mapModel = Maps.newHashMap(); mapModel.put("x", Arrays.asList('0', '1', '2', '3', '4', '5', '6', '7', '8', '9')); @@ -214,8 +198,8 @@ public void testArraySlice() throws TemplateParserException { @Test public void testListImplicitBooleanConversion() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater.parse("{{not not listempty}} {{not not justnull}} {{not not oneitem}}"); + + Template template = parse("{{not not listempty}} {{not not justnull}} {{not not oneitem}}"); HashMap mapModel = Maps.newHashMap(); mapModel.put("listempty", Arrays.asList()); @@ -226,8 +210,7 @@ public void testListImplicitBooleanConversion() throws TemplateParserException { @Test public void testMapImplicitBooleanConversion() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater.parse("{{not not mapempty}} {{not not justnull}} {{not not oneitem}}"); + Template template = parse("{{not not mapempty}} {{not not justnull}} {{not not oneitem}}"); HashMap mapModel = Maps.newHashMap(); mapModel.put("mapempty", Collections.EMPTY_MAP); @@ -240,8 +223,7 @@ public void testMapImplicitBooleanConversion() throws TemplateParserException { @Test public void testObjectImplicitBooleanConversion() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater.parse("{{not not emptystring}} {{not not string0}} {{not not string1}} {{not not object1}}"); + Template template = parse("{{not not emptystring}} {{not not string0}} {{not not string1}} {{not not object1}}"); HashMap mapModel = Maps.newHashMap(); mapModel.put("emptystring", ""); @@ -253,9 +235,7 @@ public void testObjectImplicitBooleanConversion() throws TemplateParserException @Test public void testComparisons() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater - .parse("{{x1 > y1}} {{x1 > y2}} {{x2 > y1}} {{x2 > y2}} {{x1 < y1}} {{x1 < y2}} {{x2 < y1}} {{x2 < y2}}"); + Template template = parse("{{x1 > y1}} {{x1 > y2}} {{x2 > y1}} {{x2 > y2}} {{x1 < y1}} {{x1 < y2}} {{x2 < y1}} {{x2 < y2}}"); HashMap mapModel = Maps.newHashMap(); mapModel.put("x1", 1); @@ -267,9 +247,7 @@ public void testComparisons() throws TemplateParserException { @Test public void testComparisons2() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater - .parse("{{x1 >= y1}} {{x1 >= y2}} {{x2 >= y1}} {{x2 >= y2}} {{x1 <= y1}} {{x1 <= y2}} {{x2 <= y1}} {{x2 <= y2}}"); + Template template = parse("{{x1 >= y1}} {{x1 >= y2}} {{x2 >= y1}} {{x2 >= y2}} {{x1 <= y1}} {{x1 <= y2}} {{x2 <= y1}} {{x2 <= y2}}"); HashMap mapModel = Maps.newHashMap(); mapModel.put("x1", 1); @@ -285,8 +263,7 @@ public void testComparisons2() throws TemplateParserException { */ @Test public void testStringComparisons() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater.parse("{{ x == y }} {{ x != y }} {{ x < y }} {{ x > y }} {{ x <= y }} {{ x >= y }}"); + Template template = parse("{{ x == y }} {{ x != y }} {{ x < y }} {{ x > y }} {{ x <= y }} {{ x >= y }}"); Object[] stringLikes = new Object[] { 'a', "a", new StringBuilder("a"), new StringBuffer("a"), CharBuffer.wrap(new char[] { 'a' }) }; @@ -302,8 +279,7 @@ public void testStringComparisons() throws TemplateParserException { @Test public void testComparisonsEnumString() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater.parse("{{x == 'BLAH'}} {{ x != 'BLAH' }} {{ x == 'FOO' }} {{ x != 'FOO' }}"); + Template template = parse("{{x == 'BLAH'}} {{ x != 'BLAH' }} {{ x == 'FOO' }} {{ x != 'FOO' }}"); HashMap mapModel = Maps.newHashMap(); mapModel.put("x", EnumValues.BLAH); @@ -313,8 +289,7 @@ public void testComparisonsEnumString() throws TemplateParserException { @Test public void testComparisonsEnumInteger() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater.parse("{{x == 0}} {{ x != 0 }} {{ x == 1 }} {{ x != 1 }}"); + Template template = parse("{{x == 0}} {{ x != 0 }} {{ x == 1 }} {{ x != 1 }}"); HashMap mapModel = Maps.newHashMap(); mapModel.put("x", EnumValues.BLAH); @@ -324,8 +299,7 @@ public void testComparisonsEnumInteger() throws TemplateParserException { @Test public void testComparisonsNull() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater.parse("{{null < zeroString}} {{null < oneString}} {{null < zeroInt}} {{null < null}} " + Template template = parse("{{null < zeroString}} {{null < oneString}} {{null < zeroInt}} {{null < null}} " + "{{null > zeroString}} {{null > oneString}} {{null > zeroInt}} {{null > null}} " + "{{null == zeroString}} {{null == oneString}} {{null == zeroInt}} {{null == null}}"); @@ -338,7 +312,6 @@ public void testComparisonsNull() throws TemplateParserException { @Test public void testConditional() throws TemplateParserException { - Templater templater = createTemplater(); Object[][] tests = new Object[][] { { "zeroString", true }, // "0" is // true, // since @@ -357,7 +330,7 @@ public void testConditional() throws TemplateParserException { resultString.append(test[1]).append(' '); } - Template template = templater.parse(templateString.toString()); + Template template = parse(templateString.toString()); HashMap mapModel = Maps.newHashMap(); mapModel.put("zeroString", "0"); @@ -373,8 +346,7 @@ public void testConditional() throws TemplateParserException { */ @Test public void testWarningCount() throws TemplateParserException, IOException { - Templater templater = createTemplater(); - Template template = templater.parse("{{ x.a|bool }} {% if x.a %}yes{% else %}no{% end %}"); + Template template = parse("{{ x.a|bool }} {% if x.a %}yes{% else %}no{% end %}"); StringWriter writer = new StringWriter(); HashMap mapModel = Maps.newHashMap(); @@ -390,9 +362,7 @@ public void testWarningCount() throws TemplateParserException, IOException { */ @Test public void testAndOrShortCircuit() throws TemplateParserException, IOException { - Templater templater = createTemplater(); - Template template = templater - .parse("{{ (x and x.a)|bool }} {{ not x or not x.a }} {% if x and x.a %}yes{% else %}no{% end %} {% if not x or not x.a %}yes{% else %}no{% end %}"); + Template template = parse("{{ (x and x.a)|bool }} {{ not x or not x.a }} {% if x and x.a %}yes{% else %}no{% end %} {% if not x or not x.a %}yes{% else %}no{% end %}"); StringWriter writer = new StringWriter(); HashMap mapModel = Maps.newHashMap(); @@ -411,8 +381,7 @@ public void testAndOrShortCircuit() throws TemplateParserException, IOException */ @Test public void testShortCircuitDot() throws TemplateParserException, IOException { - Templater templater = createTemplater(); - Template template = templater.parse("{{ x|bool }} {{ x?.a|bool }}"); + Template template = parse("{{ x|bool }} {{ x?.a|bool }}"); StringWriter writer = new StringWriter(); HashMap mapModel = Maps.newHashMap(); @@ -425,11 +394,9 @@ public void testShortCircuitDot() throws TemplateParserException, IOException { @Test public void testAndOrXorPrecedence() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater - .parse("{{ true or true and false }} {{ false and true or true }} {{ true and true ^ false and false }} {{ true or true ^ true or false }} " - + "{{ 1 or 1 and 0 }} {{ 0 and 1 or 1 }} {{ 1 and 1 ^ 0 and 0 }} {{ 1 or 1 ^ 1 or 0 }}"); - // Template template = templater.parse("${var x}x = '{{ x }}'${x = 1}"); + Template template = parse("{{ true or true and false }} {{ false and true or true }} {{ true and true ^ false and false }} {{ true or true ^ true or false }} " + + "{{ 1 or 1 and 0 }} {{ 0 and 1 or 1 }} {{ 1 and 1 ^ 0 and 0 }} {{ 1 or 1 ^ 1 or 0 }}"); + // Template template = parse("${var x}x = '{{ x }}'${x = 1}"); HashMap mapModel = Maps.newHashMap(); @@ -438,8 +405,7 @@ public void testAndOrXorPrecedence() throws TemplateParserException { @Test public void testVariables() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater.parse("{% var x %}x = '{{ x }}'{% x = 1 %} x = '{{ x }}'"); + Template template = parse("{% var x %}x = '{{ x }}'{% x = 1 %} x = '{{ x }}'"); HashMap mapModel = Maps.newHashMap(); mapModel.put("y", Arrays.asList(1, 2, 3)); @@ -449,8 +415,7 @@ public void testVariables() throws TemplateParserException { @Test public void testVariables2() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater.parse("{% var x = 1 %}x = '{{ x }}'{% x = 2 %} x = '{{ x }}'"); + Template template = parse("{% var x = 1 %}x = '{{ x }}'{% x = 2 %} x = '{{ x }}'"); HashMap mapModel = Maps.newHashMap(); mapModel.put("y", Arrays.asList(1, 2, 3)); @@ -460,10 +425,8 @@ public void testVariables2() throws TemplateParserException { @Test public void testVariables3() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater - .parse("{% var x %}x = '{{ x }}'{% x = 1 %} x = '{{ x }}' {% for z in y %}{% x = z %}x = '{{ x }}' {% end %}x = '{{ x }}'"); - // Template template = templater.parse("${var x}x = '{{ x }}'${x = 1}"); + Template template = parse("{% var x %}x = '{{ x }}'{% x = 1 %} x = '{{ x }}' {% for z in y %}{% x = z %}x = '{{ x }}' {% end %}x = '{{ x }}'"); + // Template template = parse("${var x}x = '{{ x }}'${x = 1}"); HashMap mapModel = Maps.newHashMap(); mapModel.put("y", Arrays.asList(1, 2, 3)); @@ -473,10 +436,8 @@ public void testVariables3() throws TemplateParserException { @Test public void testVariableWithAssignment() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater - .parse("{% var x = true %}x = '{{ x }}'{% x = 1 %} x = '{{ x }}' {% for z in y %}{% x = z %}x = '{{ x }}' {% end %}x = '{{ x }}'"); - // Template template = templater.parse("${var x}x = '{{ x }}'${x = 1}"); + Template template = parse("{% var x = true %}x = '{{ x }}'{% x = 1 %} x = '{{ x }}' {% for z in y %}{% x = z %}x = '{{ x }}' {% end %}x = '{{ x }}'"); + // Template template = parse("${var x}x = '{{ x }}'${x = 1}"); HashMap mapModel = Maps.newHashMap(); mapModel.put("y", Arrays.asList(1, 2, 3)); @@ -486,9 +447,7 @@ public void testVariableWithAssignment() throws TemplateParserException { @Test public void testAllIntegerOperators() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater - .parse("{{x + 1}} {{x - 1}} {{x * 2}} {{x / 2}} {{-x}} {{+x}} {{~x}} {{not x}} {{x << 1}} {{x >> 1}} {{x ^ 1}} {{x and 1}} {{x or 1}} {{x iand 14}} {{ x ior 1 }}"); + Template template = parse("{{x + 1}} {{x - 1}} {{x * 2}} {{x / 2}} {{-x}} {{+x}} {{~x}} {{not x}} {{x << 1}} {{x >> 1}} {{x ^ 1}} {{x and 1}} {{x or 1}} {{x iand 14}} {{ x ior 1 }}"); HashMap mapModel = Maps.newHashMap(); mapModel.put("x", 10); @@ -498,25 +457,21 @@ public void testAllIntegerOperators() throws TemplateParserException { @Test public void testIntegerLiterals() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater.parse("{{1000}} {{0x1000}} {{0xfFfFfF}}"); + Template template = parse("{{1000}} {{0x1000}} {{0xfFfFfF}}"); assertEquals(1000 + " " + 0x1000 + " " + 0xffffff, template.process(null)); } @Test public void testListLiterals() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater.parse("{{ [1,2,3] }}"); + Template template = parse("{{ [1,2,3] }}"); assertEquals("[1, 2, 3]", template.process(null)); } @Test public void testAllDoubleOperators() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater - .parse("{{x + 1}} {{x - 1}} {{x * 2}} {{x / 2}} {{-x}} {{+x}} {{~x}} {{not x}} {{x << 1}} {{x >> 1}} {{x ^ 1}} {{x and 1}} {{x or 1}}"); + Template template = parse("{{x + 1}} {{x - 1}} {{x * 2}} {{x / 2}} {{-x}} {{+x}} {{~x}} {{not x}} {{x << 1}} {{x >> 1}} {{x ^ 1}} {{x and 1}} {{x or 1}}"); HashMap mapModel = Maps.newHashMap(); mapModel.put("x", 6.5); @@ -526,8 +481,7 @@ public void testAllDoubleOperators() throws TemplateParserException { @Test public void testAllStringOperators() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater.parse("{{x + '1'}}"); // Any others? + Template template = parse("{{x + '1'}}"); // Any others? HashMap mapModel = Maps.newHashMap(); mapModel.put("x", "10"); @@ -537,24 +491,21 @@ public void testAllStringOperators() throws TemplateParserException { @Test public void testMultilineStrings() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater.parse("{{'foo\nbar'}}"); + Template template = parse("{{'foo\nbar'}}"); assertEquals("foo\nbar", template.process(null)); } @Test public void testMultilineStrings2() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater.parse("{{\"\"\"\"\\x66foo\nbar\"\"\"}} {{'''foo\"\"\"bar'''}}"); + Template template = parse("{{\"\"\"\"\\x66foo\nbar\"\"\"}} {{'''foo\"\"\"bar'''}}"); assertEquals("\"\\x66foo\nbar foo\"\"\"bar", template.process(null)); } @Test public void testStringEscapes() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater.parse("{{x + '\\\\\"' + \"\\\'\" + '\\xff\\u202a'}}"); + Template template = parse("{{x + '\\\\\"' + \"\\\'\" + '\\xff\\u202a'}}"); HashMap mapModel = Maps.newHashMap(); mapModel.put("x", "10"); @@ -564,9 +515,7 @@ public void testStringEscapes() throws TemplateParserException { @Test public void testComplex() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater - .parse(".{% if flag %}.{% for x in data %}<1{{ x }}>{% else %}[1empty]{% end %}.{% else %}.{% for x in data %}<2{{ x }}>{% else %}[2empty]{% end %}.{% end %}."); + Template template = parse(".{% if flag %}.{% for x in data %}<1{{ x }}>{% else %}[1empty]{% end %}.{% else %}.{% for x in data %}<2{{ x }}>{% else %}[2empty]{% end %}.{% end %}."); HashMap mapModel = Maps.newHashMap(); @@ -589,8 +538,7 @@ public void testComplex() throws TemplateParserException { @Test public void testMultipleStatements() throws TemplateParserException, IOException { - Templater templater = createTemplater(); - Template template = templater.parseText(TemplateFile.fromResource(getClass(), "multiline.txt")); + Template template = parseResource("multiline.txt"); HashMap mapModel = Maps.newHashMap(); @@ -613,8 +561,7 @@ public void testMultipleStatements() throws TemplateParserException, IOException @Test public void testFilter() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater.parseXml(false, "{{ '123'|size }}"); + Template template = parse("{{ '123'|size }}"); HashMap mapModel = Maps.newHashMap(); @@ -623,8 +570,7 @@ public void testFilter() throws TemplateParserException { @Test public void testFilterWithArgs() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater.parse("{{ x|join('[', ']') }}"); + Template template = parse("{{ x|join('[', ']') }}"); HashMap mapModel = Maps.newHashMap(); mapModel.put("x", Arrays.asList(1, 2, 3)); @@ -633,11 +579,7 @@ public void testFilterWithArgs() throws TemplateParserException { @Test public void testFilters() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater - .parseXml( - false, - "{{ x|raw }} {{ '<'|escape }} {{ x|size }} {{ '123'|size }} {{ [1,2,3]|size }} {{ '1.5'|float + 1 }} {{ '1'|int + 1 }} {{ 1|bool }} {{ 0|bool }} {{ true|bool }} {{ false|bool }} {{ 'http://test?c=SPORTSHEADS&url=http%3A%2F%2Fhosted2%2Eap%2E'|urlencode }}"); + Template template = parseXml("{{ x|raw }} {{ '<'|escape }} {{ x|size }} {{ '123'|size }} {{ [1,2,3]|size }} {{ '1.5'|float + 1 }} {{ '1'|int + 1 }} {{ 1|bool }} {{ 0|bool }} {{ true|bool }} {{ false|bool }} {{ 'http://test?c=SPORTSHEADS&url=http%3A%2F%2Fhosted2%2Eap%2E'|urlencode }}"); HashMap mapModel = Maps.newHashMap(); mapModel.put("x", "<"); @@ -649,11 +591,7 @@ public void testFilters() throws TemplateParserException { @Test public void testFormatFilter() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater - .parseXml( - false, - "{{ array|format('%d/%d') }} {{ array2|format('%d/%d') }} {{ array3|format('%d/%d') }} {{ array4|format('%d/%d') }} {{ array5|format('%d/%d') }} {{ list|format('%d/%d') }} {{ [1,2]|format('%d/%d') }} {{ 123|format('%d') }}"); + Template template = parseXml("{{ array|format('%d/%d') }} {{ array2|format('%d/%d') }} {{ array3|format('%d/%d') }} {{ array4|format('%d/%d') }} {{ array5|format('%d/%d') }} {{ list|format('%d/%d') }} {{ [1,2]|format('%d/%d') }} {{ 123|format('%d') }}"); HashMap mapModel = Maps.newHashMap(); mapModel.put("array", new int[] { 1, 2 }); @@ -668,9 +606,7 @@ public void testFormatFilter() throws TemplateParserException { @Test public void testJoinFilter() throws TemplateParserException { - Templater templater = createTemplater(); - Template template = templater - .parse("{{ x|join }} {{ x|join(',') }} {{ x|join('[', ']') }} {{ x|join('[', ',', ']') }} {{ x|join('', '[', ',', ']', '') }} {{ x|join('{', '[', ']', '}') }} {{ x|join('{', '[', ',', ']', '}') }}"); + Template template = parse("{{ x|join }} {{ x|join(',') }} {{ x|join('[', ']') }} {{ x|join('[', ',', ']') }} {{ x|join('', '[', ',', ']', '') }} {{ x|join('{', '[', ']', '}') }} {{ x|join('{', '[', ',', ']', '}') }}"); HashMap mapModel = Maps.newHashMap(); mapModel.put("x", Arrays.asList(1, 2, 3)); @@ -682,15 +618,13 @@ public void testStaticFilters() throws TemplateParserException { TemplateRootScope rootScope = new TemplateRootScope(); rootScope.addFilters(StaticTestMethods.class); - Templater templater = new Templater(rootScope); - Template template = templater.parse("{{ 'foo'|add_x }} {{ 1|add_x }} {{ 10|sub_1 }} {{ '10'|sub_1 }}"); + Template template = parse(rootScope, "{{ 'foo'|add_x }} {{ 1|add_x }} {{ 10|sub_1 }} {{ '10'|sub_1 }}"); assertEquals("foox 1x 9 9", template.process(null)); } @Test public void testXml() throws TemplateParserException, IOException { - Templater templater = createTemplater(); - Template template = templater.parseXml(false, "{% if x %}{% else %}{% end %}"); + Template template = parseXml("{% if x %}{% else %}{% end %}"); HashMap mapModel = Maps.newHashMap(); mapModel.put("x", true); @@ -699,18 +633,14 @@ public void testXml() throws TemplateParserException, IOException { @Test public void testXmlWithHtmlEntities() throws TemplateParserException, IOException { - Templater templater = createTemplater(); - Template template = templater.parseXml(false, " "); + Template template = parseXml(" "); assertEquals(" ", template.process(null)); } @Test public void testXmlEscaping() throws TemplateParserException, IOException { - Templater templater = createTemplater(); - Template template = templater - .parseXml(false, - "{% if x %}&<>\"'{% end %}"); + Template template = parseXml("{% if x %}&<>\"'{% end %}"); HashMap mapModel = Maps.newHashMap(); mapModel.put("x", true); @@ -722,23 +652,18 @@ public void testXmlEscaping() throws TemplateParserException, IOException { @Test public void testXmlAsHtml() throws TemplateParserException, IOException { - Templater templater = createTemplater(); - Template template = templater - .parseXml( - true, - "