Skip to content

Commit 8a44c3c

Browse files
committed
Add CircularByteBufferTest
Rework of PR #514
1 parent 6b6e350 commit 8a44c3c

2 files changed

Lines changed: 70 additions & 0 deletions

File tree

src/changes/changes.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,12 @@ The <action> type attribute can be add,update,fix,remove.
4848

4949
<body>
5050
<release version="2.15.2" date="202Y-MM-DD" description="Java 8 is required.">
51+
<!-- Fix -->
5152
<action dev="ggregory" type="fix" due-to="Elliotte Rusty Harold">Fix and reenable testSkip_RequiredCharsets #518.</action>
5253
<action dev="ggregory" type="fix" issue="IO-824" due-to="Miguel Munoz, Gary Gregory">SymbolicLineFileFilter documentation fixes.</action>
5354
<action dev="ggregory" type="fix" issue="IO-795" due-to="Miguel Munoz, Gary Gregory">CharSequenceInputStream.reset() only works once #520.</action>
5455
<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>
56+
<action dev="ggregory" type="fix" issue="IO-825" due-to="dkdal, Gary Gregory">Add missing test case CircularByteBufferTest.</action>
5557
</release>
5658
<release version="2.15.1" date="2023-11-24" description="Java 8 is required.">
5759
<!-- FIX -->
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package org.apache.commons.io.input.buffer;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
import static org.junit.jupiter.api.Assertions.assertThrows;
6+
7+
import org.junit.jupiter.api.Test;
8+
9+
/**
10+
* Tests {@link CircularByteBuffer}.
11+
*/
12+
public class CircularByteBufferTest {
13+
14+
@Test
15+
public void testAddInvalidOffset() {
16+
final CircularByteBuffer cbb = new CircularByteBuffer();
17+
assertThrows(IllegalArgumentException.class, () -> cbb.add(new byte[] { 1, 2, 3 }, -1, 3));
18+
}
19+
20+
@Test
21+
public void testAddNegativeLength() {
22+
final CircularByteBuffer cbb = new CircularByteBuffer();
23+
final byte[] targetBuffer = { 1, 2, 3 };
24+
assertThrows(IllegalArgumentException.class, () -> cbb.add(targetBuffer, 0, -1));
25+
}
26+
27+
@Test
28+
public void testAddNullBuffer() {
29+
final CircularByteBuffer cbb = new CircularByteBuffer();
30+
assertThrows(NullPointerException.class, () -> cbb.add(null, 0, 3));
31+
}
32+
33+
/**
34+
* Tests for add function with 3 arguments of type byte[], int and int.
35+
*/
36+
@Test
37+
public void testAddValidData() {
38+
final CircularByteBuffer cbb = new CircularByteBuffer();
39+
final int length = 3;
40+
cbb.add(new byte[] { 3, 6, 9 }, 0, length);
41+
assertEquals(length, cbb.getCurrentNumberOfBytes());
42+
}
43+
44+
@Test
45+
public void testPeekWithExcessiveLength() {
46+
assertFalse(new CircularByteBuffer().peek(new byte[] { 1, 3, 5, 7, 9 }, 0, 6));
47+
}
48+
49+
@Test
50+
public void testPeekWithInvalidOffset() {
51+
final CircularByteBuffer cbb = new CircularByteBuffer();
52+
final IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> cbb.peek(new byte[] { 2, 4, 6, 8, 10 }, -1, 5));
53+
assertEquals("Illegal offset: -1", e.getMessage());
54+
}
55+
56+
@Test
57+
public void testPeekWithNegativeLength() {
58+
final CircularByteBuffer cbb = new CircularByteBuffer();
59+
final IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> cbb.peek(new byte[] { 1, 4, 3 }, 0, -1));
60+
assertEquals("Illegal length: -1", e.getMessage());
61+
}
62+
63+
// Tests for peek function
64+
@Test
65+
public void testPeekWithValidArguments() {
66+
assertFalse(new CircularByteBuffer().peek(new byte[] { 5, 10, 15, 20, 25 }, 0, 5));
67+
}
68+
}

0 commit comments

Comments
 (0)