1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.shared.utils.io;
20
21 import java.io.BufferedInputStream;
22 import java.io.BufferedOutputStream;
23 import java.io.BufferedReader;
24 import java.io.BufferedWriter;
25 import java.io.ByteArrayInputStream;
26 import java.io.ByteArrayOutputStream;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.io.OutputStream;
30 import java.io.Reader;
31 import java.io.StringReader;
32 import java.io.StringWriter;
33 import java.io.UnsupportedEncodingException;
34 import java.io.Writer;
35 import java.util.concurrent.atomic.AtomicBoolean;
36
37 import org.junit.Test;
38
39 import static org.hamcrest.CoreMatchers.is;
40 import static org.hamcrest.MatcherAssert.assertThat;
41
42 @SuppressWarnings("deprecation")
43 public class IOUtilTest {
44
45 private static final long INFINITE_LOOP_TIMEOUT = 500;
46
47 @Test
48 public void closeReaderWithNull() throws Exception {
49 IOUtil.close((Reader) null);
50 }
51
52 @Test
53 public void closeWriterWithNull() throws Exception {
54 IOUtil.close((Writer) null);
55 }
56
57 @Test
58 public void closeInputStreamWithNull() throws Exception {
59 IOUtil.close(nullInputStream());
60 }
61
62 @Test
63 public void closeOutputStreamWithNull() throws Exception {
64 IOUtil.close(nullOutputStream());
65 }
66
67 @Test
68 public void closeReaderWithIOE() throws Exception {
69 IOUtil.close(new BufferedReader(new StringReader(emptyString())) {
70 @Override
71 public void close() throws IOException {
72 super.close();
73 throw new IOException("don't bomb out");
74 }
75 });
76 }
77
78 @Test
79 public void closeWriterWithIOE() throws Exception {
80 IOUtil.close(new BufferedWriter(new StringWriter()) {
81 @Override
82 public void close() throws IOException {
83 super.close();
84 throw new IOException("don't bomb out");
85 }
86 });
87 }
88
89 @Test
90 public void closeInputStreamWithIOE() throws Exception {
91 IOUtil.close(new BufferedInputStream(emptyInputStream()) {
92 @Override
93 public void close() throws IOException {
94 super.close();
95 throw new IOException("don't bomb out");
96 }
97 });
98 }
99
100 @Test
101 public void closeOutputStreamWithIOE() throws Exception {
102 IOUtil.close(new BufferedOutputStream(new ByteArrayOutputStream()) {
103 @Override
104 public void close() throws IOException {
105 super.close();
106 throw new IOException("don't bomb out");
107 }
108 });
109 }
110
111 @Test
112 public void closeReaderCloses() throws Exception {
113 final AtomicBoolean closed = new AtomicBoolean(false);
114 IOUtil.close(new BufferedReader(new StringReader(emptyString())) {
115 @Override
116 public void close() throws IOException {
117 closed.set(true);
118 super.close();
119 }
120 });
121 assertThat(closed.get(), is(true));
122 }
123
124 @Test
125 public void closeWriterCloses() throws Exception {
126 final AtomicBoolean closed = new AtomicBoolean(false);
127 IOUtil.close(new BufferedWriter(new StringWriter()) {
128 @Override
129 public void close() throws IOException {
130 closed.set(true);
131 super.close();
132 }
133 });
134 assertThat(closed.get(), is(true));
135 }
136
137 @Test
138 public void closeInputStreamCloses() throws Exception {
139 final AtomicBoolean closed = new AtomicBoolean(false);
140 IOUtil.close(new BufferedInputStream(emptyInputStream()) {
141 @Override
142 public void close() throws IOException {
143 closed.set(true);
144 super.close();
145 }
146 });
147 assertThat(closed.get(), is(true));
148 }
149
150 @Test
151 public void closeOutputStreamCloses() throws Exception {
152 final AtomicBoolean closed = new AtomicBoolean(false);
153 IOUtil.close(new BufferedOutputStream(new ByteArrayOutputStream()) {
154 @Override
155 public void close() throws IOException {
156 closed.set(true);
157 super.close();
158 }
159 });
160 assertThat(closed.get(), is(true));
161 }
162
163 @Test
164 public void toByteArrayFromString() throws Exception {
165 String probe = "A string \u2345\u00ef";
166 assertThat(IOUtil.toByteArray(probe), is(probe.getBytes()));
167 }
168
169 @Test
170 public void toByteArrayFromReader() throws Exception {
171 String probe = "A string \u2345\u00ef";
172 assertThat(IOUtil.toByteArray(new StringReader(probe)), is(probe.getBytes()));
173 }
174
175 @Test
176 public void toByteArrayFromInputStream() throws Exception {
177 String probe = "A string \u2345\u00ef";
178 assertThat(
179 IOUtil.toByteArray(new DontCloseByteArrayInputStream(IOUtil.toByteArray(probe))), is(probe.getBytes()));
180 }
181
182 @Test(expected = NullPointerException.class)
183 public void toByteArrayNullString() throws Exception {
184 IOUtil.toByteArray((String) null);
185 }
186
187 @Test(expected = NullPointerException.class)
188 public void toByteArrayNullReader() throws Exception {
189 IOUtil.toByteArray((Reader) null);
190 }
191
192 @Test(expected = NullPointerException.class)
193 public void toByteArrayNullInputStream() throws Exception {
194 IOUtil.toByteArray(nullInputStream());
195 }
196
197 @Test(expected = IOException.class)
198 public void contentEqualNullNull() throws Exception {
199 IOUtil.contentEquals(null, null);
200 }
201
202 @Test(expected = IOException.class)
203 public void contentEqualNonNullNull() throws Exception {
204 IOUtil.contentEquals(new DontCloseByteArrayInputStream(emptyByteArray()), null);
205 }
206
207 @Test(expected = IOException.class)
208 public void contentEqualNullNonNull() throws Exception {
209 IOUtil.contentEquals(new DontCloseByteArrayInputStream(emptyByteArray()), null);
210 }
211
212 @Test
213 public void contentEqualEmptyEmpty() throws Exception {
214 assertThat(
215 IOUtil.contentEquals(
216 new DontCloseByteArrayInputStream(emptyByteArray()),
217 new DontCloseByteArrayInputStream(emptyByteArray())),
218 is(true));
219 }
220
221 @Test
222 public void contentEqualNonEmptyEmpty() throws Exception {
223 assertThat(
224 IOUtil.contentEquals(
225 new DontCloseByteArrayInputStream(new byte[1]),
226 new DontCloseByteArrayInputStream(emptyByteArray())),
227 is(false));
228 }
229
230 @Test
231 public void contentEqualEmptyNonEmpty() throws Exception {
232 assertThat(
233 IOUtil.contentEquals(
234 new DontCloseByteArrayInputStream(emptyByteArray()),
235 new DontCloseByteArrayInputStream(new byte[1])),
236 is(false));
237 }
238
239 @Test
240 public void contentEqualNonEmptyNonEmpty() throws Exception {
241 assertThat(
242 IOUtil.contentEquals(
243 new DontCloseByteArrayInputStream(new byte[1]), new DontCloseByteArrayInputStream(new byte[1])),
244 is(true));
245 }
246
247 @Test
248 public void contentEqualMostlySame() throws Exception {
249 assertThat(
250 IOUtil.contentEquals(
251 new DontCloseByteArrayInputStream(new byte[] {1, 2, 3, 4, 5, 6}),
252 new DontCloseByteArrayInputStream(new byte[] {1, 2, 3, 4, 5, 7})),
253 is(false));
254 }
255
256 @Test
257 public void contentEqualLargeSame() throws Exception {
258 assertThat(
259 IOUtil.contentEquals(
260 new DontCloseByteArrayInputStream(new byte[8192]),
261 new DontCloseByteArrayInputStream(new byte[8192])),
262 is(true));
263 }
264
265 @Test
266 public void contentEqualLargeDifferent() throws Exception {
267 byte[] buf = new byte[8192];
268 buf[8191] = 1;
269 assertThat(
270 IOUtil.contentEquals(
271 new DontCloseByteArrayInputStream(new byte[8192]), new DontCloseByteArrayInputStream(buf)),
272 is(false));
273 }
274
275 @Test(expected = NullPointerException.class)
276 public void toStringNullByteArray() throws Exception {
277 IOUtil.toString(nullByteArray());
278 }
279
280 @Test
281 public void toStringEmptyByteArray() throws Exception {
282 assertThat(IOUtil.toString(emptyByteArray()), is(emptyString()));
283 }
284
285 @Test
286 public void toStringByteArray() throws Exception {
287 String probe = "A string \u2345\u00ef";
288 assertThat(IOUtil.toString(probe.getBytes()).getBytes(), is(probe.getBytes()));
289 }
290
291 @Test(expected = NullPointerException.class)
292 public void toStringNullByteArrayNegBufSz() throws Exception {
293 IOUtil.toString(nullByteArray(), -1);
294 }
295
296 @Test(expected = NegativeArraySizeException.class)
297 public void toStringEmptyByteArrayNegBufSz() throws Exception {
298 assertThat(IOUtil.toString(emptyByteArray(), -1), is(emptyString()));
299 }
300
301 @Test(expected = NegativeArraySizeException.class)
302 public void toStringByteArrayNegBufSz() throws Exception {
303 String probe = "A string \u2345\u00ef";
304 assertThat(IOUtil.toString(probe.getBytes(), -1), is(probe));
305 }
306
307 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
308 public void toStringNullByteArrayZeroBufSz() throws Exception {
309 IOUtil.toString(nullByteArray(), 0);
310 }
311
312 @Test(expected = NullPointerException.class)
313 public void toStringNullByteArrayPosBufSz() throws Exception {
314 IOUtil.toString(nullByteArray(), 1);
315 }
316
317 @Test
318 public void toStringEmptyByteArrayPosBufSz() throws Exception {
319 assertThat(IOUtil.toString(emptyByteArray(), 1), is(emptyString()));
320 }
321
322 @Test
323 public void toStringByteArrayPosBufSz() throws Exception {
324 String probe = "A string \u2345\u00ef";
325 assertThat(IOUtil.toString(probe.getBytes(), 1).getBytes(), is(probe.getBytes()));
326 }
327
328 @Test(expected = NullPointerException.class)
329 public void toStringNullByteArrayNullEncoding() throws Exception {
330 IOUtil.toString(nullByteArray(), null);
331 }
332
333 @Test(expected = NullPointerException.class)
334 public void toStringEmptyByteArrayNullEncoding() throws Exception {
335 assertThat(IOUtil.toString(emptyByteArray(), null), is(emptyString()));
336 }
337
338 @Test(expected = NullPointerException.class)
339 public void toStringByteArrayNullEncoding() throws Exception {
340 String probe = "A string \u2345\u00ef";
341 assertThat(IOUtil.toString(probe.getBytes(), null).getBytes(), is(probe.getBytes()));
342 }
343
344 @Test(expected = NullPointerException.class)
345 public void toStringNullByteArrayJunkEncoding() throws Exception {
346 IOUtil.toString(nullByteArray(), "junk");
347 }
348
349 @Test(expected = UnsupportedEncodingException.class)
350 public void toStringEmptyByteArrayJunkEncoding() throws Exception {
351 assertThat(IOUtil.toString(emptyByteArray(), "junk"), is(emptyString()));
352 }
353
354 @Test(expected = UnsupportedEncodingException.class)
355 public void toStringByteArrayJunkEncoding() throws Exception {
356 String probe = "A string \u2345\u00ef";
357 assertThat(IOUtil.toString(probe.getBytes(), "junk").getBytes(), is(probe.getBytes()));
358 }
359
360 @Test(expected = NullPointerException.class)
361 public void toStringNullByteArrayValidEncoding() throws Exception {
362 IOUtil.toString(nullByteArray(), "utf-16");
363 }
364
365 @Test
366 public void toStringEmptyByteArrayValidEncoding() throws Exception {
367 assertThat(IOUtil.toString(emptyByteArray(), "utf-16"), is(emptyString()));
368 }
369
370 @Test
371 public void toStringByteArrayValidEncoding() throws Exception {
372 String probe = "A string \u2345\u00ef";
373 assertThat(IOUtil.toString(probe.getBytes("utf-16"), "utf-16").getBytes("utf-8"), is(probe.getBytes("utf-8")));
374 }
375
376 @Test(expected = NullPointerException.class)
377 public void toStringNullByteArrayNullEncodingNegBufSz() throws Exception {
378 IOUtil.toString(nullByteArray(), null, -1);
379 }
380
381 @Test(expected = NullPointerException.class)
382 public void toStringEmptyByteArrayNullEncodingNegBufSz() throws Exception {
383 assertThat(IOUtil.toString(emptyByteArray(), null, -1), is(emptyString()));
384 }
385
386 @Test(expected = NullPointerException.class)
387 public void toStringByteArrayNullEncodingNegBufSz() throws Exception {
388 String probe = "A string \u2345\u00ef";
389 assertThat(IOUtil.toString(probe.getBytes(), null, -1).getBytes(), is(probe.getBytes()));
390 }
391
392 @Test(expected = NullPointerException.class)
393 public void toStringNullByteArrayJunkEncodingNegBufSz() throws Exception {
394 IOUtil.toString(nullByteArray(), "junk", -1);
395 }
396
397 @Test(expected = UnsupportedEncodingException.class)
398 public void toStringEmptyByteArrayJunkEncodingNegBufSz() throws Exception {
399 assertThat(IOUtil.toString(emptyByteArray(), "junk", -1), is(emptyString()));
400 }
401
402 @Test(expected = UnsupportedEncodingException.class)
403 public void toStringByteArrayJunkEncodingNegBufSz() throws Exception {
404 String probe = "A string \u2345\u00ef";
405 assertThat(IOUtil.toString(probe.getBytes(), "junk", -1).getBytes(), is(probe.getBytes()));
406 }
407
408 @Test(expected = NullPointerException.class)
409 public void toStringNullByteArrayValidEncodingNegBufSz() throws Exception {
410 IOUtil.toString(nullByteArray(), "utf-16", -1);
411 }
412
413 @Test(expected = NegativeArraySizeException.class)
414 public void toStringEmptyByteArrayValidEncodingNegBufSz() throws Exception {
415 assertThat(IOUtil.toString(emptyByteArray(), "utf-16", -1), is(emptyString()));
416 }
417
418 @Test(expected = NegativeArraySizeException.class)
419 public void toStringByteArrayValidEncodingNegBufSz() throws Exception {
420 String probe = "A string \u2345\u00ef";
421 assertThat(
422 IOUtil.toString(probe.getBytes("utf-16"), "utf-16", -1).getBytes("utf-8"), is(probe.getBytes("utf-8")));
423 }
424
425 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
426 public void toStringNullByteArrayNullEncodingZeroBufSz() throws Exception {
427 IOUtil.toString(nullByteArray(), null, 0);
428 }
429
430 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
431 public void toStringEmptyByteArrayNullEncodingZeroBufSz() throws Exception {
432 assertThat(IOUtil.toString(emptyByteArray(), null, 0), is(emptyString()));
433 }
434
435 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
436 public void toStringByteArrayNullEncodingZeroBufSz() throws Exception {
437 String probe = "A string \u2345\u00ef";
438 assertThat(IOUtil.toString(probe.getBytes(), null, 0).getBytes(), is(probe.getBytes()));
439 }
440
441 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
442 public void toStringNullByteArrayJunkEncodingZeroBufSz() throws Exception {
443 IOUtil.toString(nullByteArray(), "junk", 0);
444 }
445
446 @Test(expected = UnsupportedEncodingException.class, timeout = INFINITE_LOOP_TIMEOUT)
447 public void toStringEmptyByteArrayJunkEncodingZeroBufSz() throws Exception {
448 assertThat(IOUtil.toString(emptyByteArray(), "junk", 0), is(emptyString()));
449 }
450
451 @Test(expected = UnsupportedEncodingException.class, timeout = INFINITE_LOOP_TIMEOUT)
452 public void toStringByteArrayJunkEncodingZeroBufSz() throws Exception {
453 String probe = "A string \u2345\u00ef";
454 assertThat(IOUtil.toString(probe.getBytes(), "junk", 0).getBytes(), is(probe.getBytes()));
455 }
456
457 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
458 public void toStringNullByteArrayValidEncodingZeroBufSz() throws Exception {
459 IOUtil.toString(nullByteArray(), "utf-16", 0);
460 }
461
462
463
464
465
466 @Test(expected = NullPointerException.class)
467 public void copyNullByteArrayNullOutputStream() throws Exception {
468 IOUtil.copy(nullByteArray(), nullOutputStream());
469 }
470
471 @Test(expected = NullPointerException.class)
472 public void copyNullByteArrayValidOutputStream() throws Exception {
473 IOUtil.copy(nullByteArray(), new DontCloseByteArrayOutputStream());
474 }
475
476 @Test(expected = NullPointerException.class)
477 public void copyEmptyByteArrayNullOutputStream() throws Exception {
478 IOUtil.copy(emptyByteArray(), nullOutputStream());
479 }
480
481 @Test
482 public void copyEmptyByteArrayValidOutputStream() throws Exception {
483 IOUtil.copy(emptyByteArray(), new DontCloseByteArrayOutputStream());
484 }
485
486 @Test
487 public void copyByteArrayValidOutputStream() throws Exception {
488 ByteArrayOutputStream outputStream = new DontCloseByteArrayOutputStream();
489 byte[] input = {1, 2, 3, 4, 5, 6};
490 IOUtil.copy(input, outputStream);
491 assertThat(outputStream.toByteArray(), is(input));
492 }
493
494
495
496
497
498 @Test(expected = NullPointerException.class)
499 public void copyNullByteArrayNullOutputStreamNegBufSz() throws Exception {
500 IOUtil.copy(nullByteArray(), nullOutputStream());
501 }
502
503 @Test(expected = NullPointerException.class)
504 public void copyNullByteArrayValidOutputStreamNegBufSz() throws Exception {
505 IOUtil.copy(nullByteArray(), new DontCloseByteArrayOutputStream());
506 }
507
508 @Test(expected = NullPointerException.class)
509 public void copyEmptyByteArrayNullOutputStreamNegBufSz() throws Exception {
510 IOUtil.copy(emptyByteArray(), nullOutputStream());
511 }
512
513 @Test
514 public void copyEmptyByteArrayValidOutputStreamNegBufSz() throws Exception {
515 IOUtil.copy(emptyByteArray(), new DontCloseByteArrayOutputStream());
516 }
517
518 @Test
519 public void copyByteArrayValidOutputStreamNegBufSz() throws Exception {
520 ByteArrayOutputStream outputStream = new DontCloseByteArrayOutputStream();
521 byte[] input = {1, 2, 3, 4, 5, 6};
522 IOUtil.copy(input, outputStream);
523 assertThat(outputStream.toByteArray(), is(input));
524 }
525
526 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
527 public void copyNullByteArrayNullOutputStreamZeroBufSz() throws Exception {
528 IOUtil.copy(nullByteArray(), nullOutputStream());
529 }
530
531 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
532 public void copyNullByteArrayValidOutputStreamZeroBufSz() throws Exception {
533 IOUtil.copy(nullByteArray(), new DontCloseByteArrayOutputStream());
534 }
535
536 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
537 public void copyEmptyByteArrayNullOutputStreamZeroBufSz() throws Exception {
538 IOUtil.copy(emptyByteArray(), nullOutputStream());
539 }
540
541 @Test(timeout = INFINITE_LOOP_TIMEOUT)
542 public void copyEmptyByteArrayValidOutputStreamZeroBufSz() throws Exception {
543 IOUtil.copy(emptyByteArray(), new DontCloseByteArrayOutputStream());
544 }
545
546 @Test(timeout = INFINITE_LOOP_TIMEOUT)
547 public void copyByteArrayValidOutputStreamZeroBufSz() throws Exception {
548 ByteArrayOutputStream outputStream = new DontCloseByteArrayOutputStream();
549 byte[] input = {1, 2, 3, 4, 5, 6};
550 IOUtil.copy(input, outputStream);
551 assertThat(outputStream.toByteArray(), is(input));
552 }
553
554 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
555 public void copyNullByteArrayNullOutputStreamPosBufSz() throws Exception {
556 IOUtil.copy(nullByteArray(), nullOutputStream());
557 }
558
559 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
560 public void copyNullByteArrayValidOutputStreamPosBufSz() throws Exception {
561 IOUtil.copy(nullByteArray(), new DontCloseByteArrayOutputStream());
562 }
563
564 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
565 public void copyEmptyByteArrayNullOutputStreamPosBufSz() throws Exception {
566 IOUtil.copy(emptyByteArray(), nullOutputStream());
567 }
568
569 @Test(timeout = INFINITE_LOOP_TIMEOUT)
570 public void copyEmptyByteArrayValidOutputStreamPosBufSz() throws Exception {
571 IOUtil.copy(emptyByteArray(), new DontCloseByteArrayOutputStream());
572 }
573
574 @Test(timeout = INFINITE_LOOP_TIMEOUT)
575 public void copyByteArrayValidOutputStreamPosBufSz() throws Exception {
576 ByteArrayOutputStream outputStream = new DontCloseByteArrayOutputStream();
577 byte[] input = {1, 2, 3, 4, 5, 6};
578 IOUtil.copy(input, outputStream);
579 assertThat(outputStream.toByteArray(), is(input));
580 }
581
582 @Test(expected = NullPointerException.class)
583 public void copyNullInputStreamNullOutputStream() throws Exception {
584 IOUtil.copy(nullInputStream(), nullOutputStream());
585 }
586
587 @Test(expected = NullPointerException.class)
588 public void copyNullInputStreamValidOutputStream() throws Exception {
589 IOUtil.copy(nullInputStream(), new DontCloseByteArrayOutputStream());
590 }
591
592 @Test
593 public void copyEmptyInputStreamNullOutputStream() throws Exception {
594 IOUtil.copy(new DontCloseByteArrayInputStream(emptyByteArray()), nullOutputStream());
595 }
596
597 @Test
598 public void copyEmptyInputStreamValidOutputStream() throws Exception {
599 IOUtil.copy(new DontCloseByteArrayInputStream(emptyByteArray()), new DontCloseByteArrayOutputStream());
600 }
601
602 @Test
603 public void copyInputStreamValidOutputStream() throws Exception {
604 ByteArrayOutputStream outputStream = new DontCloseByteArrayOutputStream();
605 byte[] input = {1, 2, 3, 4, 5, 6};
606 IOUtil.copy(new DontCloseByteArrayInputStream(input), outputStream);
607 assertThat(outputStream.toByteArray(), is(input));
608 }
609
610 @Test(expected = NegativeArraySizeException.class)
611 public void copyNullInputStreamNullOutputStreamNegBufSz() throws Exception {
612 IOUtil.copy(nullInputStream(), nullOutputStream(), -1);
613 }
614
615 @Test(expected = NegativeArraySizeException.class)
616 public void copyNullInputStreamValidOutputStreamNegBufSz() throws Exception {
617 IOUtil.copy(nullInputStream(), new DontCloseByteArrayOutputStream(), -1);
618 }
619
620 @Test(expected = NegativeArraySizeException.class)
621 public void copyEmptyInputStreamNullOutputStreamNegBufSz() throws Exception {
622 IOUtil.copy(new DontCloseByteArrayInputStream(emptyByteArray()), nullOutputStream(), -1);
623 }
624
625 @Test(expected = NegativeArraySizeException.class)
626 public void copyEmptyInputStreamValidOutputStreamNegBufSz() throws Exception {
627 IOUtil.copy(new DontCloseByteArrayInputStream(emptyByteArray()), new DontCloseByteArrayOutputStream(), -1);
628 }
629
630 @Test(expected = NegativeArraySizeException.class)
631 public void copyInputStreamValidOutputStreamNegBufSz() throws Exception {
632 ByteArrayOutputStream outputStream = new DontCloseByteArrayOutputStream();
633 byte[] input = {1, 2, 3, 4, 5, 6};
634 IOUtil.copy(new DontCloseByteArrayInputStream(input), outputStream, -1);
635 assertThat(outputStream.toByteArray(), is(input));
636 }
637
638 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
639 public void copyNullInputStreamNullOutputStreamZeroBufSz() throws Exception {
640 IOUtil.copy(nullInputStream(), nullOutputStream(), 0);
641 }
642
643 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
644 public void copyNullInputStreamValidOutputStreamZeroBufSz() throws Exception {
645 IOUtil.copy(nullInputStream(), new ByteArrayOutputStream(), 0);
646 }
647
648 @Test(timeout = INFINITE_LOOP_TIMEOUT)
649 public void copyEmptyInputStreamNullOutputStreamZeroBufSz() throws Exception {
650 IOUtil.copy(new DontCloseByteArrayInputStream(emptyByteArray()), nullOutputStream(), 0);
651 }
652
653 @Test(timeout = INFINITE_LOOP_TIMEOUT)
654 public void copyEmptyInputStreamValidOutputStreamZeroBufSz() throws Exception {
655 IOUtil.copy(new DontCloseByteArrayInputStream(emptyByteArray()), new DontCloseByteArrayOutputStream(), 0);
656 }
657
658 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
659 public void copyNullInputStreamNullOutputStreamPosBufSz() throws Exception {
660 IOUtil.copy(nullInputStream(), nullOutputStream(), 1);
661 }
662
663 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
664 public void copyNullInputStreamValidOutputStreamPosBufSz() throws Exception {
665 IOUtil.copy(nullInputStream(), new ByteArrayOutputStream(), 1);
666 }
667
668 @Test(timeout = INFINITE_LOOP_TIMEOUT)
669 public void copyEmptyInputStreamNullOutputStreamPosBufSz() throws Exception {
670 IOUtil.copy(new DontCloseByteArrayInputStream(emptyByteArray()), nullOutputStream(), 1);
671 }
672
673 @Test(timeout = INFINITE_LOOP_TIMEOUT)
674 public void copyEmptyInputStreamValidOutputStreamPosBufSz() throws Exception {
675 IOUtil.copy(new DontCloseByteArrayInputStream(emptyByteArray()), new DontCloseByteArrayOutputStream(), 1);
676 }
677
678 @Test(timeout = INFINITE_LOOP_TIMEOUT)
679 public void copyInputStreamValidOutputStreamPosBufSz() throws Exception {
680 ByteArrayOutputStream outputStream = new DontCloseByteArrayOutputStream();
681 byte[] input = {1, 2, 3, 4, 5, 6};
682 IOUtil.copy(new DontCloseByteArrayInputStream(input), outputStream, 1);
683 assertThat(outputStream.toByteArray(), is(input));
684 }
685
686 @Test(expected = NullPointerException.class)
687 public void toStringNullInputStream() throws Exception {
688 IOUtil.toString(nullInputStream());
689 }
690
691 @Test
692 public void toStringEmptyInputStream() throws Exception {
693 assertThat(IOUtil.toString(emptyInputStream()), is(emptyString()));
694 }
695
696 @Test
697 public void toStringInputStream() throws Exception {
698 String probe = "A string \u2345\u00ef";
699 assertThat(IOUtil.toString(new ByteArrayInputStream(probe.getBytes())).getBytes(), is(probe.getBytes()));
700 }
701
702 @Test(expected = NullPointerException.class)
703 public void toStringNullInputStreamNegBufSz() throws Exception {
704 IOUtil.toString(nullInputStream(), -1);
705 }
706
707 @Test(expected = NegativeArraySizeException.class)
708 public void toStringEmptyInputStreamNegBufSz() throws Exception {
709 assertThat(IOUtil.toString(emptyInputStream(), -1), is(emptyString()));
710 }
711
712 @Test(expected = NegativeArraySizeException.class)
713 public void toStringInputStreamNegBufSz() throws Exception {
714 String probe = "A string \u2345\u00ef";
715 assertThat(IOUtil.toString(new ByteArrayInputStream(probe.getBytes()), -1), is(probe));
716 }
717
718 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
719 public void toStringNullInputStreamZeroBufSz() throws Exception {
720 IOUtil.toString(nullInputStream(), 0);
721 }
722
723 @Test(expected = NullPointerException.class)
724 public void toStringNullInputStreamPosBufSz() throws Exception {
725 IOUtil.toString(nullInputStream(), 1);
726 }
727
728 @Test
729 public void toStringEmptyInputStreamPosBufSz() throws Exception {
730 assertThat(IOUtil.toString(emptyInputStream(), 1), is(emptyString()));
731 }
732
733 @Test
734 public void toStringInputStreamPosBufSz() throws Exception {
735 String probe = "A string \u2345\u00ef";
736 assertThat(
737 IOUtil.toString(new ByteArrayInputStream(probe.getBytes()), 1).getBytes(), is(probe.getBytes()));
738 }
739
740 @Test(expected = NullPointerException.class)
741 public void toStringNullInputStreamNullEncoding() throws Exception {
742 IOUtil.toString(nullInputStream(), null);
743 }
744
745 @Test(expected = NullPointerException.class)
746 public void toStringEmptyInputStreamNullEncoding() throws Exception {
747 assertThat(IOUtil.toString(emptyInputStream(), null), is(emptyString()));
748 }
749
750 @Test(expected = NullPointerException.class)
751 public void toStringInputStreamNullEncoding() throws Exception {
752 String probe = "A string \u2345\u00ef";
753 assertThat(
754 IOUtil.toString(new ByteArrayInputStream(probe.getBytes()), null)
755 .getBytes(),
756 is(probe.getBytes()));
757 }
758
759 @Test(expected = NullPointerException.class)
760 public void toStringNullInputStreamJunkEncoding() throws Exception {
761 IOUtil.toString(nullInputStream(), "junk");
762 }
763
764 @Test(expected = UnsupportedEncodingException.class)
765 public void toStringEmptyInputStreamJunkEncoding() throws Exception {
766 assertThat(IOUtil.toString(emptyInputStream(), "junk"), is(emptyString()));
767 }
768
769 @Test(expected = UnsupportedEncodingException.class)
770 public void toStringInputStreamJunkEncoding() throws Exception {
771 String probe = "A string \u2345\u00ef";
772 assertThat(
773 IOUtil.toString(new ByteArrayInputStream(probe.getBytes()), "junk")
774 .getBytes(),
775 is(probe.getBytes()));
776 }
777
778 @Test(expected = NullPointerException.class)
779 public void toStringNullInputStreamValidEncoding() throws Exception {
780 IOUtil.toString(nullInputStream(), "utf-16");
781 }
782
783 @Test
784 public void toStringEmptyInputStreamValidEncoding() throws Exception {
785 assertThat(IOUtil.toString(emptyInputStream(), "utf-16"), is(emptyString()));
786 }
787
788 @Test
789 public void toStringInputStreamValidEncoding() throws Exception {
790 String probe = "A string \u2345\u00ef";
791 assertThat(
792 IOUtil.toString(new ByteArrayInputStream(probe.getBytes("utf-16")), "utf-16")
793 .getBytes("utf-8"),
794 is(probe.getBytes("utf-8")));
795 }
796
797 @Test(expected = NullPointerException.class)
798 public void toStringNullInputStreamNullEncodingNegBufSz() throws Exception {
799 IOUtil.toString(nullInputStream(), null, -1);
800 }
801
802 @Test(expected = NullPointerException.class)
803 public void toStringEmptyInputStreamNullEncodingNegBufSz() throws Exception {
804 assertThat(IOUtil.toString(emptyInputStream(), null, -1), is(emptyString()));
805 }
806
807 @Test(expected = NullPointerException.class)
808 public void toStringInputStreamNullEncodingNegBufSz() throws Exception {
809 String probe = "A string \u2345\u00ef";
810 assertThat(
811 IOUtil.toString(new ByteArrayInputStream(probe.getBytes()), null, -1)
812 .getBytes(),
813 is(probe.getBytes()));
814 }
815
816 @Test(expected = NullPointerException.class)
817 public void toStringNullInputStreamJunkEncodingNegBufSz() throws Exception {
818 IOUtil.toString(nullInputStream(), "junk", -1);
819 }
820
821 @Test(expected = UnsupportedEncodingException.class)
822 public void toStringEmptyInputStreamJunkEncodingNegBufSz() throws Exception {
823 assertThat(IOUtil.toString(emptyInputStream(), "junk", -1), is(emptyString()));
824 }
825
826 @Test(expected = UnsupportedEncodingException.class)
827 public void toStringInputStreamJunkEncodingNegBufSz() throws Exception {
828 String probe = "A string \u2345\u00ef";
829 assertThat(
830 IOUtil.toString(new ByteArrayInputStream(probe.getBytes()), "junk", -1)
831 .getBytes(),
832 is(probe.getBytes()));
833 }
834
835 @Test(expected = NullPointerException.class)
836 public void toStringNullInputStreamValidEncodingNegBufSz() throws Exception {
837 IOUtil.toString(nullInputStream(), "utf-16", -1);
838 }
839
840 @Test(expected = NegativeArraySizeException.class)
841 public void toStringEmptyInputStreamValidEncodingNegBufSz() throws Exception {
842 assertThat(IOUtil.toString(emptyInputStream(), "utf-16", -1), is(emptyString()));
843 }
844
845 @Test(expected = NegativeArraySizeException.class)
846 public void toStringInputStreamValidEncodingNegBufSz() throws Exception {
847 String probe = "A string \u2345\u00ef";
848 assertThat(
849 IOUtil.toString(new ByteArrayInputStream(probe.getBytes("utf-16")), "utf-16", -1)
850 .getBytes("utf-8"),
851 is(probe.getBytes("utf-8")));
852 }
853
854 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
855 public void toStringNullInputStreamNullEncodingZeroBufSz() throws Exception {
856 IOUtil.toString(nullInputStream(), null, 0);
857 }
858
859 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
860 public void toStringEmptyInputStreamNullEncodingZeroBufSz() throws Exception {
861 assertThat(IOUtil.toString(emptyInputStream(), null, 0), is(emptyString()));
862 }
863
864 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
865 public void toStringInputStreamNullEncodingZeroBufSz() throws Exception {
866 String probe = "A string \u2345\u00ef";
867 assertThat(
868 IOUtil.toString(new ByteArrayInputStream(probe.getBytes()), null, 0)
869 .getBytes(),
870 is(probe.getBytes()));
871 }
872
873 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
874 public void toStringNullInputStreamJunkEncodingZeroBufSz() throws Exception {
875 IOUtil.toString(nullInputStream(), "junk", 0);
876 }
877
878 @Test(expected = UnsupportedEncodingException.class, timeout = INFINITE_LOOP_TIMEOUT)
879 public void toStringEmptyInputStreamJunkEncodingZeroBufSz() throws Exception {
880 assertThat(IOUtil.toString(emptyInputStream(), "junk", 0), is(emptyString()));
881 }
882
883 @Test(expected = UnsupportedEncodingException.class, timeout = INFINITE_LOOP_TIMEOUT)
884 public void toStringInputStreamJunkEncodingZeroBufSz() throws Exception {
885 String probe = "A string \u2345\u00ef";
886 assertThat(
887 IOUtil.toString(new ByteArrayInputStream(probe.getBytes()), "junk", 0)
888 .getBytes(),
889 is(probe.getBytes()));
890 }
891
892 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
893 public void toStringNullInputStreamValidEncodingZeroBufSz() throws Exception {
894 IOUtil.toString(nullInputStream(), "utf-16", 0);
895 }
896
897
898
899
900
901 @Test(expected = NullPointerException.class)
902 public void copyNullInputStreamNullWriter() throws Exception {
903 IOUtil.copy(nullInputStream(), nullWriter());
904 }
905
906 @Test(expected = NullPointerException.class)
907 public void copyEmptyInputStreamNullWriter() throws Exception {
908 IOUtil.copy(emptyInputStream(), nullWriter());
909 }
910
911 @Test
912 public void copyEmptyInputStreamValidWriter() throws Exception {
913 StringWriter writer = new DontCloseStringWriter();
914 IOUtil.copy(emptyInputStream(), writer);
915 assertThat(writer.toString(), is(emptyString()));
916 }
917
918 @Test(expected = NullPointerException.class)
919 public void copyInputStreamNullWriter() throws Exception {
920 String probe = "A string \u2345\u00ef";
921 IOUtil.copy(new ByteArrayInputStream(probe.getBytes()), nullWriter());
922 }
923
924 @Test
925 public void copyInputStreamValidWriter() throws Exception {
926 String probe = "A string \u2345\u00ef";
927 StringWriter writer = new DontCloseStringWriter();
928 IOUtil.copy(new ByteArrayInputStream(probe.getBytes()), writer);
929 assertThat(writer.toString().getBytes(), is(probe.getBytes()));
930 }
931
932
933
934
935
936 @Test(expected = NullPointerException.class)
937 public void copyNullInputStreamNullWriterNegBufSz() throws Exception {
938 IOUtil.copy(nullInputStream(), nullWriter(), -1);
939 }
940
941 @Test(expected = NegativeArraySizeException.class)
942 public void copyEmptyInputStreamNullWriterNegBufSz() throws Exception {
943 IOUtil.copy(emptyInputStream(), nullWriter(), -1);
944 }
945
946 @Test(expected = NegativeArraySizeException.class)
947 public void copyEmptyInputStreamValidWriterNegBufSz() throws Exception {
948 IOUtil.copy(emptyInputStream(), new DontCloseStringWriter(), -1);
949 }
950
951 @Test(expected = NegativeArraySizeException.class)
952 public void copyInputStreamNullWriterNegBufSz() throws Exception {
953 String probe = "A string \u2345\u00ef";
954 IOUtil.copy(new ByteArrayInputStream(probe.getBytes()), nullWriter(), -1);
955 }
956
957 @Test(expected = NegativeArraySizeException.class)
958 public void copyInputStreamValidWriterNegBufSz() throws Exception {
959 String probe = "A string \u2345\u00ef";
960 StringWriter writer = new DontCloseStringWriter();
961 IOUtil.copy(new ByteArrayInputStream(probe.getBytes()), writer, -1);
962 assertThat(writer.toString().getBytes(), is(probe.getBytes()));
963 }
964
965 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
966 public void copyNullInputStreamNullWriterZeroBufSz() throws Exception {
967 IOUtil.copy(nullInputStream(), nullWriter(), 0);
968 }
969
970 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
971 public void copyNullInputStreamValidWriterZeroBufSz() throws Exception {
972 IOUtil.copy(nullInputStream(), new DontCloseStringWriter(), 0);
973 }
974
975 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
976 public void copyEmptyInputStreamNullWriterZeroBufSz() throws Exception {
977 IOUtil.copy(emptyInputStream(), nullWriter(), 0);
978 }
979
980 @Test(expected = NullPointerException.class)
981 public void copyNullInputStreamNullWriterPosBufSz() throws Exception {
982 IOUtil.copy(nullInputStream(), nullWriter(), 1);
983 }
984
985 @Test(expected = NullPointerException.class)
986 public void copyNullInputStreamValidWriterPosBufSz() throws Exception {
987 IOUtil.copy(nullInputStream(), new DontCloseStringWriter(), 1);
988 }
989
990 @Test(expected = NullPointerException.class)
991 public void copyEmptyInputStreamNullWriterPosBufSz() throws Exception {
992 IOUtil.copy(emptyInputStream(), nullWriter(), 1);
993 }
994
995 @Test
996 public void copyEmptyInputStreamValidWriterPosBufSz() throws Exception {
997 StringWriter writer = new DontCloseStringWriter();
998 IOUtil.copy(emptyInputStream(), writer, 1);
999 assertThat(writer.toString(), is(emptyString()));
1000 }
1001
1002 @Test
1003 public void copyInputStreamValidWriterPosBufSz() throws Exception {
1004 String probe = "A string \u2345\u00ef";
1005 StringWriter writer = new DontCloseStringWriter();
1006 IOUtil.copy(new ByteArrayInputStream(probe.getBytes()), writer, 1);
1007 assertThat(writer.toString().getBytes(), is(probe.getBytes()));
1008 }
1009
1010
1011
1012
1013
1014 @Test(expected = NullPointerException.class)
1015 public void copyNullInputStreamNullWriterNullEncoding() throws Exception {
1016 IOUtil.copy(nullInputStream(), nullWriter(), null);
1017 }
1018
1019 @Test(expected = NullPointerException.class)
1020 public void copyNullInputStreamValidWriterNullEncoding() throws Exception {
1021 IOUtil.copy(nullInputStream(), new DontCloseStringWriter(), null);
1022 }
1023
1024 @Test(expected = NullPointerException.class)
1025 public void copyEmptyInputStreamNullWriterNullEncoding() throws Exception {
1026 IOUtil.copy(emptyInputStream(), nullWriter(), null);
1027 }
1028
1029 @Test(expected = NullPointerException.class)
1030 public void copyEmptyInputStreamValidWriterNullEncoding() throws Exception {
1031 IOUtil.copy(emptyInputStream(), new DontCloseStringWriter(), null);
1032 }
1033
1034 @Test(expected = NullPointerException.class)
1035 public void copyInputStreamNullEncoding() throws Exception {
1036 String probe = "A string \u2345\u00ef";
1037 StringWriter writer = new DontCloseStringWriter();
1038 IOUtil.copy(new ByteArrayInputStream(probe.getBytes()), writer, null);
1039 assertThat(writer.toString().getBytes(), is(probe.getBytes()));
1040 }
1041
1042 @Test(expected = NullPointerException.class)
1043 public void copyNullInputStreamNullWriterJunkEncoding() throws Exception {
1044 IOUtil.copy(nullInputStream(), nullWriter(), "junk");
1045 }
1046
1047 @Test(expected = NullPointerException.class)
1048 public void copyNullInputStreamValidWriterJunkEncoding() throws Exception {
1049 IOUtil.copy(nullInputStream(), new DontCloseStringWriter(), "junk");
1050 }
1051
1052 @Test(expected = UnsupportedEncodingException.class)
1053 public void copyEmptyInputStreamNullWriterJunkEncoding() throws Exception {
1054 IOUtil.copy(emptyInputStream(), nullWriter(), "junk");
1055 }
1056
1057 @Test(expected = UnsupportedEncodingException.class)
1058 public void copyEmptyInputStreamValidWriterJunkEncoding() throws Exception {
1059 IOUtil.copy(emptyInputStream(), new DontCloseStringWriter(), "junk");
1060 }
1061
1062 @Test(expected = UnsupportedEncodingException.class)
1063 public void copyInputStreamNullWriterJunkEncoding() throws Exception {
1064 String probe = "A string \u2345\u00ef";
1065 IOUtil.copy(new ByteArrayInputStream(probe.getBytes()), nullWriter(), "junk");
1066 }
1067
1068 @Test(expected = UnsupportedEncodingException.class)
1069 public void copyInputStreamValidWriterJunkEncoding() throws Exception {
1070 String probe = "A string \u2345\u00ef";
1071 StringWriter writer = new DontCloseStringWriter();
1072 IOUtil.copy(new ByteArrayInputStream(probe.getBytes()), writer, "junk");
1073 assertThat(writer.toString().getBytes(), is(probe.getBytes()));
1074 }
1075
1076 @Test(expected = NullPointerException.class)
1077 public void copyNullInputStreamNullWriterValidEncoding() throws Exception {
1078 IOUtil.copy(nullInputStream(), nullWriter(), "utf-16");
1079 }
1080
1081 @Test(expected = NullPointerException.class)
1082 public void copyEmptyInputStreamNullWriterValidEncoding() throws Exception {
1083 IOUtil.copy(emptyInputStream(), nullWriter(), "utf-16");
1084 }
1085
1086 @Test(expected = NullPointerException.class)
1087 public void copyNullInputStreamValidWriterValidEncoding() throws Exception {
1088 IOUtil.copy(nullInputStream(), new DontCloseStringWriter(), "utf-16");
1089 }
1090
1091 @Test
1092 public void copyEmptyInputStreamValidWriterValidEncoding() throws Exception {
1093 StringWriter writer = new DontCloseStringWriter();
1094 IOUtil.copy(emptyInputStream(), writer, "utf-16");
1095 assertThat(writer.toString(), is(emptyString()));
1096 }
1097
1098 @Test(expected = NullPointerException.class)
1099 public void copyInputStreamNullWriterValidEncoding() throws Exception {
1100 String probe = "A string \u2345\u00ef";
1101 IOUtil.copy(new ByteArrayInputStream(probe.getBytes("utf-16")), nullWriter(), "utf-16");
1102 }
1103
1104 @Test
1105 public void copyInputStreamValidWriterValidEncoding() throws Exception {
1106 String probe = "A string \u2345\u00ef";
1107 StringWriter writer = new DontCloseStringWriter();
1108 IOUtil.copy(new ByteArrayInputStream(probe.getBytes("utf-16")), writer, "utf-16");
1109 assertThat(writer.toString().getBytes("utf-8"), is(probe.getBytes("utf-8")));
1110 }
1111
1112
1113
1114
1115
1116 @Test(expected = NullPointerException.class)
1117 public void copyNullInputStreamNullWriterNullEncodingNegBufSz() throws Exception {
1118 IOUtil.copy(nullInputStream(), nullWriter(), null, -1);
1119 }
1120
1121 @Test(expected = NullPointerException.class)
1122 public void copyNullInputStreamValidWriterNullEncodingNegBufSz() throws Exception {
1123 IOUtil.copy(nullInputStream(), new DontCloseStringWriter(), null, -1);
1124 }
1125
1126 @Test(expected = NullPointerException.class)
1127 public void copyEmptyInputStreamNullWriterNullEncodingNegBufSz() throws Exception {
1128 IOUtil.copy(emptyInputStream(), nullWriter(), null, -1);
1129 }
1130
1131 @Test(expected = NullPointerException.class)
1132 public void copyEmptyInputStreamValidWriterNullEncodingNegBufSz() throws Exception {
1133 StringWriter writer = new DontCloseStringWriter();
1134 IOUtil.copy(emptyInputStream(), writer, null, -1);
1135 assertThat(writer.toString(), is(emptyString()));
1136 }
1137
1138 @Test(expected = NullPointerException.class)
1139 public void copyInputStreamNullWriterNullEncodingNegBufSz() throws Exception {
1140 String probe = "A string \u2345\u00ef";
1141 IOUtil.copy(new ByteArrayInputStream(probe.getBytes()), nullWriter(), null, -1);
1142 }
1143
1144 @Test(expected = NullPointerException.class)
1145 public void copyInputStreamValidWriterNullEncodingNegBufSz() throws Exception {
1146 String probe = "A string \u2345\u00ef";
1147 StringWriter writer = new DontCloseStringWriter();
1148 IOUtil.copy(new ByteArrayInputStream(probe.getBytes()), writer, null, -1);
1149 assertThat(writer.toString().getBytes(), is(probe.getBytes()));
1150 }
1151
1152 @Test(expected = NullPointerException.class)
1153 public void copyNullInputStreamNullWriterJunkEncodingNegBufSz() throws Exception {
1154 IOUtil.copy(nullInputStream(), nullWriter(), "junk", -1);
1155 }
1156
1157 @Test(expected = NullPointerException.class)
1158 public void copyNullInputStreamValidWriterJunkEncodingNegBufSz() throws Exception {
1159 IOUtil.copy(nullInputStream(), new DontCloseStringWriter(), "junk", -1);
1160 }
1161
1162 @Test(expected = UnsupportedEncodingException.class)
1163 public void copyEmptyInputStreamNullWriterJunkEncodingNegBufSz() throws Exception {
1164 IOUtil.copy(emptyInputStream(), nullWriter(), "junk", -1);
1165 }
1166
1167 @Test(expected = UnsupportedEncodingException.class)
1168 public void copyEmptyInputStreamJunkEncodingNegBufSz() throws Exception {
1169 StringWriter writer = new DontCloseStringWriter();
1170 IOUtil.copy(emptyInputStream(), writer, "junk", -1);
1171 assertThat(writer.toString(), is(emptyString()));
1172 }
1173
1174 @Test(expected = UnsupportedEncodingException.class)
1175 public void copyInputStreamNullWriterJunkEncodingNegBufSz() throws Exception {
1176 String probe = "A string \u2345\u00ef";
1177 IOUtil.copy(new ByteArrayInputStream(probe.getBytes()), nullWriter(), "junk", -1);
1178 }
1179
1180 @Test(expected = UnsupportedEncodingException.class)
1181 public void copyInputStreamValidWriterJunkEncodingNegBufSz() throws Exception {
1182 String probe = "A string \u2345\u00ef";
1183 StringWriter writer = new DontCloseStringWriter();
1184 IOUtil.copy(new ByteArrayInputStream(probe.getBytes()), writer, "junk", -1);
1185 assertThat(writer.toString().getBytes(), is(probe.getBytes()));
1186 }
1187
1188 @Test(expected = NullPointerException.class)
1189 public void copyNullInputStreamNullWriterValidEncodingNegBufSz() throws Exception {
1190 IOUtil.copy(nullInputStream(), nullWriter(), "utf-16", -1);
1191 }
1192
1193 @Test(expected = NullPointerException.class)
1194 public void copyNullInputStreamValidWriterValidEncodingNegBufSz() throws Exception {
1195 IOUtil.copy(nullInputStream(), new DontCloseStringWriter(), "utf-16", -1);
1196 }
1197
1198 @Test(expected = NegativeArraySizeException.class)
1199 public void copyEmptyInputStreamNullWriterValidEncodingNegBufSz() throws Exception {
1200 IOUtil.copy(emptyInputStream(), nullWriter(), "utf-16", -1);
1201 }
1202
1203 @Test(expected = NegativeArraySizeException.class)
1204 public void copyEmptyInputStreamValidWriterValidEncodingNegBufSz() throws Exception {
1205 StringWriter writer = new DontCloseStringWriter();
1206 IOUtil.copy(emptyInputStream(), writer, "utf-16", -1);
1207 assertThat(writer.toString(), is(emptyString()));
1208 }
1209
1210 @Test(expected = NegativeArraySizeException.class)
1211 public void copyInputStreamNullWriterValidEncodingNegBufSz() throws Exception {
1212 String probe = "A string \u2345\u00ef";
1213 IOUtil.copy(new ByteArrayInputStream(probe.getBytes("utf-16")), nullWriter(), -1);
1214 }
1215
1216 @Test(expected = NegativeArraySizeException.class)
1217 public void copyInputStreamValidEncodingNegBufSz() throws Exception {
1218 String probe = "A string \u2345\u00ef";
1219 StringWriter writer = new DontCloseStringWriter();
1220 IOUtil.copy(new ByteArrayInputStream(probe.getBytes("utf-16")), writer, "utf-16", -1);
1221 assertThat(writer.toString().getBytes("utf-8"), is(probe.getBytes("utf-8")));
1222 }
1223
1224 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
1225 public void copyNullInputStreamNullWriterNullEncodingZeroBufSz() throws Exception {
1226 IOUtil.copy(nullInputStream(), nullWriter(), null, 0);
1227 }
1228
1229 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
1230 public void copyNullInputStreamValidWriterNullEncodingZeroBufSz() throws Exception {
1231 IOUtil.copy(nullInputStream(), new DontCloseStringWriter(), null, 0);
1232 }
1233
1234 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
1235 public void copyEmptyInputStreamNullWriterNullEncodingZeroBufSz() throws Exception {
1236 IOUtil.copy(emptyInputStream(), nullWriter(), null, 0);
1237 }
1238
1239 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
1240 public void copyEmptyInputStreamValidWriterNullEncodingZeroBufSz() throws Exception {
1241 StringWriter writer = new DontCloseStringWriter();
1242 IOUtil.copy(emptyInputStream(), writer, null, 0);
1243 assertThat(writer.toString(), is(emptyString()));
1244 }
1245
1246 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
1247 public void copyInputStreamNullWriterNullEncodingZeroBufSz() throws Exception {
1248 String probe = "A string \u2345\u00ef";
1249 IOUtil.copy(new ByteArrayInputStream(probe.getBytes()), nullWriter(), null, 0);
1250 }
1251
1252 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
1253 public void copyInputStreamValidWriterNullEncodingZeroBufSz() throws Exception {
1254 String probe = "A string \u2345\u00ef";
1255 StringWriter writer = new DontCloseStringWriter();
1256 IOUtil.copy(new ByteArrayInputStream(probe.getBytes()), writer, null, 0);
1257 assertThat(writer.toString().getBytes(), is(probe.getBytes()));
1258 }
1259
1260 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
1261 public void copyNullInputStreamNullWriterJunkEncodingZeroBufSz() throws Exception {
1262 IOUtil.copy(nullInputStream(), nullWriter(), "junk", 0);
1263 }
1264
1265 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
1266 public void copyNullInputStreamValidWriterJunkEncodingZeroBufSz() throws Exception {
1267 IOUtil.copy(nullInputStream(), new DontCloseStringWriter(), "junk", 0);
1268 }
1269
1270 @Test(expected = UnsupportedEncodingException.class, timeout = INFINITE_LOOP_TIMEOUT)
1271 public void copyEmptyInputStreamNullWriterJunkEncodingZeroBufSz() throws Exception {
1272 IOUtil.copy(emptyInputStream(), nullWriter(), "junk", 0);
1273 }
1274
1275 @Test(expected = UnsupportedEncodingException.class, timeout = INFINITE_LOOP_TIMEOUT)
1276 public void copyEmptyInputStreamValidWriterJunkEncodingZeroBufSz() throws Exception {
1277 StringWriter writer = new DontCloseStringWriter();
1278 IOUtil.copy(emptyInputStream(), writer, "junk", 0);
1279 assertThat(writer.toString(), is(emptyString()));
1280 }
1281
1282 @Test(expected = UnsupportedEncodingException.class, timeout = INFINITE_LOOP_TIMEOUT)
1283 public void copyInputStreamNullWriterJunkEncodingZeroBufSz() throws Exception {
1284 String probe = "A string \u2345\u00ef";
1285 IOUtil.copy(new ByteArrayInputStream(probe.getBytes()), nullWriter(), "junk", 0);
1286 }
1287
1288 @Test(expected = UnsupportedEncodingException.class, timeout = INFINITE_LOOP_TIMEOUT)
1289 public void copyInputStreamValidWriterJunkEncodingZeroBufSz() throws Exception {
1290 String probe = "A string \u2345\u00ef";
1291 StringWriter writer = new DontCloseStringWriter();
1292 IOUtil.copy(new ByteArrayInputStream(probe.getBytes()), writer, "junk", 0);
1293 assertThat(writer.toString().getBytes(), is(probe.getBytes()));
1294 }
1295
1296 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
1297 public void copyNullInputStreamNullWriterValidEncodingZeroBufSz() throws Exception {
1298 IOUtil.copy(nullInputStream(), nullWriter(), "utf-16", 0);
1299 }
1300
1301 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
1302 public void copyNullInputStreamValidWriterValidEncodingZeroBufSz() throws Exception {
1303 IOUtil.copy(nullInputStream(), new DontCloseStringWriter(), "utf-16", 0);
1304 }
1305
1306
1307
1308
1309
1310 @Test(expected = NullPointerException.class)
1311 public void copyNullStringNullWriter() throws Exception {
1312 IOUtil.copy(nullString(), nullWriter());
1313 }
1314
1315 @Test(expected = NullPointerException.class)
1316 public void copyEmptyStringNullWriter() throws Exception {
1317 IOUtil.copy(emptyString(), nullWriter());
1318 }
1319
1320 @Test
1321 public void copyNullStringValidWriter() throws Exception {
1322 IOUtil.copy(nullString(), new DontCloseStringWriter());
1323 }
1324
1325 @Test
1326 public void copyEmptyStringValidWriter() throws Exception {
1327 StringWriter writer = new DontCloseStringWriter();
1328 IOUtil.copy(emptyString(), writer);
1329 assertThat(writer.toString(), is(emptyString()));
1330 }
1331
1332 @Test(expected = NullPointerException.class)
1333 public void copyStringNullWriter() throws Exception {
1334 String probe = "A string \u2345\u00ef";
1335 IOUtil.copy(probe, nullWriter());
1336 }
1337
1338 @Test
1339 public void copyStringValidWriter() throws Exception {
1340 String probe = "A string \u2345\u00ef";
1341 StringWriter writer = new DontCloseStringWriter();
1342 IOUtil.copy(probe, writer);
1343 assertThat(writer.toString(), is(probe));
1344 }
1345
1346 @Test(expected = NullPointerException.class)
1347 public void copyNullStringNullOutputStream() throws Exception {
1348 IOUtil.copy(nullString(), nullOutputStream());
1349 }
1350
1351 @Test(expected = NullPointerException.class)
1352 public void copyEmptyStringNullOutputStream() throws Exception {
1353 IOUtil.copy(emptyString(), nullOutputStream());
1354 }
1355
1356 @Test(expected = NullPointerException.class)
1357 public void copyNullStringValidOutputStream() throws Exception {
1358 IOUtil.copy(nullString(), new DontCloseByteArrayOutputStream());
1359 }
1360
1361 @Test
1362 public void copyEmptyStringValidOutputStream() throws Exception {
1363 ByteArrayOutputStream OutputStream = new DontCloseByteArrayOutputStream();
1364 IOUtil.copy(emptyString(), OutputStream);
1365 assertThat(OutputStream.toByteArray(), is(emptyString().getBytes()));
1366 }
1367
1368 @Test(expected = NullPointerException.class)
1369 public void copyStringNullOutputStream() throws Exception {
1370 String probe = "A string \u2345\u00ef";
1371 IOUtil.copy(probe, nullOutputStream());
1372 }
1373
1374 @Test
1375 public void copyStringValidOutputStream() throws Exception {
1376 String probe = "A string \u2345\u00ef";
1377 ByteArrayOutputStream OutputStream = new DontCloseByteArrayOutputStream();
1378 IOUtil.copy(probe, OutputStream);
1379 assertThat(OutputStream.toByteArray(), is(probe.getBytes()));
1380 }
1381
1382 @Test(expected = NullPointerException.class)
1383 public void copyNullStringNullOutputStreamNegBufSz() throws Exception {
1384 IOUtil.copy(nullString(), nullOutputStream(), -1);
1385 }
1386
1387 @Test(expected = NullPointerException.class)
1388 public void copyEmptyStringNullOutputStreamNegBufSz() throws Exception {
1389 IOUtil.copy(emptyString(), nullOutputStream(), -1);
1390 }
1391
1392 @Test(expected = NullPointerException.class)
1393 public void copyNullStringValidOutputStreamNegBufSz() throws Exception {
1394 IOUtil.copy(nullString(), new DontCloseByteArrayOutputStream(), -1);
1395 }
1396
1397 @Test(expected = NegativeArraySizeException.class)
1398 public void copyEmptyStringValidOutputStreamNegBufSz() throws Exception {
1399 ByteArrayOutputStream OutputStream = new DontCloseByteArrayOutputStream();
1400 IOUtil.copy(emptyString(), OutputStream, -1);
1401 assertThat(OutputStream.toByteArray(), is(emptyString().getBytes()));
1402 }
1403
1404 @Test(expected = NullPointerException.class)
1405 public void copyStringNullOutputStreamNegBufSz() throws Exception {
1406 String probe = "A string \u2345\u00ef";
1407 IOUtil.copy(probe, nullOutputStream(), -1);
1408 }
1409
1410 @Test(expected = NegativeArraySizeException.class)
1411 public void copyStringValidOutputStreamNegBufSz() throws Exception {
1412 String probe = "A string \u2345\u00ef";
1413 ByteArrayOutputStream OutputStream = new DontCloseByteArrayOutputStream();
1414 IOUtil.copy(probe, OutputStream, -1);
1415 assertThat(OutputStream.toByteArray(), is(probe.getBytes()));
1416 }
1417
1418 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
1419 public void copyNullStringNullOutputStreamZeroBufSz() throws Exception {
1420 IOUtil.copy(nullString(), nullOutputStream(), 0);
1421 }
1422
1423 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
1424 public void copyEmptyStringNullOutputStreamZeroBufSz() throws Exception {
1425 IOUtil.copy(emptyString(), nullOutputStream(), 0);
1426 }
1427
1428 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
1429 public void copyNullStringValidOutputStreamZeroBufSz() throws Exception {
1430 IOUtil.copy(nullString(), new DontCloseByteArrayOutputStream(), 0);
1431 }
1432
1433 @Test(expected = NullPointerException.class)
1434 public void copyNullStringNullOutputStreamPosBufSz() throws Exception {
1435 IOUtil.copy(nullString(), nullOutputStream(), 1);
1436 }
1437
1438 @Test(expected = NullPointerException.class)
1439 public void copyEmptyStringNullOutputStreamPosBufSz() throws Exception {
1440 IOUtil.copy(emptyString(), nullOutputStream(), 1);
1441 }
1442
1443 @Test(expected = NullPointerException.class)
1444 public void copyNullStringValidOutputStreamPosBufSz() throws Exception {
1445 IOUtil.copy(nullString(), new DontCloseByteArrayOutputStream(), 1);
1446 }
1447
1448 @Test
1449 public void copyEmptyStringValidOutputStreamPosBufSz() throws Exception {
1450 ByteArrayOutputStream OutputStream = new DontCloseByteArrayOutputStream();
1451 IOUtil.copy(emptyString(), OutputStream, 1);
1452 assertThat(OutputStream.toByteArray(), is(emptyString().getBytes()));
1453 }
1454
1455 @Test(expected = NullPointerException.class)
1456 public void copyStringNullOutputStreamPosBufSz() throws Exception {
1457 String probe = "A string \u2345\u00ef";
1458 IOUtil.copy(probe, nullOutputStream(), 1);
1459 }
1460
1461 @Test
1462 public void copyStringValidOutputStreamPosBufSz() throws Exception {
1463 String probe = "A string \u2345\u00ef";
1464 ByteArrayOutputStream OutputStream = new DontCloseByteArrayOutputStream();
1465 IOUtil.copy(probe, OutputStream, 1);
1466 assertThat(OutputStream.toByteArray(), is(probe.getBytes()));
1467 }
1468
1469 @Test(expected = NullPointerException.class)
1470 public void copyNullReaderNullWriter() throws Exception {
1471 IOUtil.copy(nullReader(), nullWriter());
1472 }
1473
1474 @Test(expected = NullPointerException.class)
1475 public void copyEmptyReaderNullWriter() throws Exception {
1476 IOUtil.copy(emptyReader(), nullWriter());
1477 }
1478
1479 @Test(expected = NullPointerException.class)
1480 public void copyNullReaderValidWriter() throws Exception {
1481 IOUtil.copy(nullReader(), new DontCloseStringWriter());
1482 }
1483
1484 @Test
1485 public void copyEmptyReaderValidWriter() throws Exception {
1486 StringWriter writer = new DontCloseStringWriter();
1487 IOUtil.copy(emptyReader(), writer);
1488 assertThat(writer.toString(), is(emptyString()));
1489 }
1490
1491 @Test(expected = NullPointerException.class)
1492 public void copyReaderNullWriter() throws Exception {
1493 String probe = "A string \u2345\u00ef";
1494 IOUtil.copy(new StringReader(probe), nullWriter());
1495 }
1496
1497 @Test
1498 public void copyReaderValidWriter() throws Exception {
1499 String probe = "A string \u2345\u00ef";
1500 StringWriter writer = new DontCloseStringWriter();
1501 IOUtil.copy(new StringReader(probe), writer);
1502 assertThat(writer.toString(), is(probe));
1503 }
1504
1505
1506
1507
1508
1509 @Test(expected = NegativeArraySizeException.class)
1510 public void copyNullReaderNullWriterNegBufSz() throws Exception {
1511 IOUtil.copy(nullReader(), nullWriter(), -1);
1512 }
1513
1514 @Test(expected = NegativeArraySizeException.class)
1515 public void copyEmptyReaderNullWriterNegBufSz() throws Exception {
1516 IOUtil.copy(emptyReader(), nullWriter(), -1);
1517 }
1518
1519 @Test(expected = NegativeArraySizeException.class)
1520 public void copyNullReaderValidWriterNegBufSz() throws Exception {
1521 IOUtil.copy(nullReader(), new DontCloseStringWriter(), -1);
1522 }
1523
1524 @Test(expected = NegativeArraySizeException.class)
1525 public void copyEmptyReaderValidWriterNegBufSz() throws Exception {
1526 StringWriter writer = new DontCloseStringWriter();
1527 IOUtil.copy(emptyReader(), writer, -1);
1528 assertThat(writer.toString(), is(emptyString()));
1529 }
1530
1531 @Test(expected = NegativeArraySizeException.class)
1532 public void copyReaderNullWriterNegBufSz() throws Exception {
1533 String probe = "A string \u2345\u00ef";
1534 IOUtil.copy(new StringReader(probe), nullWriter(), -1);
1535 }
1536
1537 @Test(expected = NegativeArraySizeException.class)
1538 public void copyReaderValidWriterNegBufSz() throws Exception {
1539 String probe = "A string \u2345\u00ef";
1540 StringWriter writer = new DontCloseStringWriter();
1541 IOUtil.copy(new StringReader(probe), writer, -1);
1542 assertThat(writer.toString(), is(probe));
1543 }
1544
1545 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
1546 public void copyNullReaderNullWriterZeroBufSz() throws Exception {
1547 IOUtil.copy(nullReader(), nullWriter(), 0);
1548 }
1549
1550 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
1551 public void copyEmptyReaderNullWriterZeroBufSz() throws Exception {
1552 IOUtil.copy(emptyReader(), nullWriter(), 0);
1553 }
1554
1555 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
1556 public void copyNullReaderValidWriterZeroBufSz() throws Exception {
1557 IOUtil.copy(nullReader(), new DontCloseStringWriter(), 0);
1558 }
1559
1560 @Test(expected = NullPointerException.class)
1561 public void copyNullReaderNullWriterPosBufSz() throws Exception {
1562 IOUtil.copy(nullReader(), nullWriter(), 1);
1563 }
1564
1565 @Test(expected = NullPointerException.class)
1566 public void copyEmptyReaderNullWriterPosBufSz() throws Exception {
1567 IOUtil.copy(emptyReader(), nullWriter(), 1);
1568 }
1569
1570 @Test(expected = NullPointerException.class)
1571 public void copyNullReaderValidWriterPosBufSz() throws Exception {
1572 IOUtil.copy(nullReader(), new DontCloseStringWriter(), 1);
1573 }
1574
1575 @Test
1576 public void copyEmptyReaderValidWriterPosBufSz() throws Exception {
1577 StringWriter writer = new DontCloseStringWriter();
1578 IOUtil.copy(emptyReader(), writer, 1);
1579 assertThat(writer.toString(), is(emptyString()));
1580 }
1581
1582 @Test(expected = NullPointerException.class)
1583 public void copyReaderNullWriterPosBufSz() throws Exception {
1584 String probe = "A string \u2345\u00ef";
1585 IOUtil.copy(new StringReader(probe), nullWriter(), 1);
1586 }
1587
1588 @Test
1589 public void copyReaderValidWriterPosBufSz() throws Exception {
1590 String probe = "A string \u2345\u00ef";
1591 StringWriter writer = new DontCloseStringWriter();
1592 IOUtil.copy(new StringReader(probe), writer, 1);
1593 assertThat(writer.toString(), is(probe));
1594 }
1595
1596
1597
1598
1599
1600 @Test(expected = NegativeArraySizeException.class)
1601 public void toByteArrayFromInputStreamNegBufSz() throws Exception {
1602 String probe = "A string \u2345\u00ef";
1603 assertThat(
1604 IOUtil.toByteArray(new DontCloseByteArrayInputStream(IOUtil.toByteArray(probe)), -1),
1605 is(probe.getBytes()));
1606 }
1607
1608 @Test(expected = NegativeArraySizeException.class)
1609 public void toByteArrayNullInputStreamNegBufSz() throws Exception {
1610 IOUtil.toByteArray(nullInputStream(), -1);
1611 }
1612
1613 @Test
1614 public void toByteArrayFromInputStreamPosBufSz() throws Exception {
1615 String probe = "A string \u2345\u00ef";
1616 assertThat(
1617 IOUtil.toByteArray(new DontCloseByteArrayInputStream(IOUtil.toByteArray(probe)), +1),
1618 is(probe.getBytes()));
1619 }
1620
1621 @Test(expected = NullPointerException.class)
1622 public void toByteArrayNullInputStreamPosBufSz() throws Exception {
1623 IOUtil.toByteArray(nullInputStream(), +1);
1624 }
1625
1626
1627
1628
1629
1630 @Test(expected = NegativeArraySizeException.class)
1631 public void toByteArrayFromReaderNegBufSz() throws Exception {
1632 String probe = "A string \u2345\u00ef";
1633 assertThat(IOUtil.toByteArray(new DontCloseStringReader(probe), -1), is(probe.getBytes()));
1634 }
1635
1636 @Test(expected = NegativeArraySizeException.class)
1637 public void toByteArrayNullReaderNegBufSz() throws Exception {
1638 IOUtil.toByteArray(nullReader(), -1);
1639 }
1640
1641 @Test
1642 public void toByteArrayFromReaderPosBufSz() throws Exception {
1643 String probe = "A string \u2345\u00ef";
1644 assertThat(IOUtil.toByteArray(new DontCloseStringReader(probe), +1), is(probe.getBytes()));
1645 }
1646
1647 @Test(expected = NullPointerException.class)
1648 public void toByteArrayNullReaderPosBufSz() throws Exception {
1649 IOUtil.toByteArray(nullReader(), +1);
1650 }
1651
1652
1653
1654
1655
1656 @Test(expected = NegativeArraySizeException.class)
1657 public void toByteArrayFromStringNegBufSz() throws Exception {
1658 String probe = "A string \u2345\u00ef";
1659 assertThat(IOUtil.toByteArray(probe, -1), is(probe.getBytes()));
1660 }
1661
1662 @Test(expected = NullPointerException.class)
1663 public void toByteArrayNullStringNegBufSz() throws Exception {
1664 IOUtil.toByteArray(nullString(), -1);
1665 }
1666
1667 @Test
1668 public void toByteArrayFromStringPosBufSz() throws Exception {
1669 String probe = "A string \u2345\u00ef";
1670 assertThat(IOUtil.toByteArray(probe, +1), is(probe.getBytes()));
1671 }
1672
1673 @Test(expected = NullPointerException.class)
1674 public void toByteArrayNullStringPosBufSz() throws Exception {
1675 IOUtil.toByteArray(nullString(), +1);
1676 }
1677
1678
1679
1680
1681
1682 @Test(expected = NegativeArraySizeException.class)
1683 public void toStringFromReaderNegBufSz() throws Exception {
1684 String probe = "A string \u2345\u00ef";
1685 assertThat(IOUtil.toString(new DontCloseStringReader(probe), -1), is(probe));
1686 }
1687
1688 @Test(expected = NegativeArraySizeException.class)
1689 public void toStringNullReaderNegBufSz() throws Exception {
1690 IOUtil.toString(nullReader(), -1);
1691 }
1692
1693 @Test
1694 public void toStringFromReaderPosBufSz() throws Exception {
1695 String probe = "A string \u2345\u00ef";
1696 assertThat(IOUtil.toString(new DontCloseStringReader(probe), +1), is(probe));
1697 }
1698
1699 @Test(expected = NullPointerException.class)
1700 public void toStringNullReaderPosBufSz() throws Exception {
1701 IOUtil.toString(nullReader(), +1);
1702 }
1703
1704
1705
1706
1707
1708 @Test(expected = NullPointerException.class)
1709 public void copyNullReaderNullOutputStream() throws Exception {
1710 IOUtil.copy(nullReader(), nullOutputStream());
1711 }
1712
1713 @Test(expected = NullPointerException.class)
1714 public void copyNullReaderValidOutputStream() throws Exception {
1715 IOUtil.copy(nullReader(), new DontCloseByteArrayOutputStream());
1716 }
1717
1718 @Test(expected = NullPointerException.class)
1719 public void copyEmptyReaderNullOutputStream() throws Exception {
1720 IOUtil.copy(emptyReader(), nullOutputStream());
1721 }
1722
1723 @Test
1724 public void copyEmptyReaderValidOutputStream() throws Exception {
1725 IOUtil.copy(emptyReader(), new DontCloseByteArrayOutputStream());
1726 }
1727
1728 @Test
1729 public void copyReaderValidOutputStream() throws Exception {
1730 ByteArrayOutputStream outputStream = new DontCloseByteArrayOutputStream();
1731 String probe = "A string \u2345\u00ef";
1732 IOUtil.copy(new DontCloseStringReader(probe), outputStream);
1733 assertThat(outputStream.toByteArray(), is(probe.getBytes()));
1734 }
1735
1736
1737
1738
1739
1740 @Test(expected = NullPointerException.class)
1741 public void copyNullReaderNullOutputStreamNegBufSz() throws Exception {
1742 IOUtil.copy(nullReader(), nullOutputStream(), -1);
1743 }
1744
1745 @Test(expected = NegativeArraySizeException.class)
1746 public void copyNullReaderValidOutputStreamNegBufSz() throws Exception {
1747 IOUtil.copy(nullReader(), new DontCloseByteArrayOutputStream(), -1);
1748 }
1749
1750 @Test(expected = NullPointerException.class)
1751 public void copyEmptyReaderNullOutputStreamNegBufSz() throws Exception {
1752 IOUtil.copy(emptyReader(), nullOutputStream(), -1);
1753 }
1754
1755 @Test(expected = NegativeArraySizeException.class)
1756 public void copyEmptyReaderValidOutputStreamNegBufSz() throws Exception {
1757 IOUtil.copy(emptyReader(), new DontCloseByteArrayOutputStream(), -1);
1758 }
1759
1760 @Test(expected = NegativeArraySizeException.class)
1761 public void copyReaderValidOutputStreamNegBufSz() throws Exception {
1762 ByteArrayOutputStream outputStream = new DontCloseByteArrayOutputStream();
1763 String probe = "A string \u2345\u00ef";
1764 IOUtil.copy(new DontCloseStringReader(probe), outputStream, -1);
1765 assertThat(outputStream.toByteArray(), is(probe.getBytes()));
1766 }
1767
1768 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
1769 public void copyNullReaderNullOutputStreamZeroBufSz() throws Exception {
1770 IOUtil.copy(nullReader(), nullOutputStream(), 0);
1771 }
1772
1773 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
1774 public void copyNullReaderValidOutputStreamZeroBufSz() throws Exception {
1775 IOUtil.copy(nullReader(), new DontCloseByteArrayOutputStream(), 0);
1776 }
1777
1778 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
1779 public void copyEmptyReaderNullOutputStreamZeroBufSz() throws Exception {
1780 IOUtil.copy(emptyReader(), nullOutputStream(), 0);
1781 }
1782
1783 @Test(expected = NullPointerException.class)
1784 public void copyNullReaderNullOutputStreamPosBufSz() throws Exception {
1785 IOUtil.copy(nullReader(), nullOutputStream(), 1);
1786 }
1787
1788 @Test(expected = NullPointerException.class)
1789 public void copyNullReaderValidOutputStreamPosBufSz() throws Exception {
1790 IOUtil.copy(nullReader(), new DontCloseByteArrayOutputStream(), 1);
1791 }
1792
1793 @Test(expected = NullPointerException.class)
1794 public void copyEmptyReaderNullOutputStreamPosBufSz() throws Exception {
1795 IOUtil.copy(emptyReader(), nullOutputStream(), 1);
1796 }
1797
1798 @Test
1799 public void copyEmptyReaderValidOutputStreamPosBufSz() throws Exception {
1800 IOUtil.copy(emptyReader(), new DontCloseByteArrayOutputStream(), 1);
1801 }
1802
1803 @Test
1804 public void copyReaderValidOutputStreamPosBufSz() throws Exception {
1805 ByteArrayOutputStream outputStream = new DontCloseByteArrayOutputStream();
1806 String probe = "A string \u2345\u00ef";
1807 IOUtil.copy(new DontCloseStringReader(probe), outputStream, 1);
1808 assertThat(outputStream.toByteArray(), is(probe.getBytes()));
1809 }
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830 @Test(expected = NullPointerException.class)
1831 public void copyNullByteArrayNullWriter() throws Exception {
1832 IOUtil.copy(nullByteArray(), nullWriter());
1833 }
1834
1835 @Test(expected = NullPointerException.class)
1836 public void copyEmptyByteArrayNullWriter() throws Exception {
1837 IOUtil.copy(emptyByteArray(), nullWriter());
1838 }
1839
1840 @Test
1841 public void copyEmptyByteArrayValidWriter() throws Exception {
1842 StringWriter writer = new DontCloseStringWriter();
1843 IOUtil.copy(emptyByteArray(), writer);
1844 assertThat(writer.toString(), is(emptyString()));
1845 }
1846
1847 @Test(expected = NullPointerException.class)
1848 public void copyByteArrayNullWriter() throws Exception {
1849 String probe = "A string \u2345\u00ef";
1850 IOUtil.copy(probe.getBytes(), nullWriter());
1851 }
1852
1853 @Test
1854 public void copyByteArrayValidWriter() throws Exception {
1855 String probe = "A string \u2345\u00ef";
1856 StringWriter writer = new DontCloseStringWriter();
1857 IOUtil.copy(probe.getBytes(), writer);
1858 assertThat(writer.toString().getBytes(), is(probe.getBytes()));
1859 }
1860
1861
1862
1863
1864
1865 @Test(expected = NullPointerException.class)
1866 public void copyNullByteArrayNullWriterNegBufSz() throws Exception {
1867 IOUtil.copy(nullByteArray(), nullWriter(), -1);
1868 }
1869
1870 @Test(expected = NegativeArraySizeException.class)
1871 public void copyEmptyByteArrayNullWriterNegBufSz() throws Exception {
1872 IOUtil.copy(emptyByteArray(), nullWriter(), -1);
1873 }
1874
1875 @Test(expected = NegativeArraySizeException.class)
1876 public void copyEmptyByteArrayValidWriterNegBufSz() throws Exception {
1877 IOUtil.copy(emptyByteArray(), new DontCloseStringWriter(), -1);
1878 }
1879
1880 @Test(expected = NegativeArraySizeException.class)
1881 public void copyByteArrayNullWriterNegBufSz() throws Exception {
1882 String probe = "A string \u2345\u00ef";
1883 IOUtil.copy(probe.getBytes(), nullWriter(), -1);
1884 }
1885
1886 @Test(expected = NegativeArraySizeException.class)
1887 public void copyByteArrayValidWriterNegBufSz() throws Exception {
1888 String probe = "A string \u2345\u00ef";
1889 StringWriter writer = new DontCloseStringWriter();
1890 IOUtil.copy(probe.getBytes(), writer, -1);
1891 assertThat(writer.toString().getBytes(), is(probe.getBytes()));
1892 }
1893
1894 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
1895 public void copyNullByteArrayNullWriterZeroBufSz() throws Exception {
1896 IOUtil.copy(nullByteArray(), nullWriter(), 0);
1897 }
1898
1899 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
1900 public void copyNullByteArrayValidWriterZeroBufSz() throws Exception {
1901 IOUtil.copy(nullByteArray(), new DontCloseStringWriter(), 0);
1902 }
1903
1904 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
1905 public void copyEmptyByteArrayNullWriterZeroBufSz() throws Exception {
1906 IOUtil.copy(emptyByteArray(), nullWriter(), 0);
1907 }
1908
1909 @Test(expected = NullPointerException.class)
1910 public void copyNullByteArrayNullWriterPosBufSz() throws Exception {
1911 IOUtil.copy(nullByteArray(), nullWriter(), 1);
1912 }
1913
1914 @Test(expected = NullPointerException.class)
1915 public void copyNullByteArrayValidWriterPosBufSz() throws Exception {
1916 IOUtil.copy(nullByteArray(), new DontCloseStringWriter(), 1);
1917 }
1918
1919 @Test(expected = NullPointerException.class)
1920 public void copyEmptyByteArrayNullWriterPosBufSz() throws Exception {
1921 IOUtil.copy(emptyByteArray(), nullWriter(), 1);
1922 }
1923
1924 @Test
1925 public void copyEmptyByteArrayValidWriterPosBufSz() throws Exception {
1926 StringWriter writer = new DontCloseStringWriter();
1927 IOUtil.copy(emptyByteArray(), writer, 1);
1928 assertThat(writer.toString(), is(emptyString()));
1929 }
1930
1931 @Test
1932 public void copyByteArrayValidWriterPosBufSz() throws Exception {
1933 String probe = "A string \u2345\u00ef";
1934 StringWriter writer = new DontCloseStringWriter();
1935 IOUtil.copy(probe.getBytes(), writer, 1);
1936 assertThat(writer.toString().getBytes(), is(probe.getBytes()));
1937 }
1938
1939
1940
1941
1942
1943 @Test(expected = NullPointerException.class)
1944 public void copyNullByteArrayNullWriterNullEncoding() throws Exception {
1945 IOUtil.copy(nullByteArray(), nullWriter(), null);
1946 }
1947
1948 @Test(expected = NullPointerException.class)
1949 public void copyNullByteArrayValidWriterNullEncoding() throws Exception {
1950 IOUtil.copy(nullByteArray(), new DontCloseStringWriter(), null);
1951 }
1952
1953 @Test(expected = NullPointerException.class)
1954 public void copyEmptyByteArrayNullWriterNullEncoding() throws Exception {
1955 IOUtil.copy(emptyByteArray(), nullWriter(), null);
1956 }
1957
1958 @Test(expected = NullPointerException.class)
1959 public void copyEmptyByteArrayValidWriterNullEncoding() throws Exception {
1960 IOUtil.copy(emptyByteArray(), new DontCloseStringWriter(), null);
1961 }
1962
1963 @Test(expected = NullPointerException.class)
1964 public void copyByteArrayNullEncoding() throws Exception {
1965 String probe = "A string \u2345\u00ef";
1966 StringWriter writer = new DontCloseStringWriter();
1967 IOUtil.copy(probe.getBytes(), writer, null);
1968 assertThat(writer.toString().getBytes(), is(probe.getBytes()));
1969 }
1970
1971 @Test(expected = NullPointerException.class)
1972 public void copyNullByteArrayNullWriterJunkEncoding() throws Exception {
1973 IOUtil.copy(nullByteArray(), nullWriter(), "junk");
1974 }
1975
1976 @Test(expected = NullPointerException.class)
1977 public void copyNullByteArrayValidWriterJunkEncoding() throws Exception {
1978 IOUtil.copy(nullByteArray(), new DontCloseStringWriter(), "junk");
1979 }
1980
1981 @Test(expected = UnsupportedEncodingException.class)
1982 public void copyEmptyByteArrayNullWriterJunkEncoding() throws Exception {
1983 IOUtil.copy(emptyByteArray(), nullWriter(), "junk");
1984 }
1985
1986 @Test(expected = UnsupportedEncodingException.class)
1987 public void copyEmptyByteArrayValidWriterJunkEncoding() throws Exception {
1988 IOUtil.copy(emptyByteArray(), new DontCloseStringWriter(), "junk");
1989 }
1990
1991 @Test(expected = UnsupportedEncodingException.class)
1992 public void copyByteArrayNullWriterJunkEncoding() throws Exception {
1993 String probe = "A string \u2345\u00ef";
1994 IOUtil.copy(probe.getBytes(), nullWriter(), "junk");
1995 }
1996
1997 @Test(expected = UnsupportedEncodingException.class)
1998 public void copyByteArrayValidWriterJunkEncoding() throws Exception {
1999 String probe = "A string \u2345\u00ef";
2000 StringWriter writer = new DontCloseStringWriter();
2001 IOUtil.copy(probe.getBytes(), writer, "junk");
2002 assertThat(writer.toString().getBytes(), is(probe.getBytes()));
2003 }
2004
2005 @Test(expected = NullPointerException.class)
2006 public void copyNullByteArrayNullWriterValidEncoding() throws Exception {
2007 IOUtil.copy(nullByteArray(), nullWriter(), "utf-16");
2008 }
2009
2010 @Test(expected = NullPointerException.class)
2011 public void copyEmptyByteArrayNullWriterValidEncoding() throws Exception {
2012 IOUtil.copy(emptyByteArray(), nullWriter(), "utf-16");
2013 }
2014
2015 @Test(expected = NullPointerException.class)
2016 public void copyNullByteArrayValidWriterValidEncoding() throws Exception {
2017 IOUtil.copy(nullByteArray(), new DontCloseStringWriter(), "utf-16");
2018 }
2019
2020 @Test
2021 public void copyEmptyByteArrayValidWriterValidEncoding() throws Exception {
2022 StringWriter writer = new DontCloseStringWriter();
2023 IOUtil.copy(emptyByteArray(), writer, "utf-16");
2024 assertThat(writer.toString(), is(emptyString()));
2025 }
2026
2027 @Test(expected = NullPointerException.class)
2028 public void copyByteArrayNullWriterValidEncoding() throws Exception {
2029 String probe = "A string \u2345\u00ef";
2030 IOUtil.copy(probe.getBytes("utf-16"), nullWriter(), "utf-16");
2031 }
2032
2033 @Test
2034 public void copyByteArrayValidWriterValidEncoding() throws Exception {
2035 String probe = "A string \u2345\u00ef";
2036 StringWriter writer = new DontCloseStringWriter();
2037 IOUtil.copy(probe.getBytes("utf-16"), writer, "utf-16");
2038 assertThat(writer.toString().getBytes("utf-8"), is(probe.getBytes("utf-8")));
2039 }
2040
2041
2042
2043
2044
2045 @Test(expected = NullPointerException.class)
2046 public void copyNullByteArrayNullWriterNullEncodingNegBufSz() throws Exception {
2047 IOUtil.copy(nullByteArray(), nullWriter(), null, -1);
2048 }
2049
2050 @Test(expected = NullPointerException.class)
2051 public void copyNullByteArrayValidWriterNullEncodingNegBufSz() throws Exception {
2052 IOUtil.copy(nullByteArray(), new DontCloseStringWriter(), null, -1);
2053 }
2054
2055 @Test(expected = NullPointerException.class)
2056 public void copyEmptyByteArrayNullWriterNullEncodingNegBufSz() throws Exception {
2057 IOUtil.copy(emptyByteArray(), nullWriter(), null, -1);
2058 }
2059
2060 @Test(expected = NullPointerException.class)
2061 public void copyEmptyByteArrayValidWriterNullEncodingNegBufSz() throws Exception {
2062 StringWriter writer = new DontCloseStringWriter();
2063 IOUtil.copy(emptyByteArray(), writer, null, -1);
2064 assertThat(writer.toString(), is(emptyString()));
2065 }
2066
2067 @Test(expected = NullPointerException.class)
2068 public void copyByteArrayNullWriterNullEncodingNegBufSz() throws Exception {
2069 String probe = "A string \u2345\u00ef";
2070 IOUtil.copy(probe.getBytes(), nullWriter(), null, -1);
2071 }
2072
2073 @Test(expected = NullPointerException.class)
2074 public void copyByteArrayValidWriterNullEncodingNegBufSz() throws Exception {
2075 String probe = "A string \u2345\u00ef";
2076 StringWriter writer = new DontCloseStringWriter();
2077 IOUtil.copy(probe.getBytes(), writer, null, -1);
2078 assertThat(writer.toString().getBytes(), is(probe.getBytes()));
2079 }
2080
2081 @Test(expected = NullPointerException.class)
2082 public void copyNullByteArrayNullWriterJunkEncodingNegBufSz() throws Exception {
2083 IOUtil.copy(nullByteArray(), nullWriter(), "junk", -1);
2084 }
2085
2086 @Test(expected = NullPointerException.class)
2087 public void copyNullByteArrayValidWriterJunkEncodingNegBufSz() throws Exception {
2088 IOUtil.copy(nullByteArray(), new DontCloseStringWriter(), "junk", -1);
2089 }
2090
2091 @Test(expected = UnsupportedEncodingException.class)
2092 public void copyEmptyByteArrayNullWriterJunkEncodingNegBufSz() throws Exception {
2093 IOUtil.copy(emptyByteArray(), nullWriter(), "junk", -1);
2094 }
2095
2096 @Test(expected = UnsupportedEncodingException.class)
2097 public void copyEmptyByteArrayJunkEncodingNegBufSz() throws Exception {
2098 StringWriter writer = new DontCloseStringWriter();
2099 IOUtil.copy(emptyByteArray(), writer, "junk", -1);
2100 assertThat(writer.toString(), is(emptyString()));
2101 }
2102
2103 @Test(expected = UnsupportedEncodingException.class)
2104 public void copyByteArrayNullWriterJunkEncodingNegBufSz() throws Exception {
2105 String probe = "A string \u2345\u00ef";
2106 IOUtil.copy(probe.getBytes(), nullWriter(), "junk", -1);
2107 }
2108
2109 @Test(expected = UnsupportedEncodingException.class)
2110 public void copyByteArrayValidWriterJunkEncodingNegBufSz() throws Exception {
2111 String probe = "A string \u2345\u00ef";
2112 StringWriter writer = new DontCloseStringWriter();
2113 IOUtil.copy(probe.getBytes(), writer, "junk", -1);
2114 assertThat(writer.toString().getBytes(), is(probe.getBytes()));
2115 }
2116
2117 @Test(expected = NullPointerException.class)
2118 public void copyNullByteArrayNullWriterValidEncodingNegBufSz() throws Exception {
2119 IOUtil.copy(nullByteArray(), nullWriter(), "utf-16", -1);
2120 }
2121
2122 @Test(expected = NullPointerException.class)
2123 public void copyNullByteArrayValidWriterValidEncodingNegBufSz() throws Exception {
2124 IOUtil.copy(nullByteArray(), new DontCloseStringWriter(), "utf-16", -1);
2125 }
2126
2127 @Test(expected = NegativeArraySizeException.class)
2128 public void copyEmptyByteArrayNullWriterValidEncodingNegBufSz() throws Exception {
2129 IOUtil.copy(emptyByteArray(), nullWriter(), "utf-16", -1);
2130 }
2131
2132 @Test(expected = NegativeArraySizeException.class)
2133 public void copyEmptyByteArrayValidWriterValidEncodingNegBufSz() throws Exception {
2134 StringWriter writer = new DontCloseStringWriter();
2135 IOUtil.copy(emptyByteArray(), writer, "utf-16", -1);
2136 assertThat(writer.toString(), is(emptyString()));
2137 }
2138
2139 @Test(expected = NegativeArraySizeException.class)
2140 public void copyByteArrayNullWriterValidEncodingNegBufSz() throws Exception {
2141 String probe = "A string \u2345\u00ef";
2142 IOUtil.copy(probe.getBytes("utf-16"), nullWriter(), -1);
2143 }
2144
2145 @Test(expected = NegativeArraySizeException.class)
2146 public void copyByteArrayValidEncodingNegBufSz() throws Exception {
2147 String probe = "A string \u2345\u00ef";
2148 StringWriter writer = new DontCloseStringWriter();
2149 IOUtil.copy(probe.getBytes("utf-16"), writer, "utf-16", -1);
2150 assertThat(writer.toString().getBytes("utf-8"), is(probe.getBytes("utf-8")));
2151 }
2152
2153 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
2154 public void copyNullByteArrayNullWriterNullEncodingZeroBufSz() throws Exception {
2155 IOUtil.copy(nullByteArray(), nullWriter(), null, 0);
2156 }
2157
2158 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
2159 public void copyNullByteArrayValidWriterNullEncodingZeroBufSz() throws Exception {
2160 IOUtil.copy(nullByteArray(), new DontCloseStringWriter(), null, 0);
2161 }
2162
2163 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
2164 public void copyEmptyByteArrayNullWriterNullEncodingZeroBufSz() throws Exception {
2165 IOUtil.copy(emptyByteArray(), nullWriter(), null, 0);
2166 }
2167
2168 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
2169 public void copyEmptyByteArrayValidWriterNullEncodingZeroBufSz() throws Exception {
2170 StringWriter writer = new DontCloseStringWriter();
2171 IOUtil.copy(emptyByteArray(), writer, null, 0);
2172 assertThat(writer.toString(), is(emptyString()));
2173 }
2174
2175 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
2176 public void copyByteArrayNullWriterNullEncodingZeroBufSz() throws Exception {
2177 String probe = "A string \u2345\u00ef";
2178 IOUtil.copy(probe.getBytes(), nullWriter(), null, 0);
2179 }
2180
2181 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
2182 public void copyByteArrayValidWriterNullEncodingZeroBufSz() throws Exception {
2183 String probe = "A string \u2345\u00ef";
2184 StringWriter writer = new DontCloseStringWriter();
2185 IOUtil.copy(probe.getBytes(), writer, null, 0);
2186 assertThat(writer.toString().getBytes(), is(probe.getBytes()));
2187 }
2188
2189 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
2190 public void copyNullByteArrayNullWriterJunkEncodingZeroBufSz() throws Exception {
2191 IOUtil.copy(nullByteArray(), nullWriter(), "junk", 0);
2192 }
2193
2194 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
2195 public void copyNullByteArrayValidWriterJunkEncodingZeroBufSz() throws Exception {
2196 IOUtil.copy(nullByteArray(), new DontCloseStringWriter(), "junk", 0);
2197 }
2198
2199 @Test(expected = UnsupportedEncodingException.class, timeout = INFINITE_LOOP_TIMEOUT)
2200 public void copyEmptyByteArrayNullWriterJunkEncodingZeroBufSz() throws Exception {
2201 IOUtil.copy(emptyByteArray(), nullWriter(), "junk", 0);
2202 }
2203
2204 @Test(expected = UnsupportedEncodingException.class, timeout = INFINITE_LOOP_TIMEOUT)
2205 public void copyEmptyByteArrayValidWriterJunkEncodingZeroBufSz() throws Exception {
2206 StringWriter writer = new DontCloseStringWriter();
2207 IOUtil.copy(emptyByteArray(), writer, "junk", 0);
2208 assertThat(writer.toString(), is(emptyString()));
2209 }
2210
2211 @Test(expected = UnsupportedEncodingException.class, timeout = INFINITE_LOOP_TIMEOUT)
2212 public void copyByteArrayNullWriterJunkEncodingZeroBufSz() throws Exception {
2213 String probe = "A string \u2345\u00ef";
2214 IOUtil.copy(probe.getBytes(), nullWriter(), "junk", 0);
2215 }
2216
2217 @Test(expected = UnsupportedEncodingException.class, timeout = INFINITE_LOOP_TIMEOUT)
2218 public void copyByteArrayValidWriterJunkEncodingZeroBufSz() throws Exception {
2219 String probe = "A string \u2345\u00ef";
2220 StringWriter writer = new DontCloseStringWriter();
2221 IOUtil.copy(probe.getBytes(), writer, "junk", 0);
2222 assertThat(writer.toString().getBytes(), is(probe.getBytes()));
2223 }
2224
2225 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
2226 public void copyNullByteArrayNullWriterValidEncodingZeroBufSz() throws Exception {
2227 IOUtil.copy(nullByteArray(), nullWriter(), "utf-16", 0);
2228 }
2229
2230 @Test(expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT)
2231 public void copyNullByteArrayValidWriterValidEncodingZeroBufSz() throws Exception {
2232 IOUtil.copy(nullByteArray(), new DontCloseStringWriter(), "utf-16", 0);
2233 }
2234
2235
2236
2237
2238 private static byte[] nullByteArray() {
2239 return null;
2240 }
2241
2242 private static String nullString() {
2243 return null;
2244 }
2245
2246 private static OutputStream nullOutputStream() {
2247 return null;
2248 }
2249
2250 private static InputStream nullInputStream() {
2251 return null;
2252 }
2253
2254 private static Writer nullWriter() {
2255 return null;
2256 }
2257
2258 private static Reader nullReader() {
2259 return null;
2260 }
2261
2262 private static ByteArrayInputStream emptyInputStream() {
2263 return new ByteArrayInputStream(emptyByteArray());
2264 }
2265
2266 private static Reader emptyReader() {
2267 return new StringReader(emptyString());
2268 }
2269
2270 private static String emptyString() {
2271 return "";
2272 }
2273
2274 private static byte[] emptyByteArray() {
2275 return new byte[0];
2276 }
2277
2278 private static class DontCloseStringWriter extends StringWriter {
2279 @Override
2280 public void close() throws IOException {
2281 throw new UnsupportedOperationException("should not be called");
2282 }
2283 }
2284
2285 private static class DontCloseStringReader extends StringReader {
2286
2287 public DontCloseStringReader(String s) {
2288 super(s);
2289 }
2290
2291 @Override
2292 public void close() {
2293 throw new UnsupportedOperationException("should not be called");
2294 }
2295 }
2296
2297 private static class DontCloseByteArrayInputStream extends ByteArrayInputStream {
2298 public DontCloseByteArrayInputStream(byte[] input) {
2299 super(input);
2300 }
2301
2302 @Override
2303 public void close() throws IOException {
2304 throw new UnsupportedOperationException("should not be called");
2305 }
2306 }
2307
2308 private static class DontCloseByteArrayOutputStream extends ByteArrayOutputStream {
2309 @Override
2310 public void close() throws IOException {
2311 throw new UnsupportedOperationException("should not be called");
2312 }
2313 }
2314 }