This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-io.git
The following commit(s) were added to refs/heads/master by this push: new d15f480 [IO-617] Add class CloseShieldWriter. #83. d15f480 is described below commit d15f480dd81b373f6815340c15b7a80490e8272d Author: Gary Gregory <gardgreg...@gmail.com> AuthorDate: Fri Aug 9 11:26:40 2019 -0400 [IO-617] Add class CloseShieldWriter. #83. - Applied partial patch from GitHub PR #83 from Rob Spoor which contained duplication from another PR. - Reformatted some files. - Closes #83. --- src/changes/changes.xml | 3 ++ .../commons/io/output/CloseShieldOutputStream.java | 2 +- ...eldOutputStream.java => CloseShieldWriter.java} | 27 ++++++------ ...tStreamTest.java => CloseShieldWriterTest.java} | 49 ++++++++++++---------- .../commons/io/output/ClosedOutputStreamTest.java | 2 +- 5 files changed, 43 insertions(+), 40 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index bd246d9..f186243 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -125,6 +125,9 @@ The <action> type attribute can be add,update,fix,remove. <action issue="IO-616" dev="ggregory" type="add" due-to="Rob Spoor"> Add class AppendableWriter. #87. </action> + <action issue="IO-617" dev="ggregory" type="add" due-to="Rob Spoor, Gary Gregory"> + Add class CloseShieldWriter. #83. + </action> </release> <release version="2.6" date="2017-10-15" description="Java 7 required, Java 9 supported."> diff --git a/src/main/java/org/apache/commons/io/output/CloseShieldOutputStream.java b/src/main/java/org/apache/commons/io/output/CloseShieldOutputStream.java index 8518b7b..8146533 100644 --- a/src/main/java/org/apache/commons/io/output/CloseShieldOutputStream.java +++ b/src/main/java/org/apache/commons/io/output/CloseShieldOutputStream.java @@ -47,7 +47,7 @@ public class CloseShieldOutputStream extends ProxyOutputStream { */ @Override public void close() { - out = new ClosedOutputStream(); + out = ClosedOutputStream.CLOSED_OUTPUT_STREAM; } } diff --git a/src/main/java/org/apache/commons/io/output/CloseShieldOutputStream.java b/src/main/java/org/apache/commons/io/output/CloseShieldWriter.java similarity index 54% copy from src/main/java/org/apache/commons/io/output/CloseShieldOutputStream.java copy to src/main/java/org/apache/commons/io/output/CloseShieldWriter.java index 8518b7b..73a351b 100644 --- a/src/main/java/org/apache/commons/io/output/CloseShieldOutputStream.java +++ b/src/main/java/org/apache/commons/io/output/CloseShieldWriter.java @@ -16,38 +16,35 @@ */ package org.apache.commons.io.output; -import java.io.OutputStream; +import java.io.Writer; /** - * Proxy stream that prevents the underlying output stream from being closed. + * Proxy stream that prevents the underlying writer from being closed. * <p> - * This class is typically used in cases where an output stream needs to be - * passed to a component that wants to explicitly close the stream even if - * other components would still use the stream for output. + * This class is typically used in cases where a writer needs to be passed to a component that wants to explicitly close + * the writer even if other components would still use the writer for output. * </p> * - * @since 1.4 + * @since 2.7 */ -public class CloseShieldOutputStream extends ProxyOutputStream { +public class CloseShieldWriter extends ProxyWriter { /** - * Creates a proxy that shields the given output stream from being - * closed. + * Creates a proxy that shields the given writer from being closed. * - * @param out underlying output stream + * @param out underlying writer */ - public CloseShieldOutputStream(final OutputStream out) { + public CloseShieldWriter(final Writer out) { super(out); } /** - * Replaces the underlying output stream with a {@link ClosedOutputStream} - * sentinel. The original output stream will remain open, but this proxy - * will appear closed. + * Replaces the underlying writer with a {@link ClosedWriter} sentinel. The original writer will remain open, but + * this proxy will appear closed. */ @Override public void close() { - out = new ClosedOutputStream(); + out = ClosedWriter.CLOSED_WRITER; } } diff --git a/src/test/java/org/apache/commons/io/output/ClosedOutputStreamTest.java b/src/test/java/org/apache/commons/io/output/CloseShieldWriterTest.java similarity index 52% copy from src/test/java/org/apache/commons/io/output/ClosedOutputStreamTest.java copy to src/test/java/org/apache/commons/io/output/CloseShieldWriterTest.java index 45e3cab..72f728a 100644 --- a/src/test/java/org/apache/commons/io/output/ClosedOutputStreamTest.java +++ b/src/test/java/org/apache/commons/io/output/CloseShieldWriterTest.java @@ -16,43 +16,46 @@ */ package org.apache.commons.io.output; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; import java.io.IOException; +import java.io.Writer; +import org.junit.Before; import org.junit.Test; /** - * JUnit Test Case for {@link ClosedOutputStream}. + * JUnit Test Case for {@link CloseShieldWriter}. */ -public class ClosedOutputStreamTest { +public class CloseShieldWriterTest { - /** - * Test the <code>write(b)</code> method. - * @throws Exception - */ - @Test - public void testRead() throws Exception { - try (ClosedOutputStream cos = new ClosedOutputStream()) { - cos.write('x'); - fail("write(b)"); - } catch (final IOException e) { - // expected - } + private StringBuilderWriter original; + + private Writer shielded; + + @Before + public void setUp() { + original = spy(new StringBuilderWriter()); + shielded = new CloseShieldWriter(original); } - /** - * Test the <code>flush()</code> method. - * @throws Exception - */ @Test - public void testFlush() throws Exception { - try (ClosedOutputStream cos = new ClosedOutputStream()) { - cos.flush(); - fail("flush()"); - } catch (final IOException e) { + public void testClose() throws IOException { + shielded.close(); + verify(original, never()).close(); + try { + shielded.write('x'); + fail("write(c)"); + } catch (final IOException ignore) { // expected } + original.write('y'); + assertEquals(1, original.getBuilder().length()); + assertEquals('y', original.toString().charAt(0)); } } diff --git a/src/test/java/org/apache/commons/io/output/ClosedOutputStreamTest.java b/src/test/java/org/apache/commons/io/output/ClosedOutputStreamTest.java index 45e3cab..e5d8321 100644 --- a/src/test/java/org/apache/commons/io/output/ClosedOutputStreamTest.java +++ b/src/test/java/org/apache/commons/io/output/ClosedOutputStreamTest.java @@ -32,7 +32,7 @@ public class ClosedOutputStreamTest { * @throws Exception */ @Test - public void testRead() throws Exception { + public void testWrite() throws Exception { try (ClosedOutputStream cos = new ClosedOutputStream()) { cos.write('x'); fail("write(b)");