From 2be7535e09e359d70b924013c9f4d25149fdf0ae Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Thu, 6 Sep 2018 08:57:51 -0400 Subject: java: add src/main/java/org/pablotron/luigi/ResultHandler.java, update tests --- .../java/org/pablotron/luigi/ResultHandler.java | 5 +++++ .../main/java/org/pablotron/luigi/Template.java | 9 ++++++++ .../org/pablotron/luigi/tests/TemplateTest.java | 24 ++++++++++++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 java/src/main/java/org/pablotron/luigi/ResultHandler.java (limited to 'java') diff --git a/java/src/main/java/org/pablotron/luigi/ResultHandler.java b/java/src/main/java/org/pablotron/luigi/ResultHandler.java new file mode 100644 index 0000000..32690a2 --- /dev/null +++ b/java/src/main/java/org/pablotron/luigi/ResultHandler.java @@ -0,0 +1,5 @@ +package org.pablotron.luigi; + +public interface ResultHandler { + public abstract void append(final String s); +}; diff --git a/java/src/main/java/org/pablotron/luigi/Template.java b/java/src/main/java/org/pablotron/luigi/Template.java index 2b17a54..9af3a74 100644 --- a/java/src/main/java/org/pablotron/luigi/Template.java +++ b/java/src/main/java/org/pablotron/luigi/Template.java @@ -35,6 +35,15 @@ public final class Template { return r.toString(); } + public void run( + final Map args, + final ResultHandler r + ) throws LuigiError { + for (Action a: this.actions) { + r.append(a.run(this.filters, args)); + } + } + public String toString() { return this.template; } diff --git a/java/src/test/java/org/pablotron/luigi/tests/TemplateTest.java b/java/src/test/java/org/pablotron/luigi/tests/TemplateTest.java index 5ff9ede..94e259e 100644 --- a/java/src/test/java/org/pablotron/luigi/tests/TemplateTest.java +++ b/java/src/test/java/org/pablotron/luigi/tests/TemplateTest.java @@ -3,6 +3,7 @@ import java.util.HashMap; import org.pablotron.luigi.LuigiError; import org.pablotron.luigi.Template; +import org.pablotron.luigi.ResultHandler; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -35,6 +36,29 @@ public final class TemplateTest { assertEquals("foofoo", r); } + private static final class TestResultHandler implements ResultHandler { + private final StringBuilder sb; + public TestResultHandler(final StringBuilder sb) { + this.sb = sb; + } + + public void append(final String s) { + sb.append(s); + } + }; + + @Test + public void testResultHandler() throws LuigiError { + final Template t = new Template("foo%{bar}"); + final StringBuilder sb = new StringBuilder(); + final TestResultHandler rh = new TestResultHandler(sb); + + t.run(TEST_ARGS, rh); + final String r = sb.toString(); + + assertEquals("foofoo", sb.toString()); + } + @Test public void testMultipleKeys() throws LuigiError { final Template t = new Template("foo%{bar}%{baz}"); -- cgit v1.2.3