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