File tree Expand file tree Collapse file tree
main/java/org/baeldung/guava
test/java/org/baeldung/guava Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package org .baeldung .guava ;
2+
3+ public class CountingByteWritter {
4+
5+ }
Original file line number Diff line number Diff line change 11package org .baeldung .guava ;
22
3- import static org .junit .Assert .assertEquals ;
4-
3+ import java .io .ByteArrayInputStream ;
54import java .io .ByteArrayOutputStream ;
65
76import org .junit .Test ;
87
98import com .google .common .io .CountingOutputStream ;
109
1110public class GuavaCountingOutputStreamTest {
11+ public static final int MAX = 5 ;
1212
13- @ Test
14- public void givenData_whenWrittenToStream_thenGetCorrectCount () throws Exception {
13+ @ Test ( expected = RuntimeException . class )
14+ public void givenData_whenCountReachesLimit_thenThrowException () throws Exception {
1515 ByteArrayOutputStream out = new ByteArrayOutputStream ();
1616 CountingOutputStream cos = new CountingOutputStream (out );
17-
18- byte [] data = new byte [10 ];
19-
20- cos .write (data );
21-
22- assertEquals (10 , cos .getCount ());
23-
17+
18+ byte [] data = new byte [1024 ];
19+ ByteArrayInputStream in = new ByteArrayInputStream (data );
20+
21+ while (in .read () != -1 ) {
22+ cos .write (data );
23+ cos .flush ();
24+ if (cos .getCount () >= MAX ) {
25+ throw new RuntimeException ("Write limit reached" );
26+ }
27+ }
2428 }
2529}
You can’t perform that action at this time.
0 commit comments