You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2023/12/21 13:26:23 UTC
(commons-io) branch master updated: [IO-826] Reimplemented add runtime exception support to broken streams (#530)
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 dd63fa3b [IO-826] Reimplemented add runtime exception support to broken streams (#530)
dd63fa3b is described below
commit dd63fa3b53b4029b026d276a3c5564fa5aac42d1
Author: markslater <ma...@mail.com>
AuthorDate: Thu Dec 21 13:26:18 2023 +0000
[IO-826] Reimplemented add runtime exception support to broken streams (#530)
* Introduce ability to throw RuntimeExceptions from BrokenReader / BrokenOutputStream / BrokenWriter following example of BrokenInputStream.
---------
Co-authored-by: Gary Gregory <ga...@users.noreply.github.com>
---
.../apache/commons/io/input/BrokenInputStream.java | 24 ++--
.../org/apache/commons/io/input/BrokenReader.java | 75 ++++++++----
.../commons/io/output/BrokenOutputStream.java | 55 ++++++---
.../org/apache/commons/io/output/BrokenWriter.java | 57 ++++++---
.../org/apache/commons/io/BrokenTestFactories.java | 50 ++++++++
.../commons/io/input/BrokenInputStreamTest.java | 60 ++++-----
.../apache/commons/io/input/BrokenReaderTest.java | 96 ++++++++++-----
.../commons/io/output/BrokenOutputStreamTest.java | 68 +++++++----
.../apache/commons/io/output/BrokenWriterTest.java | 135 ++++++++++++---------
9 files changed, 399 insertions(+), 221 deletions(-)
diff --git a/src/main/java/org/apache/commons/io/input/BrokenInputStream.java b/src/main/java/org/apache/commons/io/input/BrokenInputStream.java
index aee56278..1679430f 100644
--- a/src/main/java/org/apache/commons/io/input/BrokenInputStream.java
+++ b/src/main/java/org/apache/commons/io/input/BrokenInputStream.java
@@ -23,7 +23,7 @@ import java.util.function.Supplier;
import org.apache.commons.io.function.Erase;
/**
- * Always throws an {@link IOException} from all the {@link InputStream} methods where the exception is declared.
+ * Always throws an exception from all {@link InputStream} methods where {@link IOException} is declared.
* <p>
* This class is mostly useful for testing error handling.
* </p>
@@ -33,7 +33,7 @@ import org.apache.commons.io.function.Erase;
public class BrokenInputStream extends InputStream {
/**
- * The singleton instance.
+ * The singleton instance using a default IOException.
*
* @since 2.12.0
*/
@@ -73,7 +73,7 @@ public class BrokenInputStream extends InputStream {
}
/**
- * Constructs a new stream that always throws an {@link IOException}.
+ * Constructs a new stream that always throws the supplied exception.
*
* @param exceptionSupplier a supplier for the IOException or RuntimeException to be thrown.
* @since 2.12.0
@@ -85,8 +85,8 @@ public class BrokenInputStream extends InputStream {
/**
* Throws the configured exception.
*
- * @return nothing
- * @throws IOException always thrown
+ * @return nothing.
+ * @throws IOException always throws the exception configured in a constructor.
*/
@Override
public int available() throws IOException {
@@ -96,7 +96,7 @@ public class BrokenInputStream extends InputStream {
/**
* Throws the configured exception.
*
- * @throws IOException always thrown
+ * @throws IOException always throws the exception configured in a constructor.
*/
@Override
public void close() throws IOException {
@@ -106,8 +106,8 @@ public class BrokenInputStream extends InputStream {
/**
* Throws the configured exception.
*
- * @return nothing
- * @throws IOException always thrown
+ * @return nothing.
+ * @throws IOException always throws the exception configured in a constructor.
*/
@Override
public int read() throws IOException {
@@ -117,7 +117,7 @@ public class BrokenInputStream extends InputStream {
/**
* Throws the configured exception.
*
- * @throws IOException always thrown
+ * @throws IOException always throws the exception configured in a constructor.
*/
@Override
public synchronized void reset() throws IOException {
@@ -136,9 +136,9 @@ public class BrokenInputStream extends InputStream {
/**
* Throws the configured exception.
*
- * @param n ignored
- * @return nothing
- * @throws IOException always thrown
+ * @param n ignored.
+ * @return nothing.
+ * @throws IOException always throws the exception configured in a constructor.
*/
@Override
public long skip(final long n) throws IOException {
diff --git a/src/main/java/org/apache/commons/io/input/BrokenReader.java b/src/main/java/org/apache/commons/io/input/BrokenReader.java
index 03530e8c..96557dd4 100644
--- a/src/main/java/org/apache/commons/io/input/BrokenReader.java
+++ b/src/main/java/org/apache/commons/io/input/BrokenReader.java
@@ -20,8 +20,10 @@ import java.io.IOException;
import java.io.Reader;
import java.util.function.Supplier;
+import org.apache.commons.io.function.Erase;
+
/**
- * Always throws an {@link IOException} from all the {@link Reader} methods where the exception is declared.
+ * Always throws an exception from all {@link Reader} methods where {@link IOException} is declared.
* <p>
* This class is mostly useful for testing error handling.
* </p>
@@ -40,7 +42,7 @@ public class BrokenReader extends Reader {
/**
* A supplier for the exception that is thrown by all methods of this class.
*/
- private final Supplier<IOException> exceptionSupplier;
+ private final Supplier<Throwable> exceptionSupplier;
/**
* Constructs a new reader that always throws an {@link IOException}.
@@ -53,87 +55,108 @@ public class BrokenReader extends Reader {
* Constructs a new reader that always throws the given exception.
*
* @param exception the exception to be thrown.
+ * @deprecated Use {@link #BrokenReader(Throwable)}.
*/
+ @Deprecated
public BrokenReader(final IOException exception) {
this(() -> exception);
}
/**
- * Constructs a new reader that always throws an {@link IOException}
+ * Constructs a new reader that always throws the given exception.
+ *
+ * @param exception the exception to be thrown.
+ * @since 2.16.0
+ */
+ public BrokenReader(final Throwable exception) {
+ this(() -> exception);
+ }
+
+ /**
+ * Constructs a new reader that always throws the supplied exception.
*
- * @param exceptionSupplier a supplier for the exception to be thrown.
+ * @param exceptionSupplier a supplier for the IOException or RuntimeException to be thrown.
* @since 2.12.0
*/
- public BrokenReader(final Supplier<IOException> exceptionSupplier) {
+ public BrokenReader(final Supplier<Throwable> exceptionSupplier) {
this.exceptionSupplier = exceptionSupplier;
}
/**
* Throws the configured exception.
*
- * @throws IOException always thrown
+ * @throws IOException always throws the exception configured in a constructor.
*/
@Override
public void close() throws IOException {
- throw exceptionSupplier.get();
+ throw rethrow();
}
/**
* Throws the configured exception.
*
- * @param readAheadLimit ignored
- * @throws IOException always thrown
+ * @param readAheadLimit ignored.
+ * @throws IOException always throws the exception configured in a constructor.
*/
@Override
public void mark(final int readAheadLimit) throws IOException {
- throw exceptionSupplier.get();
+ throw rethrow();
}
/**
* Throws the configured exception.
*
- * @param cbuf ignored
- * @param off ignored
- * @param len ignored
- * @return nothing
- * @throws IOException always thrown
+ * @param cbuf ignored.
+ * @param off ignored.
+ * @param len ignored.
+ * @return nothing.
+ * @throws IOException always throws the exception configured in a constructor.
*/
@Override
public int read(final char[] cbuf, final int off, final int len) throws IOException {
- throw exceptionSupplier.get();
+ throw rethrow();
}
/**
* Throws the configured exception.
*
- * @return nothing
- * @throws IOException always thrown
+ * @return nothing.
+ * @throws IOException always throws the exception configured in a constructor.
*/
@Override
public boolean ready() throws IOException {
- throw exceptionSupplier.get();
+ throw rethrow();
}
/**
* Throws the configured exception.
*
- * @throws IOException always thrown
+ * @throws IOException always throws the exception configured in a constructor.
*/
@Override
- public synchronized void reset() throws IOException {
- throw exceptionSupplier.get();
+ public void reset() throws IOException {
+ throw rethrow();
+ }
+
+ /**
+ * Throws the configured exception from its supplier.
+ *
+ * @return Throws the configured exception from its supplier.
+ */
+ private RuntimeException rethrow() {
+ return Erase.rethrow(exceptionSupplier.get());
}
/**
* Throws the configured exception.
*
- * @param n ignored
- * @return nothing
- * @throws IOException always thrown
+ * @param n ignored.
+ * @return nothing.
+ * @throws IOException always throws the exception configured in a constructor.
*/
@Override
public long skip(final long n) throws IOException {
- throw exceptionSupplier.get();
+ throw rethrow();
}
}
diff --git a/src/main/java/org/apache/commons/io/output/BrokenOutputStream.java b/src/main/java/org/apache/commons/io/output/BrokenOutputStream.java
index 4accc56d..eaef8d12 100644
--- a/src/main/java/org/apache/commons/io/output/BrokenOutputStream.java
+++ b/src/main/java/org/apache/commons/io/output/BrokenOutputStream.java
@@ -20,12 +20,12 @@ import java.io.IOException;
import java.io.OutputStream;
import java.util.function.Supplier;
+import org.apache.commons.io.function.Erase;
+
/**
- * Broken output stream. This stream always throws an {@link IOException} from
- * all {@link OutputStream} methods.
+ * Always throws an exception from all {@link OutputStream} methods where {@link IOException} is declared.
* <p>
- * This class is mostly useful for testing error handling in code that uses an
- * output stream.
+ * This class is mostly useful for testing error handling.
* </p>
*
* @since 2.0
@@ -33,16 +33,16 @@ import java.util.function.Supplier;
public class BrokenOutputStream extends OutputStream {
/**
- * A singleton instance.
+ * The singleton instance using a default IOException.
*
* @since 2.12.0
*/
public static final BrokenOutputStream INSTANCE = new BrokenOutputStream();
/**
- * A supplier for the exception that is thrown by all methods of this class.
+ * Supplies the exception that is thrown by all methods of this class.
*/
- private final Supplier<IOException> exceptionSupplier;
+ private final Supplier<Throwable> exceptionSupplier;
/**
* Constructs a new stream that always throws an {@link IOException}.
@@ -55,50 +55,71 @@ public class BrokenOutputStream extends OutputStream {
* Constructs a new stream that always throws the given exception.
*
* @param exception the exception to be thrown.
+ * @deprecated Use {@link #BrokenOutputStream(Throwable)}.
*/
+ @Deprecated
public BrokenOutputStream(final IOException exception) {
this(() -> exception);
}
/**
- * Constructs a new stream that always throws an {@link IOException}.
+ * Constructs a new stream that always throws the given exception.
*
- * @param exceptionSupplier a supplier for the exception to be thrown.
+ * @param exception the exception to be thrown.
+ * @since 2.16.0
+ */
+ public BrokenOutputStream(final Throwable exception) {
+ this(() -> exception);
+ }
+
+ /**
+ * Constructs a new stream that always throws the supplied exception.
+ *
+ * @param exceptionSupplier a supplier for the IOException or RuntimeException to be thrown.
* @since 2.12.0
*/
- public BrokenOutputStream(final Supplier<IOException> exceptionSupplier) {
+ public BrokenOutputStream(final Supplier<Throwable> exceptionSupplier) {
this.exceptionSupplier = exceptionSupplier;
}
/**
* Throws the configured exception.
*
- * @throws IOException always thrown
+ * @throws IOException always throws the exception configured in a constructor.
*/
@Override
public void close() throws IOException {
- throw exceptionSupplier.get();
+ throw rethrow();
}
/**
* Throws the configured exception.
*
- * @throws IOException always thrown
+ * @throws IOException always throws the exception configured in a constructor.
*/
@Override
public void flush() throws IOException {
- throw exceptionSupplier.get();
+ throw rethrow();
+ }
+
+ /**
+ * Throws the configured exception from its supplier.
+ *
+ * @return Throws the configured exception from its supplier.
+ */
+ private RuntimeException rethrow() {
+ return Erase.rethrow(exceptionSupplier.get());
}
/**
* Throws the configured exception.
*
- * @param b ignored
- * @throws IOException always thrown
+ * @param b ignored.
+ * @throws IOException always throws the exception configured in a constructor.
*/
@Override
public void write(final int b) throws IOException {
- throw exceptionSupplier.get();
+ throw rethrow();
}
}
diff --git a/src/main/java/org/apache/commons/io/output/BrokenWriter.java b/src/main/java/org/apache/commons/io/output/BrokenWriter.java
index c56d9a31..241e8dd1 100644
--- a/src/main/java/org/apache/commons/io/output/BrokenWriter.java
+++ b/src/main/java/org/apache/commons/io/output/BrokenWriter.java
@@ -20,10 +20,12 @@ import java.io.IOException;
import java.io.Writer;
import java.util.function.Supplier;
+import org.apache.commons.io.function.Erase;
+
/**
- * Always throws an {@link IOException} from all {@link Writer} methods.
+ * Always throws an exception from all {@link Writer} methods where {@link IOException} is declared.
* <p>
- * This class is mostly useful for testing error handling in code that uses a writer.
+ * This class is mostly useful for testing error handling.
* </p>
*
* @since 2.0
@@ -31,16 +33,16 @@ import java.util.function.Supplier;
public class BrokenWriter extends Writer {
/**
- * The singleton instance.
+ * The singleton instance using a default IOException.
*
* @since 2.12.0
*/
public static final BrokenWriter INSTANCE = new BrokenWriter();
/**
- * A supplier for the exception that is thrown by all methods of this class.
+ * Supplies the exception that is thrown by all methods of this class.
*/
- private final Supplier<IOException> exceptionSupplier;
+ private final Supplier<Throwable> exceptionSupplier;
/**
* Constructs a new writer that always throws an {@link IOException}.
@@ -53,52 +55,73 @@ public class BrokenWriter extends Writer {
* Constructs a new writer that always throws the given exception.
*
* @param exception the exception to be thrown.
+ * @deprecated Use {@link #BrokenWriter(Throwable)}.
*/
+ @Deprecated
public BrokenWriter(final IOException exception) {
this(() -> exception);
}
/**
- * Constructs a new writer that always throws an {@link IOException}.
+ * Constructs a new writer that always throws the given exception.
*
- * @param exceptionSupplier a supplier for the exception to be thrown.
+ * @param exception the exception to be thrown.
+ * @since 2.16.0
+ */
+ public BrokenWriter(final Throwable exception) {
+ this(() -> exception);
+ }
+
+ /**
+ * Constructs a new writer that always throws the supplied exception.
+ *
+ * @param exceptionSupplier a supplier for the IOException or RuntimeException to be thrown.
* @since 2.12.0
*/
- public BrokenWriter(final Supplier<IOException> exceptionSupplier) {
+ public BrokenWriter(final Supplier<Throwable> exceptionSupplier) {
this.exceptionSupplier = exceptionSupplier;
}
/**
* Throws the configured exception.
*
- * @throws IOException always thrown
+ * @throws IOException always throws the exception configured in a constructor.
*/
@Override
public void close() throws IOException {
- throw exceptionSupplier.get();
+ throw rethrow();
}
/**
* Throws the configured exception.
*
- * @throws IOException always thrown
+ * @throws IOException always throws the exception configured in a constructor.
*/
@Override
public void flush() throws IOException {
- throw exceptionSupplier.get();
+ throw rethrow();
+ }
+
+ /**
+ * Throws the configured exception from its supplier.
+ *
+ * @return Throws the configured exception from its supplier.
+ */
+ private RuntimeException rethrow() {
+ return Erase.rethrow(exceptionSupplier.get());
}
/**
* Throws the configured exception.
*
- * @param cbuf ignored
- * @param off ignored
- * @param len ignored
- * @throws IOException always thrown
+ * @param cbuf ignored.
+ * @param off ignored.
+ * @param len ignored.
+ * @throws IOException always throws the exception configured in a constructor.
*/
@Override
public void write(final char[] cbuf, final int off, final int len) throws IOException {
- throw exceptionSupplier.get();
+ throw rethrow();
}
}
diff --git a/src/test/java/org/apache/commons/io/BrokenTestFactories.java b/src/test/java/org/apache/commons/io/BrokenTestFactories.java
new file mode 100644
index 00000000..0c483c9b
--- /dev/null
+++ b/src/test/java/org/apache/commons/io/BrokenTestFactories.java
@@ -0,0 +1,50 @@
+/*
+ * 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;
+
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.nio.file.FileSystemNotFoundException;
+import java.util.stream.Stream;
+
+/**
+ * Factory for parameterized tests of BrokenInputStream, BrokenReader, BrokenOutputStream, and BrokenWriter.
+ */
+public class BrokenTestFactories {
+
+ public static final class CustomException extends Exception {
+
+ private static final long serialVersionUID = 1L;
+
+ }
+
+ public static Stream<Class<? extends Throwable>> parameters() {
+ // @formatter:off
+ return Stream.of(
+ IOException.class,
+ FileNotFoundException.class,
+ FileSystemNotFoundException.class,
+ RuntimeException.class,
+ IllegalArgumentException.class,
+ IllegalStateException.class,
+ Error.class,
+ ExceptionInInitializerError.class,
+ CustomException.class
+ );
+ // @formatter:on
+ }
+}
diff --git a/src/test/java/org/apache/commons/io/input/BrokenInputStreamTest.java b/src/test/java/org/apache/commons/io/input/BrokenInputStreamTest.java
index a8b7cea2..8592cce1 100644
--- a/src/test/java/org/apache/commons/io/input/BrokenInputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/input/BrokenInputStreamTest.java
@@ -17,13 +17,11 @@
package org.apache.commons.io.input;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
-import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
-import java.nio.file.FileSystemNotFoundException;
-import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
@@ -34,29 +32,7 @@ import org.junit.jupiter.params.provider.MethodSource;
*/
public class BrokenInputStreamTest {
- static final class CustomeException extends Exception {
-
- private static final long serialVersionUID = 1L;
-
- }
-
- static Stream<Class<? extends Throwable>> parameters() {
- // @formatter:off
- return Stream.of(
- IOException.class,
- FileNotFoundException.class,
- FileSystemNotFoundException.class,
- RuntimeException.class,
- IllegalArgumentException.class,
- IllegalStateException.class,
- Error.class,
- ExceptionInInitializerError.class,
- CustomeException.class
- );
- // @formatter:on
- }
-
- private BrokenInputStream createBrokenInputStream(final Throwable exception) {
+ private static BrokenInputStream createBrokenInputStream(final Throwable exception) {
if (exception instanceof IOException) {
return new BrokenInputStream((IOException) exception);
}
@@ -64,54 +40,59 @@ public class BrokenInputStreamTest {
}
@ParameterizedTest
- @MethodSource("parameters")
+ @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
public void testAvailable(final Class<Exception> clazz) throws Exception {
final Throwable exception = clazz.newInstance();
@SuppressWarnings("resource")
final BrokenInputStream stream = createBrokenInputStream(exception);
- assertEquals(exception, assertThrows(exception.getClass(), () -> stream.available()));
+ assertEquals(exception, assertThrows(clazz, () -> stream.available()));
}
@ParameterizedTest
- @MethodSource("parameters")
+ @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
public void testClose(final Class<Exception> clazz) throws Exception {
final Throwable exception = clazz.newInstance();
@SuppressWarnings("resource")
final BrokenInputStream stream = createBrokenInputStream(exception);
- assertEquals(exception, assertThrows(exception.getClass(), () -> stream.close()));
+ assertEquals(exception, assertThrows(clazz, () -> stream.close()));
+ }
+
+ @Test
+ public void testInstance() {
+ assertNotNull(BrokenInputStream.INSTANCE);
}
@ParameterizedTest
- @MethodSource("parameters")
+ @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
public void testRead(final Class<Exception> clazz) throws Exception {
final Throwable exception = clazz.newInstance();
@SuppressWarnings("resource")
final BrokenInputStream stream = createBrokenInputStream(exception);
- assertEquals(exception, assertThrows(exception.getClass(), () -> stream.read()));
- assertEquals(exception, assertThrows(exception.getClass(), () -> stream.read(new byte[1])));
- assertEquals(exception, assertThrows(exception.getClass(), () -> stream.read(new byte[1], 0, 1)));
+ assertEquals(exception, assertThrows(clazz, () -> stream.read()));
+ assertEquals(exception, assertThrows(clazz, () -> stream.read(new byte[1])));
+ assertEquals(exception, assertThrows(clazz, () -> stream.read(new byte[1], 0, 1)));
}
@ParameterizedTest
- @MethodSource("parameters")
+ @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
public void testReset(final Class<Exception> clazz) throws Exception {
final Throwable exception = clazz.newInstance();
@SuppressWarnings("resource")
final BrokenInputStream stream = createBrokenInputStream(exception);
- assertEquals(exception, assertThrows(exception.getClass(), () -> stream.reset()));
+ assertEquals(exception, assertThrows(clazz, () -> stream.reset()));
}
@ParameterizedTest
- @MethodSource("parameters")
+ @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
public void testSkip(final Class<Exception> clazz) throws Exception {
final Throwable exception = clazz.newInstance();
@SuppressWarnings("resource")
final BrokenInputStream stream = createBrokenInputStream(exception);
- assertEquals(exception, assertThrows(exception.getClass(), () -> stream.skip(1)));
+ assertEquals(exception, assertThrows(clazz, () -> stream.skip(1)));
}
@Test
- public void testTryWithResources() throws Exception {
+ public void testTryWithResources() {
final IOException thrown = assertThrows(IOException.class, () -> {
try (InputStream newStream = new BrokenInputStream()) {
newStream.read();
@@ -124,4 +105,5 @@ public class BrokenInputStreamTest {
assertEquals(IOException.class, suppressed[0].getClass());
assertEquals("Broken input stream", suppressed[0].getMessage());
}
+
}
diff --git a/src/test/java/org/apache/commons/io/input/BrokenReaderTest.java b/src/test/java/org/apache/commons/io/input/BrokenReaderTest.java
index dec1efc9..ecafa751 100644
--- a/src/test/java/org/apache/commons/io/input/BrokenReaderTest.java
+++ b/src/test/java/org/apache/commons/io/input/BrokenReaderTest.java
@@ -23,27 +23,29 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.IOException;
import java.io.Reader;
-import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
/**
* Tests {@link BrokenReader}.
*/
public class BrokenReaderTest {
- private IOException exception;
-
- private Reader brokenReader;
-
- @BeforeEach
- public void setUp() {
- exception = new IOException("test exception");
- brokenReader = new BrokenReader(exception);
+ private static BrokenReader createBrokenReader(final Throwable exception) {
+ if (exception instanceof IOException) {
+ return new BrokenReader((IOException) exception);
+ }
+ return new BrokenReader(exception);
}
- @Test
- public void testClose() {
- assertEquals(exception, assertThrows(IOException.class, () -> brokenReader.close()));
+ @ParameterizedTest
+ @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
+ public void testClose(final Class<Exception> clazz) throws Exception {
+ final Throwable exception = clazz.newInstance();
+ @SuppressWarnings("resource")
+ final BrokenReader brokenReader = createBrokenReader(exception);
+ assertEquals(exception, assertThrows(clazz, () -> brokenReader.close()));
}
@Test
@@ -51,39 +53,67 @@ public class BrokenReaderTest {
assertNotNull(BrokenReader.INSTANCE);
}
- @Test
- public void testMark() {
- assertEquals(exception, assertThrows(IOException.class, () -> brokenReader.mark(1)));
+ @ParameterizedTest
+ @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
+ public void testMark(final Class<Throwable> clazz) throws Exception {
+ final Throwable exception = clazz.newInstance();
+ @SuppressWarnings("resource")
+ final BrokenReader brokenReader = createBrokenReader(exception);
+ assertEquals(exception, assertThrows(clazz, () -> brokenReader.mark(1)));
}
- @Test
- public void testRead() {
- assertEquals(exception, assertThrows(IOException.class, () -> brokenReader.read()));
+ @ParameterizedTest
+ @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
+ public void testRead(final Class<Throwable> clazz) throws Exception {
+ final Throwable exception = clazz.newInstance();
+ @SuppressWarnings("resource")
+ final BrokenReader brokenReader = createBrokenReader(exception);
+ assertEquals(exception, assertThrows(clazz, () -> brokenReader.read()));
}
- @Test
- public void testReadCharArray() {
- assertEquals(exception, assertThrows(IOException.class, () -> brokenReader.read(new char[1])));
+ @ParameterizedTest
+ @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
+ public void testReadCharArray(final Class<Throwable> clazz) throws Exception {
+ final Throwable exception = clazz.newInstance();
+ @SuppressWarnings("resource")
+ final BrokenReader brokenReader = createBrokenReader(exception);
+ assertEquals(exception, assertThrows(clazz, () -> brokenReader.read(new char[1])));
}
- @Test
- public void testReadCharArrayIndexed() {
- assertEquals(exception, assertThrows(IOException.class, () -> brokenReader.read(new char[1], 0, 1)));
+ @ParameterizedTest
+ @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
+ public void testReadCharArrayIndexed(final Class<Throwable> clazz) throws Exception {
+ final Throwable exception = clazz.newInstance();
+ @SuppressWarnings("resource")
+ final BrokenReader brokenReader = createBrokenReader(exception);
+ assertEquals(exception, assertThrows(clazz, () -> brokenReader.read(new char[1], 0, 1)));
}
- @Test
- public void testReady() {
- assertEquals(exception, assertThrows(IOException.class, () -> brokenReader.ready()));
+ @ParameterizedTest
+ @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
+ public void testReady(final Class<Throwable> clazz) throws Exception {
+ final Throwable exception = clazz.newInstance();
+ @SuppressWarnings("resource")
+ final BrokenReader brokenReader = createBrokenReader(exception);
+ assertEquals(exception, assertThrows(clazz, () -> brokenReader.ready()));
}
- @Test
- public void testReset() {
- assertEquals(exception, assertThrows(IOException.class, () -> brokenReader.reset()));
+ @ParameterizedTest
+ @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
+ public void testReset(final Class<Throwable> clazz) throws Exception {
+ final Throwable exception = clazz.newInstance();
+ @SuppressWarnings("resource")
+ final BrokenReader brokenReader = createBrokenReader(exception);
+ assertEquals(exception, assertThrows(clazz, () -> brokenReader.reset()));
}
- @Test
- public void testSkip() {
- assertEquals(exception, assertThrows(IOException.class, () -> brokenReader.skip(1)));
+ @ParameterizedTest
+ @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
+ public void testSkip(final Class<Throwable> clazz) throws Exception {
+ final Throwable exception = clazz.newInstance();
+ @SuppressWarnings("resource")
+ final BrokenReader brokenReader = createBrokenReader(exception);
+ assertEquals(exception, assertThrows(clazz, () -> brokenReader.skip(1)));
}
@Test
diff --git a/src/test/java/org/apache/commons/io/output/BrokenOutputStreamTest.java b/src/test/java/org/apache/commons/io/output/BrokenOutputStreamTest.java
index c392514f..a3a127ba 100644
--- a/src/test/java/org/apache/commons/io/output/BrokenOutputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/output/BrokenOutputStreamTest.java
@@ -17,37 +17,49 @@
package org.apache.commons.io.output;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.IOException;
import java.io.OutputStream;
-import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
/**
* Tests {@link BrokenOutputStream}.
*/
public class BrokenOutputStreamTest {
- private IOException exception;
-
- private OutputStream stream;
+ private static BrokenOutputStream createBrokenOutputStream(final Throwable exception) {
+ if (exception instanceof IOException) {
+ return new BrokenOutputStream((IOException) exception);
+ }
+ return new BrokenOutputStream(exception);
+ }
- @BeforeEach
- public void setUp() {
- exception = new IOException("test exception");
- stream = new BrokenOutputStream(exception);
+ @ParameterizedTest
+ @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
+ public void testClose(final Class<Throwable> clazz) throws Exception {
+ final Throwable exception = clazz.newInstance();
+ @SuppressWarnings("resource")
+ final BrokenOutputStream stream = createBrokenOutputStream(exception);
+ assertEquals(exception, assertThrows(clazz, () -> stream.close()));
}
- @Test
- public void testClose() {
- assertEquals(exception, assertThrows(IOException.class, () -> stream.close()));
+ @ParameterizedTest
+ @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
+ public void testFlush(final Class<Throwable> clazz) throws Exception {
+ final Throwable exception = clazz.newInstance();
+ @SuppressWarnings("resource")
+ final BrokenOutputStream stream = createBrokenOutputStream(exception);
+ assertEquals(exception, assertThrows(clazz, () -> stream.flush()));
}
@Test
- public void testFlush() {
- assertEquals(exception, assertThrows(IOException.class, () -> stream.flush()));
+ public void testInstance() {
+ assertNotNull(BrokenOutputStream.INSTANCE);
}
@Test
@@ -65,19 +77,31 @@ public class BrokenOutputStreamTest {
assertEquals("Broken output stream", suppressed[0].getMessage());
}
- @Test
- public void testWriteByteArray() {
- assertEquals(exception, assertThrows(IOException.class, () -> stream.write(new byte[1])));
+ @ParameterizedTest
+ @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
+ public void testWriteByteArray(final Class<Throwable> clazz) throws Exception {
+ final Throwable exception = clazz.newInstance();
+ @SuppressWarnings("resource")
+ final BrokenOutputStream stream = createBrokenOutputStream(exception);
+ assertEquals(exception, assertThrows(clazz, () -> stream.write(new byte[1])));
}
- @Test
- public void testWriteByteArrayIndexed() {
- assertEquals(exception, assertThrows(IOException.class, () -> stream.write(new byte[1], 0, 1)));
+ @ParameterizedTest
+ @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
+ public void testWriteByteArrayIndexed(final Class<Throwable> clazz) throws Exception {
+ final Throwable exception = clazz.newInstance();
+ @SuppressWarnings("resource")
+ final BrokenOutputStream stream = createBrokenOutputStream(exception);
+ assertEquals(exception, assertThrows(clazz, () -> stream.write(new byte[1], 0, 1)));
}
- @Test
- public void testWriteInt() {
- assertEquals(exception, assertThrows(IOException.class, () -> stream.write(1)));
+ @ParameterizedTest
+ @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
+ public void testWriteInt(final Class<Throwable> clazz) throws Exception {
+ final Throwable exception = clazz.newInstance();
+ @SuppressWarnings("resource")
+ final BrokenOutputStream stream = createBrokenOutputStream(exception);
+ assertEquals(exception, assertThrows(clazz, () -> stream.write(1)));
}
}
diff --git a/src/test/java/org/apache/commons/io/output/BrokenWriterTest.java b/src/test/java/org/apache/commons/io/output/BrokenWriterTest.java
index af2c4212..f9e83e08 100644
--- a/src/test/java/org/apache/commons/io/output/BrokenWriterTest.java
+++ b/src/test/java/org/apache/commons/io/output/BrokenWriterTest.java
@@ -17,71 +17,76 @@
package org.apache.commons.io.output;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.IOException;
import java.io.Writer;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
/**
* Tests {@link BrokenWriter}.
*/
public class BrokenWriterTest {
- private IOException exception;
-
- private Writer brokenWriter;
-
- @BeforeEach
- public void setUp() {
- exception = new IOException("test exception");
- brokenWriter = new BrokenWriter(exception);
- }
-
- @Test
- public void testAppendChar() {
- assertEquals(exception, assertThrows(IOException.class, () -> brokenWriter.append('1')));
+ private static BrokenWriter createBrokenWriter(final Throwable exception) {
+ if (exception instanceof IOException) {
+ return new BrokenWriter((IOException) exception);
+ }
+ return new BrokenWriter(exception);
}
- @Test
- public void testAppendCharSequence() {
- assertEquals(exception, assertThrows(IOException.class, () -> brokenWriter.append("01")));
- }
-
- @Test
- public void testAppendCharSequenceIndexed() {
- assertEquals(exception, assertThrows(IOException.class, () -> brokenWriter.append("01", 0, 1)));
+ @ParameterizedTest
+ @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
+ public void testAppendChar(final Class<Exception> clazz) throws Exception {
+ final Throwable exception = clazz.newInstance();
+ @SuppressWarnings("resource")
+ final BrokenWriter brokenWriter = createBrokenWriter(exception);
+ assertEquals(exception, assertThrows(clazz, () -> brokenWriter.append('1')));
}
- @Test
- public void testClose() {
- assertEquals(exception, assertThrows(IOException.class, () -> brokenWriter.close()));
+ @ParameterizedTest
+ @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
+ public void testAppendCharSequence(final Class<Throwable> clazz) throws Exception {
+ final Throwable exception = clazz.newInstance();
+ @SuppressWarnings("resource")
+ final BrokenWriter brokenWriter = createBrokenWriter(exception);
+ assertEquals(exception, assertThrows(clazz, () -> brokenWriter.append("01")));
}
- @Test
- @Disabled("What should happen here?")
- public void testEquals() {
- assertEquals(exception, assertThrows(IOException.class, () -> brokenWriter.equals(null)));
+ @ParameterizedTest
+ @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
+ public void testAppendCharSequenceIndexed(final Class<Throwable> clazz) throws Exception {
+ final Throwable exception = clazz.newInstance();
+ @SuppressWarnings("resource")
+ final BrokenWriter brokenWriter = createBrokenWriter(exception);
+ assertEquals(exception, assertThrows(clazz, () -> brokenWriter.append("01", 0, 1)));
}
- @Test
- public void testFlush() {
- assertEquals(exception, assertThrows(IOException.class, () -> brokenWriter.flush()));
+ @ParameterizedTest
+ @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
+ public void testClose(final Class<Throwable> clazz) throws Exception {
+ final Throwable exception = clazz.newInstance();
+ @SuppressWarnings("resource")
+ final BrokenWriter brokenWriter = createBrokenWriter(exception);
+ assertEquals(exception, assertThrows(clazz, () -> brokenWriter.close()));
}
- @Test
- @Disabled("What should happen here?")
- public void testHashCode() {
- assertEquals(exception, assertThrows(IOException.class, () -> brokenWriter.hashCode()));
+ @ParameterizedTest
+ @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
+ public void testFlush(final Class<Throwable> clazz) throws Exception {
+ final Throwable exception = clazz.newInstance();
+ @SuppressWarnings("resource")
+ final BrokenWriter brokenWriter = createBrokenWriter(exception);
+ assertEquals(exception, assertThrows(clazz, () -> brokenWriter.flush()));
}
@Test
- @Disabled("What should happen here?")
- public void testToString() {
- assertEquals(exception, assertThrows(IOException.class, () -> brokenWriter.toString()));
+ public void testInstance() {
+ assertNotNull(BrokenWriter.INSTANCE);
}
@Test
@@ -99,29 +104,49 @@ public class BrokenWriterTest {
assertEquals("Broken writer", suppressed[0].getMessage());
}
- @Test
- public void testWriteCharArray() {
- assertEquals(exception, assertThrows(IOException.class, () -> brokenWriter.write(new char[1])));
+ @ParameterizedTest
+ @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
+ public void testWriteCharArray(final Class<Throwable> clazz) throws Exception {
+ final Throwable exception = clazz.newInstance();
+ @SuppressWarnings("resource")
+ final BrokenWriter brokenWriter = createBrokenWriter(exception);
+ assertEquals(exception, assertThrows(clazz, () -> brokenWriter.write(new char[1])));
}
- @Test
- public void testWriteCharArrayIndexed() {
- assertEquals(exception, assertThrows(IOException.class, () -> brokenWriter.write(new char[1], 0, 1)));
+ @ParameterizedTest
+ @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
+ public void testWriteCharArrayIndexed(final Class<Throwable> clazz) throws Exception {
+ final Throwable exception = clazz.newInstance();
+ @SuppressWarnings("resource")
+ final BrokenWriter brokenWriter = createBrokenWriter(exception);
+ assertEquals(exception, assertThrows(clazz, () -> brokenWriter.write(new char[1], 0, 1)));
}
- @Test
- public void testWriteInt() {
- assertEquals(exception, assertThrows(IOException.class, () -> brokenWriter.write(1)));
+ @ParameterizedTest
+ @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
+ public void testWriteInt(final Class<Throwable> clazz) throws Exception {
+ final Throwable exception = clazz.newInstance();
+ @SuppressWarnings("resource")
+ final BrokenWriter brokenWriter = createBrokenWriter(exception);
+ assertEquals(exception, assertThrows(clazz, () -> brokenWriter.write(1)));
}
- @Test
- public void testWriteString() {
- assertEquals(exception, assertThrows(IOException.class, () -> brokenWriter.write("01")));
+ @ParameterizedTest
+ @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
+ public void testWriteString(final Class<Throwable> clazz) throws Exception {
+ final Throwable exception = clazz.newInstance();
+ @SuppressWarnings("resource")
+ final BrokenWriter brokenWriter = createBrokenWriter(exception);
+ assertEquals(exception, assertThrows(clazz, () -> brokenWriter.write("01")));
}
- @Test
- public void testWriteStringIndexed() {
- assertEquals(exception, assertThrows(IOException.class, () -> brokenWriter.write("01", 0, 1)));
+ @ParameterizedTest
+ @MethodSource("org.apache.commons.io.BrokenTestFactories#parameters")
+ public void testWriteStringIndexed(final Class<Throwable> clazz) throws Exception {
+ final Throwable exception = clazz.newInstance();
+ @SuppressWarnings("resource")
+ final BrokenWriter brokenWriter = createBrokenWriter(exception);
+ assertEquals(exception, assertThrows(clazz, () -> brokenWriter.write("01", 0, 1)));
}
}