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    *      https://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;
18  
19  import static org.junit.jupiter.api.Assertions.assertArrayEquals;
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.util.Arrays;
27  
28  import org.apache.commons.io.IOUtils;
29  import org.junit.jupiter.api.Test;
30  
31  /**
32   * Tests {@link CircularInputStream}.
33   */
34  class CircularInputStreamTest {
35  
36      private void assertStreamOutput(final byte[] toCycle, final byte[] expected) throws IOException {
37          final byte[] actual = new byte[expected.length];
38  
39          try (InputStream inputStream = createInputStream(toCycle, -1)) {
40              final int actualReadBytes = inputStream.read(actual);
41              assertArrayEquals(expected, actual);
42              assertEquals(expected.length, actualReadBytes);
43          }
44      }
45  
46      private InputStream createInputStream(final byte[] repeatContent, final long targetByteCount) {
47          return new CircularInputStream(repeatContent, targetByteCount);
48      }
49  
50      @SuppressWarnings("resource")
51      @Test
52      void testAvailableAfterClose() throws Exception {
53          final InputStream shadow;
54          try (InputStream in = createInputStream(new byte[] { 1, 2 }, 4)) {
55              assertTrue(in.available() > 0);
56              assertEquals(1, in.read());
57              assertEquals(2, in.read());
58              assertEquals(1, in.read());
59              shadow = in;
60          }
61          assertEquals(0, shadow.available());
62      }
63  
64      @Test
65      void testAvailableAfterOpen() throws Exception {
66          try (InputStream in = createInputStream(new byte[] { 1, 2 }, 1)) {
67              assertTrue(in.available() > 0);
68              assertEquals(1, in.read());
69              assertTrue(in.available() > 0);
70          }
71      }
72  
73      @Test
74      void testContainsEofInputSize0() {
75          assertThrows(IllegalArgumentException.class, () -> createInputStream(new byte[] { -1 }, 0));
76      }
77  
78      @Test
79      void testCount0() throws IOException {
80          try (InputStream in = createInputStream(new byte[] { 1, 2 }, 0)) {
81              assertEquals(IOUtils.EOF, in.read());
82          }
83      }
84  
85      @Test
86      void testCount0InputSize0() {
87          assertThrows(IllegalArgumentException.class, () -> createInputStream(new byte[] {}, 0));
88      }
89  
90      @Test
91      void testCount0InputSize1() throws IOException {
92          try (InputStream in = createInputStream(new byte[] { 1 }, 0)) {
93              assertEquals(IOUtils.EOF, in.read());
94          }
95      }
96  
97      @Test
98      void testCount1InputSize1() throws IOException {
99          try (InputStream in = createInputStream(new byte[] { 1 }, 1)) {
100             assertEquals(1, in.read());
101             assertEquals(IOUtils.EOF, in.read());
102         }
103     }
104 
105     @Test
106     void testCycleBytes() throws IOException {
107         final byte[] input = { 1, 2 };
108         final byte[] expected = { 1, 2, 1, 2, 1 };
109         assertStreamOutput(input, expected);
110     }
111 
112     @Test
113     void testNullInputSize0() {
114         assertThrows(NullPointerException.class, () -> createInputStream(null, 0));
115     }
116 
117     @SuppressWarnings("resource")
118     @Test
119     void testReaderAfterClose() throws Exception {
120         final InputStream shadow;
121         try (InputStream in = createInputStream(new byte[] { 1, 2 }, 4)) {
122             assertTrue(in.available() > 0);
123             assertEquals(1, in.read());
124             assertEquals(2, in.read());
125             assertEquals(1, in.read());
126             shadow = in;
127         }
128         assertEquals(IOUtils.EOF, shadow.read());
129     }
130 
131     @Test
132     void testWholeRangeOfBytes() throws IOException {
133         final int size = Byte.MAX_VALUE - Byte.MIN_VALUE + 1;
134         final byte[] contentToCycle = new byte[size];
135         byte value = Byte.MIN_VALUE;
136         for (int i = 0; i < contentToCycle.length; i++) {
137             contentToCycle[i] = value == IOUtils.EOF ? 0 : value;
138             value++;
139         }
140         final byte[] expectedOutput = Arrays.copyOf(contentToCycle, size);
141         assertStreamOutput(contentToCycle, expectedOutput);
142     }
143 
144 }