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.juneau.commons.io;
18  
19  import static org.junit.jupiter.api.Assertions.*;
20  
21  import java.io.*;
22  import java.nio.charset.*;
23  
24  import org.apache.juneau.*;
25  import org.junit.jupiter.api.*;
26  
27  class ReaderInputStream_Test extends TestBase {
28  
29  	//====================================================================================================
30  	// Constructor tests
31  	//====================================================================================================
32  	@Test void a01_constructorWithReaderAndCharset() throws IOException {
33  		var reader = new StringReader("test");
34  		var is = new ReaderInputStream(reader, StandardCharsets.UTF_8);
35  		var bytes = is.readAllBytes();
36  		assertEquals("test", new String(bytes, StandardCharsets.UTF_8));
37  		is.close();
38  	}
39  
40  	@Test void a02_constructorWithReaderAndCharsetName() throws IOException {
41  		var reader = new StringReader("test");
42  		var is = new ReaderInputStream(reader, "UTF-8");
43  		var bytes = is.readAllBytes();
44  		assertEquals("test", new String(bytes, StandardCharsets.UTF_8));
45  		is.close();
46  	}
47  
48  	@Test void a03_constructorWithReaderAndCharsetAndBufferSize() throws IOException {
49  		var reader = new StringReader("test");
50  		var is = new ReaderInputStream(reader, StandardCharsets.UTF_8, 512);
51  		var bytes = is.readAllBytes();
52  		assertEquals("test", new String(bytes, StandardCharsets.UTF_8));
53  		is.close();
54  	}
55  
56  	@Test void a04_constructorWithReaderAndCharsetNameAndBufferSize() throws IOException {
57  		var reader = new StringReader("test");
58  		var is = new ReaderInputStream(reader, "UTF-8", 512);
59  		var bytes = is.readAllBytes();
60  		assertEquals("test", new String(bytes, StandardCharsets.UTF_8));
61  		is.close();
62  	}
63  
64  	@Test void a05_constructorWithReaderAndEncoder() throws IOException {
65  		var reader = new StringReader("test");
66  		var encoder = StandardCharsets.UTF_8.newEncoder();
67  		var is = new ReaderInputStream(reader, encoder);
68  		var bytes = is.readAllBytes();
69  		assertEquals("test", new String(bytes, StandardCharsets.UTF_8));
70  		is.close();
71  	}
72  
73  	@Test void a06_constructorWithReaderAndEncoderAndBufferSize() throws IOException {
74  		var reader = new StringReader("test");
75  		var encoder = StandardCharsets.UTF_8.newEncoder();
76  		var is = new ReaderInputStream(reader, encoder, 512);
77  		var bytes = is.readAllBytes();
78  		assertEquals("test", new String(bytes, StandardCharsets.UTF_8));
79  		is.close();
80  	}
81  
82  	@Test void a07_constructorWithReaderAndEncoderAndBufferSize_zero() {
83  		var reader = new StringReader("test");
84  		var encoder = StandardCharsets.UTF_8.newEncoder();
85  		assertThrows(IllegalArgumentException.class, () -> {
86  			new ReaderInputStream(reader, encoder, 0);
87  		});
88  	}
89  
90  	@Test void a08_constructorWithReaderAndEncoderAndBufferSize_negative() {
91  		var reader = new StringReader("test");
92  		var encoder = StandardCharsets.UTF_8.newEncoder();
93  		assertThrows(IllegalArgumentException.class, () -> {
94  			new ReaderInputStream(reader, encoder, -1);
95  		});
96  	}
97  
98  	@Test void a09_constructorWithReaderAndCharsetAndBufferSize_zero() {
99  		var reader = new StringReader("test");
100 		assertThrows(IllegalArgumentException.class, () -> {
101 			new ReaderInputStream(reader, StandardCharsets.UTF_8, 0);
102 		});
103 	}
104 
105 	@Test void a10_constructorWithReaderAndCharsetAndBufferSize_negative() {
106 		var reader = new StringReader("test");
107 		assertThrows(IllegalArgumentException.class, () -> {
108 			new ReaderInputStream(reader, StandardCharsets.UTF_8, -1);
109 		});
110 	}
111 
112 	@Test void a11_constructorWithReaderAndCharsetNameAndBufferSize_zero() {
113 		var reader = new StringReader("test");
114 		assertThrows(IllegalArgumentException.class, () -> {
115 			new ReaderInputStream(reader, "UTF-8", 0);
116 		});
117 	}
118 
119 	@Test void a12_constructorWithReaderAndCharsetNameAndBufferSize_negative() {
120 		var reader = new StringReader("test");
121 		assertThrows(IllegalArgumentException.class, () -> {
122 			new ReaderInputStream(reader, "UTF-8", -1);
123 		});
124 	}
125 
126 	//====================================================================================================
127 	// read() tests
128 	//====================================================================================================
129 	@Test void b01_readSingleByte() throws IOException {
130 		var reader = new StringReader("ABC");
131 		var is = new ReaderInputStream(reader, StandardCharsets.UTF_8);
132 		assertEquals('A', is.read());
133 		assertEquals('B', is.read());
134 		assertEquals('C', is.read());
135 		assertEquals(-1, is.read());
136 		is.close();
137 	}
138 
139 	@Test void b02_readEndOfStream() throws IOException {
140 		var reader = new StringReader("");
141 		var is = new ReaderInputStream(reader, StandardCharsets.UTF_8);
142 		assertEquals(-1, is.read());
143 		assertEquals(-1, is.read()); // Should continue returning -1
144 		is.close();
145 	}
146 
147 	//====================================================================================================
148 	// read(byte[]) tests
149 	//====================================================================================================
150 	@Test void c01_readByteArray() throws IOException {
151 		var reader = new StringReader("test");
152 		var is = new ReaderInputStream(reader, StandardCharsets.UTF_8);
153 		var buf = new byte[100];
154 		var count = is.read(buf);
155 		assertTrue(count > 0);
156 		assertEquals("test", new String(buf, 0, count, StandardCharsets.UTF_8));
157 		is.close();
158 	}
159 
160 	@Test void c02_readByteArray_empty() throws IOException {
161 		var reader = new StringReader("");
162 		var is = new ReaderInputStream(reader, StandardCharsets.UTF_8);
163 		var buf = new byte[100];
164 		var count = is.read(buf);
165 		assertEquals(-1, count);
166 		is.close();
167 	}
168 
169 	//====================================================================================================
170 	// read(byte[], int, int) tests
171 	//====================================================================================================
172 	@Test void d01_readByteArrayWithOffset() throws IOException {
173 		var reader = new StringReader("test");
174 		var is = new ReaderInputStream(reader, StandardCharsets.UTF_8);
175 		var buf = new byte[100];
176 		var count = is.read(buf, 10, 50);
177 		assertTrue(count > 0);
178 		assertEquals("test", new String(buf, 10, count, StandardCharsets.UTF_8));
179 		is.close();
180 	}
181 
182 	@Test void d02_readByteArrayWithOffset_zeroLength() throws IOException {
183 		var reader = new StringReader("test");
184 		var is = new ReaderInputStream(reader, StandardCharsets.UTF_8);
185 		var buf = new byte[100];
186 		var count = is.read(buf, 0, 0);
187 		assertEquals(0, count);
188 		is.close();
189 	}
190 
191 	@Test void d03_readByteArrayWithOffset_nullArray() {
192 		var reader = new StringReader("test");
193 		var is = new ReaderInputStream(reader, StandardCharsets.UTF_8);
194 		assertThrows(IllegalArgumentException.class, () -> {
195 			is.read(null, 0, 10);
196 		});
197 	}
198 
199 	@Test void d04_readByteArrayWithOffset_invalidOffset() {
200 		var reader = new StringReader("test");
201 		var is = new ReaderInputStream(reader, StandardCharsets.UTF_8);
202 		var buf = new byte[10];
203 		assertThrows(IndexOutOfBoundsException.class, () -> {
204 			is.read(buf, -1, 5);
205 		});
206 	}
207 
208 	@Test void d05_readByteArrayWithOffset_invalidLength() {
209 		var reader = new StringReader("test");
210 		var is = new ReaderInputStream(reader, StandardCharsets.UTF_8);
211 		var buf = new byte[10];
212 		assertThrows(IndexOutOfBoundsException.class, () -> {
213 			is.read(buf, 0, -1);
214 		});
215 	}
216 
217 	@Test void d06_readByteArrayWithOffset_offsetPlusLengthTooLarge() {
218 		var reader = new StringReader("test");
219 		var is = new ReaderInputStream(reader, StandardCharsets.UTF_8);
220 		var buf = new byte[10];
221 		assertThrows(IndexOutOfBoundsException.class, () -> {
222 			is.read(buf, 5, 10);
223 		});
224 	}
225 
226 	//====================================================================================================
227 	// close() tests
228 	//====================================================================================================
229 	@Test void e01_close() throws IOException {
230 		var reader = new StringReader("test");
231 		var is = new ReaderInputStream(reader, StandardCharsets.UTF_8);
232 		is.close(); // Should close the underlying reader
233 		// Reader should be closed
234 		assertThrows(IOException.class, () -> {
235 			reader.read();
236 		});
237 	}
238 
239 	//====================================================================================================
240 	// Charset encoding tests
241 	//====================================================================================================
242 	@Test void f01_utf8() throws IOException {
243 		var reader = new StringReader("test");
244 		var is = new ReaderInputStream(reader, StandardCharsets.UTF_8);
245 		var bytes = is.readAllBytes();
246 		assertArrayEquals("test".getBytes(StandardCharsets.UTF_8), bytes);
247 		is.close();
248 	}
249 
250 	@Test void f02_iso8859_1() throws IOException {
251 		var reader = new StringReader("test");
252 		var is = new ReaderInputStream(reader, StandardCharsets.ISO_8859_1);
253 		var bytes = is.readAllBytes();
254 		assertArrayEquals("test".getBytes(StandardCharsets.ISO_8859_1), bytes);
255 		is.close();
256 	}
257 
258 	@Test void f03_unicode() throws IOException {
259 		var reader = new StringReader("héllo");
260 		var is = new ReaderInputStream(reader, StandardCharsets.UTF_8);
261 		var bytes = is.readAllBytes();
262 		assertEquals("héllo", new String(bytes, StandardCharsets.UTF_8));
263 		is.close();
264 	}
265 
266 	//====================================================================================================
267 	// Large content tests
268 	//====================================================================================================
269 	@Test void g01_largeContent() throws IOException {
270 		var sb = new StringBuilder();
271 		for (int i = 0; i < 1000; i++) {
272 			sb.append("test");
273 		}
274 		var reader = new StringReader(sb.toString());
275 		var is = new ReaderInputStream(reader, StandardCharsets.UTF_8);
276 		var bytes = is.readAllBytes();
277 		assertEquals(sb.toString(), new String(bytes, StandardCharsets.UTF_8));
278 		is.close();
279 	}
280 
281 	@Test void g02_multipleReads() throws IOException {
282 		var reader = new StringReader("hello world");
283 		var is = new ReaderInputStream(reader, StandardCharsets.UTF_8);
284 		var buf1 = new byte[5];
285 		var count1 = is.read(buf1);
286 		var buf2 = new byte[100];
287 		var count2 = is.read(buf2);
288 		var result = new String(buf1, 0, count1, StandardCharsets.UTF_8) +
289 			new String(buf2, 0, count2, StandardCharsets.UTF_8);
290 		assertEquals("hello world", result);
291 		is.close();
292 	}
293 }
294