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 2025/02/16 22:01:23 UTC
(commons-io) branch master updated (93309324e -> ce20c6acc)
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git
from 93309324e Fix typos in Javadoc and comments
new 00a39a925 [IO-867] Add ThresholdingOutputStreamTest.testResetByteCount()
new 025850a68 Add BrokenOutputStream.BrokenOutputStream(Function<String>, Throwable>)
new ce20c6acc [IO-867] Add ThresholdingOutputStreamTest.testResetByteCountBrokenOutputStream()
The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
src/changes/changes.xml | 1 +
.../commons/io/output/BrokenOutputStream.java | 37 ++++++++++----
.../commons/io/output/BrokenOutputStreamTest.java | 7 +--
.../io/output/ThresholdingOutputStreamTest.java | 57 ++++++++++++++++++++--
4 files changed, 82 insertions(+), 20 deletions(-)
(commons-io) 02/03: Add BrokenOutputStream.BrokenOutputStream(Function, Throwable>)
Posted by gg...@apache.org.
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
commit 025850a680eab989dcfc0536cfdecc00c57d566e
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Sun Feb 16 16:57:54 2025 -0500
Add BrokenOutputStream.BrokenOutputStream(Function<String>, Throwable>)
Deprecate <String> constructor
---
src/changes/changes.xml | 1 +
.../commons/io/output/BrokenOutputStream.java | 37 ++++++++++++++++------
.../commons/io/output/BrokenOutputStreamTest.java | 7 ++--
3 files changed, 30 insertions(+), 15 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 2589d47d7..4390a3c18 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -67,6 +67,7 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="ggregory" type="add" due-to="Gary Gregory">Add RandomAccessFileInputStream.copy(long, long, OutputStream).</action>
<action dev="ggregory" type="add" due-to="Gary Gregory">Add ProxyOutputStream.Builder.</action>
<action dev="ggregory" type="add" due-to="Gary Gregory">Add ByteOrderMark.matches(int[]).</action>
+ <action dev="ggregory" type="add" due-to="Gary Gregory">Add BrokenOutputStream.BrokenOutputStream(Function<String>, Throwable>) and deprecate Supplier<String> constructor.</action>
<!-- UPDATE -->
<action dev="ggregory" type="update" due-to="Dependabot, Gary Gregory">Bump commons.bytebuddy.version from 1.15.10 to 1.17.0 #710, #715, #720.</action>
<action dev="ggregory" type="update" due-to="Gary Gregory">Bump commons-codec:commons-codec from 1.17.1 to 1.18.0. #717.</action>
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 5b1dad042..726cce7a8 100644
--- a/src/main/java/org/apache/commons/io/output/BrokenOutputStream.java
+++ b/src/main/java/org/apache/commons/io/output/BrokenOutputStream.java
@@ -18,6 +18,7 @@
import java.io.IOException;
import java.io.OutputStream;
+import java.util.function.Function;
import java.util.function.Supplier;
import org.apache.commons.io.function.Erase;
@@ -42,13 +43,13 @@ public class BrokenOutputStream extends OutputStream {
/**
* Supplies the exception that is thrown by all methods of this class.
*/
- private final Supplier<Throwable> exceptionSupplier;
+ private final Function<String, Throwable> exceptionFunction;
/**
* Constructs a new stream that always throws an {@link IOException}.
*/
public BrokenOutputStream() {
- this(() -> new IOException("Broken output stream"));
+ this(m -> new IOException("Broken output stream: " + m));
}
/**
@@ -59,7 +60,20 @@ public BrokenOutputStream() {
*/
@Deprecated
public BrokenOutputStream(final IOException exception) {
- this(() -> exception);
+ this(m -> exception);
+ }
+
+ /**
+ * Constructs a new stream that always throws the supplied exception.
+ * <p>
+ * This class uses the invoked method name as the function input.
+ * </p>
+ *
+ * @param exceptionFunction a supplier for the IOException or RuntimeException to be thrown.
+ * @since 2.19.0
+ */
+ public BrokenOutputStream(final Function<String, Throwable> exceptionFunction) {
+ this.exceptionFunction = exceptionFunction;
}
/**
@@ -67,9 +81,11 @@ public BrokenOutputStream(final IOException exception) {
*
* @param exceptionSupplier a supplier for the IOException or RuntimeException to be thrown.
* @since 2.12.0
+ * @deprecated Use {@link #BrokenOutputStream(Function)}.
*/
+ @Deprecated
public BrokenOutputStream(final Supplier<Throwable> exceptionSupplier) {
- this.exceptionSupplier = exceptionSupplier;
+ this.exceptionFunction = m -> exceptionSupplier.get();
}
/**
@@ -79,7 +95,7 @@ public BrokenOutputStream(final Supplier<Throwable> exceptionSupplier) {
* @since 2.16.0
*/
public BrokenOutputStream(final Throwable exception) {
- this(() -> exception);
+ this(m -> exception);
}
/**
@@ -89,7 +105,7 @@ public BrokenOutputStream(final Throwable exception) {
*/
@Override
public void close() throws IOException {
- throw rethrow();
+ throw rethrow("close()");
}
/**
@@ -99,16 +115,17 @@ public void close() throws IOException {
*/
@Override
public void flush() throws IOException {
- throw rethrow();
+ throw rethrow("flush()");
}
/**
* Throws the configured exception from its supplier.
+ * @param method TODO
*
* @return Throws the configured exception from its supplier.
*/
- private RuntimeException rethrow() {
- return Erase.rethrow(exceptionSupplier.get());
+ private RuntimeException rethrow(final String method) {
+ return Erase.rethrow(exceptionFunction.apply(method));
}
/**
@@ -119,7 +136,7 @@ private RuntimeException rethrow() {
*/
@Override
public void write(final int b) throws IOException {
- throw rethrow();
+ throw rethrow("write(int)");
}
}
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 a3a127baf..b507321f0 100644
--- a/src/test/java/org/apache/commons/io/output/BrokenOutputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/output/BrokenOutputStreamTest.java
@@ -31,7 +31,6 @@
* Tests {@link BrokenOutputStream}.
*/
public class BrokenOutputStreamTest {
-
private static BrokenOutputStream createBrokenOutputStream(final Throwable exception) {
if (exception instanceof IOException) {
return new BrokenOutputStream((IOException) exception);
@@ -69,12 +68,11 @@ public void testTryWithResources() {
newStream.write(1);
}
});
- assertEquals("Broken output stream", thrown.getMessage());
-
+ assertEquals("Broken output stream: write(int)", thrown.getMessage());
final Throwable[] suppressed = thrown.getSuppressed();
assertEquals(1, suppressed.length);
assertEquals(IOException.class, suppressed[0].getClass());
- assertEquals("Broken output stream", suppressed[0].getMessage());
+ assertEquals("Broken output stream: close()", suppressed[0].getMessage());
}
@ParameterizedTest
@@ -103,5 +101,4 @@ public void testWriteInt(final Class<Throwable> clazz) throws Exception {
final BrokenOutputStream stream = createBrokenOutputStream(exception);
assertEquals(exception, assertThrows(clazz, () -> stream.write(1)));
}
-
}
(commons-io) 01/03: [IO-867] Add ThresholdingOutputStreamTest.testResetByteCount()
Posted by gg...@apache.org.
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
commit 00a39a9250167e2ebf951c57f39d19a99bed4dde
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Sun Feb 16 16:32:21 2025 -0500
[IO-867] Add ThresholdingOutputStreamTest.testResetByteCount()
---
.../io/output/ThresholdingOutputStreamTest.java | 22 ++++++++++++++++++++++
1 file changed, 22 insertions(+)
diff --git a/src/test/java/org/apache/commons/io/output/ThresholdingOutputStreamTest.java b/src/test/java/org/apache/commons/io/output/ThresholdingOutputStreamTest.java
index 827ff4c56..20a0b4ade 100644
--- a/src/test/java/org/apache/commons/io/output/ThresholdingOutputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/output/ThresholdingOutputStreamTest.java
@@ -26,6 +26,7 @@
import java.io.IOException;
import java.io.OutputStream;
import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
import org.junit.jupiter.api.Test;
@@ -49,6 +50,27 @@ static void assertThresholdingInitialState(final ThresholdingOutputStream out, f
assertEquals(expectedByeCount, out.getByteCount());
}
+ @Test
+ public void testResetByteCount() throws Exception {
+ final int threshold = 1;
+ final AtomicInteger counter = new AtomicInteger();
+ try (ByteArrayOutputStream os = new ByteArrayOutputStream(); ThresholdingOutputStream out = new ThresholdingOutputStream(threshold, tos -> {
+ counter.incrementAndGet();
+ tos.resetByteCount();
+ }, o -> os)) {
+ assertThresholdingInitialState(out, threshold, 0);
+ assertEquals(0, counter.get());
+ out.write('a');
+ assertFalse(out.isThresholdExceeded());
+ out.write('a');
+ assertEquals(1, counter.get());
+ assertFalse(out.isThresholdExceeded());
+ out.write('a');
+ out.write('a');
+ assertEquals(3, counter.get());
+ }
+ }
+
@Test
public void testSetByteCountOutputStream() throws Exception {
final AtomicBoolean reached = new AtomicBoolean();
(commons-io) 03/03: [IO-867] Add ThresholdingOutputStreamTest.testResetByteCountBrokenOutputStream()
Posted by gg...@apache.org.
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
commit ce20c6acc3f82b73597b2fdde3b24ad42edc3c9b
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Sun Feb 16 17:01:17 2025 -0500
[IO-867] Add
ThresholdingOutputStreamTest.testResetByteCountBrokenOutputStream()
---
.../io/output/ThresholdingOutputStreamTest.java | 37 ++++++++++++++++++----
1 file changed, 31 insertions(+), 6 deletions(-)
diff --git a/src/test/java/org/apache/commons/io/output/ThresholdingOutputStreamTest.java b/src/test/java/org/apache/commons/io/output/ThresholdingOutputStreamTest.java
index 20a0b4ade..6d0c71392 100644
--- a/src/test/java/org/apache/commons/io/output/ThresholdingOutputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/output/ThresholdingOutputStreamTest.java
@@ -51,7 +51,7 @@ static void assertThresholdingInitialState(final ThresholdingOutputStream out, f
}
@Test
- public void testResetByteCount() throws Exception {
+ public void testResetByteCount() throws IOException {
final int threshold = 1;
final AtomicInteger counter = new AtomicInteger();
try (ByteArrayOutputStream os = new ByteArrayOutputStream(); ThresholdingOutputStream out = new ThresholdingOutputStream(threshold, tos -> {
@@ -72,7 +72,32 @@ public void testResetByteCount() throws Exception {
}
@Test
- public void testSetByteCountOutputStream() throws Exception {
+ public void testResetByteCountBrokenOutputStream() throws IOException {
+ final int threshold = 1;
+ final AtomicInteger counter = new AtomicInteger();
+ final IOException e = assertThrows(IOException.class, () -> {
+ try (ByteArrayOutputStream os = new ByteArrayOutputStream(); ThresholdingOutputStream out = new ThresholdingOutputStream(threshold, tos -> {
+ counter.incrementAndGet();
+ tos.resetByteCount();
+ }, o -> BrokenOutputStream.INSTANCE)) {
+ assertThresholdingInitialState(out, threshold, 0);
+ assertEquals(0, counter.get());
+ assertThrows(IOException.class, () -> out.write('a'));
+ assertFalse(out.isThresholdExceeded());
+ assertThrows(IOException.class, () -> out.write('a'));
+ assertEquals(0, counter.get());
+ assertFalse(out.isThresholdExceeded());
+ assertThrows(IOException.class, () -> out.write('a'));
+ assertThrows(IOException.class, () -> out.write('a'));
+ assertEquals(0, counter.get());
+ }
+ });
+ // Should only happen on close
+ assertEquals("Broken output stream: close()", e.getMessage());
+ }
+
+ @Test
+ public void testSetByteCountOutputStream() throws IOException {
final AtomicBoolean reached = new AtomicBoolean();
final int initCount = 2;
final int threshold = 3;
@@ -102,7 +127,7 @@ protected void thresholdReached() throws IOException {
}
@Test
- public void testSetByteCountStream() throws Exception {
+ public void testSetByteCountStream() throws IOException {
final AtomicBoolean reached = new AtomicBoolean();
final int initCount = 2;
final int threshold = 3;
@@ -132,7 +157,7 @@ protected void thresholdReached() throws IOException {
}
@Test
- public void testThresholdIOConsumer() throws Exception {
+ public void testThresholdIOConsumer() throws IOException {
final int threshold = 1;
// Null threshold consumer
try (ThresholdingOutputStream out = new ThresholdingOutputStream(threshold, null,
@@ -170,7 +195,7 @@ public void testThresholdIOConsumer() throws Exception {
}
@Test
- public void testThresholdIOConsumerIOException() throws Exception {
+ public void testThresholdIOConsumerIOException() throws IOException {
final int threshold = 1;
try (ThresholdingOutputStream out = new ThresholdingOutputStream(threshold, os -> {
throw new IOException("Threshold reached.");
@@ -184,7 +209,7 @@ public void testThresholdIOConsumerIOException() throws Exception {
}
@Test
- public void testThresholdIOConsumerUncheckedException() throws Exception {
+ public void testThresholdIOConsumerUncheckedException() throws IOException {
final int threshold = 1;
try (ThresholdingOutputStream out = new ThresholdingOutputStream(threshold, os -> {
throw new IllegalStateException("Threshold reached.");