1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.io.input.buffer;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertFalse;
21 import static org.junit.jupiter.api.Assertions.assertThrows;
22
23 import org.junit.jupiter.api.Test;
24
25
26
27
28 public class CircularByteBufferTest {
29
30 @Test
31 public void testAddInvalidOffset() {
32 final CircularByteBuffer cbb = new CircularByteBuffer();
33 assertThrows(IllegalArgumentException.class, () -> cbb.add(new byte[] { 1, 2, 3 }, -1, 3));
34 }
35
36 @Test
37 public void testAddNegativeLength() {
38 final CircularByteBuffer cbb = new CircularByteBuffer();
39 final byte[] targetBuffer = { 1, 2, 3 };
40 assertThrows(IllegalArgumentException.class, () -> cbb.add(targetBuffer, 0, -1));
41 }
42
43 @Test
44 public void testAddNullBuffer() {
45 final CircularByteBuffer cbb = new CircularByteBuffer();
46 assertThrows(NullPointerException.class, () -> cbb.add(null, 0, 3));
47 }
48
49
50
51
52 @Test
53 public void testAddValidData() {
54 final CircularByteBuffer cbb = new CircularByteBuffer();
55 final int length = 3;
56 cbb.add(new byte[] { 3, 6, 9 }, 0, length);
57 assertEquals(length, cbb.getCurrentNumberOfBytes());
58 }
59
60 @Test
61 public void testPeekWithExcessiveLength() {
62 assertFalse(new CircularByteBuffer().peek(new byte[] { 1, 3, 5, 7, 9 }, 0, 6));
63 }
64
65 @Test
66 public void testPeekWithInvalidOffset() {
67 final CircularByteBuffer cbb = new CircularByteBuffer();
68 final IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> cbb.peek(new byte[] { 2, 4, 6, 8, 10 }, -1, 5));
69 assertEquals("Illegal offset: -1", e.getMessage());
70 }
71
72 @Test
73 public void testPeekWithNegativeLength() {
74 final CircularByteBuffer cbb = new CircularByteBuffer();
75 final IllegalArgumentException e = assertThrows(IllegalArgumentException.class, () -> cbb.peek(new byte[] { 1, 4, 3 }, 0, -1));
76 assertEquals("Illegal length: -1", e.getMessage());
77 }
78
79
80 @Test
81 public void testPeekWithValidArguments() {
82 assertFalse(new CircularByteBuffer().peek(new byte[] { 5, 10, 15, 20, 25 }, 0, 5));
83 }
84 }