|
| 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