1919
2020import static org .junit .jupiter .api .Assertions .assertEquals ;
2121import static org .junit .jupiter .api .Assertions .assertFalse ;
22+ import static org .junit .jupiter .api .Assertions .assertInstanceOf ;
2223import static org .junit .jupiter .api .Assertions .assertThrows ;
2324import static org .junit .jupiter .api .Assertions .assertTrue ;
2425
@@ -71,8 +72,10 @@ protected void thresholdReached() throws IOException {
7172 assertThresholdingInitialState (out , threshold , initCount );
7273 out .write ('a' );
7374 assertFalse (reached .get ());
75+ assertFalse (out .isThresholdExceeded ());
7476 out .write ('a' );
7577 assertTrue (reached .get ());
78+ assertTrue (out .isThresholdExceeded ());
7679 }
7780 }
7881
@@ -99,33 +102,36 @@ protected void thresholdReached() throws IOException {
99102 assertThresholdingInitialState (out , threshold , initCount );
100103 out .write ('a' );
101104 assertFalse (reached .get ());
105+ assertFalse (out .isThresholdExceeded ());
102106 out .write ('a' );
103107 assertTrue (reached .get ());
108+ assertTrue (out .isThresholdExceeded ());
104109 }
105110 }
106111
107112 @ Test
108113 public void testThresholdIOConsumer () throws Exception {
109- final AtomicBoolean reached = new AtomicBoolean ();
110- // Null threshold consumer
111- reached .set (false );
112114 final int threshold = 1 ;
115+ // Null threshold consumer
113116 try (ThresholdingOutputStream out = new ThresholdingOutputStream (threshold , null ,
114117 os -> new ByteArrayOutputStream (4 ))) {
115118 assertThresholdingInitialState (out , threshold , 0 );
116119 out .write ('a' );
117- assertFalse (reached . get ());
120+ assertFalse (out . isThresholdExceeded ());
118121 out .write ('a' );
119- assertFalse ( reached . get ());
122+ assertTrue ( out . isThresholdExceeded ());
120123 }
121124 // Null output stream function
125+ final AtomicBoolean reached = new AtomicBoolean ();
122126 reached .set (false );
123127 try (ThresholdingOutputStream out = new ThresholdingOutputStream (threshold , os -> reached .set (true ), null )) {
124128 assertThresholdingInitialState (out , threshold , 0 );
125129 out .write ('a' );
126130 assertFalse (reached .get ());
131+ assertFalse (out .isThresholdExceeded ());
127132 out .write ('a' );
128133 assertTrue (reached .get ());
134+ assertTrue (out .isThresholdExceeded ());
129135 }
130136 // non-null inputs.
131137 reached .set (false );
@@ -134,8 +140,10 @@ public void testThresholdIOConsumer() throws Exception {
134140 assertThresholdingInitialState (out , threshold , 0 );
135141 out .write ('a' );
136142 assertFalse (reached .get ());
143+ assertFalse (out .isThresholdExceeded ());
137144 out .write ('a' );
138145 assertTrue (reached .get ());
146+ assertTrue (out .isThresholdExceeded ());
139147 }
140148 }
141149
@@ -147,7 +155,9 @@ public void testThresholdIOConsumerIOException() throws Exception {
147155 }, os -> new ByteArrayOutputStream (4 ))) {
148156 assertThresholdingInitialState (out , threshold , 0 );
149157 out .write ('a' );
158+ assertFalse (out .isThresholdExceeded ());
150159 assertThrows (IOException .class , () -> out .write ('a' ));
160+ assertFalse (out .isThresholdExceeded ());
151161 }
152162 }
153163
@@ -159,7 +169,11 @@ public void testThresholdIOConsumerUncheckedException() throws Exception {
159169 }, os -> new ByteArrayOutputStream (4 ))) {
160170 assertThresholdingInitialState (out , threshold , 0 );
161171 out .write ('a' );
172+ assertFalse (out .isThresholdExceeded ());
162173 assertThrows (IllegalStateException .class , () -> out .write ('a' ));
174+ assertFalse (out .isThresholdExceeded ());
175+ assertInstanceOf (ByteArrayOutputStream .class , out .getOutputStream ());
176+ assertFalse (out .isThresholdExceeded ());
163177 }
164178 }
165179
@@ -181,6 +195,8 @@ protected void thresholdReached() throws IOException {
181195 out .write (89 );
182196 assertTrue (reached .get ());
183197 assertTrue (out .isThresholdExceeded ());
198+ assertInstanceOf (NullOutputStream .class , out .getOutputStream ());
199+ assertTrue (out .isThresholdExceeded ());
184200 }
185201 }
186202
@@ -198,6 +214,8 @@ protected void thresholdReached() throws IOException {
198214 out .write (89 );
199215 assertTrue (reached .get ());
200216 assertTrue (out .isThresholdExceeded ());
217+ assertInstanceOf (NullOutputStream .class , out .getOutputStream ());
218+ assertTrue (out .isThresholdExceeded ());
201219 }
202220 }
203221
@@ -212,6 +230,7 @@ public void testThresholdZeroWrite() throws IOException {
212230 try (final ThresholdingOutputStream out = new ThresholdingOutputStream (threshold ) {
213231 @ Override
214232 protected void thresholdReached () throws IOException {
233+ super .thresholdReached ();
215234 reached .set (true );
216235 }
217236 }) {
@@ -220,6 +239,8 @@ protected void thresholdReached() throws IOException {
220239 out .write (new byte [0 ]);
221240 assertFalse (out .isThresholdExceeded ());
222241 assertFalse (reached .get ());
242+ assertInstanceOf (NullOutputStream .class , out .getOutputStream ());
243+ assertFalse (out .isThresholdExceeded ());
223244 }
224245 }
225246}
0 commit comments