1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.io.input;
19
20 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
21 import static org.junit.jupiter.api.Assertions.assertEquals;
22 import static org.junit.jupiter.api.Assertions.assertSame;
23
24 import java.io.ByteArrayInputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.util.Arrays;
28
29 import org.junit.jupiter.api.Test;
30
31
32
33
34
35
36 public class ProxyInputStreamTest<T extends ProxyInputStream> {
37
38 private static final class ProxyInputStreamFixture extends ProxyInputStream {
39
40 public ProxyInputStreamFixture(final InputStream proxy) {
41 super(proxy);
42 }
43
44 }
45
46 @SuppressWarnings({ "resource", "unused" })
47 protected T createFixture() throws IOException {
48 return (T) new ProxyInputStreamFixture(createProxySource());
49 }
50
51 protected InputStream createProxySource() {
52 return CharSequenceInputStream.builder().setCharSequence("abc").get();
53 }
54
55 protected void testEos(final T inputStream) {
56
57 }
58
59 @Test
60 public void testRead() throws IOException {
61 try (T inputStream = createFixture()) {
62 int found = inputStream.read();
63 assertEquals('a', found);
64 found = inputStream.read();
65 assertEquals('b', found);
66 found = inputStream.read();
67 assertEquals('c', found);
68 found = inputStream.read();
69 assertEquals(-1, found);
70 testEos(inputStream);
71 }
72 }
73
74 @Test
75 public void testReadArrayAtMiddleFully() throws IOException {
76 try (T inputStream = createFixture()) {
77 final byte[] dest = new byte[5];
78 int found = inputStream.read(dest, 2, 3);
79 assertEquals(3, found);
80 assertArrayEquals(new byte[] { 0, 0, 'a', 'b', 'c' }, dest);
81 found = inputStream.read(dest, 2, 3);
82 assertEquals(-1, found);
83 testEos(inputStream);
84 }
85 }
86
87 @Test
88 public void testReadArrayAtStartFully() throws IOException {
89 try (T inputStream = createFixture()) {
90 final byte[] dest = new byte[5];
91 int found = inputStream.read(dest, 0, 5);
92 assertEquals(3, found);
93 assertArrayEquals(new byte[] { 'a', 'b', 'c', 0, 0 }, dest);
94 found = inputStream.read(dest, 0, 5);
95 assertEquals(-1, found);
96 testEos(inputStream);
97 }
98 }
99
100 @Test
101 public void testReadArrayAtStartPartial() throws IOException {
102 try (T inputStream = createFixture()) {
103 final byte[] dest = new byte[5];
104 int found = inputStream.read(dest, 0, 2);
105 assertEquals(2, found);
106 assertArrayEquals(new byte[] { 'a', 'b', 0, 0, 0 }, dest);
107 Arrays.fill(dest, (byte) 0);
108 found = inputStream.read(dest, 0, 2);
109 assertEquals(1, found);
110 assertArrayEquals(new byte[] { 'c', 0, 0, 0, 0 }, dest);
111 found = inputStream.read(dest, 0, 2);
112 assertEquals(-1, found);
113 testEos(inputStream);
114 }
115 }
116
117 @Test
118 public void testReadArrayFully() throws IOException {
119 try (T inputStream = createFixture()) {
120 final byte[] dest = new byte[5];
121 int found = inputStream.read(dest);
122 assertEquals(3, found);
123 assertArrayEquals(new byte[] { 'a', 'b', 'c', 0, 0 }, dest);
124 found = inputStream.read(dest);
125 assertEquals(-1, found);
126 testEos(inputStream);
127 }
128 }
129
130 @Test
131 public void testReadArrayPartial() throws IOException {
132 try (T inputStream = createFixture()) {
133 final byte[] dest = new byte[2];
134 int found = inputStream.read(dest);
135 assertEquals(2, found);
136 assertArrayEquals(new byte[] { 'a', 'b' }, dest);
137 Arrays.fill(dest, (byte) 0);
138 found = inputStream.read(dest);
139 assertEquals(1, found);
140 assertArrayEquals(new byte[] { 'c', 0 }, dest);
141 found = inputStream.read(dest);
142 assertEquals(-1, found);
143 testEos(inputStream);
144 }
145 }
146
147 @Test
148 public void testReadEof() throws Exception {
149 final ByteArrayInputStream proxy = new ByteArrayInputStream(new byte[2]);
150 try (ProxyInputStream inputStream = new ProxyInputStreamFixture(proxy)) {
151 assertSame(proxy, inputStream.unwrap());
152 int found = inputStream.read();
153 assertEquals(0, found);
154 found = inputStream.read();
155 assertEquals(0, found);
156 found = inputStream.read();
157 assertEquals(-1, found);
158 }
159 }
160
161 }