Skip to content

Commit f7bd5f9

Browse files
committed
Add missing test
1 parent 1f8fb97 commit f7bd5f9

1 file changed

Lines changed: 144 additions & 0 deletions

File tree

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
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+
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+
23+
import java.io.ByteArrayInputStream;
24+
import java.io.IOException;
25+
import java.io.InputStream;
26+
import java.util.Arrays;
27+
28+
import org.junit.jupiter.api.Test;
29+
30+
/**
31+
* Tests {@link ProxyInputStream}.
32+
*/
33+
public class ProxyInputStreamTest {
34+
35+
private static final class ProxyInputStreamFixture extends ProxyInputStream {
36+
37+
public ProxyInputStreamFixture(final InputStream proxy) {
38+
super(proxy);
39+
}
40+
41+
}
42+
43+
@Test
44+
public void testRead() throws IOException {
45+
try (CharSequenceInputStream proxy = CharSequenceInputStream.builder().setCharSequence("abc").get();
46+
ProxyInputStream inputStream = new ProxyInputStreamFixture(proxy)) {
47+
int found = inputStream.read();
48+
assertEquals('a', found);
49+
found = inputStream.read();
50+
assertEquals('b', found);
51+
found = inputStream.read();
52+
assertEquals('c', found);
53+
found = inputStream.read();
54+
assertEquals(-1, found);
55+
}
56+
}
57+
58+
@Test
59+
public void testReadArrayAtMiddleFully() throws IOException {
60+
try (CharSequenceInputStream proxy = CharSequenceInputStream.builder().setCharSequence("abc").get();
61+
ProxyInputStream inputStream = new ProxyInputStreamFixture(proxy)) {
62+
final byte[] dest = new byte[5];
63+
int found = inputStream.read(dest, 2, 3);
64+
assertEquals(3, found);
65+
assertArrayEquals(new byte[] { 0, 0, 'a', 'b', 'c' }, dest);
66+
found = inputStream.read(dest, 2, 3);
67+
assertEquals(-1, found);
68+
}
69+
}
70+
71+
@Test
72+
public void testReadArrayAtStartFully() throws IOException {
73+
try (CharSequenceInputStream proxy = CharSequenceInputStream.builder().setCharSequence("abc").get();
74+
ProxyInputStream inputStream = new ProxyInputStreamFixture(proxy)) {
75+
final byte[] dest = new byte[5];
76+
int found = inputStream.read(dest, 0, 5);
77+
assertEquals(3, found);
78+
assertArrayEquals(new byte[] { 'a', 'b', 'c', 0, 0 }, dest);
79+
found = inputStream.read(dest, 0, 5);
80+
assertEquals(-1, found);
81+
}
82+
}
83+
84+
@Test
85+
public void testReadArrayAtStartPartial() throws IOException {
86+
try (CharSequenceInputStream proxy = CharSequenceInputStream.builder().setCharSequence("abc").get();
87+
ProxyInputStream inputStream = new ProxyInputStreamFixture(proxy)) {
88+
final byte[] dest = new byte[5];
89+
int found = inputStream.read(dest, 0, 2);
90+
assertEquals(2, found);
91+
assertArrayEquals(new byte[] { 'a', 'b', 0, 0, 0 }, dest);
92+
Arrays.fill(dest, (byte) 0);
93+
found = inputStream.read(dest, 0, 2);
94+
assertEquals(1, found);
95+
assertArrayEquals(new byte[] { 'c', 0, 0, 0, 0 }, dest);
96+
found = inputStream.read(dest, 0, 2);
97+
assertEquals(-1, found);
98+
}
99+
}
100+
101+
@Test
102+
public void testReadArrayFully() throws IOException {
103+
try (CharSequenceInputStream proxy = CharSequenceInputStream.builder().setCharSequence("abc").get();
104+
ProxyInputStream inputStream = new ProxyInputStreamFixture(proxy)) {
105+
final byte[] dest = new byte[5];
106+
int found = inputStream.read(dest);
107+
assertEquals(3, found);
108+
assertArrayEquals(new byte[] { 'a', 'b', 'c', 0, 0 }, dest);
109+
found = inputStream.read(dest);
110+
assertEquals(-1, found);
111+
}
112+
}
113+
114+
@Test
115+
public void testReadArrayPartial() throws IOException {
116+
try (CharSequenceInputStream proxy = CharSequenceInputStream.builder().setCharSequence("abc").get();
117+
ProxyInputStream inputStream = new ProxyInputStreamFixture(proxy)) {
118+
final byte[] dest = new byte[2];
119+
int found = inputStream.read(dest);
120+
assertEquals(2, found);
121+
assertArrayEquals(new byte[] { 'a', 'b' }, dest);
122+
Arrays.fill(dest, (byte) 0);
123+
found = inputStream.read(dest);
124+
assertEquals(1, found);
125+
assertArrayEquals(new byte[] { 'c', 0 }, dest);
126+
found = inputStream.read(dest);
127+
assertEquals(-1, found);
128+
}
129+
}
130+
131+
@Test
132+
public void testReadEof() throws Exception {
133+
final ByteArrayInputStream proxy = new ByteArrayInputStream(new byte[2]);
134+
try (ProxyInputStream inputStream = new ProxyInputStreamFixture(proxy)) {
135+
int found = inputStream.read();
136+
assertEquals(0, found);
137+
found = inputStream.read();
138+
assertEquals(0, found);
139+
found = inputStream.read();
140+
assertEquals(-1, found);
141+
}
142+
}
143+
144+
}

0 commit comments

Comments
 (0)