Skip to content

Commit

Permalink
[IO-867] Fix ThresholdingOutputStream#isThresholdExceeded
Browse files Browse the repository at this point in the history
Add more assertions
  • Loading branch information
garydgregory committed Feb 1, 2025
1 parent 341d259 commit 3ca717b
Showing 1 changed file with 58 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.commons.io.output;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand All @@ -32,12 +33,27 @@
*/
public class ThresholdingOutputStreamTest {

/**
* Asserts initial state without changing it.
*
* @param out the stream to test.
* @param expectedThreshold the expected threshold.
* @param expectedByeCount the expected byte count.
*/
private static void assertInitialState(final ThresholdingOutputStream out, final int expectedThreshold, final int expectedByeCount) {
assertFalse(out.isThresholdExceeded());
assertEquals(expectedThreshold, out.getThreshold());
assertEquals(expectedByeCount, out.getByteCount());
}

@Test
public void testSetByteCountOutputStream() throws Exception {
final AtomicBoolean reached = new AtomicBoolean();
try (ThresholdingOutputStream tos = new ThresholdingOutputStream(3) {
final int initCount = 2;
final int threshold = 3;
try (ThresholdingOutputStream out = new ThresholdingOutputStream(threshold) {
{
setByteCount(2);
setByteCount(initCount);
}

@Override
Expand All @@ -50,19 +66,22 @@ protected void thresholdReached() throws IOException {
reached.set(true);
}
}) {
tos.write('a');
assertInitialState(out, threshold, initCount);
out.write('a');
assertFalse(reached.get());
tos.write('a');
out.write('a');
assertTrue(reached.get());
}
}

@Test
public void testSetByteCountStream() throws Exception {
final AtomicBoolean reached = new AtomicBoolean();
try (ThresholdingOutputStream tos = new ThresholdingOutputStream(3) {
final int initCount = 2;
final int threshold = 3;
try (ThresholdingOutputStream out = new ThresholdingOutputStream(threshold) {
{
setByteCount(2);
setByteCount(initCount);
}

@Override
Expand All @@ -75,9 +94,10 @@ protected void thresholdReached() throws IOException {
reached.set(true);
}
}) {
tos.write('a');
assertInitialState(out, threshold, initCount);
out.write('a');
assertFalse(reached.get());
tos.write('a');
out.write('a');
assertTrue(reached.get());
}
}
Expand All @@ -87,49 +107,57 @@ public void testThresholdIOConsumer() throws Exception {
final AtomicBoolean reached = new AtomicBoolean();
// Null threshold consumer
reached.set(false);
try (ThresholdingOutputStream tos = new ThresholdingOutputStream(1, null,
final int threshold = 1;
try (ThresholdingOutputStream out = new ThresholdingOutputStream(threshold, null,
os -> new ByteArrayOutputStream(4))) {
tos.write('a');
assertInitialState(out, threshold, 0);
out.write('a');
assertFalse(reached.get());
tos.write('a');
out.write('a');
assertFalse(reached.get());
}
// Null output stream function
reached.set(false);
try (ThresholdingOutputStream tos = new ThresholdingOutputStream(1, os -> reached.set(true), null)) {
tos.write('a');
try (ThresholdingOutputStream out = new ThresholdingOutputStream(threshold, os -> reached.set(true), null)) {
assertInitialState(out, threshold, 0);
out.write('a');
assertFalse(reached.get());
tos.write('a');
out.write('a');
assertTrue(reached.get());
}
// non-null inputs.
reached.set(false);
try (ThresholdingOutputStream tos = new ThresholdingOutputStream(1, os -> reached.set(true),
try (ThresholdingOutputStream out = new ThresholdingOutputStream(threshold, os -> reached.set(true),
os -> new ByteArrayOutputStream(4))) {
tos.write('a');
assertInitialState(out, threshold, 0);
out.write('a');
assertFalse(reached.get());
tos.write('a');
out.write('a');
assertTrue(reached.get());
}
}

@Test
public void testThresholdIOConsumerIOException() throws Exception {
try (ThresholdingOutputStream tos = new ThresholdingOutputStream(1, os -> {
final int threshold = 1;
try (ThresholdingOutputStream out = new ThresholdingOutputStream(threshold, os -> {
throw new IOException("Threshold reached.");
}, os -> new ByteArrayOutputStream(4))) {
tos.write('a');
assertThrows(IOException.class, () -> tos.write('a'));
assertInitialState(out, threshold, 0);
out.write('a');
assertThrows(IOException.class, () -> out.write('a'));
}
}

@Test
public void testThresholdIOConsumerUncheckedException() throws Exception {
try (ThresholdingOutputStream tos = new ThresholdingOutputStream(1, os -> {
final int threshold = 1;
try (ThresholdingOutputStream out = new ThresholdingOutputStream(threshold, os -> {
throw new IllegalStateException("Threshold reached.");
}, os -> new ByteArrayOutputStream(4))) {
tos.write('a');
assertThrows(IllegalStateException.class, () -> tos.write('a'));
assertInitialState(out, threshold, 0);
out.write('a');
assertThrows(IllegalStateException.class, () -> out.write('a'));
}
}

Expand All @@ -146,6 +174,7 @@ protected void thresholdReached() throws IOException {
reached.set(true);
}
}) {
assertInitialState(out, 0, 0);
assertFalse(reached.get());
out.write(89);
assertTrue(reached.get());
Expand All @@ -156,13 +185,14 @@ protected void thresholdReached() throws IOException {
@Test
public void testThresholdZero() throws IOException {
final AtomicBoolean reached = new AtomicBoolean();
try (final ThresholdingOutputStream out = new ThresholdingOutputStream(0) {
final int threshold = 0;
try (final ThresholdingOutputStream out = new ThresholdingOutputStream(threshold) {
@Override
protected void thresholdReached() throws IOException {
reached.set(true);
}
}) {
assertFalse(out.isThresholdExceeded());
assertInitialState(out, threshold, 0);
out.write(89);
assertTrue(reached.get());
assertTrue(out.isThresholdExceeded());
Expand All @@ -176,13 +206,14 @@ protected void thresholdReached() throws IOException {
@Test
public void testThresholdZeroWrite() throws IOException {
final AtomicBoolean reached = new AtomicBoolean();
try (final ThresholdingOutputStream out = new ThresholdingOutputStream(7) {
final int threshold = 7;
try (final ThresholdingOutputStream out = new ThresholdingOutputStream(threshold) {
@Override
protected void thresholdReached() throws IOException {
reached.set(true);
}
}) {
assertFalse(out.isThresholdExceeded());
assertInitialState(out, threshold, 0);
assertFalse(reached.get());
out.write(new byte[0]);
assertFalse(out.isThresholdExceeded());
Expand Down

0 comments on commit 3ca717b

Please sign in to comment.