1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
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 }