diff --git a/guava/pom.xml b/guava/pom.xml index ca9e1e528e..e2538d090b 100644 --- a/guava/pom.xml +++ b/guava/pom.xml @@ -59,7 +59,7 @@ - 21.0 + 24.0-jre 3.5 4.1 diff --git a/guava/src/test/java/org/baeldung/guava/GuavaCountingOutputStreamTest.java b/guava/src/test/java/org/baeldung/guava/GuavaCountingOutputStreamTest.java new file mode 100644 index 0000000000..5e96f3597c --- /dev/null +++ b/guava/src/test/java/org/baeldung/guava/GuavaCountingOutputStreamTest.java @@ -0,0 +1,29 @@ +package org.baeldung.guava; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; + +import org.junit.Test; + +import com.google.common.io.CountingOutputStream; + +public class GuavaCountingOutputStreamTest { + public static final int MAX = 5; + + @Test(expected = RuntimeException.class) + public void givenData_whenCountReachesLimit_thenThrowException() throws Exception { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + CountingOutputStream cos = new CountingOutputStream(out); + + byte[] data = new byte[1024]; + ByteArrayInputStream in = new ByteArrayInputStream(data); + + int b; + while ((b = in.read()) != -1) { + cos.write(b); + if (cos.getCount() >= MAX) { + throw new RuntimeException("Write limit reached"); + } + } + } +}