TemplateEncoder extends Encoder<Object> { @Override public void write(Object value, OutputStream outputStream) throws IOException { write(value, outputStream, Locale.getDefault()); } TemplateEncoder(URL url);  TemplateEncoder(URL url, Charset charset); Map<String, Modifier> getModifiers(); String getBaseName(); void setBaseName(String baseName); Map<String, ?> getContext(); void setContext(Map<String, ?> context); @Override void write(Object value, OutputStream outputStream); void write(Object value, OutputStream outputStream, Locale locale); void write(Object value, OutputStream outputStream, Locale locale, TimeZone timeZone); @Override void write(Object value, Writer writer); void write(Object value, Writer writer, Locale locale); void write(Object value, Writer writer, Locale locale, TimeZone timeZone); static Map<String, Modifier> getDefaultModifiers();  }
@Test public void testNull() throws IOException { TemplateEncoder encoder = new TemplateEncoder(getClass().getResource("dictionary.txt")); String result; try (StringWriter writer = new StringWriter()) { encoder.write(null, writer); result = writer.toString(); } assertEquals("", result); }
@Test public void testDictionary() throws IOException { TemplateEncoder encoder = new TemplateEncoder(getClass().getResource("dictionary.txt")); Map<String, ?> dictionary = mapOf( entry("a", "hello"), entry("b", 42), entry("c", mapOf( entry("d", false) )) ); String result; try (StringWriter writer = new StringWriter()) { encoder.write(dictionary, writer); result = writer.toString(); } assertEquals(String.format("{a=%s,b=%s,c.d=%s,e=,f.g=}", dictionary.get("a"), dictionary.get("b"), ((Map<?, ?>)dictionary.get("c")).get("d")), result); }
@Test public void testEmptySection() throws IOException { TemplateEncoder encoder = new TemplateEncoder(getClass().getResource("section1.txt")); String result; try (StringWriter writer = new StringWriter()) { encoder.write(mapOf(entry("list", emptyList())), writer); result = writer.toString(); } assertEquals("[]", result); }
@Test public void testSingleElementSection() throws IOException { TemplateEncoder encoder = new TemplateEncoder(getClass().getResource("section1.txt")); Map<String, ?> dictionary = mapOf( entry("a", "hello"), entry("b", 1L), entry("c", 2.0)); List<?> list = listOf(dictionary); String result; try (StringWriter writer = new StringWriter()) { encoder.write(mapOf(entry("list", list)), writer); result = writer.toString(); } assertEquals(String.format("[{a=%s,b=%s,c=%s}]", dictionary.get("a"), dictionary.get("b"), dictionary.get("c")), result); }
@Test public void testMultiElementSection() throws IOException { TemplateEncoder encoder = new TemplateEncoder(getClass().getResource("section1.txt")); Map<String, ?> dictionary1 = mapOf( entry("a", "hello"), entry("b", 1L), entry("c", 2.0)); Map<String, ?> dictionary2 = mapOf( entry("a", "goodbye"), entry("b", 2L), entry("c", 4.0)); List<?> list = listOf(dictionary1, dictionary2); String result; try (StringWriter writer = new StringWriter()) { encoder.write(mapOf(entry("list", list)), writer); result = writer.toString(); } assertEquals(String.format("[{a=%s,b=%s,c=%s}{a=%s,b=%s,c=%s}]", dictionary1.get("a"), dictionary1.get("b"), dictionary1.get("c"), dictionary2.get("a"), dictionary2.get("b"), dictionary2.get("c")), result); }
@Test public void testNestedSection1() throws IOException { TemplateEncoder encoder = new TemplateEncoder(getClass().getResource("section2.txt")); Map<String, ?> dictionary = mapOf( entry("abc", "ABC"), entry("list1", listOf(mapOf( entry("def", "DEF"), entry("list2", listOf(mapOf( entry("one", 1), entry("two", 2), entry("three", 3) ))) ))) ); String result; try (StringWriter writer = new StringWriter()) { encoder.write(dictionary, writer); result = writer.toString(); } assertEquals("{abc=ABC,list1=[{def=DEF,list2=[{one=1,two=2,three=3}]]}", result); }
@Test public void testNestedSection2() throws IOException { TemplateEncoder encoder = new TemplateEncoder(getClass().getResource("section3.txt")); List<?> value = listOf(listOf(listOf(mapOf(entry("a", "hello"))))); String result; try (StringWriter writer = new StringWriter()) { encoder.write(value, writer); result = writer.toString(); } assertEquals("[[[hello]]]", result); }
@Test public void testNestedEmptySection() throws IOException { TemplateEncoder encoder = new TemplateEncoder(getClass().getResource("section3.txt")); String result; try (StringWriter writer = new StringWriter()) { encoder.write(emptyList(), writer); result = writer.toString(); } assertEquals("[]", result); }
@Test public void testPrimitiveSection() throws IOException { TemplateEncoder encoder = new TemplateEncoder(getClass().getResource("section4.txt")); List<?> value = listOf("hello", 42, false); String result; try (StringWriter writer = new StringWriter()) { encoder.write(value, writer); result = writer.toString(); } assertEquals("[(hello)(42)(false)]", result); }
@Test public void testSectionSeparator() throws IOException { TemplateEncoder encoder = new TemplateEncoder(getClass().getResource("section5.txt")); List<?> value = listOf("a", "b", "c"); String result; try (StringWriter writer = new StringWriter()) { encoder.write(value, writer); result = writer.toString(); } assertEquals("a,b,c", result); }
@Test public void testComment() throws IOException { TemplateEncoder encoder = new TemplateEncoder(getClass().getResource("comment.txt")); String result; try (StringWriter writer = new StringWriter()) { encoder.write(emptyMap(), writer); result = writer.toString(); } assertEquals("><", result); }
@Test public void testFloatFormatModifier() throws IOException { TemplateEncoder encoder = new TemplateEncoder(getClass().getResource("format1.txt")); String result; try (StringWriter writer = new StringWriter()) { encoder.write(4.5, writer); result = writer.toString(); } assertEquals("4.50", result); }
@Test public void testDateFormatModifiers() throws IOException { TemplateEncoder encoder = new TemplateEncoder(getClass().getResource("format2.txt")); Date date = new Date(); LocalDate localDate = LocalDate.now(); LocalTime localTime = LocalTime.now(); LocalDateTime localDateTime = LocalDateTime.now(); String result; try (StringWriter writer = new StringWriter()) { encoder.write(mapOf( entry("timestamp", date.getTime()), entry("date", date), entry("localDate", localDate), entry("localTime", localTime), entry("localDateTime", localDateTime) ), writer); result = writer.toString(); } ZonedDateTime now = ZonedDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()); assertEquals(DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).format(now) + ",\n" + DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(now) + ",\n" + DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).format(now) + ",\n" + DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(now) + ",\n" + DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).format(localDate) + ",\n" + DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT).format(localTime) + ",\n" + DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).format(localDateTime), result); }
@Test public void testURLEscapeModifier() throws IOException { TemplateEncoder encoder = new TemplateEncoder(getClass().getResource("url.txt")); String result; try (StringWriter writer = new StringWriter()) { encoder.write("abc:def&xyz", writer); result = writer.toString(); } assertEquals("abc%3Adef%26xyz", result); }
@Test public void testJSONEscapeModifier() throws IOException { TemplateEncoder encoder = new TemplateEncoder(getClass().getResource("json.txt")); String result; try (StringWriter writer = new StringWriter()) { encoder.write("\"\\\b\f\n\r\t", writer); result = writer.toString(); } assertEquals("\\\"\\\\\\b\\f\\n\\r\\t", result); }
@Test public void testCSVEscapeModifierNumber() throws IOException { TemplateEncoder encoder = new TemplateEncoder(getClass().getResource("csv.txt")); String result; try (StringWriter writer = new StringWriter()) { encoder.write(10, writer); result = writer.toString(); } assertEquals("10", result); }
@Test public void testCSVEscapeModifierString() throws IOException { TemplateEncoder encoder = new TemplateEncoder(getClass().getResource("csv.txt")); String result; try (StringWriter writer = new StringWriter()) { encoder.write("a\"b\"c", writer); result = writer.toString(); } assertEquals("\"a\"\"b\"\"c\"", result); }
@Test public void testMarkupEscapeModifier() throws IOException { TemplateEncoder encoder = new TemplateEncoder(getClass().getResource("html.txt")); String result; try (StringWriter writer = new StringWriter()) { encoder.write("a<b>c&d\"e", writer); result = writer.toString(); } assertEquals("a&lt;b&gt;c&amp;d&quot;e", result); }
@Test public void testSimpleInclude() throws IOException { TemplateEncoder encoder = new TemplateEncoder(getClass().getResource("master1.txt")); String result; try (StringWriter writer = new StringWriter()) { encoder.write("hello", writer); result = writer.toString(); } assertEquals("(hello)", result); }
@Test public void testSectionInclude() throws IOException { TemplateEncoder encoder = new TemplateEncoder(getClass().getResource("master2.txt")); String result; try (StringWriter writer = new StringWriter()) { encoder.write(listOf("a", "b", "c"), writer); result = writer.toString(); } assertEquals("[(a)(b)(c)]", result); }
@Test public void testRecursion() throws IOException { TemplateEncoder encoder = new TemplateEncoder(getClass().getResource("recursion.txt")); List<?> list = listOf( listOf( listOf(), listOf() ), listOf( listOf(), listOf(), listOf() ), listOf( listOf() ) ); String result; try (StringWriter writer = new StringWriter()) { encoder.write(list, writer); result = writer.toString(); } assertEquals("[[[][]][[][][]][[]]]", result); }
@Test public void testEmptyRecursion() throws IOException { TemplateEncoder encoder = new TemplateEncoder(getClass().getResource("recursion.txt")); List<?> list = emptyList(); String result; try (StringWriter writer = new StringWriter()) { encoder.write(list, writer); result = writer.toString(); } assertEquals("[]", result); }
@Test public void testMissingContextProperty() throws IOException { TemplateEncoder encoder = new TemplateEncoder(getClass().getResource("context.txt")); String result; try (StringWriter writer = new StringWriter()) { encoder.write("B", writer); result = writer.toString(); } assertEquals("/B", result); }