This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-io.git
The following commit(s) were added to refs/heads/master by this push: new 8a44c3c1 Add CircularByteBufferTest 8a44c3c1 is described below commit 8a44c3c13304914d42bb513f3680d81dd6cc4a5d Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Thu Dec 7 11:15:15 2023 -0500 Add CircularByteBufferTest Rework of PR #514 --- src/changes/changes.xml | 2 + .../io/input/buffer/CircularByteBufferTest.java | 68 ++++++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index e8bd0731..829d43cd 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -48,10 +48,12 @@ The <action> type attribute can be add,update,fix,remove. <body> <release version="2.15.2" date="202Y-MM-DD" description="Java 8 is required."> + <!-- Fix --> <action dev="ggregory" type="fix" due-to="Elliotte Rusty Harold">Fix and reenable testSkip_RequiredCharsets #518.</action> <action dev="ggregory" type="fix" issue="IO-824" due-to="Miguel Munoz, Gary Gregory">SymbolicLineFileFilter documentation fixes.</action> <action dev="ggregory" type="fix" issue="IO-795" due-to="Miguel Munoz, Gary Gregory">CharSequenceInputStream.reset() only works once #520.</action> <action dev="ggregory" type="fix" issue="IO-825" due-to="Arthur Chan, Gary Gregory">Add byte array size validation for methods in EndianUtils #521.</action> + <action dev="ggregory" type="fix" issue="IO-825" due-to="dkdal, Gary Gregory">Add missing test case CircularByteBufferTest.</action> </release> <release version="2.15.1" date="2023-11-24" description="Java 8 is required."> <!-- FIX --> diff --git a/src/test/java/org/apache/commons/io/input/buffer/CircularByteBufferTest.java b/src/test/java/org/apache/commons/io/input/buffer/CircularByteBufferTest.java new file mode 100644 index 00000000..673bb055 --- /dev/null +++ b/src/test/java/org/apache/commons/io/input/buffer/CircularByteBufferTest.java @@ -0,0 +1,68 @@ +package org.apache.commons.io.input.buffer; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.Test; + +/** + * Tests {@link CircularByteBuffer}. + */ +public class CircularByteBufferTest { + + @Test + public void testAddInvalidOffset() { + final CircularByteBuffer cbb = new CircularByteBuffer(); + assertThrows(IllegalArgumentException.class, () -> cbb.add(new byte[] { 1, 2, 3 }, -1, 3)); + } + + @Test + public void testAddNegativeLength() { + final CircularByteBuffer cbb = new CircularByteBuffer(); + final byte[] targetBuffer = { 1, 2, 3 }; + assertThrows(IllegalArgumentException.class, () -> cbb.add(targetBuffer, 0, -1)); + } + + @Test + public void testAddNullBuffer() { + final CircularByteBuffer cbb = new CircularByteBuffer(); + assertThrows(NullPointerException.class, () -> cbb.add(null, 0, 3)); + } + + /** + * Tests for add function with 3 arguments of type byte[], int and int. + */ + @Test + public void testAddValidData() { + final CircularByteBuffer cbb = new CircularByteBuffer(); + final int length = 3; + cbb.add(new byte[] { 3, 6, 9 }, 0, length); + assertEquals(length, cbb.getCurrentNumberOfBytes()); + } + + @Test + public void testPeekWithExcessiveLength() { + assertFalse(new CircularByteBuffer().peek(new byte[] { 1, 3, 5, 7, 9 }, 0, 6)); + } + + @Test + public void testPeekWithInvalidOffset() { + final CircularByteBuffer cbb = new CircularByteBuffer(); + final IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> cbb.peek(new byte[] { 2, 4, 6, 8, 10 }, -1, 5)); + assertEquals("Illegal offset: -1", e.getMessage()); + } + + @Test + public void testPeekWithNegativeLength() { + final CircularByteBuffer cbb = new CircularByteBuffer(); + final IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> cbb.peek(new byte[] { 1, 4, 3 }, 0, -1)); + assertEquals("Illegal length: -1", e.getMessage()); + } + + // Tests for peek function + @Test + public void testPeekWithValidArguments() { + assertFalse(new CircularByteBuffer().peek(new byte[] { 5, 10, 15, 20, 25 }, 0, 5)); + } +}