TemplateTest now detects bugs where the output Writer is closed to early
Project: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/commit/560247a0 Tree: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/tree/560247a0 Diff: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/diff/560247a0 Branch: refs/heads/2.3 Commit: 560247a0928cd31176dfda172ed2cbebbdcfd426 Parents: f27310b Author: ddekany <ddek...@apache.org> Authored: Thu Sep 28 01:17:18 2017 +0200 Committer: ddekany <ddek...@apache.org> Committed: Thu Sep 28 01:17:18 2017 +0200 ---------------------------------------------------------------------- src/test/java/freemarker/test/TemplateTest.java | 38 ++++++++++++++++++-- 1 file changed, 35 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/560247a0/src/test/java/freemarker/test/TemplateTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/freemarker/test/TemplateTest.java b/src/test/java/freemarker/test/TemplateTest.java index 99e2db1..9377a45 100644 --- a/src/test/java/freemarker/test/TemplateTest.java +++ b/src/test/java/freemarker/test/TemplateTest.java @@ -22,6 +22,7 @@ package freemarker.test; import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; +import java.io.FilterWriter; import java.io.IOException; import java.io.InputStream; import java.io.StringWriter; @@ -145,9 +146,40 @@ public abstract class TemplateTest { protected String getOutput(Template t) throws TemplateException, IOException { StringWriter out = new StringWriter(); - t.process(getDataModel(), out); - String actualOut = out.toString(); - return actualOut; + t.process(getDataModel(), new FilterWriter(out) { + private boolean closed; + + @Override + public void write(int c) throws IOException { + checkNotClosed(); + super.write(c); + } + + @Override + public void write(char[] cbuf, int off, int len) throws IOException { + checkNotClosed(); + super.write(cbuf, off, len); + } + + @Override + public void write(String str, int off, int len) throws IOException { + checkNotClosed(); + super.write(str, off, len); + } + + @Override + public void close() throws IOException { + super.close(); + closed = true; + } + + private void checkNotClosed() throws IOException { + if (closed) { + throw new IOException("Writer is already closed"); + } + } + }); + return out.toString(); } protected Configuration createConfiguration() throws Exception {