Skip to content

Commit ce20c6a

Browse files
committed
[IO-867] Add
ThresholdingOutputStreamTest.testResetByteCountBrokenOutputStream()
1 parent 025850a commit ce20c6a

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

src/test/java/org/apache/commons/io/output/ThresholdingOutputStreamTest.java

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ static void assertThresholdingInitialState(final ThresholdingOutputStream out, f
5151
}
5252

5353
@Test
54-
public void testResetByteCount() throws Exception {
54+
public void testResetByteCount() throws IOException {
5555
final int threshold = 1;
5656
final AtomicInteger counter = new AtomicInteger();
5757
try (ByteArrayOutputStream os = new ByteArrayOutputStream(); ThresholdingOutputStream out = new ThresholdingOutputStream(threshold, tos -> {
@@ -72,7 +72,32 @@ public void testResetByteCount() throws Exception {
7272
}
7373

7474
@Test
75-
public void testSetByteCountOutputStream() throws Exception {
75+
public void testResetByteCountBrokenOutputStream() throws IOException {
76+
final int threshold = 1;
77+
final AtomicInteger counter = new AtomicInteger();
78+
final IOException e = assertThrows(IOException.class, () -> {
79+
try (ByteArrayOutputStream os = new ByteArrayOutputStream(); ThresholdingOutputStream out = new ThresholdingOutputStream(threshold, tos -> {
80+
counter.incrementAndGet();
81+
tos.resetByteCount();
82+
}, o -> BrokenOutputStream.INSTANCE)) {
83+
assertThresholdingInitialState(out, threshold, 0);
84+
assertEquals(0, counter.get());
85+
assertThrows(IOException.class, () -> out.write('a'));
86+
assertFalse(out.isThresholdExceeded());
87+
assertThrows(IOException.class, () -> out.write('a'));
88+
assertEquals(0, counter.get());
89+
assertFalse(out.isThresholdExceeded());
90+
assertThrows(IOException.class, () -> out.write('a'));
91+
assertThrows(IOException.class, () -> out.write('a'));
92+
assertEquals(0, counter.get());
93+
}
94+
});
95+
// Should only happen on close
96+
assertEquals("Broken output stream: close()", e.getMessage());
97+
}
98+
99+
@Test
100+
public void testSetByteCountOutputStream() throws IOException {
76101
final AtomicBoolean reached = new AtomicBoolean();
77102
final int initCount = 2;
78103
final int threshold = 3;
@@ -102,7 +127,7 @@ protected void thresholdReached() throws IOException {
102127
}
103128

104129
@Test
105-
public void testSetByteCountStream() throws Exception {
130+
public void testSetByteCountStream() throws IOException {
106131
final AtomicBoolean reached = new AtomicBoolean();
107132
final int initCount = 2;
108133
final int threshold = 3;
@@ -132,7 +157,7 @@ protected void thresholdReached() throws IOException {
132157
}
133158

134159
@Test
135-
public void testThresholdIOConsumer() throws Exception {
160+
public void testThresholdIOConsumer() throws IOException {
136161
final int threshold = 1;
137162
// Null threshold consumer
138163
try (ThresholdingOutputStream out = new ThresholdingOutputStream(threshold, null,
@@ -170,7 +195,7 @@ public void testThresholdIOConsumer() throws Exception {
170195
}
171196

172197
@Test
173-
public void testThresholdIOConsumerIOException() throws Exception {
198+
public void testThresholdIOConsumerIOException() throws IOException {
174199
final int threshold = 1;
175200
try (ThresholdingOutputStream out = new ThresholdingOutputStream(threshold, os -> {
176201
throw new IOException("Threshold reached.");
@@ -184,7 +209,7 @@ public void testThresholdIOConsumerIOException() throws Exception {
184209
}
185210

186211
@Test
187-
public void testThresholdIOConsumerUncheckedException() throws Exception {
212+
public void testThresholdIOConsumerUncheckedException() throws IOException {
188213
final int threshold = 1;
189214
try (ThresholdingOutputStream out = new ThresholdingOutputStream(threshold, os -> {
190215
throw new IllegalStateException("Threshold reached.");

0 commit comments

Comments
 (0)