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.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.nio.charset.StandardCharsets;
27 import java.nio.file.Files;
28 import java.nio.file.Path;
29
30 import org.apache.commons.io.IOUtils;
31 import org.apache.commons.lang3.StringUtils;
32 import org.junit.jupiter.api.AfterEach;
33 import org.junit.jupiter.api.BeforeEach;
34 import org.junit.jupiter.api.Test;
35
36
37
38
39
40
41
42 public class UnsynchronizedFilterInputStreamTest {
43
44 public static final String DATA = StringUtils.repeat("This is a test.", 500);
45
46 private Path fileName;
47
48 private InputStream is;
49
50 byte[] ibuf = new byte[4096];
51
52
53
54
55
56
57 @SuppressWarnings("resource")
58 @BeforeEach
59 protected void setUp() throws IOException {
60 fileName = Files.createTempFile(getClass().getSimpleName(), ".tst");
61 Files.write(fileName, DATA.getBytes(StandardCharsets.UTF_8));
62 is = UnsynchronizedFilterInputStream.builder().setInputStream(Files.newInputStream(fileName)).get();
63 }
64
65
66
67
68
69
70 @AfterEach
71 protected void tearDown() throws IOException {
72 IOUtils.closeQuietly(is);
73 Files.deleteIfExists(fileName);
74 }
75
76
77
78
79
80
81 @Test
82 public void test_available() throws IOException {
83 assertEquals(DATA.length(), is.available(), "Returned incorrect number of available bytes");
84 }
85
86
87
88
89
90
91 @Test
92 public void test_close() throws IOException {
93 is.close();
94 assertThrows(IOException.class, () -> is.read(), "Able to read from closed stream");
95 }
96
97
98
99
100 @Test
101 public void test_markI() {
102 assertTrue(true, "Mark not supported by parent InputStream");
103 }
104
105
106
107
108 @Test
109 public void test_markSupported() {
110 assertTrue(!is.markSupported(), "markSupported returned true");
111 }
112
113
114
115
116
117
118 @Test
119 public void test_read() throws IOException {
120 final int c = is.read();
121 assertEquals(DATA.charAt(0), c, "read returned incorrect char");
122 }
123
124
125
126
127
128
129 @Test
130 public void test_read$B() throws IOException {
131 final byte[] buf1 = new byte[100];
132 assertEquals(buf1.length, is.read(buf1));
133 assertTrue(new String(buf1, 0, buf1.length, StandardCharsets.UTF_8).equals(DATA.substring(0, 100)), "Failed to read correct data");
134 }
135
136
137
138
139
140
141 @Test
142 public void test_read$BII() throws IOException {
143 final byte[] buf1 = new byte[100];
144 is.skip(3000);
145 is.mark(1000);
146 is.read(buf1, 0, buf1.length);
147 assertTrue(new String(buf1, 0, buf1.length, StandardCharsets.UTF_8).equals(DATA.substring(3000, 3100)), "Failed to read correct data");
148 }
149
150
151
152
153 @Test
154 public void test_reset() {
155 assertThrows(IOException.class, () -> is.reset(), "should throw IOException");
156
157 }
158
159
160
161
162
163
164 @Test
165 public void test_skipJ() throws IOException {
166 final byte[] buf1 = new byte[10];
167 is.skip(1000);
168 is.read(buf1, 0, buf1.length);
169 assertTrue(new String(buf1, 0, buf1.length, StandardCharsets.UTF_8).equals(DATA.substring(1000, 1010)), "Failed to skip to correct position");
170 }
171 }