You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2024/07/14 23:09:37 UTC
(commons-io) 07/07: Add CircularByteBufferTest.testReadByteArray()
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
commit 5a68e208189ebe1b8a9f2eabea94ae0b8a0d99ca
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Jul 14 18:58:23 2024 -0400
Add CircularByteBufferTest.testReadByteArray()
Add CircularByteBufferTest.testHasSpaceInt()
---
.../io/input/buffer/CircularByteBufferTest.java | 37 ++++++++++++++++++++++
1 file changed, 37 insertions(+)
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
index dacd16fe1..da92e1dee 100644
--- a/src/test/java/org/apache/commons/io/input/buffer/CircularByteBufferTest.java
+++ b/src/test/java/org/apache/commons/io/input/buffer/CircularByteBufferTest.java
@@ -21,6 +21,8 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import java.nio.charset.StandardCharsets;
+
import org.junit.jupiter.api.Test;
/**
@@ -101,6 +103,20 @@ public class CircularByteBufferTest {
assertTrue(cbb.hasSpace());
}
+ @Test
+ public void testHasSpaceInt() {
+ final CircularByteBuffer cbb = new CircularByteBuffer(1);
+ assertTrue(cbb.hasSpace(1));
+ cbb.add((byte) 1);
+ assertFalse(cbb.hasSpace(1));
+ assertEquals(1, cbb.read());
+ assertTrue(cbb.hasSpace(1));
+ cbb.add((byte) 2);
+ assertFalse(cbb.hasSpace(1));
+ assertEquals(2, cbb.read());
+ assertTrue(cbb.hasSpace(1));
+ }
+
@Test
public void testPeekWithExcessiveLength() {
assertFalse(new CircularByteBuffer().peek(new byte[] { 1, 3, 5, 7, 9 }, 0, 6));
@@ -125,4 +141,25 @@ public class CircularByteBufferTest {
public void testPeekWithValidArguments() {
assertFalse(new CircularByteBuffer().peek(new byte[] { 5, 10, 15, 20, 25 }, 0, 5));
}
+
+ @Test
+ public void testReadByteArray() {
+ final CircularByteBuffer cbb = new CircularByteBuffer();
+ final String string = "0123456789";
+ final byte[] bytesIn = string.getBytes(StandardCharsets.UTF_8);
+ cbb.add(bytesIn, 0, 10);
+ final byte[] bytesOut = new byte[10];
+ cbb.read(bytesOut, 0, 10);
+ assertEquals(string, new String(bytesOut, StandardCharsets.UTF_8));
+ }
+
+ @Test
+ public void testReadByteArrayIllegalArgumentException() {
+ final CircularByteBuffer cbb = new CircularByteBuffer();
+ final byte[] bytesOut = new byte[10];
+ // targetOffset < 0
+ assertThrows(IllegalArgumentException.class, () -> cbb.read(bytesOut, -1, 10));
+ // targetOffset >= targetBuffer.length
+ assertThrows(IllegalArgumentException.class, () -> cbb.read(bytesOut, 0, bytesOut.length + 1));
+ }
}