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 9268f57 Add UncheckedFilterWriter.
9268f57 is described below
commit 9268f576df22c596d1b5082cbd3abfb78eef78b5
Author: Gary Gregory <[email protected]>
AuthorDate: Mon Jul 12 09:04:24 2021 -0400
Add UncheckedFilterWriter.
---
src/changes/changes.xml | 3 +
.../commons/io/output/UncheckedFilterWriter.java | 180 ++++++++++++++++++++
.../io/output/UncheckedFilterWriterTest.java | 183 +++++++++++++++++++++
3 files changed, 366 insertions(+)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 383adef..77a0b8a 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -60,6 +60,9 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="ggregory" type="add" due-to="Gary Gregory">
Add UncheckedFilterReader.
</action>
+ <action dev="ggregory" type="add" due-to="Gary Gregory">
+ Add UncheckedFilterWriter.
+ </action>
<!-- UPDATE -->
<action dev="ggregory" type="update" due-to="Dependabot">
Bump Maven Javadoc plugin from 3.2.0 to 3.3.0.
diff --git
a/src/main/java/org/apache/commons/io/output/UncheckedFilterWriter.java
b/src/main/java/org/apache/commons/io/output/UncheckedFilterWriter.java
new file mode 100644
index 0000000..e9ad59b
--- /dev/null
+++ b/src/main/java/org/apache/commons/io/output/UncheckedFilterWriter.java
@@ -0,0 +1,180 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.io.output;
+
+import java.io.FilterWriter;
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.io.Writer;
+
+/**
+ * A {@link FilterWriter} that throws {@link UncheckedIOException} instead of
{@link IOException}.
+ *
+ * @see FilterWriter
+ * @see IOException
+ * @see UncheckedIOException
+ * @since 2.12.0
+ */
+public class UncheckedFilterWriter extends FilterWriter {
+
+ /**
+ * Creates a new filtered writer.
+ *
+ * @param writer a Writer object providing the underlying stream.
+ * @return a new UncheckedFilterReader.
+ * @throws NullPointerException if {@code writer} is {@code null}.
+ */
+ public static UncheckedFilterWriter on(final Writer writer) {
+ return new UncheckedFilterWriter(writer);
+ }
+
+ /**
+ * Creates a new filtered writer.
+ *
+ * @param writer a Writer object providing the underlying stream.
+ * @throws NullPointerException if {@code writer} is {@code null}.
+ */
+ protected UncheckedFilterWriter(Writer writer) {
+ super(writer);
+ }
+
+ /**
+ * Calls this method's super and rethrow {@link IOException} as {@link
UncheckedIOException}.
+ */
+ @Override
+ public Writer append(char c) throws UncheckedIOException {
+ try {
+ return super.append(c);
+ } catch (IOException e) {
+ throw uncheck(e);
+ }
+ }
+
+ /**
+ * Calls this method's super and rethrow {@link IOException} as {@link
UncheckedIOException}.
+ */
+ @Override
+ public Writer append(CharSequence csq) throws UncheckedIOException {
+ try {
+ return super.append(csq);
+ } catch (IOException e) {
+ throw uncheck(e);
+ }
+ }
+
+ /**
+ * Calls this method's super and rethrow {@link IOException} as {@link
UncheckedIOException}.
+ */
+ @Override
+ public Writer append(CharSequence csq, int start, int end) throws
UncheckedIOException {
+ try {
+ return super.append(csq, start, end);
+ } catch (IOException e) {
+ throw uncheck(e);
+ }
+ }
+
+ /**
+ * Calls this method's super and rethrow {@link IOException} as {@link
UncheckedIOException}.
+ */
+ @Override
+ public void close() throws UncheckedIOException {
+ try {
+ super.close();
+ } catch (IOException e) {
+ throw uncheck(e);
+ }
+ }
+
+ /**
+ * Calls this method's super and rethrow {@link IOException} as {@link
UncheckedIOException}.
+ */
+ @Override
+ public void flush() throws UncheckedIOException {
+ try {
+ super.flush();
+ } catch (IOException e) {
+ throw uncheck(e);
+ }
+ }
+
+ private UncheckedIOException uncheck(IOException e) {
+ return new UncheckedIOException(e);
+ }
+
+ /**
+ * Calls this method's super and rethrow {@link IOException} as {@link
UncheckedIOException}.
+ */
+ @Override
+ public void write(char[] cbuf) throws UncheckedIOException {
+ try {
+ super.write(cbuf);
+ } catch (IOException e) {
+ throw uncheck(e);
+ }
+ }
+
+ /**
+ * Calls this method's super and rethrow {@link IOException} as {@link
UncheckedIOException}.
+ */
+ @Override
+ public void write(char[] cbuf, int off, int len) throws
UncheckedIOException {
+ try {
+ super.write(cbuf, off, len);
+ } catch (IOException e) {
+ throw uncheck(e);
+ }
+ }
+
+ /**
+ * Calls this method's super and rethrow {@link IOException} as {@link
UncheckedIOException}.
+ */
+ @Override
+ public void write(int c) throws UncheckedIOException {
+ try {
+ super.write(c);
+ } catch (IOException e) {
+ throw uncheck(e);
+ }
+ }
+
+ /**
+ * Calls this method's super and rethrow {@link IOException} as {@link
UncheckedIOException}.
+ */
+ @Override
+ public void write(String str) throws UncheckedIOException {
+ try {
+ super.write(str);
+ } catch (IOException e) {
+ throw uncheck(e);
+ }
+ }
+
+ /**
+ * Calls this method's super and rethrow {@link IOException} as {@link
UncheckedIOException}.
+ */
+ @Override
+ public void write(String str, int off, int len) throws
UncheckedIOException {
+ try {
+ super.write(str, off, len);
+ } catch (IOException e) {
+ throw uncheck(e);
+ }
+ }
+
+}
diff --git
a/src/test/java/org/apache/commons/io/output/UncheckedFilterWriterTest.java
b/src/test/java/org/apache/commons/io/output/UncheckedFilterWriterTest.java
new file mode 100644
index 0000000..10b0ee3
--- /dev/null
+++ b/src/test/java/org/apache/commons/io/output/UncheckedFilterWriterTest.java
@@ -0,0 +1,183 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.io.output;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import java.io.IOException;
+import java.io.StringWriter;
+import java.io.UncheckedIOException;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Disabled;
+import org.junit.jupiter.api.Test;
+
+/**
+ * JUnit Test Case for {@link BrokenWriter}.
+ */
+public class UncheckedFilterWriterTest {
+
+ private IOException exception;
+
+ private UncheckedFilterWriter brokenWriter;
+ private UncheckedFilterWriter stringWriter;
+
+ @SuppressWarnings("resource")
+ @BeforeEach
+ public void setUp() {
+ exception = new IOException("test exception");
+ brokenWriter = UncheckedFilterWriter.on(new BrokenWriter(exception));
+ stringWriter = UncheckedFilterWriter.on(new StringWriter());
+ }
+
+ @SuppressWarnings("resource")
+ @Test
+ public void testAppendChar() {
+ stringWriter.append('1');
+ }
+
+ @SuppressWarnings("resource")
+ @Test
+ public void testAppendCharSequence() {
+ stringWriter.append("01");
+ }
+
+ @SuppressWarnings("resource")
+ @Test
+ public void testAppendCharSequenceIndexed() {
+ stringWriter.append("01", 0, 1);
+ }
+
+ @Test
+ public void testAppendCharSequenceIndexedThrows() {
+ assertEquals(exception, assertThrows(UncheckedIOException.class, () ->
brokenWriter.append("01", 0, 1)).getCause());
+ }
+
+ @Test
+ public void testAppendCharSequenceThrows() {
+ assertEquals(exception, assertThrows(UncheckedIOException.class, () ->
brokenWriter.append("01")).getCause());
+ }
+
+ @Test
+ public void testAppendCharThrows() {
+ assertEquals(exception, assertThrows(UncheckedIOException.class, () ->
brokenWriter.append('1')).getCause());
+ }
+
+ @Test
+ public void testClose() {
+ stringWriter.close();
+ }
+
+ @Test
+ public void testCloseThrows() {
+ assertEquals(exception, assertThrows(UncheckedIOException.class, () ->
brokenWriter.close()).getCause());
+ }
+
+ @Test
+ public void testEquals() {
+ stringWriter.equals(null);
+ }
+
+ @Test
+ @Disabled("What should happen here?")
+ public void testEqualsThrows() {
+ assertEquals(exception, assertThrows(UncheckedIOException.class, () ->
brokenWriter.equals(null)).getCause());
+ }
+
+ @Test
+ public void testFlush() {
+ stringWriter.flush();
+ }
+
+ @Test
+ public void testFlushThrows() {
+ assertEquals(exception, assertThrows(UncheckedIOException.class, () ->
brokenWriter.flush()).getCause());
+ }
+
+ @Test
+ public void testHashCode() {
+ stringWriter.hashCode();
+ }
+
+ @Test
+ @Disabled("What should happen here?")
+ public void testHashCodeThrows() {
+ assertEquals(exception, assertThrows(UncheckedIOException.class, () ->
brokenWriter.hashCode()).getCause());
+ }
+
+ @Test
+ public void testToString() {
+ stringWriter.toString();
+ }
+
+ @Test
+ @Disabled("What should happen here?")
+ public void testToStringThrows() {
+ assertEquals(exception, assertThrows(UncheckedIOException.class, () ->
brokenWriter.toString()).getCause());
+ }
+
+ @Test
+ public void testWriteCharArray() {
+ stringWriter.write(new char[1]);
+ }
+
+ @Test
+ public void testWriteCharArrayIndexed() {
+ stringWriter.write(new char[1], 0, 1);
+ }
+
+ @Test
+ public void testWriteCharArrayIndexedThrows() {
+ assertEquals(exception, assertThrows(UncheckedIOException.class, () ->
brokenWriter.write(new char[1], 0, 1)).getCause());
+ }
+
+ @Test
+ public void testWriteCharArrayThrows() {
+ assertEquals(exception, assertThrows(UncheckedIOException.class, () ->
brokenWriter.write(new char[1])).getCause());
+ }
+
+ @Test
+ public void testWriteInt() {
+ stringWriter.write(1);
+ }
+
+ @Test
+ public void testWriteIntThrows() {
+ assertEquals(exception, assertThrows(UncheckedIOException.class, () ->
brokenWriter.write(1)).getCause());
+ }
+
+ @Test
+ public void testWriteString() {
+ stringWriter.write("01");
+ }
+
+ @Test
+ public void testWriteStringIndexed() {
+ stringWriter.write("01", 0, 1);
+ }
+
+ @Test
+ public void testWriteStringIndexedThrows() {
+ assertEquals(exception, assertThrows(UncheckedIOException.class, () ->
brokenWriter.write("01", 0, 1)).getCause());
+ }
+
+ @Test
+ public void testWriteStringThrows() {
+ assertEquals(exception, assertThrows(UncheckedIOException.class, () ->
brokenWriter.write("01")).getCause());
+ }
+}