Repository: decorators-squad/eo-yaml
Pull request #316 (2020-04-12 12:02:44)

#315 closed the Writer after printing
PR for #315 


Patch:
diff --git a/src/main/java/com/amihaiemil/eoyaml/RtYamlPrinter.java b/src/main/java/com/amihaiemil/eoyaml/RtYamlPrinter.java
index 334d562..0aa6415 100644
--- a/src/main/java/com/amihaiemil/eoyaml/RtYamlPrinter.java
+++ b/src/main/java/com/amihaiemil/eoyaml/RtYamlPrinter.java
@@ -57,18 +57,22 @@ final class RtYamlPrinter implements YamlPrinter {
 
     @Override
     public void print(final YamlNode node) throws IOException  {
-        if(node instanceof Scalar) {
-            this.writer.append("---").append(System.lineSeparator());
-            this.printScalar((Scalar) node, 0);
-            this.writer.append(System.lineSeparator()).append("...");
-        } else if(node instanceof YamlSequence) {
-            this.printPossibleComment(node, "");
-            this.printSequence((YamlSequence) node, 0);
-        } else if(node instanceof YamlMapping) {
-            this.printPossibleComment(node, "");
-            this.printMapping((YamlMapping) node, 0);
-        } else if(node instanceof YamlStream) {
-            this.printStream((YamlStream) node, 0);
+        try {
+            if (node instanceof Scalar) {
+                this.writer.append("---").append(System.lineSeparator());
+                this.printScalar((Scalar) node, 0);
+                this.writer.append(System.lineSeparator()).append("...");
+            } else if (node instanceof YamlSequence) {
+                this.printPossibleComment(node, "");
+                this.printSequence((YamlSequence) node, 0);
+            } else if (node instanceof YamlMapping) {
+                this.printPossibleComment(node, "");
+                this.printMapping((YamlMapping) node, 0);
+            } else if (node instanceof YamlStream) {
+                this.printStream((YamlStream) node, 0);
+            }
+        } finally {
+            this.writer.close();
         }
     }
 


Test patch:
diff --git a/src/test/java/com/amihaiemil/eoyaml/RtYamlPrinterTest.java b/src/test/java/com/amihaiemil/eoyaml/RtYamlPrinterTest.java
new file mode 100644
index 0000000..00c6fe0
--- /dev/null
+++ b/src/test/java/com/amihaiemil/eoyaml/RtYamlPrinterTest.java
@@ -0,0 +1,81 @@
+/**
+ * Copyright (c) 2016-2020, Mihai Emil Andronache
+ * All rights reserved.
+ * <p>
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * Neither the name of the copyright holder nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+package com.amihaiemil.eoyaml;
+
+import org.hamcrest.MatcherAssert;
+import org.hamcrest.Matchers;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.io.*;
+
+/**
+ *
+ * Unit tests for {@link RtYamlPrinter}.
+ * @author Mihai Andronache (amihaiemil@gmail.com)
+ * @version $Id$
+ * @since 4.3.2
+ */
+public final class RtYamlPrinterTest {
+
+    /**
+     * The provided Writer should be closed after the print is done.
+     * @throws Exception If something goes wrong.
+     */
+    @Test
+    public void writerIsBeingClosed() throws Exception {
+        final Scalar scalar = new PlainStringScalar("scalar");
+
+        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+
+        final YamlPrinter printer = new RtYamlPrinter(
+            new OutputStreamWriter(baos)
+        );
+        printer.print(scalar);
+        MatcherAssert.assertThat(
+            baos.toString(),
+            Matchers.equalTo(
+                "---"
+               + System.lineSeparator()
+               + "scalar"
+               + System.lineSeparator()
+               + "..."
+            )
+        );
+        try {
+            printer.print(scalar);
+            Assert.fail("IOException was expected!");
+        } catch (final IOException ex) {
+            MatcherAssert.assertThat(
+                ex.getMessage(),
+                Matchers.equalTo("Stream closed")
+            );
+        }
+    }
+
+}