From 9896a263138b8401c6cf1c2b68a180e3aff8b67f Mon Sep 17 00:00:00 2001 From: Russell Howe Date: Sat, 13 Aug 2022 09:53:55 +0100 Subject: [PATCH] Update tests for jUnit 4.x Introduce use of assertThrows() from jUnit 4.x Clean up exception declarations in tests --- .../mustachejava/AbstractClassTest.java | 10 +- .../mustachejava/ArraysIndexesTest.java | 14 +- .../github/mustachejava/ConcurrencyTest.java | 2 +- .../ConvertMethodsToFunctionsTest.java | 2 +- .../mustachejava/FailOnMissingTest.java | 13 +- .../com/github/mustachejava/FallbackTest.java | 17 +- .../github/mustachejava/InterpreterTest.java | 399 +++++++++--------- .../github/mustachejava/PreTranslateTest.java | 2 +- .../com/github/mustachejava/SpecTest.java | 2 +- .../JsonInterpreterTest.java | 21 +- 10 files changed, 234 insertions(+), 248 deletions(-) diff --git a/compiler/src/test/java/com/github/mustachejava/AbstractClassTest.java b/compiler/src/test/java/com/github/mustachejava/AbstractClassTest.java index f029f7ece..f52bcaafb 100644 --- a/compiler/src/test/java/com/github/mustachejava/AbstractClassTest.java +++ b/compiler/src/test/java/com/github/mustachejava/AbstractClassTest.java @@ -3,13 +3,15 @@ import org.junit.Test; import java.io.IOException; -import java.io.OutputStreamWriter; import java.io.StringReader; +import java.io.StringWriter; import java.io.Writer; import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import static org.junit.Assert.assertEquals; + public class AbstractClassTest { static abstract class AbstractFoo { public abstract String getValue(); @@ -42,12 +44,13 @@ public void testAbstractClass() throws IOException { containers.add(new Container(new Foo())); containers.add(new Container(new Bar())); HashMap scopes = new HashMap<>(); - Writer writer = new OutputStreamWriter(System.out); + Writer writer = new StringWriter(); MustacheFactory mf = new DefaultMustacheFactory(); Mustache mustache = mf.compile(new StringReader("{{#containers}} {{foo.value}} {{/containers}}"), "example"); scopes.put("containers", containers); mustache.execute(writer, scopes); writer.flush(); + assertEquals(" I am Foo I am Bar ", writer.toString()); } @Test @@ -56,11 +59,12 @@ public void testAbstractClassNoDots() throws IOException { containers.add(new Container(new Foo())); containers.add(new Container(new Bar())); HashMap scopes = new HashMap<>(); - Writer writer = new OutputStreamWriter(System.out); + Writer writer = new StringWriter(); MustacheFactory mf = new DefaultMustacheFactory(); Mustache mustache = mf.compile(new StringReader("{{#containers}} {{#foo}}{{value}}{{/foo}} {{/containers}}"), "example"); scopes.put("containers", containers); mustache.execute(writer, scopes); writer.flush(); + assertEquals(" I am Foo I am Bar ", writer.toString()); } } diff --git a/compiler/src/test/java/com/github/mustachejava/ArraysIndexesTest.java b/compiler/src/test/java/com/github/mustachejava/ArraysIndexesTest.java index 20bc13c09..5cb4bdc37 100644 --- a/compiler/src/test/java/com/github/mustachejava/ArraysIndexesTest.java +++ b/compiler/src/test/java/com/github/mustachejava/ArraysIndexesTest.java @@ -12,7 +12,7 @@ import java.util.Set; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; +import static org.junit.Assert.assertThrows; /** * Shows a simple way to add indexes for arrays. @@ -66,8 +66,8 @@ public Object coerce(final Object object) { } @Test - public void testThrowMustacheExceptionInCaseOfNullHandler() throws Exception { - try { + public void testThrowMustacheExceptionInCaseOfNullHandler() { + MustacheException expected = assertThrows(MustacheException.class, () -> { String template = "
    \n" + "
  1. {{test.1}}
  2. \n" + "
  3. {{test.0}}
  4. \n" + @@ -79,16 +79,14 @@ public void testThrowMustacheExceptionInCaseOfNullHandler() throws Exception { "{{/test}}\n" + "
"; Object scope = new Object() { - String[] test = new String[]{ "a", "b", "c", "d" }; + String[] test = new String[]{"a", "b", "c", "d"}; }; DefaultMustacheFactory mf = new DefaultMustacheFactory(); mf.setObjectHandler(null); Mustache m = mf.compile(new StringReader(template), "template"); m.execute(new StringWriter(), scope).flush(); - fail("should have thrown MustacheException"); - } catch (MustacheException expected) { - assertEquals("Failed to get value for test.1 @[template:2]", expected.getMessage()); - } + }); + assertEquals("Failed to get value for test.1 @[template:2]", expected.getMessage()); } private static class ArrayMap extends AbstractMap implements Iterable { diff --git a/compiler/src/test/java/com/github/mustachejava/ConcurrencyTest.java b/compiler/src/test/java/com/github/mustachejava/ConcurrencyTest.java index a0f08e10a..c29f2fbfb 100644 --- a/compiler/src/test/java/com/github/mustachejava/ConcurrencyTest.java +++ b/compiler/src/test/java/com/github/mustachejava/ConcurrencyTest.java @@ -14,7 +14,7 @@ import java.util.concurrent.atomic.AtomicInteger; import static com.github.mustachejavabenchmarks.BenchmarkTest.skip; -import static junit.framework.Assert.assertEquals; +import static org.junit.Assert.assertEquals; /** * Inspired by an unconfirmed bug report. diff --git a/compiler/src/test/java/com/github/mustachejava/ConvertMethodsToFunctionsTest.java b/compiler/src/test/java/com/github/mustachejava/ConvertMethodsToFunctionsTest.java index 5b30420dc..8d85fdeb4 100644 --- a/compiler/src/test/java/com/github/mustachejava/ConvertMethodsToFunctionsTest.java +++ b/compiler/src/test/java/com/github/mustachejava/ConvertMethodsToFunctionsTest.java @@ -18,7 +18,7 @@ import java.util.List; import java.util.function.Function; -import static junit.framework.Assert.assertEquals; +import static org.junit.Assert.assertEquals; public class ConvertMethodsToFunctionsTest { diff --git a/compiler/src/test/java/com/github/mustachejava/FailOnMissingTest.java b/compiler/src/test/java/com/github/mustachejava/FailOnMissingTest.java index d4ac3899c..eec834470 100644 --- a/compiler/src/test/java/com/github/mustachejava/FailOnMissingTest.java +++ b/compiler/src/test/java/com/github/mustachejava/FailOnMissingTest.java @@ -6,13 +6,12 @@ import com.github.mustachejava.util.Wrapper; import org.junit.Test; -import java.io.IOException; import java.io.StringReader; import java.io.StringWriter; import java.util.List; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; +import static org.junit.Assert.assertThrows; public class FailOnMissingTest { @Test @@ -34,7 +33,7 @@ protected synchronized Wrapper getWrapper(String name, List scopes) { }; DefaultMustacheFactory dmf = new DefaultMustacheFactory(); dmf.setObjectHandler(roh); - try { + MustacheException me = assertThrows(MustacheException.class, () -> { Mustache test = dmf.compile(new StringReader("{{test}}"), "test"); StringWriter sw = new StringWriter(); test.execute(sw, new Object() { @@ -42,11 +41,7 @@ protected synchronized Wrapper getWrapper(String name, List scopes) { }).close(); assertEquals("ok", sw.toString()); test.execute(new StringWriter(), new Object()); - fail("Should have failed"); - } catch (MustacheException me) { - assertEquals("test not found in [test:1] @[test:1]", me.getCause().getMessage()); - } catch (IOException e) { - e.printStackTrace(); - } + }); + assertEquals("test not found in [test:1] @[test:1]", me.getCause().getMessage()); } } diff --git a/compiler/src/test/java/com/github/mustachejava/FallbackTest.java b/compiler/src/test/java/com/github/mustachejava/FallbackTest.java index 2a5594929..8beccd5f9 100644 --- a/compiler/src/test/java/com/github/mustachejava/FallbackTest.java +++ b/compiler/src/test/java/com/github/mustachejava/FallbackTest.java @@ -6,11 +6,10 @@ import java.io.*; import java.util.HashMap; import java.util.Map; -import java.util.concurrent.ExecutionException; import static com.github.mustachejava.TestUtil.getContents; -import static junit.framework.Assert.fail; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; public class FallbackTest { @@ -18,7 +17,7 @@ public class FallbackTest { private static File rootOverride; @Test - public void testDefaultPage1() throws MustacheException, IOException, ExecutionException, InterruptedException { + public void testDefaultPage1() throws MustacheException, IOException { MustacheFactory c = new FallbackMustacheFactory(rootDefault, rootDefault); // no override Mustache m = c.compile("page1.html"); StringWriter sw = new StringWriter(); @@ -29,7 +28,7 @@ public void testDefaultPage1() throws MustacheException, IOException, ExecutionE } @Test - public void testOverridePage1() throws MustacheException, IOException, ExecutionException, InterruptedException { + public void testOverridePage1() throws MustacheException, IOException { MustacheFactory c = new FallbackMustacheFactory(rootOverride, rootDefault); Mustache m = c.compile("page1.html"); StringWriter sw = new StringWriter(); @@ -40,7 +39,7 @@ public void testOverridePage1() throws MustacheException, IOException, Execution } @Test - public void testOverridePage2() throws MustacheException, IOException, ExecutionException, InterruptedException { + public void testOverridePage2() throws MustacheException, IOException { MustacheFactory c = new FallbackMustacheFactory(rootOverride, rootDefault); Mustache m = c.compile("page2.html"); StringWriter sw = new StringWriter(); @@ -53,13 +52,11 @@ public void testOverridePage2() throws MustacheException, IOException, Execution @Test public void testMustacheNotFoundException() { String nonExistingMustache = "404"; - try { + MustacheNotFoundException e = assertThrows(MustacheNotFoundException.class, () -> { MustacheFactory c = new FallbackMustacheFactory(rootOverride, rootDefault); c.compile(nonExistingMustache); - fail("Didn't throw an exception"); - } catch (MustacheNotFoundException e) { - assertEquals(nonExistingMustache, e.getName()); - } + }); + assertEquals(nonExistingMustache, e.getName()); } @BeforeClass diff --git a/compiler/src/test/java/com/github/mustachejava/InterpreterTest.java b/compiler/src/test/java/com/github/mustachejava/InterpreterTest.java index 2dbbe9b2c..bb5ce4996 100644 --- a/compiler/src/test/java/com/github/mustachejava/InterpreterTest.java +++ b/compiler/src/test/java/com/github/mustachejava/InterpreterTest.java @@ -14,8 +14,7 @@ import com.github.mustachejava.util.Wrapper; import com.github.mustachejavabenchmarks.JsonCapturer; import com.github.mustachejavabenchmarks.JsonInterpreterTest; -import junit.framework.TestCase; -import org.junit.Assert; +import org.junit.Before; import org.junit.Test; import java.io.*; @@ -27,6 +26,10 @@ import static com.github.mustachejava.TestUtil.getContents; import static java.util.Collections.singletonList; import static java.util.Collections.singletonMap; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; /** * Tests for the compiler. @@ -36,10 +39,11 @@ * Time: 10:23:54 AM */ @SuppressWarnings("unused") -public class InterpreterTest extends TestCase { +public class InterpreterTest { protected File root; - public void testSimple() throws MustacheException, IOException, ExecutionException, InterruptedException { + @Test + public void testSimple() throws MustacheException, IOException { MustacheFactory c = createMustacheFactory(); Mustache m = c.compile("simple.html"); StringWriter sw = new StringWriter(); @@ -56,7 +60,8 @@ int taxed_value() { assertEquals(getContents(root, "simple.txt"), sw.toString()); } - public void testSafeSimple() throws MustacheException, IOException, ExecutionException, InterruptedException { + @Test + public void testSafeSimple() throws MustacheException, IOException { MustacheFactory c = new SafeMustacheFactory(Collections.singleton("simple.html"), root); Mustache m = c.compile("simple.html"); StringWriter sw = new StringWriter(); @@ -73,18 +78,16 @@ public int taxed_value() { assertEquals(getContents(root, "simple.txt"), sw.toString()); } - public void testSafe() throws MustacheException, IOException, ExecutionException, InterruptedException { - try { + @Test + public void testSafe() throws MustacheException, IOException { + assertThrows(MustacheException.class, () -> { MustacheFactory c = new SafeMustacheFactory(Collections.singleton("notsimple.html"), root); // Not present in allowed list - Mustache m = c.compile("simple.html"); - fail("Should not be allowed"); - } catch (MustacheException me) { - // Success - } + c.compile("simple.html"); + }); MustacheFactory c = new SafeMustacheFactory(Collections.singleton("simple.html"), root); - Mustache m = c.compile("simple.html"); { + Mustache m = c.compile("simple.html"); StringWriter sw = new StringWriter(); m.execute(sw, new Object() { // It won't find this since it isn't public @@ -100,6 +103,7 @@ public int taxed_value() { assertNotSame(getContents(root, "simple.txt"), sw.toString()); } { + Mustache m = c.compile("simple.html"); StringWriter sw = new StringWriter(); m.execute(sw, new Object() { public String name = "Chris"; @@ -115,65 +119,44 @@ int taxed_value() { assertNotSame(getContents(root, "simple.txt"), sw.toString()); } { - m = c.compile(new StringReader("{{toString}}"), "test"); - StringWriter sw = new StringWriter(); - try { - m.execute(sw, new Object() { + assertThrows(MustacheException.class, () -> { + Mustache m = c.compile(new StringReader("{{toString}}"), "test"); + m.execute(new StringWriter(), new Object() { public String toString() { return ""; } }); - fail("Got value for toString"); - } catch (MustacheException me) { - // Success - } + }); } { - try { - m = c.compile(new StringReader("{{>/etc/passwd}}"), "test"); - StringWriter sw = new StringWriter(); - m.execute(sw, new Object() { + assertThrows("Can't access that partial", MustacheException.class, () -> { + Mustache m = c.compile(new StringReader("{{>/etc/passwd}}"), "test"); + m.execute(new StringWriter(), new Object() { }); - fail("Can't access that partial"); - } catch (MustacheException me) { - // Success - } + }); } { - try { - m = c.compile(new StringReader("{{%pragma}}"), "test"); - StringWriter sw = new StringWriter(); - m.execute(sw, new Object() { + assertThrows("Can't use pragmas", MustacheException.class, () -> { + Mustache m = c.compile(new StringReader("{{%pragma}}"), "test"); + m.execute(new StringWriter(), new Object() { }); - fail("Can't use pragmas"); - } catch (MustacheException me) { - // Success - } + }); } { - try { - m = c.compile(new StringReader("{{{rawtext}}}"), "test"); - StringWriter sw = new StringWriter(); - m.execute(sw, new Object() { + assertThrows("Can't use raw text", MustacheException.class, () -> { + Mustache m = c.compile(new StringReader("{{{rawtext}}}"), "test"); + m.execute(new StringWriter(), new Object() { public String rawtext() { return ""; } }); - fail("Can't use raw text"); - } catch (MustacheException me) { - // Success - } + }); } { - try { - m = c.compile(new StringReader("{{=[[]]=}}"), "test"); - StringWriter sw = new StringWriter(); - m.execute(sw, new Object() { - }); - fail("Can't change delimiters"); - } catch (MustacheException me) { - // Success - } + assertThrows("Can't change delimiters", MustacheException.class, () -> { + Mustache m = c.compile(new StringReader("{{=[[]]=}}"), "test"); + m.execute(new StringWriter(), new Object() {}); + }); } } @@ -209,7 +192,8 @@ public Reader getReader(String resourceName) { } } - public void testSimpleI18N() throws MustacheException, IOException, ExecutionException, InterruptedException { + @Test + public void testSimpleI18N() throws MustacheException, IOException { { MustacheFactory c = new DefaultMustacheFactory(new LocalizedMustacheResolver(root, Locale.KOREAN)); Mustache m = c.compile("simple.html"); @@ -244,16 +228,13 @@ int taxed_value() { } } - public void testRootCheck() throws MustacheException, IOException, ExecutionException, InterruptedException { + @Test + public void testRootCheck() throws MustacheException { MustacheFactory c = createMustacheFactory(); - try { - Mustache m = c.compile("../../../pom.xml"); - fail("Should have failed to compile"); - } catch (MustacheException e) { - // Success - } + assertThrows(MustacheException.class, () -> c.compile("../../../pom.xml")); } + @Test public void testIssue280() throws IOException { MustacheFactory c = createMustacheFactory(); StringWriter sw = new StringWriter(); @@ -271,7 +252,8 @@ String template() throws IOException { assertTrue(sw.toString().startsWith("")); } - public void testSimpleFiltered() throws MustacheException, IOException, ExecutionException, InterruptedException { + @Test + public void testSimpleFiltered() throws MustacheException, IOException { MustacheFactory c = new DefaultMustacheFactory(root) { @Override public String filterText(String appended, boolean startOfLine) { @@ -299,7 +281,8 @@ int taxed_value() { assertEquals(getContents(root, "simplefiltered.txt"), sw.toString()); } - public void testTypedSimple() throws MustacheException, IOException, ExecutionException, InterruptedException { + @Test + public void testTypedSimple() throws MustacheException, IOException { final Object scope = new Object() { final String name = "Chris"; final int value = 10000; @@ -328,6 +311,7 @@ private DefaultMustacheFactory createMustacheFactory() { return new DefaultMustacheFactory(root); } + @Test public void testRecurision() throws IOException { StringWriter sw = execute("recursion.html", new Object() { final Object value = new Object() { @@ -337,6 +321,7 @@ public void testRecurision() throws IOException { assertEquals(getContents(root, "recursion.txt"), sw.toString()); } + @Test public void testRecursionWithInheritance() throws IOException { StringWriter sw = execute("recursion_with_inheritance.html", new Object() { final Object value = new Object() { @@ -346,6 +331,7 @@ public void testRecursionWithInheritance() throws IOException { assertEquals(getContents(root, "recursion.txt"), sw.toString()); } + @Test public void testPartialRecursionWithInheritance() throws IOException { StringWriter sw = execute("recursive_partial_inheritance.html", new Object() { final Object test = new Object() { @@ -355,6 +341,7 @@ public void testPartialRecursionWithInheritance() throws IOException { assertEquals(getContents(root, "recursive_partial_inheritance.txt"), sw.toString()); } + @Test public void testChainedInheritance() throws IOException { StringWriter sw = execute("page.html", new Object() { final Object test = new Object() { @@ -364,7 +351,8 @@ public void testChainedInheritance() throws IOException { assertEquals(getContents(root, "page.txt"), sw.toString()); } - public void testDefaultValue() throws IOException { + @Test + public void testDefaultValue() { DefaultMustacheFactory mf = new DefaultMustacheFactory(root); mf.setObjectHandler(new ReflectionObjectHandler() { @Override @@ -395,7 +383,8 @@ public Wrapper find(String name, List scopes) { assertEquals("foo BAR baz", sw.toString()); } - public void testSimplePragma() throws MustacheException, IOException, ExecutionException, InterruptedException { + @Test + public void testSimplePragma() throws MustacheException, IOException { MustacheFactory c = createMustacheFactory(); Mustache m = c.compile("simplepragma.html"); StringWriter sw = new StringWriter(); @@ -418,7 +407,8 @@ public boolean isItOk() { } } - public void testNestedAccessWithSimpleObjectHandler() throws IOException { + @Test + public void testNestedAccessWithSimpleObjectHandler() { assertEquals(getOutput(false), getOutput(true)); } @@ -435,6 +425,7 @@ private String getOutput(final boolean setObjectHandler) { return writer.toString(); } + @Test public void testClosingReader() { final AtomicBoolean closed = new AtomicBoolean(); StringReader reader = new StringReader("{{test") { @@ -444,16 +435,13 @@ public void close() { } }; 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()); - } + assertThrows(MustacheException.class, () -> mf.compile(reader, "test")); + // The reader should be closed now + assertTrue(closed.get()); } - public void testMultipleWrappers() throws MustacheException, IOException, ExecutionException, InterruptedException { + @Test + public void testMultipleWrappers() throws MustacheException, IOException { MustacheFactory c = createMustacheFactory(); Mustache m = c.compile("simple.html"); StringWriter sw = new StringWriter(); @@ -479,6 +467,7 @@ o, new Object() { assertEquals(getContents(root, "simplerewrap.txt"), sw.toString()); } + @Test public void testNestedLatchesIterable() throws IOException { DefaultMustacheFactory c = createMustacheFactory(); c.setExecutorService(Executors.newCachedThreadPool()); @@ -512,6 +501,7 @@ public void testNestedLatchesIterable() throws IOException { assertEquals("you?areHow", sb.toString()); } + @Test public void testConcurrency() throws IOException { DefaultMustacheFactory c = createMustacheFactory(); c.setExecutorService(Executors.newCachedThreadPool()); @@ -538,6 +528,7 @@ public void testConcurrency() throws IOException { assertEquals("How ARE you?", sw.toString()); } + @Test public void testNestedLatches() throws IOException { DefaultMustacheFactory c = createMustacheFactory(); c.setExecutorService(Executors.newCachedThreadPool()); @@ -562,8 +553,9 @@ public void testNestedLatches() throws IOException { assertEquals("\nHow\nare\nyou?\n\n", sw.toString()); } - public void testBrokenSimple() throws MustacheException, IOException, ExecutionException, InterruptedException { - try { + @Test + public void testBrokenSimple() throws MustacheException { + assertThrows(MustacheException.class, () -> { MustacheFactory c = createMustacheFactory(); Mustache m = c.compile("brokensimple.html"); StringWriter sw = new StringWriter(); @@ -577,12 +569,10 @@ int taxed_value() { final boolean in_ca = true; }); - fail("Should have failed: " + sw.toString()); - } catch (Exception e) { - // success - } + }); } + @Test public void testIsNotEmpty() throws IOException { Object object = new Object() { final List people = singletonList("Test"); @@ -607,6 +597,7 @@ private StringWriter execute(String name, List objects) { return sw; } + @Test public void testImmutableList() throws IOException { Object object = new Object() { final List people = singletonList("Test"); @@ -615,7 +606,8 @@ public void testImmutableList() throws IOException { assertEquals(getContents(root, "isempty.txt"), sw.toString()); } - public void testOptional() throws IOException { + @Test + public void testOptional() { MustacheFactory c = createMustacheFactory(); StringReader template = new StringReader("{{person}}{{#person}} is present{{/person}}{{^person}}Is not present{{/person}}"); Mustache m = c.compile(template, "test"); @@ -631,7 +623,8 @@ public void testOptional() throws IOException { assertEquals("Is not present", sw.toString()); } - public void testComment() throws IOException { + @Test + public void testComment() { MustacheFactory c = createMustacheFactory(); Mustache m = c.compile(new StringReader("{{#process}}{{!comment}}{{/process}}"), ""); StringWriter sw = new StringWriter(); @@ -641,6 +634,7 @@ public void testComment() throws IOException { assertEquals("[[!comment}}", sw.toString()); } + @Test public void testNumber0IsFalse() throws IOException { DefaultMustacheFactory c = createMustacheFactory(); c.setObjectHandler(new ReflectionObjectHandler() { @@ -673,7 +667,8 @@ public Writer iterate(Iteration iteration, Writer writer, Object object, List() {{ put("name", "Chris"); put("value", 10000); @@ -736,6 +734,7 @@ public void testSimpleWithMap() throws MustacheException, IOException, Execution assertEquals(getContents(root, "simple.txt"), sw.toString()); } + @Test public void testReflectiveAccessThroughInterface() { MustacheFactory c = createMustacheFactory(); Mustache m = c.compile(new StringReader("{{#entries}}{{key}}{{/entries}}"), ""); @@ -748,7 +747,8 @@ public void testReflectiveAccessThroughInterface() { assertEquals("key", sw.toString()); } - public void testPartialWithTF() throws MustacheException, IOException { + @Test + public void testPartialWithTF() throws MustacheException { MustacheFactory c = createMustacheFactory(); Mustache m = c.compile("partialintemplatefunction.html"); StringWriter sw = new StringWriter(); @@ -760,6 +760,7 @@ public TemplateFunction i() { assertEquals("This is not interesting.", sw.toString()); } + @Test public void testFunctions() throws IOException { MustacheFactory c = createMustacheFactory(); Mustache m = c.compile(new StringReader("{{#f}}{{foo}}{{/f}}"), "test"); @@ -792,6 +793,7 @@ public String apply(String s) { } } + @Test public void testComplex() throws MustacheException, IOException { StringWriter json = new StringWriter(); MappingJsonFactory jf = new MappingJsonFactory(); @@ -818,6 +820,7 @@ public MustacheVisitor createMustacheVisitor() { assertEquals(getContents(root, "complex.txt"), sw.toString()); } + @Test public void testComplexParallel() throws MustacheException, IOException { MustacheFactory c = initParallel(); Mustache m = c.compile("complex.html"); @@ -826,11 +829,13 @@ public void testComplexParallel() throws MustacheException, IOException { assertEquals(getContents(root, "complex.txt"), sw.toString()); } + @Test public void testSerialCallable() throws MustacheException, IOException { StringWriter sw = execute("complex.html", new ParallelComplexObject()); assertEquals(getContents(root, "complex.txt"), sw.toString()); } + @Test public void testDynamicPartial() throws MustacheException, IOException { // Implement >+ syntax that dynamically includes template files at runtime MustacheFactory c = new DefaultMustacheFactory(root) { @@ -887,6 +892,7 @@ public Writer execute(Writer writer, List scopes) { /* @SuppressWarnings("serial") + @Test public void testCurrentElementInArray() throws IOException, MustacheException { MustacheBuilder c = init(); @@ -913,6 +919,7 @@ public void testCurrentElementInArray() throws IOException, MustacheException { } */ + @Test public void testReadme() throws MustacheException, IOException { MustacheFactory c = createMustacheFactory(); Mustache m = c.compile("items.html"); @@ -923,6 +930,7 @@ public void testReadme() throws MustacheException, IOException { assertEquals(getContents(root, "items.txt"), sw.toString()); } + @Test public void testReadmeSerial() throws MustacheException, IOException { MustacheFactory c = createMustacheFactory(); Mustache m = c.compile("items2.html"); @@ -934,6 +942,7 @@ public void testReadmeSerial() throws MustacheException, IOException { assertTrue("Should be a little bit more than 4 seconds: " + diff, diff > 3999 && diff < 6000); } + @Test public void testReadmeParallel() throws MustacheException, IOException { MustacheFactory c = initParallel(); Mustache m = c.compile("items2.html"); @@ -972,7 +981,7 @@ static class Feature { String description; - Callable desc() throws InterruptedException { + Callable desc() { return () -> { Thread.sleep(1000); return description; @@ -981,6 +990,7 @@ Callable desc() throws InterruptedException { } } + @Test public void testDeferred() throws IOException { DefaultMustacheFactory mf = new DeferringMustacheFactory(root); mf.setExecutorService(Executors.newCachedThreadPool()); @@ -995,7 +1005,8 @@ public void testDeferred() throws IOException { assertEquals(getContents(root, "deferred.txt"), sw.toString()); } - public void testMultipleCallsWithDifferentScopes() throws IOException { + @Test + public void testMultipleCallsWithDifferentScopes() { String template = "Value: {{value}}"; Mustache mustache = new DefaultMustacheFactory().compile(new StringReader( template), "test"); @@ -1012,7 +1023,8 @@ public void testMultipleCallsWithDifferentScopes() throws IOException { assertEquals("Value: something", sw.toString()); } - public void testMultipleCallsWithDifferentMapScopes() throws IOException { + @Test + public void testMultipleCallsWithDifferentMapScopes() { String template = "Value: {{value}}"; Mustache mustache = new DefaultMustacheFactory().compile(new StringReader( template), "test"); @@ -1030,6 +1042,7 @@ public void testMultipleCallsWithDifferentMapScopes() throws IOException { assertEquals("Value: something", sw.toString()); } + @Test public void testRelativePathsSameDir() throws IOException { MustacheFactory mf = createMustacheFactory(); Mustache compile = mf.compile("relative/paths.html"); @@ -1038,6 +1051,7 @@ public void testRelativePathsSameDir() throws IOException { assertEquals(getContents(root, "relative/paths.txt"), sw.toString()); } + @Test public void testRelativePathsRootDir() throws IOException { MustacheFactory mf = createMustacheFactory(); Mustache compile = mf.compile("relative/rootpath.html"); @@ -1046,6 +1060,7 @@ public void testRelativePathsRootDir() throws IOException { assertEquals(getContents(root, "relative/paths.txt"), sw.toString()); } + @Test public void testPathsWithExtension() throws IOException { MustacheFactory mf = createMustacheFactory(); Mustache compile = mf.compile("relative/extension.html"); @@ -1054,6 +1069,7 @@ public void testPathsWithExtension() throws IOException { assertEquals(getContents(root, "relative/paths.txt"), sw.toString()); } + @Test public void testRelativePathsTemplateFunction() throws IOException { MustacheFactory mf = createMustacheFactory(); Mustache compile = mf.compile("relative/functionpaths.html"); @@ -1069,16 +1085,13 @@ public String apply(String s) { assertEquals(getContents(root, "relative/paths.txt"), sw.toString()); } - public void testRelativePathFail() throws IOException { + @Test + public void testRelativePathFail() { MustacheFactory mf = createMustacheFactory(); - try { - Mustache compile = mf.compile("relative/pathfail.html"); - fail("Should have failed to compile"); - } catch (MustacheException e) { - // Success - } + assertThrows(MustacheException.class, () -> mf.compile("relative/pathfail.html")); } + @Test public void testVariableInhertiance() throws IOException { DefaultMustacheFactory mf = createMustacheFactory(); Mustache m = mf.compile("issue_201/chat.html"); @@ -1087,6 +1100,7 @@ public void testVariableInhertiance() throws IOException { assertEquals("", sw.toString()); } + @Test public void testIterator() throws IOException { { MustacheFactory mf = createMustacheFactory(); @@ -1128,6 +1142,7 @@ Iterator values() { } } + @Test public void testObjectArray() throws IOException { MustacheFactory mf = createMustacheFactory(); Mustache m = mf.compile(new StringReader("{{#values}}{{.}}{{/values}}{{^values}}Test2{{/values}}"), "testObjectArray"); @@ -1138,6 +1153,7 @@ public void testObjectArray() throws IOException { assertEquals("123", sw.toString()); } + @Test public void testBaseArray() throws IOException { MustacheFactory mf = createMustacheFactory(); Mustache m = mf.compile(new StringReader("{{#values}}{{.}}{{/values}}{{^values}}Test2{{/values}}"), "testBaseArray"); @@ -1148,6 +1164,7 @@ public void testBaseArray() throws IOException { assertEquals("123", sw.toString()); } + @Test public void testEmptyString() throws IOException { MustacheFactory mf = createMustacheFactory(); Mustache m = mf.compile(new StringReader("{{#values}}Test1{{/values}}{{^values}}Test2{{/values}}"), "testEmptyString"); @@ -1158,6 +1175,7 @@ public void testEmptyString() throws IOException { assertEquals("Test2", sw.toString()); } + @Test public void testPrivate() throws IOException { MustacheFactory mf = createMustacheFactory(); Mustache m = mf.compile(new StringReader("{{#values}}Test1{{/values}}{{^values}}Test2{{/values}}"), "testPrivate"); @@ -1173,6 +1191,7 @@ private String values() { assertEquals("Test2", sw.toString()); } + @Test public void testSingleCurly() throws IOException { MustacheFactory mf = createMustacheFactory(); Mustache m = mf.compile(new StringReader("{{value } }}"), "testSingleCurly"); @@ -1184,6 +1203,7 @@ public void testSingleCurly() throws IOException { assertEquals("test", sw.toString()); } + @Test public void testPragma() throws IOException { final AtomicBoolean found = new AtomicBoolean(); DefaultMustacheFactory mf = new DefaultMustacheFactory() { @@ -1207,6 +1227,7 @@ public MustacheVisitor createMustacheVisitor() { assertTrue(found.get()); } + @Test public void testPragmaWhiteSpaceHandling() throws IOException { DefaultMustacheFactory mf = new DefaultMustacheFactory() { @Override @@ -1226,6 +1247,7 @@ public MustacheVisitor createMustacheVisitor() { assertEquals(" - - ", sw.toString()); } + @Test public void testNotIterableCallable() throws IOException { MustacheFactory mf = createMustacheFactory(); Mustache m = mf.compile(new StringReader("{{^value}}test{{/value}}"), "testNotIterableCallable"); @@ -1255,6 +1277,7 @@ void check() { } } + @Test public void testAccessTracker() throws IOException { { Map accessTrackingMap = createBaseMap(); @@ -1272,12 +1295,7 @@ public void testAccessTracker() throws IOException { 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 - } + assertThrows(MustacheException.class, accessTrackingMap::check); } } @@ -1288,33 +1306,22 @@ private AccessTrackingMap createBaseMap() { return accessTrackingMap; } + @Test public void testMismatch() { - try { - MustacheFactory mf = createMustacheFactory(); - Mustache m = mf.compile(new StringReader("{{#value}}"), "testMismatch"); - fail("Not mismatched"); - } catch (MustacheException e) { - // Success - try { - MustacheFactory mf = createMustacheFactory(); - Mustache m = mf.compile(new StringReader("{{#value}}{{/values}}"), "testMismatch"); - fail("Not mismatched"); - } catch (MustacheException e2) { - // Success - } - } + MustacheFactory mf = createMustacheFactory(); + assertThrows(MustacheException.class, () -> mf.compile(new StringReader("{{#value}}"), "testMismatch")); + assertThrows(MustacheException.class, () -> mf.compile(new StringReader("{{#value}}{{/values}}"), "testMismatch")); } + @Test public void testInvalidDelimiters() { - try { + assertThrows(MustacheException.class, () -> { MustacheFactory mf = createMustacheFactory(); - Mustache m = mf.compile(new StringReader("{{=toolong}}"), "testInvalidDelimiters"); - fail("Not invalid"); - } catch (MustacheException e) { - // Success - } + mf.compile(new StringReader("{{=toolong}}"), "testInvalidDelimiters"); + }); } + @Test public void testEmptyDot() { MustacheFactory mf = createMustacheFactory(); StringWriter sw = new StringWriter(); @@ -1346,6 +1353,7 @@ public void testCase() { System.out.println(result); //prints 'Hello' } + @Test public void testTemplateFunction() throws IOException { MustacheFactory mf = createMustacheFactory(); Mustache m = mf.compile(new StringReader("{{#i}}{{{test}}}{{f}}{{/i}}" + @@ -1370,6 +1378,7 @@ private static class SuperClass { String values = "value"; } + @Test public void testSuperField() throws IOException { MustacheFactory mf = createMustacheFactory(); Mustache m = mf.compile(new StringReader("{{#values}}Test1{{/values}}{{^values}}Test2{{/values}}"), "testIterator"); @@ -1380,6 +1389,7 @@ public void testSuperField() throws IOException { assertEquals("Test1", sw.toString()); } + @Test public void testRelativePathsDotDotDir() throws IOException { MustacheFactory mf = createMustacheFactory(); Mustache compile = mf.compile("relative/dotdot.html"); @@ -1388,6 +1398,7 @@ public void testRelativePathsDotDotDir() throws IOException { assertEquals(getContents(root, "uninterestingpartial.html"), sw.toString()); } + @Test public void testRelativePathsDotDotDirOverride() throws IOException { MustacheFactory mf = new DefaultMustacheFactory(root) { @Override @@ -1401,6 +1412,7 @@ public String resolvePartialPath(String dir, String name, String extension) { assertEquals(getContents(root, "nonrelative.html"), sw.toString()); } + @Test public void testOverrideExtension() throws IOException { MustacheFactory mf = new DefaultMustacheFactory(root) { @Override @@ -1424,25 +1436,20 @@ protected String partialName() { assertEquals("not interesting.", sw.toString()); } + @Test 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")); - } + MustacheException e = assertThrows(MustacheException.class, () -> new DefaultMustacheFactory().compile(new StringReader("{{}}"), "test")); + assertTrue(e.getMessage().startsWith("Empty mustache")); } + @Test public void testMustacheNotFoundException() { String nonExistingMustache = "404"; - try { - new DefaultMustacheFactory().compile(nonExistingMustache); - fail("Didn't throw an exception"); - } catch (MustacheNotFoundException e) { - assertEquals(nonExistingMustache, e.getName()); - } + MustacheNotFoundException e = assertThrows(MustacheNotFoundException.class, () -> new DefaultMustacheFactory().compile(nonExistingMustache)); + assertEquals(nonExistingMustache, e.getName()); } + @Test public void testImplicitIteratorNoScope() throws IOException { Mustache test = new DefaultMustacheFactory().compile(new StringReader("{{.}}"), "test"); StringWriter sw = new StringWriter(); @@ -1453,6 +1460,7 @@ public void testImplicitIteratorNoScope() throws IOException { assertEquals("", sw2.toString()); } + @Test public void testImplicitIteratorWithScope() throws IOException { Mustache test = new DefaultMustacheFactory().compile(new StringReader("{{#test}}_{{.}}_{{/test}}"), "test"); StringWriter sw = new StringWriter(); @@ -1462,6 +1470,7 @@ public void testImplicitIteratorWithScope() throws IOException { assertEquals("_a__b__c_", sw.toString()); } + @Test public void testCR() { Mustache m = new DefaultMustacheFactory().compile(new StringReader("{{test}}\r\n{{test}}\r\n"), "test"); StringWriter sw = new StringWriter(); @@ -1471,6 +1480,7 @@ public void testCR() { assertEquals("fred\r\nfred\r\n", sw.toString()); } + @Test public void testOutputDelimiters() { String template = "{{=## ##=}}{{##={{ }}=####"; Mustache mustache = new DefaultMustacheFactory().compile(new StringReader(template), "test"); @@ -1479,17 +1489,15 @@ public void testOutputDelimiters() { assertEquals("{{##", 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: #containers in example:1@16 @[example:1]", actual.getMessage()); - } + @Test + public void testImproperlyClosedVariable() { + MustacheException actual = assertThrows(MustacheException.class, () -> new DefaultMustacheFactory().compile(new StringReader("{{{#containers}} {{/containers}}"), "example")); + assertEquals("Improperly closed variable: #containers in example:1@16 @[example:1]", actual.getMessage()); } + @Test public void testLambdaExceptions() { - try { + assertThrows(MustacheException.class, () -> { String template = "hello {{#timesTwo}}a{{/timesTwo}}"; Mustache mustache = new DefaultMustacheFactory().compile(new StringReader(template), "test"); StringWriter sw = new StringWriter(); @@ -1498,34 +1506,26 @@ public void testLambdaExceptions() { throw new RuntimeException(); }; }); - fail("Should have throw MustacheException"); - } catch (MustacheException me) { - // works - } + }); } + @Test public void testDirectoryInsteadOfFile() { - try { + assertThrows(MustacheException.class, () -> { // there is a folder called "templates" in the resources dir (src/main/resources/templates) MustacheFactory mustacheFactory = new DefaultMustacheFactory(); Mustache template = mustacheFactory.compile("templates"); - fail("Should have throw MustacheNotFoundException"); - } catch (MustacheNotFoundException mnfe) { - // works - } + }); } + @Test 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()); - } + // Should not throw a StackOverflowError + MustacheException e = assertThrows(MustacheException.class, () -> execute("infiniteparent.html", new Context())); + assertEquals("Maximum partial recursion limit reached: 100 @[infiniteparent.html:1]", e.getMessage()); } + @Test public void testIssue191() throws IOException { MustacheFactory mustacheFactory = createMustacheFactory(); Mustache mustache = mustacheFactory.compile("templates/someTemplate.mustache"); @@ -1534,18 +1534,18 @@ public void testIssue191() throws IOException { assertEquals(getContents(root, "templates/someTemplate.txt"), stringWriter.toString()); } + @Test public void testMalformedTag() { - try { + MustacheException e = assertThrows(MustacheException.class, () -> { String template = "\n{{$value}}\n{/value}}"; Mustache mustache = new DefaultMustacheFactory().compile(new StringReader(template), "test"); StringWriter sw = new StringWriter(); mustache.execute(sw, new Object[0]); - fail("Should have failed to compile"); - } catch (MustacheException e) { - assertEquals("Failed to close 'value' tag @[test:2]", e.getMessage()); - } + }); + assertEquals("Failed to close 'value' tag @[test:2]", e.getMessage()); } + @Test public void testTemplateFunctionWithData() { String template = "{{#parse}}\n" + "{{replaceMe}}\n" + @@ -1563,6 +1563,7 @@ public TemplateFunction parse() { assertEquals("blablabla banana, blablabla apple", sw.toString()); } + @Test public void testTemplateFunctionWithImplicitParams() { String template = "{{#parse}}\n" + "{{replaceMe}}\n" + @@ -1604,6 +1605,7 @@ public TemplateFunction parse() { assertEquals("blablabla banana, blablabla apple", sw.toString()); } + @Test public void testOverrideValueCode() throws IOException { DefaultMustacheFactory mf = new DefaultMustacheFactory() { @Override @@ -1648,65 +1650,48 @@ protected MissingWrapper createMissingWrapper(String name, List guards) { } }); StringReader sr; - StringWriter sw; { - sw = new StringWriter(); sr = new StringReader("{{value}}"); Mustache m = mf.compile(sr, "value"); - try { - m.execute(sw, new Object() { - }); - fail("Should throw an exception"); - } catch (MustacheException e) { - } + assertThrows(MustacheException.class, () -> m.execute(new StringWriter(), new Object() {})); } ByteArrayOutputStream out = new ByteArrayOutputStream(); PrintStream ps = new PrintStream(out); System.setErr(ps); { - sw = new StringWriter(); sr = new StringReader("{{value}}"); Mustache m = mf.compile(sr, "value"); - try { - m.execute(sw, new Object() { - final String value = null; - }).close(); - ps.flush(); - assertTrue(new String(out.toByteArray()).contains("Variable is null")); - } catch (MustacheException e) { - } + m.execute(new StringWriter(), new Object() { + final String value = null; + }).close(); + ps.flush(); + assertTrue(new String(out.toByteArray()).contains("Variable is null")); } out.reset(); { - sw = new StringWriter(); sr = new StringReader("{{value}}"); Mustache m = mf.compile(sr, "value"); - try { - m.execute(sw, new Object() { - final String value = ""; - }).close(); - ps.flush(); - assertTrue(new String(out.toByteArray()).contains("Variable is empty string")); - } catch (MustacheException e) { - } + m.execute(new StringWriter(), new Object() { + final String value = ""; + }).close(); + ps.flush(); + assertTrue(new String(out.toByteArray()).contains("Variable is empty string")); } out.reset(); { - sw = new StringWriter(); sr = new StringReader("{{value}}"); Mustache m = mf.compile(sr, "value"); - try { - m.execute(sw, new Object() { - final String value = "value"; - }).close(); - ps.flush(); - assertEquals("", new String(out.toByteArray())); - assertEquals("value", sw.toString()); - } catch (MustacheException e) { - } + StringWriter sw = new StringWriter(); + m.execute(sw, new Object() { + final String value = "value"; + }).close(); + ps.flush(); + assertEquals("", new String(out.toByteArray())); + assertEquals("value", sw.toString()); } } + @Test public void testPropertyWithDot() throws IOException { DefaultMustacheFactory mustacheFactory = new DefaultMustacheFactory(); Reader reader = new StringReader("value=${some.value}"); @@ -1715,9 +1700,10 @@ public void testPropertyWithDot() throws IOException { properties.put("some.value", "some.value"); StringWriter writer = new StringWriter(); mustache.execute(writer, new Object[]{properties}).close(); - Assert.assertEquals("value=some.value", writer.toString()); + assertEquals("value=some.value", writer.toString()); } + @Test public void testLeavingAloneMissingVariables() throws IOException { DefaultMustacheFactory dmf = new DefaultMustacheFactory(root) { @Override @@ -1759,8 +1745,8 @@ private DefaultMustacheFactory initParallel() { return cf; } - protected void setUp() throws Exception { - super.setUp(); + @Before + public void setUp() throws Exception { File file = new File("src/test/resources"); root = new File(file, "simple.html").exists() ? file : new File("../src/test/resources"); } @@ -1779,10 +1765,11 @@ public void testMap() throws IOException { Map>> map = new HashMap<>(); map.put("names", fn); - Writer writer = new OutputStreamWriter(System.out); + Writer writer = new StringWriter(); MustacheFactory mf = new DefaultMustacheFactory(); Mustache mustache = mf.compile(new StringReader("{{#names}}

First Name : {{name}}

Last Name : {{last}}

{{/names}}"), "example"); mustache.execute(writer, map); writer.flush(); + assertEquals("

First Name : firstName

Last Name : lastName

First Name : firstName 1

Last Name : lastName 1

", writer.toString()); } } diff --git a/compiler/src/test/java/com/github/mustachejava/PreTranslateTest.java b/compiler/src/test/java/com/github/mustachejava/PreTranslateTest.java index e1d50e5cb..f368acbdb 100644 --- a/compiler/src/test/java/com/github/mustachejava/PreTranslateTest.java +++ b/compiler/src/test/java/com/github/mustachejava/PreTranslateTest.java @@ -8,7 +8,7 @@ import java.io.StringWriter; import java.util.function.Function; -import static junit.framework.Assert.assertEquals; +import static org.junit.Assert.assertEquals; /** * If you want to precompile translations, you will need to do two passes against diff --git a/compiler/src/test/java/com/github/mustachejava/SpecTest.java b/compiler/src/test/java/com/github/mustachejava/SpecTest.java index fb74b155a..4465d66b4 100644 --- a/compiler/src/test/java/com/github/mustachejava/SpecTest.java +++ b/compiler/src/test/java/com/github/mustachejava/SpecTest.java @@ -15,7 +15,7 @@ import java.util.Map; import java.util.function.Function; -import static junit.framework.Assert.assertFalse; +import static org.junit.Assert.assertFalse; /** * Specification tests diff --git a/compiler/src/test/java/com/github/mustachejavabenchmarks/JsonInterpreterTest.java b/compiler/src/test/java/com/github/mustachejavabenchmarks/JsonInterpreterTest.java index 49327c624..dce2954e1 100644 --- a/compiler/src/test/java/com/github/mustachejavabenchmarks/JsonInterpreterTest.java +++ b/compiler/src/test/java/com/github/mustachejavabenchmarks/JsonInterpreterTest.java @@ -6,7 +6,8 @@ import com.github.mustachejava.DefaultMustacheFactory; import com.github.mustachejava.Mustache; import com.github.mustachejava.MustacheException; -import junit.framework.TestCase; +import org.junit.Before; +import org.junit.Test; import java.io.File; import java.io.IOException; @@ -24,6 +25,7 @@ import java.util.concurrent.atomic.AtomicInteger; import static com.github.mustachejavabenchmarks.BenchmarkTest.skip; +import static org.junit.Assert.assertEquals; /** * Tests for the compiler. @@ -32,7 +34,7 @@ * Date: May 3, 2010 * Time: 10:23:54 AM */ -public class JsonInterpreterTest extends TestCase { +public class JsonInterpreterTest { private static final int TIME = 2; protected File root; @@ -59,7 +61,8 @@ public static Object toObject(final JsonNode node) { } } - public void testSingleThreaded() throws MustacheException, IOException, InterruptedException { + @Test + public void testSingleThreaded() throws MustacheException, IOException { if (skip()) return; final Mustache parse = getMustache(); final Object parent = getScope(); @@ -67,7 +70,8 @@ public void testSingleThreaded() throws MustacheException, IOException, Interrup singlethreaded(parse, parent); } - public void testSingleThreadedClass() throws MustacheException, IOException, InterruptedException { + @Test + public void testSingleThreadedClass() throws MustacheException { if (skip()) return; final Mustache parse = getMustache(); final Object parent = new Object() { @@ -83,6 +87,7 @@ public void testSingleThreadedClass() throws MustacheException, IOException, Int singlethreaded(parse, parent); } + @Test public void testContextPrecedence() throws IOException { Mustache m = new DefaultMustacheFactory().compile(new StringReader("{{#a}}{{b.c}}{{/a}}"), "test"); Map map = new ObjectMapper().readValue("{\"a\": {\"b\": {}}, \"b\": {\"c\": \"ERROR\"}}", Map.class); @@ -92,6 +97,7 @@ public void testContextPrecedence() throws IOException { assertEquals("", sw.toString()); } + @Test public void testCompiler() throws MustacheException, IOException, InterruptedException { if (skip()) return; for (int i = 0; i < 3; i++) { @@ -113,6 +119,7 @@ protected DefaultMustacheFactory createMustacheFactory() { return new DefaultMustacheFactory(root); } + @Test public void testMultithreaded() throws IOException, InterruptedException { if (skip()) return; final Mustache parse = getMustache(); @@ -166,7 +173,6 @@ private Mustache getMustache() { private void singlethreaded(Mustache parse, Object parent) { long start = System.currentTimeMillis(); System.out.println(System.currentTimeMillis() - start); - start = System.currentTimeMillis(); StringWriter writer = new StringWriter(); parse.execute(writer, parent); writer.flush(); @@ -211,11 +217,10 @@ private void time(Mustache parse, Object parent) { System.out.println((System.currentTimeMillis() - start)); } - protected void setUp() throws Exception { - super.setUp(); + @Before + public void setUp() throws Exception { File file = new File("src/test/resources"); root = new File(file, "simple.html").exists() ? file : new File("../src/test/resources"); } - }