Skip to content

Commit 3ca717b

Browse files
committed
[IO-867] Fix ThresholdingOutputStream#isThresholdExceeded
Add more assertions
1 parent 341d259 commit 3ca717b

File tree

1 file changed

+58
-27
lines changed

1 file changed

+58
-27
lines changed

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

Lines changed: 58 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package org.apache.commons.io.output;
1919

20+
import static org.junit.jupiter.api.Assertions.assertEquals;
2021
import static org.junit.jupiter.api.Assertions.assertFalse;
2122
import static org.junit.jupiter.api.Assertions.assertThrows;
2223
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -32,12 +33,27 @@
3233
*/
3334
public class ThresholdingOutputStreamTest {
3435

36+
/**
37+
* Asserts initial state without changing it.
38+
*
39+
* @param out the stream to test.
40+
* @param expectedThreshold the expected threshold.
41+
* @param expectedByeCount the expected byte count.
42+
*/
43+
private static void assertInitialState(final ThresholdingOutputStream out, final int expectedThreshold, final int expectedByeCount) {
44+
assertFalse(out.isThresholdExceeded());
45+
assertEquals(expectedThreshold, out.getThreshold());
46+
assertEquals(expectedByeCount, out.getByteCount());
47+
}
48+
3549
@Test
3650
public void testSetByteCountOutputStream() throws Exception {
3751
final AtomicBoolean reached = new AtomicBoolean();
38-
try (ThresholdingOutputStream tos = new ThresholdingOutputStream(3) {
52+
final int initCount = 2;
53+
final int threshold = 3;
54+
try (ThresholdingOutputStream out = new ThresholdingOutputStream(threshold) {
3955
{
40-
setByteCount(2);
56+
setByteCount(initCount);
4157
}
4258

4359
@Override
@@ -50,19 +66,22 @@ protected void thresholdReached() throws IOException {
5066
reached.set(true);
5167
}
5268
}) {
53-
tos.write('a');
69+
assertInitialState(out, threshold, initCount);
70+
out.write('a');
5471
assertFalse(reached.get());
55-
tos.write('a');
72+
out.write('a');
5673
assertTrue(reached.get());
5774
}
5875
}
5976

6077
@Test
6178
public void testSetByteCountStream() throws Exception {
6279
final AtomicBoolean reached = new AtomicBoolean();
63-
try (ThresholdingOutputStream tos = new ThresholdingOutputStream(3) {
80+
final int initCount = 2;
81+
final int threshold = 3;
82+
try (ThresholdingOutputStream out = new ThresholdingOutputStream(threshold) {
6483
{
65-
setByteCount(2);
84+
setByteCount(initCount);
6685
}
6786

6887
@Override
@@ -75,9 +94,10 @@ protected void thresholdReached() throws IOException {
7594
reached.set(true);
7695
}
7796
}) {
78-
tos.write('a');
97+
assertInitialState(out, threshold, initCount);
98+
out.write('a');
7999
assertFalse(reached.get());
80-
tos.write('a');
100+
out.write('a');
81101
assertTrue(reached.get());
82102
}
83103
}
@@ -87,49 +107,57 @@ public void testThresholdIOConsumer() throws Exception {
87107
final AtomicBoolean reached = new AtomicBoolean();
88108
// Null threshold consumer
89109
reached.set(false);
90-
try (ThresholdingOutputStream tos = new ThresholdingOutputStream(1, null,
110+
final int threshold = 1;
111+
try (ThresholdingOutputStream out = new ThresholdingOutputStream(threshold, null,
91112
os -> new ByteArrayOutputStream(4))) {
92-
tos.write('a');
113+
assertInitialState(out, threshold, 0);
114+
out.write('a');
93115
assertFalse(reached.get());
94-
tos.write('a');
116+
out.write('a');
95117
assertFalse(reached.get());
96118
}
97119
// Null output stream function
98120
reached.set(false);
99-
try (ThresholdingOutputStream tos = new ThresholdingOutputStream(1, os -> reached.set(true), null)) {
100-
tos.write('a');
121+
try (ThresholdingOutputStream out = new ThresholdingOutputStream(threshold, os -> reached.set(true), null)) {
122+
assertInitialState(out, threshold, 0);
123+
out.write('a');
101124
assertFalse(reached.get());
102-
tos.write('a');
125+
out.write('a');
103126
assertTrue(reached.get());
104127
}
105128
// non-null inputs.
106129
reached.set(false);
107-
try (ThresholdingOutputStream tos = new ThresholdingOutputStream(1, os -> reached.set(true),
130+
try (ThresholdingOutputStream out = new ThresholdingOutputStream(threshold, os -> reached.set(true),
108131
os -> new ByteArrayOutputStream(4))) {
109-
tos.write('a');
132+
assertInitialState(out, threshold, 0);
133+
out.write('a');
110134
assertFalse(reached.get());
111-
tos.write('a');
135+
out.write('a');
112136
assertTrue(reached.get());
113137
}
114138
}
115139

116140
@Test
117141
public void testThresholdIOConsumerIOException() throws Exception {
118-
try (ThresholdingOutputStream tos = new ThresholdingOutputStream(1, os -> {
142+
final int threshold = 1;
143+
try (ThresholdingOutputStream out = new ThresholdingOutputStream(threshold, os -> {
119144
throw new IOException("Threshold reached.");
120145
}, os -> new ByteArrayOutputStream(4))) {
121-
tos.write('a');
122-
assertThrows(IOException.class, () -> tos.write('a'));
146+
assertInitialState(out, threshold, 0);
147+
out.write('a');
148+
assertThrows(IOException.class, () -> out.write('a'));
123149
}
124150
}
125151

126152
@Test
127153
public void testThresholdIOConsumerUncheckedException() throws Exception {
128-
try (ThresholdingOutputStream tos = new ThresholdingOutputStream(1, os -> {
154+
final int threshold = 1;
155+
try (ThresholdingOutputStream out = new ThresholdingOutputStream(threshold, os -> {
129156
throw new IllegalStateException("Threshold reached.");
130157
}, os -> new ByteArrayOutputStream(4))) {
131-
tos.write('a');
132-
assertThrows(IllegalStateException.class, () -> tos.write('a'));
158+
assertInitialState(out, threshold, 0);
159+
out.write('a');
160+
assertThrows(IllegalStateException.class, () -> out.write('a'));
133161
}
134162
}
135163

@@ -146,6 +174,7 @@ protected void thresholdReached() throws IOException {
146174
reached.set(true);
147175
}
148176
}) {
177+
assertInitialState(out, 0, 0);
149178
assertFalse(reached.get());
150179
out.write(89);
151180
assertTrue(reached.get());
@@ -156,13 +185,14 @@ protected void thresholdReached() throws IOException {
156185
@Test
157186
public void testThresholdZero() throws IOException {
158187
final AtomicBoolean reached = new AtomicBoolean();
159-
try (final ThresholdingOutputStream out = new ThresholdingOutputStream(0) {
188+
final int threshold = 0;
189+
try (final ThresholdingOutputStream out = new ThresholdingOutputStream(threshold) {
160190
@Override
161191
protected void thresholdReached() throws IOException {
162192
reached.set(true);
163193
}
164194
}) {
165-
assertFalse(out.isThresholdExceeded());
195+
assertInitialState(out, threshold, 0);
166196
out.write(89);
167197
assertTrue(reached.get());
168198
assertTrue(out.isThresholdExceeded());
@@ -176,13 +206,14 @@ protected void thresholdReached() throws IOException {
176206
@Test
177207
public void testThresholdZeroWrite() throws IOException {
178208
final AtomicBoolean reached = new AtomicBoolean();
179-
try (final ThresholdingOutputStream out = new ThresholdingOutputStream(7) {
209+
final int threshold = 7;
210+
try (final ThresholdingOutputStream out = new ThresholdingOutputStream(threshold) {
180211
@Override
181212
protected void thresholdReached() throws IOException {
182213
reached.set(true);
183214
}
184215
}) {
185-
assertFalse(out.isThresholdExceeded());
216+
assertInitialState(out, threshold, 0);
186217
assertFalse(reached.get());
187218
out.write(new byte[0]);
188219
assertFalse(out.isThresholdExceeded());

0 commit comments

Comments
 (0)