View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Tests {@link CircularByteBuffer}.
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       * Tests for add function with 3 arguments of type byte[], int and int.
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      // Tests for peek function
80      @Test
81      public void testPeekWithValidArguments() {
82          assertFalse(new CircularByteBuffer().peek(new byte[] { 5, 10, 15, 20, 25 }, 0, 5));
83      }
84  }