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;
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  
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.util.Arrays;
26  
27  import org.apache.commons.io.IOUtils;
28  import org.junit.jupiter.api.Test;
29  
30  /**
31   * Tests {@link InfiniteCircularInputStream}.
32   */
33  public class InfiniteCircularInputStreamTest {
34  
35      private void assertStreamOutput(final byte[] toCycle, final byte[] expected) throws IOException {
36          final byte[] actual = new byte[expected.length];
37  
38          try (InputStream infStream = new InfiniteCircularInputStream(toCycle)) {
39              final int actualReadBytes = infStream.read(actual);
40  
41              assertArrayEquals(expected, actual);
42              assertEquals(expected.length, actualReadBytes);
43          }
44      }
45  
46      private InputStream createInputStream(final byte[] repeatContent) {
47          return new InfiniteCircularInputStream(repeatContent);
48      }
49  
50      @Test
51      public void testContainsEofInputSize0() {
52          assertThrows(IllegalArgumentException.class, () -> createInputStream(new byte[] { -1 }));
53      }
54  
55      @Test
56      public void testCount0InputSize0() {
57          assertThrows(IllegalArgumentException.class, () -> createInputStream(new byte[] {}));
58      }
59  
60      @Test
61      public void testCount0InputSize1() throws IOException {
62          try (InputStream in = createInputStream(new byte[] { 1 })) {
63              // empty
64          }
65      }
66  
67      @Test
68      public void testCount1InputSize1() throws IOException {
69          try (InputStream in = createInputStream(new byte[] { 1 })) {
70              assertEquals(1, in.read());
71              assertEquals(1, in.read());
72          }
73      }
74  
75      @Test
76      public void testCycleBytes() throws IOException {
77          final byte[] input = { 1, 2 };
78          final byte[] expected = { 1, 2, 1, 2, 1 };
79  
80          assertStreamOutput(input, expected);
81      }
82  
83      @Test
84      public void testNullInputSize0() {
85          assertThrows(NullPointerException.class, () -> createInputStream(null));
86      }
87  
88      @Test
89      public void testWholeRangeOfBytes() throws IOException {
90          final int size = Byte.MAX_VALUE - Byte.MIN_VALUE + 1;
91          final byte[] contentToCycle = new byte[size];
92          byte value = Byte.MIN_VALUE;
93          for (int i = 0; i < contentToCycle.length; i++) {
94              contentToCycle[i] = value == IOUtils.EOF ? 0 : value;
95              value++;
96          }
97  
98          final byte[] expectedOutput = Arrays.copyOf(contentToCycle, size);
99  
100         assertStreamOutput(contentToCycle, expectedOutput);
101     }
102 
103 }