View Javadoc
1   package org.apache.maven.shared.utils.io;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.junit.Test;
23  
24  import java.io.BufferedInputStream;
25  import java.io.BufferedOutputStream;
26  import java.io.BufferedReader;
27  import java.io.BufferedWriter;
28  import java.io.ByteArrayInputStream;
29  import java.io.ByteArrayOutputStream;
30  import java.io.IOException;
31  import java.io.InputStream;
32  import java.io.OutputStream;
33  import java.io.Reader;
34  import java.io.StringReader;
35  import java.io.StringWriter;
36  import java.io.UnsupportedEncodingException;
37  import java.io.Writer;
38  import java.util.concurrent.atomic.AtomicBoolean;
39  
40  import static org.hamcrest.CoreMatchers.is;
41  import static org.junit.Assert.assertThat;
42  
43  public class IOUtilTest
44  {
45  
46      private static final long INFINITE_LOOP_TIMEOUT = 500;
47  
48      @Test
49      public void closeReaderWithNull()
50          throws Exception
51      {
52          IOUtil.close( (Reader) null );
53      }
54  
55      @Test
56      public void closeWriterWithNull()
57          throws Exception
58      {
59          IOUtil.close( (Writer) null );
60      }
61  
62      @Test
63      public void closeInputStreamWithNull()
64          throws Exception
65      {
66          IOUtil.close( nullInputStream() );
67      }
68  
69      @Test
70      public void closeOutputStreamWithNull()
71          throws Exception
72      {
73          IOUtil.close( nullOutputStream() );
74      }
75  
76      @Test
77      public void closeReaderWithIOE()
78          throws Exception
79      {
80          IOUtil.close( new BufferedReader( new StringReader( emptyString() ) )
81          {
82              @Override
83              public void close()
84                  throws IOException
85              {
86                  super.close();
87                  throw new IOException( "don't bomb out" );
88              }
89          } );
90      }
91  
92      @Test
93      public void closeWriterWithIOE()
94          throws Exception
95      {
96          IOUtil.close( new BufferedWriter( new StringWriter() )
97          {
98              @Override
99              public void close()
100                 throws IOException
101             {
102                 super.close();
103                 throw new IOException( "don't bomb out" );
104             }
105         } );
106     }
107 
108     @Test
109     public void closeInputStreamWithIOE()
110         throws Exception
111     {
112         IOUtil.close( new BufferedInputStream( emptyInputStream() )
113         {
114             @Override
115             public void close()
116                 throws IOException
117             {
118                 super.close();
119                 throw new IOException( "don't bomb out" );
120             }
121         } );
122     }
123 
124     @Test
125     public void closeOutputStreamWithIOE()
126         throws Exception
127     {
128         IOUtil.close( new BufferedOutputStream( new ByteArrayOutputStream() )
129         {
130             @Override
131             public void close()
132                 throws IOException
133             {
134                 super.close();
135                 throw new IOException( "don't bomb out" );
136             }
137         } );
138     }
139 
140     @Test
141     public void closeReaderCloses()
142         throws Exception
143     {
144         final AtomicBoolean closed = new AtomicBoolean( false );
145         IOUtil.close( new BufferedReader( new StringReader( emptyString() ) )
146         {
147             @Override
148             public void close()
149                 throws IOException
150             {
151                 closed.set( true );
152                 super.close();
153             }
154         } );
155         assertThat( closed.get(), is( true ) );
156     }
157 
158     @Test
159     public void closeWriterCloses()
160         throws Exception
161     {
162         final AtomicBoolean closed = new AtomicBoolean( false );
163         IOUtil.close( new BufferedWriter( new StringWriter() )
164         {
165             @Override
166             public void close()
167                 throws IOException
168             {
169                 closed.set( true );
170                 super.close();
171             }
172         } );
173         assertThat( closed.get(), is( true ) );
174     }
175 
176     @Test
177     public void closeInputStreamCloses()
178         throws Exception
179     {
180         final AtomicBoolean closed = new AtomicBoolean( false );
181         IOUtil.close( new BufferedInputStream( emptyInputStream() )
182         {
183             @Override
184             public void close()
185                 throws IOException
186             {
187                 closed.set( true );
188                 super.close();
189             }
190         } );
191         assertThat( closed.get(), is( true ) );
192     }
193 
194     @Test
195     public void closeOutputStreamCloses()
196         throws Exception
197     {
198         final AtomicBoolean closed = new AtomicBoolean( false );
199         IOUtil.close( new BufferedOutputStream( new ByteArrayOutputStream() )
200         {
201             @Override
202             public void close()
203                 throws IOException
204             {
205                 closed.set( true );
206                 super.close();
207             }
208         } );
209         assertThat( closed.get(), is( true ) );
210     }
211 
212     @Test
213     public void toByteArrayFromString()
214         throws Exception
215     {
216         String probe = "A string \u2345\u00ef";
217         assertThat( IOUtil.toByteArray( probe ), is( probe.getBytes() ) );
218     }
219 
220     @Test
221     public void toByteArrayFromReader()
222         throws Exception
223     {
224         String probe = "A string \u2345\u00ef";
225         assertThat( IOUtil.toByteArray( new StringReader( probe ) ), is( probe.getBytes() ) );
226     }
227 
228     @Test
229     public void toByteArrayFromInputStream()
230         throws Exception
231     {
232         String probe = "A string \u2345\u00ef";
233         assertThat( IOUtil.toByteArray( new DontCloseByteArrayInputStream( IOUtil.toByteArray( probe ) ) ),
234                     is( probe.getBytes() ) );
235     }
236 
237     @Test( expected = NullPointerException.class )
238     public void toByteArrayNullString()
239         throws Exception
240     {
241         IOUtil.toByteArray( (String) null );
242     }
243 
244     @Test( expected = NullPointerException.class )
245     public void toByteArrayNullReader()
246         throws Exception
247     {
248         IOUtil.toByteArray( (Reader) null );
249     }
250 
251     @Test( expected = NullPointerException.class )
252     public void toByteArrayNullInputStream()
253         throws Exception
254     {
255         IOUtil.toByteArray( nullInputStream() );
256     }
257 
258     @Test( expected = IOException.class )
259     public void contentEqualNullNull()
260         throws Exception
261     {
262         IOUtil.contentEquals( null, null );
263     }
264 
265     @Test( expected = IOException.class )
266     public void contentEqualNonNullNull()
267         throws Exception
268     {
269         IOUtil.contentEquals( new DontCloseByteArrayInputStream( emptyByteArray() ), null );
270     }
271 
272     @Test( expected = IOException.class )
273     public void contentEqualNullNonNull()
274         throws Exception
275     {
276         IOUtil.contentEquals( new DontCloseByteArrayInputStream( emptyByteArray() ), null );
277     }
278 
279     @Test
280     public void contentEqualEmptyEmpty()
281         throws Exception
282     {
283         assertThat( IOUtil.contentEquals( new DontCloseByteArrayInputStream( emptyByteArray() ),
284                                           new DontCloseByteArrayInputStream( emptyByteArray() ) ), is( true ) );
285     }
286 
287     @Test
288     public void contentEqualNonEmptyEmpty()
289         throws Exception
290     {
291         assertThat( IOUtil.contentEquals( new DontCloseByteArrayInputStream( new byte[1] ),
292                                           new DontCloseByteArrayInputStream( emptyByteArray() ) ), is( false ) );
293     }
294 
295     @Test
296     public void contentEqualEmptyNonEmpty()
297         throws Exception
298     {
299         assertThat( IOUtil.contentEquals( new DontCloseByteArrayInputStream( emptyByteArray() ),
300                                           new DontCloseByteArrayInputStream( new byte[1] ) ), is( false ) );
301     }
302 
303     @Test
304     public void contentEqualNonEmptyNonEmpty()
305         throws Exception
306     {
307         assertThat( IOUtil.contentEquals( new DontCloseByteArrayInputStream( new byte[1] ),
308                                           new DontCloseByteArrayInputStream( new byte[1] ) ), is( true ) );
309     }
310 
311     @Test
312     public void contentEqualMostlySame()
313         throws Exception
314     {
315         assertThat( IOUtil.contentEquals( new DontCloseByteArrayInputStream( new byte[]{ 1, 2, 3, 4, 5, 6 } ),
316                                           new DontCloseByteArrayInputStream( new byte[]{ 1, 2, 3, 4, 5, 7 } ) ),
317                     is( false ) );
318     }
319 
320     @Test
321     public void contentEqualLargeSame()
322         throws Exception
323     {
324         assertThat( IOUtil.contentEquals( new DontCloseByteArrayInputStream( new byte[8192] ),
325                                           new DontCloseByteArrayInputStream( new byte[8192] ) ), is( true ) );
326     }
327 
328     @Test
329     public void contentEqualLargeDifferent()
330         throws Exception
331     {
332         byte[] buf = new byte[8192];
333         buf[8191] = 1;
334         assertThat( IOUtil.contentEquals( new DontCloseByteArrayInputStream( new byte[8192] ),
335                                           new DontCloseByteArrayInputStream( buf ) ), is( false ) );
336     }
337 
338     @Test( expected = NullPointerException.class )
339     public void toStringNullByteArray()
340         throws Exception
341     {
342         IOUtil.toString( nullByteArray() );
343     }
344 
345     @Test
346     public void toStringEmptyByteArray()
347         throws Exception
348     {
349         assertThat( IOUtil.toString( emptyByteArray() ), is( emptyString() ) );
350     }
351 
352     @Test
353     public void toStringByteArray()
354         throws Exception
355     {
356         String probe = "A string \u2345\u00ef";
357         assertThat( IOUtil.toString( probe.getBytes() ).getBytes(), is( probe.getBytes() ) );
358     }
359 
360     @Test( expected = NullPointerException.class )
361     public void toStringNullByteArrayNegBufSz()
362         throws Exception
363     {
364         IOUtil.toString( nullByteArray(), -1 );
365     }
366 
367     @Test( expected = NegativeArraySizeException.class )
368     public void toStringEmptyByteArrayNegBufSz()
369         throws Exception
370     {
371         assertThat( IOUtil.toString( emptyByteArray(), -1 ), is( emptyString() ) );
372     }
373 
374     @Test( expected = NegativeArraySizeException.class )
375     public void toStringByteArrayNegBufSz()
376         throws Exception
377     {
378         String probe = "A string \u2345\u00ef";
379         assertThat( IOUtil.toString( probe.getBytes(), -1 ), is( probe ) );
380     }
381 
382     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
383     public void toStringNullByteArrayZeroBufSz()
384         throws Exception
385     {
386         IOUtil.toString( nullByteArray(), 0 );
387     }
388 
389     @Test( expected = NullPointerException.class )
390     public void toStringNullByteArrayPosBufSz()
391         throws Exception
392     {
393         IOUtil.toString( nullByteArray(), 1 );
394     }
395 
396     @Test
397     public void toStringEmptyByteArrayPosBufSz()
398         throws Exception
399     {
400         assertThat( IOUtil.toString( emptyByteArray(), 1 ), is( emptyString() ) );
401     }
402 
403     @Test
404     public void toStringByteArrayPosBufSz()
405         throws Exception
406     {
407         String probe = "A string \u2345\u00ef";
408         assertThat( IOUtil.toString( probe.getBytes(), 1 ).getBytes(), is( probe.getBytes() ) );
409     }
410 
411     @Test( expected = NullPointerException.class )
412     public void toStringNullByteArrayNullEncoding()
413         throws Exception
414     {
415         IOUtil.toString( nullByteArray(), null );
416     }
417 
418     @Test( expected = NullPointerException.class )
419     public void toStringEmptyByteArrayNullEncoding()
420         throws Exception
421     {
422         assertThat( IOUtil.toString( emptyByteArray(), null ), is( emptyString() ) );
423     }
424 
425     @Test( expected = NullPointerException.class )
426     public void toStringByteArrayNullEncoding()
427         throws Exception
428     {
429         String probe = "A string \u2345\u00ef";
430         assertThat( IOUtil.toString( probe.getBytes(), null ).getBytes(), is( probe.getBytes() ) );
431     }
432 
433     @Test( expected = NullPointerException.class )
434     public void toStringNullByteArrayJunkEncoding()
435         throws Exception
436     {
437         IOUtil.toString( nullByteArray(), "junk" );
438     }
439 
440     @Test( expected = UnsupportedEncodingException.class )
441     public void toStringEmptyByteArrayJunkEncoding()
442         throws Exception
443     {
444         assertThat( IOUtil.toString( emptyByteArray(), "junk" ), is( emptyString() ) );
445     }
446 
447     @Test( expected = UnsupportedEncodingException.class )
448     public void toStringByteArrayJunkEncoding()
449         throws Exception
450     {
451         String probe = "A string \u2345\u00ef";
452         assertThat( IOUtil.toString( probe.getBytes(), "junk" ).getBytes(), is( probe.getBytes() ) );
453     }
454 
455     @Test( expected = NullPointerException.class )
456     public void toStringNullByteArrayValidEncoding()
457         throws Exception
458     {
459         IOUtil.toString( nullByteArray(), "utf-16" );
460     }
461 
462     @Test
463     public void toStringEmptyByteArrayValidEncoding()
464         throws Exception
465     {
466         assertThat( IOUtil.toString( emptyByteArray(), "utf-16" ), is( emptyString() ) );
467     }
468 
469     @Test
470     public void toStringByteArrayValidEncoding()
471         throws Exception
472     {
473         String probe = "A string \u2345\u00ef";
474         assertThat( IOUtil.toString( probe.getBytes( "utf-16" ), "utf-16" ).getBytes( "utf-8" ),
475                     is( probe.getBytes( "utf-8" ) ) );
476     }
477 
478     @Test( expected = NullPointerException.class )
479     public void toStringNullByteArrayNullEncodingNegBufSz()
480         throws Exception
481     {
482         IOUtil.toString( nullByteArray(), null, -1 );
483     }
484 
485     @Test( expected = NullPointerException.class )
486     public void toStringEmptyByteArrayNullEncodingNegBufSz()
487         throws Exception
488     {
489         assertThat( IOUtil.toString( emptyByteArray(), null, -1 ), is( emptyString() ) );
490     }
491 
492     @Test( expected = NullPointerException.class )
493     public void toStringByteArrayNullEncodingNegBufSz()
494         throws Exception
495     {
496         String probe = "A string \u2345\u00ef";
497         assertThat( IOUtil.toString( probe.getBytes(), null, -1 ).getBytes(), is( probe.getBytes() ) );
498     }
499 
500     @Test( expected = NullPointerException.class )
501     public void toStringNullByteArrayJunkEncodingNegBufSz()
502         throws Exception
503     {
504         IOUtil.toString( nullByteArray(), "junk", -1 );
505     }
506 
507     @Test( expected = UnsupportedEncodingException.class )
508     public void toStringEmptyByteArrayJunkEncodingNegBufSz()
509         throws Exception
510     {
511         assertThat( IOUtil.toString( emptyByteArray(), "junk", -1 ), is( emptyString() ) );
512     }
513 
514     @Test( expected = UnsupportedEncodingException.class )
515     public void toStringByteArrayJunkEncodingNegBufSz()
516         throws Exception
517     {
518         String probe = "A string \u2345\u00ef";
519         assertThat( IOUtil.toString( probe.getBytes(), "junk", -1 ).getBytes(), is( probe.getBytes() ) );
520     }
521 
522     @Test( expected = NullPointerException.class )
523     public void toStringNullByteArrayValidEncodingNegBufSz()
524         throws Exception
525     {
526         IOUtil.toString( nullByteArray(), "utf-16", -1 );
527     }
528 
529     @Test( expected = NegativeArraySizeException.class )
530     public void toStringEmptyByteArrayValidEncodingNegBufSz()
531         throws Exception
532     {
533         assertThat( IOUtil.toString( emptyByteArray(), "utf-16", -1 ), is( emptyString() ) );
534     }
535 
536     @Test( expected = NegativeArraySizeException.class )
537     public void toStringByteArrayValidEncodingNegBufSz()
538         throws Exception
539     {
540         String probe = "A string \u2345\u00ef";
541         assertThat( IOUtil.toString( probe.getBytes( "utf-16" ), "utf-16", -1 ).getBytes( "utf-8" ),
542                     is( probe.getBytes( "utf-8" ) ) );
543     }
544 
545     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
546     public void toStringNullByteArrayNullEncodingZeroBufSz()
547         throws Exception
548     {
549         IOUtil.toString( nullByteArray(), null, 0 );
550     }
551 
552     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
553     public void toStringEmptyByteArrayNullEncodingZeroBufSz()
554         throws Exception
555     {
556         assertThat( IOUtil.toString( emptyByteArray(), null, 0 ), is( emptyString() ) );
557     }
558 
559     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
560     public void toStringByteArrayNullEncodingZeroBufSz()
561         throws Exception
562     {
563         String probe = "A string \u2345\u00ef";
564         assertThat( IOUtil.toString( probe.getBytes(), null, 0 ).getBytes(), is( probe.getBytes() ) );
565     }
566 
567     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
568     public void toStringNullByteArrayJunkEncodingZeroBufSz()
569         throws Exception
570     {
571         IOUtil.toString( nullByteArray(), "junk", 0 );
572     }
573 
574     @Test( expected = UnsupportedEncodingException.class, timeout = INFINITE_LOOP_TIMEOUT )
575     public void toStringEmptyByteArrayJunkEncodingZeroBufSz()
576         throws Exception
577     {
578         assertThat( IOUtil.toString( emptyByteArray(), "junk", 0 ), is( emptyString() ) );
579     }
580 
581     @Test( expected = UnsupportedEncodingException.class, timeout = INFINITE_LOOP_TIMEOUT )
582     public void toStringByteArrayJunkEncodingZeroBufSz()
583         throws Exception
584     {
585         String probe = "A string \u2345\u00ef";
586         assertThat( IOUtil.toString( probe.getBytes(), "junk", 0 ).getBytes(), is( probe.getBytes() ) );
587     }
588 
589     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
590     public void toStringNullByteArrayValidEncodingZeroBufSz()
591         throws Exception
592     {
593         IOUtil.toString( nullByteArray(), "utf-16", 0 );
594     }
595 
596     /*
597      * copy(byte[],OutputStream)
598      */
599 
600     @Test( expected = NullPointerException.class )
601     public void copyNullByteArrayNullOutputStream()
602         throws Exception
603     {
604         IOUtil.copy( nullByteArray(), nullOutputStream() );
605     }
606 
607     @Test( expected = NullPointerException.class )
608     public void copyNullByteArrayValidOutputStream()
609         throws Exception
610     {
611         IOUtil.copy( nullByteArray(), new DontCloseByteArrayOutputStream() );
612     }
613 
614     @Test( expected = NullPointerException.class )
615     public void copyEmptyByteArrayNullOutputStream()
616         throws Exception
617     {
618         IOUtil.copy( emptyByteArray(), nullOutputStream() );
619     }
620 
621     @Test
622     public void copyEmptyByteArrayValidOutputStream()
623         throws Exception
624     {
625         IOUtil.copy( emptyByteArray(), new DontCloseByteArrayOutputStream() );
626     }
627 
628     @Test
629     public void copyByteArrayValidOutputStream()
630         throws Exception
631     {
632         ByteArrayOutputStream outputStream = new DontCloseByteArrayOutputStream();
633         byte[] input = { 1, 2, 3, 4, 5, 6 };
634         IOUtil.copy( input, outputStream );
635         assertThat( outputStream.toByteArray(), is( input ) );
636     }
637 
638     /*
639      * copy(byte[],OutputStream,int)
640      */
641 
642     @Test( expected = NullPointerException.class )
643     public void copyNullByteArrayNullOutputStreamNegBufSz()
644         throws Exception
645     {
646         IOUtil.copy( nullByteArray(), nullOutputStream());
647     }
648 
649     @Test( expected = NullPointerException.class )
650     public void copyNullByteArrayValidOutputStreamNegBufSz()
651         throws Exception
652     {
653         IOUtil.copy( nullByteArray(), new DontCloseByteArrayOutputStream());
654     }
655 
656     @Test( expected = NullPointerException.class )
657     public void copyEmptyByteArrayNullOutputStreamNegBufSz()
658         throws Exception
659     {
660         IOUtil.copy( emptyByteArray(), nullOutputStream());
661     }
662 
663     @Test
664     public void copyEmptyByteArrayValidOutputStreamNegBufSz()
665         throws Exception
666     {
667         IOUtil.copy( emptyByteArray(), new DontCloseByteArrayOutputStream());
668     }
669 
670     @Test
671     public void copyByteArrayValidOutputStreamNegBufSz()
672         throws Exception
673     {
674         ByteArrayOutputStream outputStream = new DontCloseByteArrayOutputStream();
675         byte[] input = { 1, 2, 3, 4, 5, 6 };
676         IOUtil.copy( input, outputStream);
677         assertThat( outputStream.toByteArray(), is( input ) );
678     }
679 
680     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
681     public void copyNullByteArrayNullOutputStreamZeroBufSz()
682         throws Exception
683     {
684         IOUtil.copy( nullByteArray(), nullOutputStream());
685     }
686 
687     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
688     public void copyNullByteArrayValidOutputStreamZeroBufSz()
689         throws Exception
690     {
691         IOUtil.copy( nullByteArray(), new DontCloseByteArrayOutputStream());
692     }
693 
694     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
695     public void copyEmptyByteArrayNullOutputStreamZeroBufSz()
696         throws Exception
697     {
698         IOUtil.copy( emptyByteArray(), nullOutputStream());
699     }
700 
701     @Test( timeout = INFINITE_LOOP_TIMEOUT )
702     public void copyEmptyByteArrayValidOutputStreamZeroBufSz()
703         throws Exception
704     {
705         IOUtil.copy( emptyByteArray(), new DontCloseByteArrayOutputStream());
706     }
707 
708     @Test( timeout = INFINITE_LOOP_TIMEOUT )
709     public void copyByteArrayValidOutputStreamZeroBufSz()
710         throws Exception
711     {
712         ByteArrayOutputStream outputStream = new DontCloseByteArrayOutputStream();
713         byte[] input = { 1, 2, 3, 4, 5, 6 };
714         IOUtil.copy( input, outputStream);
715         assertThat( outputStream.toByteArray(), is( input ) );
716     }
717 
718     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
719     public void copyNullByteArrayNullOutputStreamPosBufSz()
720         throws Exception
721     {
722         IOUtil.copy( nullByteArray(), nullOutputStream());
723     }
724 
725     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
726     public void copyNullByteArrayValidOutputStreamPosBufSz()
727         throws Exception
728     {
729         IOUtil.copy( nullByteArray(), new DontCloseByteArrayOutputStream());
730     }
731 
732     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
733     public void copyEmptyByteArrayNullOutputStreamPosBufSz()
734         throws Exception
735     {
736         IOUtil.copy( emptyByteArray(), nullOutputStream());
737     }
738 
739     @Test( timeout = INFINITE_LOOP_TIMEOUT )
740     public void copyEmptyByteArrayValidOutputStreamPosBufSz()
741         throws Exception
742     {
743         IOUtil.copy( emptyByteArray(), new DontCloseByteArrayOutputStream());
744     }
745 
746     @Test( timeout = INFINITE_LOOP_TIMEOUT )
747     public void copyByteArrayValidOutputStreamPosBufSz()
748         throws Exception
749     {
750         ByteArrayOutputStream outputStream = new DontCloseByteArrayOutputStream();
751         byte[] input = { 1, 2, 3, 4, 5, 6 };
752         IOUtil.copy( input, outputStream);
753         assertThat( outputStream.toByteArray(), is( input ) );
754     }
755 
756     @Test( expected = NullPointerException.class )
757     public void copyNullInputStreamNullOutputStream()
758         throws Exception
759     {
760         IOUtil.copy( nullInputStream(), nullOutputStream() );
761     }
762 
763     @Test( expected = NullPointerException.class )
764     public void copyNullInputStreamValidOutputStream()
765         throws Exception
766     {
767         IOUtil.copy( nullInputStream(), new DontCloseByteArrayOutputStream() );
768     }
769 
770     @Test
771     public void copyEmptyInputStreamNullOutputStream()
772         throws Exception
773     {
774         IOUtil.copy( new DontCloseByteArrayInputStream( emptyByteArray() ), nullOutputStream() );
775     }
776 
777     @Test
778     public void copyEmptyInputStreamValidOutputStream()
779         throws Exception
780     {
781         IOUtil.copy( new DontCloseByteArrayInputStream( emptyByteArray() ), new DontCloseByteArrayOutputStream() );
782     }
783 
784     @Test
785     public void copyInputStreamValidOutputStream()
786         throws Exception
787     {
788         ByteArrayOutputStream outputStream = new DontCloseByteArrayOutputStream();
789         byte[] input = { 1, 2, 3, 4, 5, 6 };
790         IOUtil.copy( new DontCloseByteArrayInputStream( input ), outputStream );
791         assertThat( outputStream.toByteArray(), is( input ) );
792     }
793 
794     @Test( expected = NegativeArraySizeException.class )
795     public void copyNullInputStreamNullOutputStreamNegBufSz()
796         throws Exception
797     {
798         IOUtil.copy( nullInputStream(), nullOutputStream(), -1 );
799     }
800 
801     @Test( expected = NegativeArraySizeException.class )
802     public void copyNullInputStreamValidOutputStreamNegBufSz()
803         throws Exception
804     {
805         IOUtil.copy( nullInputStream(), new DontCloseByteArrayOutputStream(), -1 );
806     }
807 
808     @Test( expected = NegativeArraySizeException.class )
809     public void copyEmptyInputStreamNullOutputStreamNegBufSz()
810         throws Exception
811     {
812         IOUtil.copy( new DontCloseByteArrayInputStream( emptyByteArray() ), nullOutputStream(), -1 );
813     }
814 
815     @Test( expected = NegativeArraySizeException.class )
816     public void copyEmptyInputStreamValidOutputStreamNegBufSz()
817         throws Exception
818     {
819         IOUtil.copy( new DontCloseByteArrayInputStream( emptyByteArray() ), new DontCloseByteArrayOutputStream(), -1 );
820     }
821 
822     @Test( expected = NegativeArraySizeException.class )
823     public void copyInputStreamValidOutputStreamNegBufSz()
824         throws Exception
825     {
826         ByteArrayOutputStream outputStream = new DontCloseByteArrayOutputStream();
827         byte[] input = { 1, 2, 3, 4, 5, 6 };
828         IOUtil.copy( new DontCloseByteArrayInputStream( input ), outputStream, -1 );
829         assertThat( outputStream.toByteArray(), is( input ) );
830     }
831 
832     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
833     public void copyNullInputStreamNullOutputStreamZeroBufSz()
834         throws Exception
835     {
836         IOUtil.copy( nullInputStream(), nullOutputStream(), 0 );
837     }
838 
839     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
840     public void copyNullInputStreamValidOutputStreamZeroBufSz()
841         throws Exception
842     {
843         IOUtil.copy( nullInputStream(), new ByteArrayOutputStream(), 0 );
844     }
845 
846     @Test( timeout = INFINITE_LOOP_TIMEOUT )
847     public void copyEmptyInputStreamNullOutputStreamZeroBufSz()
848         throws Exception
849     {
850         IOUtil.copy( new DontCloseByteArrayInputStream( emptyByteArray() ), nullOutputStream(), 0 );
851     }
852 
853     @Test( timeout = INFINITE_LOOP_TIMEOUT )
854     public void copyEmptyInputStreamValidOutputStreamZeroBufSz()
855         throws Exception
856     {
857         IOUtil.copy( new DontCloseByteArrayInputStream( emptyByteArray() ), new DontCloseByteArrayOutputStream(), 0 );
858     }
859     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
860     public void copyNullInputStreamNullOutputStreamPosBufSz()
861         throws Exception
862     {
863         IOUtil.copy( nullInputStream(), nullOutputStream(), 1 );
864     }
865 
866     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
867     public void copyNullInputStreamValidOutputStreamPosBufSz()
868         throws Exception
869     {
870         IOUtil.copy( nullInputStream(), new ByteArrayOutputStream(), 1 );
871     }
872 
873     @Test( timeout = INFINITE_LOOP_TIMEOUT )
874     public void copyEmptyInputStreamNullOutputStreamPosBufSz()
875         throws Exception
876     {
877         IOUtil.copy( new DontCloseByteArrayInputStream( emptyByteArray() ), nullOutputStream(), 1 );
878     }
879 
880     @Test( timeout = INFINITE_LOOP_TIMEOUT )
881     public void copyEmptyInputStreamValidOutputStreamPosBufSz()
882         throws Exception
883     {
884         IOUtil.copy( new DontCloseByteArrayInputStream( emptyByteArray() ), new DontCloseByteArrayOutputStream(), 1 );
885     }
886 
887     @Test( timeout = INFINITE_LOOP_TIMEOUT )
888     public void copyInputStreamValidOutputStreamPosBufSz()
889         throws Exception
890     {
891         ByteArrayOutputStream outputStream = new DontCloseByteArrayOutputStream();
892         byte[] input = { 1, 2, 3, 4, 5, 6 };
893         IOUtil.copy( new DontCloseByteArrayInputStream( input ), outputStream, 1 );
894         assertThat( outputStream.toByteArray(), is( input ) );
895     }
896 
897     @Test( expected = NullPointerException.class )
898     public void toStringNullInputStream()
899         throws Exception
900     {
901         IOUtil.toString( nullInputStream() );
902     }
903 
904     @Test
905     public void toStringEmptyInputStream()
906         throws Exception
907     {
908         assertThat( IOUtil.toString( emptyInputStream() ), is( emptyString() ) );
909     }
910 
911     @Test
912     public void toStringInputStream()
913         throws Exception
914     {
915         String probe = "A string \u2345\u00ef";
916         assertThat( IOUtil.toString( new ByteArrayInputStream( probe.getBytes() ) ).getBytes(),
917                     is( probe.getBytes() ) );
918     }
919 
920     @Test( expected = NullPointerException.class )
921     public void toStringNullInputStreamNegBufSz()
922         throws Exception
923     {
924         IOUtil.toString( nullInputStream(), -1 );
925     }
926 
927     @Test( expected = NegativeArraySizeException.class )
928     public void toStringEmptyInputStreamNegBufSz()
929         throws Exception
930     {
931         assertThat( IOUtil.toString( emptyInputStream(), -1 ), is( emptyString() ) );
932     }
933 
934     @Test( expected = NegativeArraySizeException.class )
935     public void toStringInputStreamNegBufSz()
936         throws Exception
937     {
938         String probe = "A string \u2345\u00ef";
939         assertThat( IOUtil.toString( new ByteArrayInputStream( probe.getBytes() ), -1 ), is( probe ) );
940     }
941 
942     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
943     public void toStringNullInputStreamZeroBufSz()
944         throws Exception
945     {
946         IOUtil.toString( nullInputStream(), 0 );
947     }
948 
949     @Test( expected = NullPointerException.class )
950     public void toStringNullInputStreamPosBufSz()
951         throws Exception
952     {
953         IOUtil.toString( nullInputStream(), 1 );
954     }
955 
956     @Test
957     public void toStringEmptyInputStreamPosBufSz()
958         throws Exception
959     {
960         assertThat( IOUtil.toString( emptyInputStream(), 1 ), is( emptyString() ) );
961     }
962 
963     @Test
964     public void toStringInputStreamPosBufSz()
965         throws Exception
966     {
967         String probe = "A string \u2345\u00ef";
968         assertThat( IOUtil.toString( new ByteArrayInputStream( probe.getBytes() ), 1 ).getBytes(),
969                     is( probe.getBytes() ) );
970     }
971 
972     @Test( expected = NullPointerException.class )
973     public void toStringNullInputStreamNullEncoding()
974         throws Exception
975     {
976         IOUtil.toString( nullInputStream(), null );
977     }
978 
979     @Test( expected = NullPointerException.class )
980     public void toStringEmptyInputStreamNullEncoding()
981         throws Exception
982     {
983         assertThat( IOUtil.toString( emptyInputStream(), null ), is( emptyString() ) );
984     }
985 
986     @Test( expected = NullPointerException.class )
987     public void toStringInputStreamNullEncoding()
988         throws Exception
989     {
990         String probe = "A string \u2345\u00ef";
991         assertThat( IOUtil.toString( new ByteArrayInputStream( probe.getBytes() ), null ).getBytes(),
992                     is( probe.getBytes() ) );
993     }
994 
995     @Test( expected = NullPointerException.class )
996     public void toStringNullInputStreamJunkEncoding()
997         throws Exception
998     {
999         IOUtil.toString( nullInputStream(), "junk" );
1000     }
1001 
1002     @Test( expected = UnsupportedEncodingException.class )
1003     public void toStringEmptyInputStreamJunkEncoding()
1004         throws Exception
1005     {
1006         assertThat( IOUtil.toString( emptyInputStream(), "junk" ), is( emptyString() ) );
1007     }
1008 
1009     @Test( expected = UnsupportedEncodingException.class )
1010     public void toStringInputStreamJunkEncoding()
1011         throws Exception
1012     {
1013         String probe = "A string \u2345\u00ef";
1014         assertThat( IOUtil.toString( new ByteArrayInputStream( probe.getBytes() ), "junk" ).getBytes(),
1015                     is( probe.getBytes() ) );
1016     }
1017 
1018     @Test( expected = NullPointerException.class )
1019     public void toStringNullInputStreamValidEncoding()
1020         throws Exception
1021     {
1022         IOUtil.toString( nullInputStream(), "utf-16" );
1023     }
1024 
1025     @Test
1026     public void toStringEmptyInputStreamValidEncoding()
1027         throws Exception
1028     {
1029         assertThat( IOUtil.toString( emptyInputStream(), "utf-16" ), is( emptyString() ) );
1030     }
1031 
1032     @Test
1033     public void toStringInputStreamValidEncoding()
1034         throws Exception
1035     {
1036         String probe = "A string \u2345\u00ef";
1037         assertThat(
1038             IOUtil.toString( new ByteArrayInputStream( probe.getBytes( "utf-16" ) ), "utf-16" ).getBytes( "utf-8" ),
1039             is( probe.getBytes( "utf-8" ) ) );
1040     }
1041 
1042     @Test( expected = NullPointerException.class )
1043     public void toStringNullInputStreamNullEncodingNegBufSz()
1044         throws Exception
1045     {
1046         IOUtil.toString( nullInputStream(), null, -1 );
1047     }
1048 
1049     @Test( expected = NullPointerException.class )
1050     public void toStringEmptyInputStreamNullEncodingNegBufSz()
1051         throws Exception
1052     {
1053         assertThat( IOUtil.toString( emptyInputStream(), null, -1 ), is( emptyString() ) );
1054     }
1055 
1056     @Test( expected = NullPointerException.class )
1057     public void toStringInputStreamNullEncodingNegBufSz()
1058         throws Exception
1059     {
1060         String probe = "A string \u2345\u00ef";
1061         assertThat( IOUtil.toString( new ByteArrayInputStream( probe.getBytes() ), null, -1 ).getBytes(),
1062                     is( probe.getBytes() ) );
1063     }
1064 
1065     @Test( expected = NullPointerException.class )
1066     public void toStringNullInputStreamJunkEncodingNegBufSz()
1067         throws Exception
1068     {
1069         IOUtil.toString( nullInputStream(), "junk", -1 );
1070     }
1071 
1072     @Test( expected = UnsupportedEncodingException.class )
1073     public void toStringEmptyInputStreamJunkEncodingNegBufSz()
1074         throws Exception
1075     {
1076         assertThat( IOUtil.toString( emptyInputStream(), "junk", -1 ), is( emptyString() ) );
1077     }
1078 
1079     @Test( expected = UnsupportedEncodingException.class )
1080     public void toStringInputStreamJunkEncodingNegBufSz()
1081         throws Exception
1082     {
1083         String probe = "A string \u2345\u00ef";
1084         assertThat( IOUtil.toString( new ByteArrayInputStream( probe.getBytes() ), "junk", -1 ).getBytes(),
1085                     is( probe.getBytes() ) );
1086     }
1087 
1088     @Test( expected = NullPointerException.class )
1089     public void toStringNullInputStreamValidEncodingNegBufSz()
1090         throws Exception
1091     {
1092         IOUtil.toString( nullInputStream(), "utf-16", -1 );
1093     }
1094 
1095     @Test( expected = NegativeArraySizeException.class )
1096     public void toStringEmptyInputStreamValidEncodingNegBufSz()
1097         throws Exception
1098     {
1099         assertThat( IOUtil.toString( emptyInputStream(), "utf-16", -1 ), is( emptyString() ) );
1100     }
1101 
1102     @Test( expected = NegativeArraySizeException.class )
1103     public void toStringInputStreamValidEncodingNegBufSz()
1104         throws Exception
1105     {
1106         String probe = "A string \u2345\u00ef";
1107         assertThat(
1108             IOUtil.toString( new ByteArrayInputStream( probe.getBytes( "utf-16" ) ), "utf-16", -1 ).getBytes( "utf-8" ),
1109             is( probe.getBytes( "utf-8" ) ) );
1110     }
1111 
1112     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
1113     public void toStringNullInputStreamNullEncodingZeroBufSz()
1114         throws Exception
1115     {
1116         IOUtil.toString( nullInputStream(), null, 0 );
1117     }
1118 
1119     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
1120     public void toStringEmptyInputStreamNullEncodingZeroBufSz()
1121         throws Exception
1122     {
1123         assertThat( IOUtil.toString( emptyInputStream(), null, 0 ), is( emptyString() ) );
1124     }
1125 
1126     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
1127     public void toStringInputStreamNullEncodingZeroBufSz()
1128         throws Exception
1129     {
1130         String probe = "A string \u2345\u00ef";
1131         assertThat( IOUtil.toString( new ByteArrayInputStream( probe.getBytes() ), null, 0 ).getBytes(),
1132                     is( probe.getBytes() ) );
1133     }
1134 
1135     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
1136     public void toStringNullInputStreamJunkEncodingZeroBufSz()
1137         throws Exception
1138     {
1139         IOUtil.toString( nullInputStream(), "junk", 0 );
1140     }
1141 
1142     @Test( expected = UnsupportedEncodingException.class, timeout = INFINITE_LOOP_TIMEOUT )
1143     public void toStringEmptyInputStreamJunkEncodingZeroBufSz()
1144         throws Exception
1145     {
1146         assertThat( IOUtil.toString( emptyInputStream(), "junk", 0 ), is( emptyString() ) );
1147     }
1148 
1149     @Test( expected = UnsupportedEncodingException.class, timeout = INFINITE_LOOP_TIMEOUT )
1150     public void toStringInputStreamJunkEncodingZeroBufSz()
1151         throws Exception
1152     {
1153         String probe = "A string \u2345\u00ef";
1154         assertThat( IOUtil.toString( new ByteArrayInputStream( probe.getBytes() ), "junk", 0 ).getBytes(),
1155                     is( probe.getBytes() ) );
1156     }
1157 
1158     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
1159     public void toStringNullInputStreamValidEncodingZeroBufSz()
1160         throws Exception
1161     {
1162         IOUtil.toString( nullInputStream(), "utf-16", 0 );
1163     }
1164 
1165     /*
1166      * copy(InputStream,Writer)
1167      */
1168 
1169     @Test( expected = NullPointerException.class )
1170     public void copyNullInputStreamNullWriter()
1171         throws Exception
1172     {
1173         IOUtil.copy( nullInputStream(), nullWriter() );
1174     }
1175 
1176     @Test( expected = NullPointerException.class )
1177     public void copyEmptyInputStreamNullWriter()
1178         throws Exception
1179     {
1180         IOUtil.copy( emptyInputStream(), nullWriter() );
1181     }
1182 
1183     @Test
1184     public void copyEmptyInputStreamValidWriter()
1185         throws Exception
1186     {
1187         StringWriter writer = new DontCloseStringWriter();
1188         IOUtil.copy( emptyInputStream(), writer );
1189         assertThat( writer.toString(), is( emptyString() ) );
1190     }
1191 
1192     @Test( expected = NullPointerException.class )
1193     public void copyInputStreamNullWriter()
1194         throws Exception
1195     {
1196         String probe = "A string \u2345\u00ef";
1197         IOUtil.copy( new ByteArrayInputStream( probe.getBytes() ), nullWriter() );
1198     }
1199 
1200     @Test
1201     public void copyInputStreamValidWriter()
1202         throws Exception
1203     {
1204         String probe = "A string \u2345\u00ef";
1205         StringWriter writer = new DontCloseStringWriter();
1206         IOUtil.copy( new ByteArrayInputStream( probe.getBytes() ), writer );
1207         assertThat( writer.toString().getBytes(), is( probe.getBytes() ) );
1208     }
1209 
1210     /*
1211      * copy(InputStream,Writer,int)
1212      */
1213 
1214     @Test( expected = NullPointerException.class )
1215     public void copyNullInputStreamNullWriterNegBufSz()
1216         throws Exception
1217     {
1218         IOUtil.copy( nullInputStream(), nullWriter(), -1 );
1219     }
1220 
1221     @Test( expected = NegativeArraySizeException.class )
1222     public void copyEmptyInputStreamNullWriterNegBufSz()
1223         throws Exception
1224     {
1225         IOUtil.copy( emptyInputStream(), nullWriter(), -1 );
1226     }
1227 
1228     @Test( expected = NegativeArraySizeException.class )
1229     public void copyEmptyInputStreamValidWriterNegBufSz()
1230         throws Exception
1231     {
1232         IOUtil.copy( emptyInputStream(), new DontCloseStringWriter(), -1 );
1233     }
1234 
1235     @Test( expected = NegativeArraySizeException.class )
1236     public void copyInputStreamNullWriterNegBufSz()
1237         throws Exception
1238     {
1239         String probe = "A string \u2345\u00ef";
1240         IOUtil.copy( new ByteArrayInputStream( probe.getBytes() ), nullWriter(), -1 );
1241     }
1242 
1243     @Test( expected = NegativeArraySizeException.class )
1244     public void copyInputStreamValidWriterNegBufSz()
1245         throws Exception
1246     {
1247         String probe = "A string \u2345\u00ef";
1248         StringWriter writer = new DontCloseStringWriter();
1249         IOUtil.copy( new ByteArrayInputStream( probe.getBytes() ), writer, -1 );
1250         assertThat( writer.toString().getBytes(), is( probe.getBytes() ) );
1251     }
1252 
1253     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
1254     public void copyNullInputStreamNullWriterZeroBufSz()
1255         throws Exception
1256     {
1257         IOUtil.copy( nullInputStream(), nullWriter(), 0 );
1258     }
1259 
1260     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
1261     public void copyNullInputStreamValidWriterZeroBufSz()
1262         throws Exception
1263     {
1264         IOUtil.copy( nullInputStream(), new DontCloseStringWriter(), 0 );
1265     }
1266 
1267     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
1268     public void copyEmptyInputStreamNullWriterZeroBufSz()
1269         throws Exception
1270     {
1271         IOUtil.copy( emptyInputStream(), nullWriter(), 0 );
1272     }
1273 
1274     @Test( expected = NullPointerException.class )
1275     public void copyNullInputStreamNullWriterPosBufSz()
1276         throws Exception
1277     {
1278         IOUtil.copy( nullInputStream(), nullWriter(), 1 );
1279     }
1280 
1281     @Test( expected = NullPointerException.class )
1282     public void copyNullInputStreamValidWriterPosBufSz()
1283         throws Exception
1284     {
1285         IOUtil.copy( nullInputStream(), new DontCloseStringWriter(), 1 );
1286     }
1287 
1288     @Test( expected = NullPointerException.class )
1289     public void copyEmptyInputStreamNullWriterPosBufSz()
1290         throws Exception
1291     {
1292         IOUtil.copy( emptyInputStream(), nullWriter(), 1 );
1293     }
1294 
1295     @Test
1296     public void copyEmptyInputStreamValidWriterPosBufSz()
1297         throws Exception
1298     {
1299         StringWriter writer = new DontCloseStringWriter();
1300         IOUtil.copy( emptyInputStream(), writer, 1 );
1301         assertThat( writer.toString(), is( emptyString() ) );
1302     }
1303 
1304     @Test
1305     public void copyInputStreamValidWriterPosBufSz()
1306         throws Exception
1307     {
1308         String probe = "A string \u2345\u00ef";
1309         StringWriter writer = new DontCloseStringWriter();
1310         IOUtil.copy( new ByteArrayInputStream( probe.getBytes() ), writer, 1 );
1311         assertThat( writer.toString().getBytes(), is( probe.getBytes() ) );
1312     }
1313 
1314     /*
1315      * copy(InputStream,Writer,String)
1316      */
1317 
1318     @Test( expected = NullPointerException.class )
1319     public void copyNullInputStreamNullWriterNullEncoding()
1320         throws Exception
1321     {
1322         IOUtil.copy( nullInputStream(), nullWriter(), null );
1323     }
1324 
1325     @Test( expected = NullPointerException.class )
1326     public void copyNullInputStreamValidWriterNullEncoding()
1327         throws Exception
1328     {
1329         IOUtil.copy( nullInputStream(), new DontCloseStringWriter(), null );
1330     }
1331 
1332     @Test( expected = NullPointerException.class )
1333     public void copyEmptyInputStreamNullWriterNullEncoding()
1334         throws Exception
1335     {
1336         IOUtil.copy( emptyInputStream(), nullWriter(), null );
1337     }
1338 
1339     @Test( expected = NullPointerException.class )
1340     public void copyEmptyInputStreamValidWriterNullEncoding()
1341         throws Exception
1342     {
1343         IOUtil.copy( emptyInputStream(), new DontCloseStringWriter(), null );
1344     }
1345 
1346     @Test( expected = NullPointerException.class )
1347     public void copyInputStreamNullEncoding()
1348         throws Exception
1349     {
1350         String probe = "A string \u2345\u00ef";
1351         StringWriter writer = new DontCloseStringWriter();
1352         IOUtil.copy( new ByteArrayInputStream( probe.getBytes() ), writer, null );
1353         assertThat( writer.toString().getBytes(), is( probe.getBytes() ) );
1354     }
1355 
1356     @Test( expected = NullPointerException.class )
1357     public void copyNullInputStreamNullWriterJunkEncoding()
1358         throws Exception
1359     {
1360         IOUtil.copy( nullInputStream(), nullWriter(), "junk" );
1361     }
1362 
1363     @Test( expected = NullPointerException.class )
1364     public void copyNullInputStreamValidWriterJunkEncoding()
1365         throws Exception
1366     {
1367         IOUtil.copy( nullInputStream(), new DontCloseStringWriter(), "junk" );
1368     }
1369 
1370     @Test( expected = UnsupportedEncodingException.class )
1371     public void copyEmptyInputStreamNullWriterJunkEncoding()
1372         throws Exception
1373     {
1374         IOUtil.copy( emptyInputStream(), nullWriter(), "junk" );
1375     }
1376 
1377     @Test( expected = UnsupportedEncodingException.class )
1378     public void copyEmptyInputStreamValidWriterJunkEncoding()
1379         throws Exception
1380     {
1381         IOUtil.copy( emptyInputStream(), new DontCloseStringWriter(), "junk" );
1382     }
1383 
1384     @Test( expected = UnsupportedEncodingException.class )
1385     public void copyInputStreamNullWriterJunkEncoding()
1386         throws Exception
1387     {
1388         String probe = "A string \u2345\u00ef";
1389         IOUtil.copy( new ByteArrayInputStream( probe.getBytes() ), nullWriter(), "junk" );
1390     }
1391 
1392     @Test( expected = UnsupportedEncodingException.class )
1393     public void copyInputStreamValidWriterJunkEncoding()
1394         throws Exception
1395     {
1396         String probe = "A string \u2345\u00ef";
1397         StringWriter writer = new DontCloseStringWriter();
1398         IOUtil.copy( new ByteArrayInputStream( probe.getBytes() ), writer, "junk" );
1399         assertThat( writer.toString().getBytes(), is( probe.getBytes() ) );
1400     }
1401 
1402     @Test( expected = NullPointerException.class )
1403     public void copyNullInputStreamNullWriterValidEncoding()
1404         throws Exception
1405     {
1406         IOUtil.copy( nullInputStream(), nullWriter(), "utf-16" );
1407     }
1408 
1409     @Test( expected = NullPointerException.class )
1410     public void copyEmptyInputStreamNullWriterValidEncoding()
1411         throws Exception
1412     {
1413         IOUtil.copy( emptyInputStream(), nullWriter(), "utf-16" );
1414     }
1415 
1416     @Test( expected = NullPointerException.class )
1417     public void copyNullInputStreamValidWriterValidEncoding()
1418         throws Exception
1419     {
1420         IOUtil.copy( nullInputStream(), new DontCloseStringWriter(), "utf-16" );
1421     }
1422 
1423     @Test
1424     public void copyEmptyInputStreamValidWriterValidEncoding()
1425         throws Exception
1426     {
1427         StringWriter writer = new DontCloseStringWriter();
1428         IOUtil.copy( emptyInputStream(), writer, "utf-16" );
1429         assertThat( writer.toString(), is( emptyString() ) );
1430     }
1431 
1432     @Test( expected = NullPointerException.class )
1433     public void copyInputStreamNullWriterValidEncoding()
1434         throws Exception
1435     {
1436         String probe = "A string \u2345\u00ef";
1437         IOUtil.copy( new ByteArrayInputStream( probe.getBytes( "utf-16" ) ), nullWriter(), "utf-16" );
1438     }
1439 
1440     @Test
1441     public void copyInputStreamValidWriterValidEncoding()
1442         throws Exception
1443     {
1444         String probe = "A string \u2345\u00ef";
1445         StringWriter writer = new DontCloseStringWriter();
1446         IOUtil.copy( new ByteArrayInputStream( probe.getBytes( "utf-16" ) ), writer, "utf-16" );
1447         assertThat( writer.toString().getBytes( "utf-8" ), is( probe.getBytes( "utf-8" ) ) );
1448     }
1449 
1450     /*
1451      * copy(InputStream,Writer,String,int)
1452      */
1453 
1454     @Test( expected = NullPointerException.class )
1455     public void copyNullInputStreamNullWriterNullEncodingNegBufSz()
1456         throws Exception
1457     {
1458         IOUtil.copy( nullInputStream(), nullWriter(), null, -1 );
1459     }
1460 
1461     @Test( expected = NullPointerException.class )
1462     public void copyNullInputStreamValidWriterNullEncodingNegBufSz()
1463         throws Exception
1464     {
1465         IOUtil.copy( nullInputStream(), new DontCloseStringWriter(), null, -1 );
1466     }
1467 
1468     @Test( expected = NullPointerException.class )
1469     public void copyEmptyInputStreamNullWriterNullEncodingNegBufSz()
1470         throws Exception
1471     {
1472         IOUtil.copy( emptyInputStream(), nullWriter(), null, -1 );
1473     }
1474 
1475     @Test( expected = NullPointerException.class )
1476     public void copyEmptyInputStreamValidWriterNullEncodingNegBufSz()
1477         throws Exception
1478     {
1479         StringWriter writer = new DontCloseStringWriter();
1480         IOUtil.copy( emptyInputStream(), writer, null, -1 );
1481         assertThat( writer.toString(), is( emptyString() ) );
1482     }
1483 
1484     @Test( expected = NullPointerException.class )
1485     public void copyInputStreamNullWriterNullEncodingNegBufSz()
1486         throws Exception
1487     {
1488         String probe = "A string \u2345\u00ef";
1489         IOUtil.copy( new ByteArrayInputStream( probe.getBytes() ), nullWriter(), null, -1 );
1490     }
1491 
1492     @Test( expected = NullPointerException.class )
1493     public void copyInputStreamValidWriterNullEncodingNegBufSz()
1494         throws Exception
1495     {
1496         String probe = "A string \u2345\u00ef";
1497         StringWriter writer = new DontCloseStringWriter();
1498         IOUtil.copy( new ByteArrayInputStream( probe.getBytes() ), writer, null, -1 );
1499         assertThat( writer.toString().getBytes(), is( probe.getBytes() ) );
1500     }
1501 
1502     @Test( expected = NullPointerException.class )
1503     public void copyNullInputStreamNullWriterJunkEncodingNegBufSz()
1504         throws Exception
1505     {
1506         IOUtil.copy( nullInputStream(), nullWriter(), "junk", -1 );
1507     }
1508 
1509     @Test( expected = NullPointerException.class )
1510     public void copyNullInputStreamValidWriterJunkEncodingNegBufSz()
1511         throws Exception
1512     {
1513         IOUtil.copy( nullInputStream(), new DontCloseStringWriter(), "junk", -1 );
1514     }
1515 
1516     @Test( expected = UnsupportedEncodingException.class )
1517     public void copyEmptyInputStreamNullWriterJunkEncodingNegBufSz()
1518         throws Exception
1519     {
1520         IOUtil.copy( emptyInputStream(), nullWriter(), "junk", -1 );
1521     }
1522 
1523     @Test( expected = UnsupportedEncodingException.class )
1524     public void copyEmptyInputStreamJunkEncodingNegBufSz()
1525         throws Exception
1526     {
1527         StringWriter writer = new DontCloseStringWriter();
1528         IOUtil.copy( emptyInputStream(), writer, "junk", -1 );
1529         assertThat( writer.toString(), is( emptyString() ) );
1530     }
1531 
1532     @Test( expected = UnsupportedEncodingException.class )
1533     public void copyInputStreamNullWriterJunkEncodingNegBufSz()
1534         throws Exception
1535     {
1536         String probe = "A string \u2345\u00ef";
1537         IOUtil.copy( new ByteArrayInputStream( probe.getBytes() ), nullWriter(), "junk", -1 );
1538     }
1539 
1540     @Test( expected = UnsupportedEncodingException.class )
1541     public void copyInputStreamValidWriterJunkEncodingNegBufSz()
1542         throws Exception
1543     {
1544         String probe = "A string \u2345\u00ef";
1545         StringWriter writer = new DontCloseStringWriter();
1546         IOUtil.copy( new ByteArrayInputStream( probe.getBytes() ), writer, "junk", -1 );
1547         assertThat( writer.toString().getBytes(), is( probe.getBytes() ) );
1548     }
1549 
1550     @Test( expected = NullPointerException.class )
1551     public void copyNullInputStreamNullWriterValidEncodingNegBufSz()
1552         throws Exception
1553     {
1554         IOUtil.copy( nullInputStream(), nullWriter(), "utf-16", -1 );
1555     }
1556 
1557     @Test( expected = NullPointerException.class )
1558     public void copyNullInputStreamValidWriterValidEncodingNegBufSz()
1559         throws Exception
1560     {
1561         IOUtil.copy( nullInputStream(), new DontCloseStringWriter(), "utf-16", -1 );
1562     }
1563 
1564     @Test( expected = NegativeArraySizeException.class )
1565     public void copyEmptyInputStreamNullWriterValidEncodingNegBufSz()
1566         throws Exception
1567     {
1568         IOUtil.copy( emptyInputStream(), nullWriter(), "utf-16", -1 );
1569     }
1570 
1571     @Test( expected = NegativeArraySizeException.class )
1572     public void copyEmptyInputStreamValidWriterValidEncodingNegBufSz()
1573         throws Exception
1574     {
1575         StringWriter writer = new DontCloseStringWriter();
1576         IOUtil.copy( emptyInputStream(), writer, "utf-16", -1 );
1577         assertThat( writer.toString(), is( emptyString() ) );
1578     }
1579 
1580     @Test( expected = NegativeArraySizeException.class )
1581     public void copyInputStreamNullWriterValidEncodingNegBufSz()
1582         throws Exception
1583     {
1584         String probe = "A string \u2345\u00ef";
1585         IOUtil.copy( new ByteArrayInputStream( probe.getBytes( "utf-16" ) ), nullWriter(), -1 );
1586     }
1587 
1588     @Test( expected = NegativeArraySizeException.class )
1589     public void copyInputStreamValidEncodingNegBufSz()
1590         throws Exception
1591     {
1592         String probe = "A string \u2345\u00ef";
1593         StringWriter writer = new DontCloseStringWriter();
1594         IOUtil.copy( new ByteArrayInputStream( probe.getBytes( "utf-16" ) ), writer, "utf-16", -1 );
1595         assertThat( writer.toString().getBytes( "utf-8" ), is( probe.getBytes( "utf-8" ) ) );
1596     }
1597 
1598     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
1599     public void copyNullInputStreamNullWriterNullEncodingZeroBufSz()
1600         throws Exception
1601     {
1602         IOUtil.copy( nullInputStream(), nullWriter(), null, 0 );
1603     }
1604 
1605     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
1606     public void copyNullInputStreamValidWriterNullEncodingZeroBufSz()
1607         throws Exception
1608     {
1609         IOUtil.copy( nullInputStream(), new DontCloseStringWriter(), null, 0 );
1610     }
1611 
1612     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
1613     public void copyEmptyInputStreamNullWriterNullEncodingZeroBufSz()
1614         throws Exception
1615     {
1616         IOUtil.copy( emptyInputStream(), nullWriter(), null, 0 );
1617     }
1618 
1619     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
1620     public void copyEmptyInputStreamValidWriterNullEncodingZeroBufSz()
1621         throws Exception
1622     {
1623         StringWriter writer = new DontCloseStringWriter();
1624         IOUtil.copy( emptyInputStream(), writer, null, 0 );
1625         assertThat( writer.toString(), is( emptyString() ) );
1626     }
1627 
1628     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
1629     public void copyInputStreamNullWriterNullEncodingZeroBufSz()
1630         throws Exception
1631     {
1632         String probe = "A string \u2345\u00ef";
1633         IOUtil.copy( new ByteArrayInputStream( probe.getBytes() ), nullWriter(), null, 0 );
1634     }
1635 
1636     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
1637     public void copyInputStreamValidWriterNullEncodingZeroBufSz()
1638         throws Exception
1639     {
1640         String probe = "A string \u2345\u00ef";
1641         StringWriter writer = new DontCloseStringWriter();
1642         IOUtil.copy( new ByteArrayInputStream( probe.getBytes() ), writer, null, 0 );
1643         assertThat( writer.toString().getBytes(), is( probe.getBytes() ) );
1644     }
1645 
1646     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
1647     public void copyNullInputStreamNullWriterJunkEncodingZeroBufSz()
1648         throws Exception
1649     {
1650         IOUtil.copy( nullInputStream(), nullWriter(), "junk", 0 );
1651     }
1652 
1653     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
1654     public void copyNullInputStreamValidWriterJunkEncodingZeroBufSz()
1655         throws Exception
1656     {
1657         IOUtil.copy( nullInputStream(), new DontCloseStringWriter(), "junk", 0 );
1658     }
1659 
1660     @Test( expected = UnsupportedEncodingException.class, timeout = INFINITE_LOOP_TIMEOUT )
1661     public void copyEmptyInputStreamNullWriterJunkEncodingZeroBufSz()
1662         throws Exception
1663     {
1664         IOUtil.copy( emptyInputStream(), nullWriter(), "junk", 0 );
1665     }
1666 
1667     @Test( expected = UnsupportedEncodingException.class, timeout = INFINITE_LOOP_TIMEOUT )
1668     public void copyEmptyInputStreamValidWriterJunkEncodingZeroBufSz()
1669         throws Exception
1670     {
1671         StringWriter writer = new DontCloseStringWriter();
1672         IOUtil.copy( emptyInputStream(), writer, "junk", 0 );
1673         assertThat( writer.toString(), is( emptyString() ) );
1674     }
1675 
1676     @Test( expected = UnsupportedEncodingException.class, timeout = INFINITE_LOOP_TIMEOUT )
1677     public void copyInputStreamNullWriterJunkEncodingZeroBufSz()
1678         throws Exception
1679     {
1680         String probe = "A string \u2345\u00ef";
1681         IOUtil.copy( new ByteArrayInputStream( probe.getBytes() ), nullWriter(), "junk", 0 );
1682     }
1683 
1684     @Test( expected = UnsupportedEncodingException.class, timeout = INFINITE_LOOP_TIMEOUT )
1685     public void copyInputStreamValidWriterJunkEncodingZeroBufSz()
1686         throws Exception
1687     {
1688         String probe = "A string \u2345\u00ef";
1689         StringWriter writer = new DontCloseStringWriter();
1690         IOUtil.copy( new ByteArrayInputStream( probe.getBytes() ), writer, "junk", 0 );
1691         assertThat( writer.toString().getBytes(), is( probe.getBytes() ) );
1692     }
1693 
1694     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
1695     public void copyNullInputStreamNullWriterValidEncodingZeroBufSz()
1696         throws Exception
1697     {
1698         IOUtil.copy( nullInputStream(), nullWriter(), "utf-16", 0 );
1699     }
1700 
1701     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
1702     public void copyNullInputStreamValidWriterValidEncodingZeroBufSz()
1703         throws Exception
1704     {
1705         IOUtil.copy( nullInputStream(), new DontCloseStringWriter(), "utf-16", 0 );
1706     }
1707 
1708     /*
1709      * copy(String,Writer)
1710      */
1711 
1712     @Test( expected = NullPointerException.class )
1713     public void copyNullStringNullWriter()
1714         throws Exception
1715     {
1716         IOUtil.copy( nullString(), nullWriter() );
1717     }
1718 
1719     @Test( expected = NullPointerException.class )
1720     public void copyEmptyStringNullWriter()
1721         throws Exception
1722     {
1723         IOUtil.copy( emptyString(), nullWriter() );
1724     }
1725 
1726     @Test
1727     public void copyNullStringValidWriter()
1728         throws Exception
1729     {
1730         IOUtil.copy( nullString(), new DontCloseStringWriter() );
1731     }
1732 
1733     @Test
1734     public void copyEmptyStringValidWriter()
1735         throws Exception
1736     {
1737         StringWriter writer = new DontCloseStringWriter();
1738         IOUtil.copy( emptyString(), writer );
1739         assertThat( writer.toString(), is( emptyString() ) );
1740     }
1741 
1742     @Test( expected = NullPointerException.class )
1743     public void copyStringNullWriter()
1744         throws Exception
1745     {
1746         String probe = "A string \u2345\u00ef";
1747         IOUtil.copy( probe, nullWriter() );
1748     }
1749 
1750     @Test
1751     public void copyStringValidWriter()
1752         throws Exception
1753     {
1754         String probe = "A string \u2345\u00ef";
1755         StringWriter writer = new DontCloseStringWriter();
1756         IOUtil.copy( probe, writer );
1757         assertThat( writer.toString(), is( probe ) );
1758     }
1759 
1760     @Test( expected = NullPointerException.class )
1761     public void copyNullStringNullOutputStream()
1762         throws Exception
1763     {
1764         IOUtil.copy( nullString(), nullOutputStream() );
1765     }
1766 
1767     @Test( expected = NullPointerException.class )
1768     public void copyEmptyStringNullOutputStream()
1769         throws Exception
1770     {
1771         IOUtil.copy( emptyString(), nullOutputStream() );
1772     }
1773 
1774     @Test( expected = NullPointerException.class )
1775     public void copyNullStringValidOutputStream()
1776         throws Exception
1777     {
1778         IOUtil.copy( nullString(), new DontCloseByteArrayOutputStream() );
1779     }
1780 
1781     @Test
1782     public void copyEmptyStringValidOutputStream()
1783         throws Exception
1784     {
1785         ByteArrayOutputStream OutputStream = new DontCloseByteArrayOutputStream();
1786         IOUtil.copy( emptyString(), OutputStream );
1787         assertThat( OutputStream.toByteArray(), is( emptyString().getBytes() ) );
1788     }
1789 
1790     @Test( expected = NullPointerException.class )
1791     public void copyStringNullOutputStream()
1792         throws Exception
1793     {
1794         String probe = "A string \u2345\u00ef";
1795         IOUtil.copy( probe, nullOutputStream() );
1796     }
1797 
1798     @Test
1799     public void copyStringValidOutputStream()
1800         throws Exception
1801     {
1802         String probe = "A string \u2345\u00ef";
1803         ByteArrayOutputStream OutputStream = new DontCloseByteArrayOutputStream();
1804         IOUtil.copy( probe, OutputStream );
1805         assertThat( OutputStream.toByteArray(), is( probe.getBytes() ) );
1806     }
1807 
1808     @Test( expected = NullPointerException.class )
1809     public void copyNullStringNullOutputStreamNegBufSz()
1810         throws Exception
1811     {
1812         IOUtil.copy( nullString(), nullOutputStream(), -1 );
1813     }
1814 
1815     @Test( expected = NullPointerException.class )
1816     public void copyEmptyStringNullOutputStreamNegBufSz()
1817         throws Exception
1818     {
1819         IOUtil.copy( emptyString(), nullOutputStream(), -1 );
1820     }
1821 
1822     @Test( expected = NullPointerException.class )
1823     public void copyNullStringValidOutputStreamNegBufSz()
1824         throws Exception
1825     {
1826         IOUtil.copy( nullString(), new DontCloseByteArrayOutputStream(), -1 );
1827     }
1828 
1829     @Test( expected = NegativeArraySizeException.class )
1830     public void copyEmptyStringValidOutputStreamNegBufSz()
1831         throws Exception
1832     {
1833         ByteArrayOutputStream OutputStream = new DontCloseByteArrayOutputStream();
1834         IOUtil.copy( emptyString(), OutputStream, -1 );
1835         assertThat( OutputStream.toByteArray(), is( emptyString().getBytes() ) );
1836     }
1837 
1838     @Test( expected = NullPointerException.class )
1839     public void copyStringNullOutputStreamNegBufSz()
1840         throws Exception
1841     {
1842         String probe = "A string \u2345\u00ef";
1843         IOUtil.copy( probe, nullOutputStream(), -1 );
1844     }
1845 
1846     @Test( expected = NegativeArraySizeException.class )
1847     public void copyStringValidOutputStreamNegBufSz()
1848         throws Exception
1849     {
1850         String probe = "A string \u2345\u00ef";
1851         ByteArrayOutputStream OutputStream = new DontCloseByteArrayOutputStream();
1852         IOUtil.copy( probe, OutputStream, -1 );
1853         assertThat( OutputStream.toByteArray(), is( probe.getBytes() ) );
1854     }
1855 
1856     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
1857     public void copyNullStringNullOutputStreamZeroBufSz()
1858         throws Exception
1859     {
1860         IOUtil.copy( nullString(), nullOutputStream(), 0 );
1861     }
1862 
1863     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
1864     public void copyEmptyStringNullOutputStreamZeroBufSz()
1865         throws Exception
1866     {
1867         IOUtil.copy( emptyString(), nullOutputStream(), 0 );
1868     }
1869 
1870     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
1871     public void copyNullStringValidOutputStreamZeroBufSz()
1872         throws Exception
1873     {
1874         IOUtil.copy( nullString(), new DontCloseByteArrayOutputStream(), 0 );
1875     }
1876     @Test( expected = NullPointerException.class )
1877     public void copyNullStringNullOutputStreamPosBufSz()
1878         throws Exception
1879     {
1880         IOUtil.copy( nullString(), nullOutputStream(), 1 );
1881     }
1882 
1883     @Test( expected = NullPointerException.class )
1884     public void copyEmptyStringNullOutputStreamPosBufSz()
1885         throws Exception
1886     {
1887         IOUtil.copy( emptyString(), nullOutputStream(), 1 );
1888     }
1889 
1890     @Test( expected = NullPointerException.class )
1891     public void copyNullStringValidOutputStreamPosBufSz()
1892         throws Exception
1893     {
1894         IOUtil.copy( nullString(), new DontCloseByteArrayOutputStream(), 1 );
1895     }
1896 
1897     @Test
1898     public void copyEmptyStringValidOutputStreamPosBufSz()
1899         throws Exception
1900     {
1901         ByteArrayOutputStream OutputStream = new DontCloseByteArrayOutputStream();
1902         IOUtil.copy( emptyString(), OutputStream, 1 );
1903         assertThat( OutputStream.toByteArray(), is( emptyString().getBytes() ) );
1904     }
1905 
1906     @Test( expected = NullPointerException.class )
1907     public void copyStringNullOutputStreamPosBufSz()
1908         throws Exception
1909     {
1910         String probe = "A string \u2345\u00ef";
1911         IOUtil.copy( probe, nullOutputStream(), 1 );
1912     }
1913 
1914     @Test
1915     public void copyStringValidOutputStreamPosBufSz()
1916         throws Exception
1917     {
1918         String probe = "A string \u2345\u00ef";
1919         ByteArrayOutputStream OutputStream = new DontCloseByteArrayOutputStream();
1920         IOUtil.copy( probe, OutputStream, 1 );
1921         assertThat( OutputStream.toByteArray(), is( probe.getBytes() ) );
1922     }
1923 
1924     @Test( expected = NullPointerException.class )
1925     public void copyNullReaderNullWriter()
1926         throws Exception
1927     {
1928         IOUtil.copy( nullReader(), nullWriter() );
1929     }
1930 
1931     @Test( expected = NullPointerException.class )
1932     public void copyEmptyReaderNullWriter()
1933         throws Exception
1934     {
1935         IOUtil.copy( emptyReader(), nullWriter() );
1936     }
1937 
1938     @Test( expected = NullPointerException.class )
1939     public void copyNullReaderValidWriter()
1940         throws Exception
1941     {
1942         IOUtil.copy( nullReader(), new DontCloseStringWriter() );
1943     }
1944 
1945     @Test
1946     public void copyEmptyReaderValidWriter()
1947         throws Exception
1948     {
1949         StringWriter writer = new DontCloseStringWriter();
1950         IOUtil.copy( emptyReader(), writer );
1951         assertThat( writer.toString(), is( emptyString() ) );
1952     }
1953 
1954     @Test( expected = NullPointerException.class )
1955     public void copyReaderNullWriter()
1956         throws Exception
1957     {
1958         String probe = "A string \u2345\u00ef";
1959         IOUtil.copy( new StringReader( probe ), nullWriter() );
1960     }
1961 
1962     @Test
1963     public void copyReaderValidWriter()
1964         throws Exception
1965     {
1966         String probe = "A string \u2345\u00ef";
1967         StringWriter writer = new DontCloseStringWriter();
1968         IOUtil.copy( new StringReader( probe ), writer );
1969         assertThat( writer.toString(), is( probe ) );
1970     }
1971 
1972     /*
1973      * copy(Reader,Writer,int)
1974      */
1975 
1976     @Test( expected = NegativeArraySizeException.class )
1977     public void copyNullReaderNullWriterNegBufSz()
1978         throws Exception
1979     {
1980         IOUtil.copy( nullReader(), nullWriter(), -1 );
1981     }
1982 
1983     @Test( expected = NegativeArraySizeException.class )
1984     public void copyEmptyReaderNullWriterNegBufSz()
1985         throws Exception
1986     {
1987         IOUtil.copy( emptyReader(), nullWriter(), -1 );
1988     }
1989 
1990     @Test( expected = NegativeArraySizeException.class )
1991     public void copyNullReaderValidWriterNegBufSz()
1992         throws Exception
1993     {
1994         IOUtil.copy( nullReader(), new DontCloseStringWriter(), -1 );
1995     }
1996 
1997     @Test( expected = NegativeArraySizeException.class )
1998     public void copyEmptyReaderValidWriterNegBufSz()
1999         throws Exception
2000     {
2001         StringWriter writer = new DontCloseStringWriter();
2002         IOUtil.copy( emptyReader(), writer, -1 );
2003         assertThat( writer.toString(), is( emptyString() ) );
2004     }
2005 
2006     @Test( expected = NegativeArraySizeException.class )
2007     public void copyReaderNullWriterNegBufSz()
2008         throws Exception
2009     {
2010         String probe = "A string \u2345\u00ef";
2011         IOUtil.copy( new StringReader( probe ), nullWriter(), -1 );
2012     }
2013 
2014     @Test( expected = NegativeArraySizeException.class )
2015     public void copyReaderValidWriterNegBufSz()
2016         throws Exception
2017     {
2018         String probe = "A string \u2345\u00ef";
2019         StringWriter writer = new DontCloseStringWriter();
2020         IOUtil.copy( new StringReader( probe ), writer, -1 );
2021         assertThat( writer.toString(), is( probe ) );
2022     }
2023 
2024     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
2025     public void copyNullReaderNullWriterZeroBufSz()
2026         throws Exception
2027     {
2028         IOUtil.copy( nullReader(), nullWriter(), 0 );
2029     }
2030 
2031     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
2032     public void copyEmptyReaderNullWriterZeroBufSz()
2033         throws Exception
2034     {
2035         IOUtil.copy( emptyReader(), nullWriter(), 0 );
2036     }
2037 
2038     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
2039     public void copyNullReaderValidWriterZeroBufSz()
2040         throws Exception
2041     {
2042         IOUtil.copy( nullReader(), new DontCloseStringWriter(), 0 );
2043     }
2044 
2045     @Test( expected = NullPointerException.class )
2046     public void copyNullReaderNullWriterPosBufSz()
2047         throws Exception
2048     {
2049         IOUtil.copy( nullReader(), nullWriter(), 1 );
2050     }
2051 
2052     @Test( expected = NullPointerException.class )
2053     public void copyEmptyReaderNullWriterPosBufSz()
2054         throws Exception
2055     {
2056         IOUtil.copy( emptyReader(), nullWriter(), 1 );
2057     }
2058 
2059     @Test( expected = NullPointerException.class )
2060     public void copyNullReaderValidWriterPosBufSz()
2061         throws Exception
2062     {
2063         IOUtil.copy( nullReader(), new DontCloseStringWriter(), 1 );
2064     }
2065 
2066     @Test
2067     public void copyEmptyReaderValidWriterPosBufSz()
2068         throws Exception
2069     {
2070         StringWriter writer = new DontCloseStringWriter();
2071         IOUtil.copy( emptyReader(), writer, 1 );
2072         assertThat( writer.toString(), is( emptyString() ) );
2073     }
2074 
2075     @Test( expected = NullPointerException.class )
2076     public void copyReaderNullWriterPosBufSz()
2077         throws Exception
2078     {
2079         String probe = "A string \u2345\u00ef";
2080         IOUtil.copy( new StringReader( probe ), nullWriter(), 1 );
2081     }
2082 
2083     @Test
2084     public void copyReaderValidWriterPosBufSz()
2085         throws Exception
2086     {
2087         String probe = "A string \u2345\u00ef";
2088         StringWriter writer = new DontCloseStringWriter();
2089         IOUtil.copy( new StringReader( probe ), writer, 1 );
2090         assertThat( writer.toString(), is( probe ) );
2091     }
2092 
2093     /*
2094      * toByteArray(InputStream,int)
2095      */
2096 
2097     @Test( expected = NegativeArraySizeException.class )
2098     public void toByteArrayFromInputStreamNegBufSz()
2099         throws Exception
2100     {
2101         String probe = "A string \u2345\u00ef";
2102         assertThat( IOUtil.toByteArray( new DontCloseByteArrayInputStream( IOUtil.toByteArray( probe ) ), -1 ),
2103                     is( probe.getBytes() ) );
2104     }
2105 
2106     @Test( expected = NegativeArraySizeException.class )
2107     public void toByteArrayNullInputStreamNegBufSz()
2108         throws Exception
2109     {
2110         IOUtil.toByteArray( nullInputStream(), -1 );
2111     }
2112 
2113     @Test
2114     public void toByteArrayFromInputStreamPosBufSz()
2115         throws Exception
2116     {
2117         String probe = "A string \u2345\u00ef";
2118         assertThat( IOUtil.toByteArray( new DontCloseByteArrayInputStream( IOUtil.toByteArray( probe ) ), +1 ),
2119                     is( probe.getBytes() ) );
2120     }
2121 
2122     @Test( expected = NullPointerException.class )
2123     public void toByteArrayNullInputStreamPosBufSz()
2124         throws Exception
2125     {
2126         IOUtil.toByteArray( nullInputStream(), +1 );
2127     }
2128 
2129     /*
2130      * toByteArray(Reader,int)
2131      */
2132 
2133     @Test( expected = NegativeArraySizeException.class )
2134     public void toByteArrayFromReaderNegBufSz()
2135         throws Exception
2136     {
2137         String probe = "A string \u2345\u00ef";
2138         assertThat( IOUtil.toByteArray( new DontCloseStringReader( probe ), -1 ),
2139                     is( probe.getBytes() ) );
2140     }
2141 
2142     @Test( expected = NegativeArraySizeException.class )
2143     public void toByteArrayNullReaderNegBufSz()
2144         throws Exception
2145     {
2146         IOUtil.toByteArray( nullReader(), -1 );
2147     }
2148 
2149     @Test
2150     public void toByteArrayFromReaderPosBufSz()
2151         throws Exception
2152     {
2153         String probe = "A string \u2345\u00ef";
2154         assertThat( IOUtil.toByteArray( new DontCloseStringReader( probe ), +1 ),
2155                     is( probe.getBytes() ) );
2156     }
2157 
2158     @Test( expected = NullPointerException.class )
2159     public void toByteArrayNullReaderPosBufSz()
2160         throws Exception
2161     {
2162         IOUtil.toByteArray( nullReader(), +1 );
2163     }
2164 
2165     /*
2166      * toByteArray(String,int)
2167      */
2168 
2169     @Test( expected = NegativeArraySizeException.class )
2170     public void toByteArrayFromStringNegBufSz()
2171         throws Exception
2172     {
2173         String probe = "A string \u2345\u00ef";
2174         assertThat( IOUtil.toByteArray( probe, -1 ), is( probe.getBytes() ) );
2175     }
2176 
2177     @Test( expected = NullPointerException.class )
2178     public void toByteArrayNullStringNegBufSz()
2179         throws Exception
2180     {
2181         IOUtil.toByteArray( nullString(), -1 );
2182     }
2183 
2184     @Test
2185     public void toByteArrayFromStringPosBufSz()
2186         throws Exception
2187     {
2188         String probe = "A string \u2345\u00ef";
2189         assertThat( IOUtil.toByteArray( probe, +1 ), is( probe.getBytes() ) );
2190     }
2191 
2192     @Test( expected = NullPointerException.class )
2193     public void toByteArrayNullStringPosBufSz()
2194         throws Exception
2195     {
2196         IOUtil.toByteArray( nullString(), +1 );
2197     }
2198 
2199     /*
2200      * toString(Reader,int)
2201      */
2202 
2203     @Test( expected = NegativeArraySizeException.class )
2204     public void toStringFromReaderNegBufSz()
2205         throws Exception
2206     {
2207         String probe = "A string \u2345\u00ef";
2208         assertThat( IOUtil.toString( new DontCloseStringReader( probe ), -1 ),
2209                     is( probe) );
2210     }
2211 
2212     @Test( expected = NegativeArraySizeException.class )
2213     public void toStringNullReaderNegBufSz()
2214         throws Exception
2215     {
2216         IOUtil.toString( nullReader(), -1 );
2217     }
2218 
2219     @Test
2220     public void toStringFromReaderPosBufSz()
2221         throws Exception
2222     {
2223         String probe = "A string \u2345\u00ef";
2224         assertThat( IOUtil.toString( new DontCloseStringReader( probe ), +1 ),
2225                     is( probe) );
2226     }
2227 
2228     @Test( expected = NullPointerException.class )
2229     public void toStringNullReaderPosBufSz()
2230         throws Exception
2231     {
2232         IOUtil.toString( nullReader(), +1 );
2233     }
2234 
2235     /*
2236      * copy(Reader,OutputStream)
2237      */
2238 
2239     @Test( expected = NullPointerException.class )
2240     public void copyNullReaderNullOutputStream()
2241         throws Exception
2242     {
2243         IOUtil.copy( nullReader(), nullOutputStream() );
2244     }
2245 
2246     @Test( expected = NullPointerException.class )
2247     public void copyNullReaderValidOutputStream()
2248         throws Exception
2249     {
2250         IOUtil.copy( nullReader(), new DontCloseByteArrayOutputStream() );
2251     }
2252 
2253     @Test( expected = NullPointerException.class )
2254     public void copyEmptyReaderNullOutputStream()
2255         throws Exception
2256     {
2257         IOUtil.copy( emptyReader(), nullOutputStream() );
2258     }
2259 
2260     @Test
2261     public void copyEmptyReaderValidOutputStream()
2262         throws Exception
2263     {
2264         IOUtil.copy( emptyReader(), new DontCloseByteArrayOutputStream() );
2265     }
2266 
2267     @Test
2268     public void copyReaderValidOutputStream()
2269         throws Exception
2270     {
2271         ByteArrayOutputStream outputStream = new DontCloseByteArrayOutputStream();
2272         String probe = "A string \u2345\u00ef";
2273         IOUtil.copy( new DontCloseStringReader( probe ), outputStream );
2274         assertThat( outputStream.toByteArray(), is( probe.getBytes()) );
2275     }
2276 
2277     /*
2278      * copy(Reader,OutputStream,int)
2279      */
2280 
2281     @Test( expected = NullPointerException.class )
2282     public void copyNullReaderNullOutputStreamNegBufSz()
2283         throws Exception
2284     {
2285         IOUtil.copy( nullReader(), nullOutputStream(), -1 );
2286     }
2287 
2288     @Test( expected = NegativeArraySizeException.class )
2289     public void copyNullReaderValidOutputStreamNegBufSz()
2290         throws Exception
2291     {
2292         IOUtil.copy( nullReader(), new DontCloseByteArrayOutputStream(), -1 );
2293     }
2294 
2295     @Test( expected = NullPointerException.class )
2296     public void copyEmptyReaderNullOutputStreamNegBufSz()
2297         throws Exception
2298     {
2299         IOUtil.copy( emptyReader(), nullOutputStream(), -1 );
2300     }
2301 
2302     @Test(expected = NegativeArraySizeException.class)
2303     public void copyEmptyReaderValidOutputStreamNegBufSz()
2304         throws Exception
2305     {
2306         IOUtil.copy( emptyReader(), new DontCloseByteArrayOutputStream(), -1 );
2307     }
2308 
2309     @Test(expected = NegativeArraySizeException.class)
2310     public void copyReaderValidOutputStreamNegBufSz()
2311         throws Exception
2312     {
2313         ByteArrayOutputStream outputStream = new DontCloseByteArrayOutputStream();
2314         String probe = "A string \u2345\u00ef";
2315         IOUtil.copy( new DontCloseStringReader( probe ), outputStream, -1 );
2316         assertThat( outputStream.toByteArray(), is( probe.getBytes()) );
2317     }
2318 
2319     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
2320     public void copyNullReaderNullOutputStreamZeroBufSz()
2321         throws Exception
2322     {
2323         IOUtil.copy( nullReader(), nullOutputStream(), 0 );
2324     }
2325 
2326     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
2327     public void copyNullReaderValidOutputStreamZeroBufSz()
2328         throws Exception
2329     {
2330         IOUtil.copy( nullReader(), new DontCloseByteArrayOutputStream(), 0 );
2331     }
2332 
2333     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
2334     public void copyEmptyReaderNullOutputStreamZeroBufSz()
2335         throws Exception
2336     {
2337         IOUtil.copy( emptyReader(), nullOutputStream(), 0 );
2338     }
2339 
2340     @Test( expected = NullPointerException.class )
2341     public void copyNullReaderNullOutputStreamPosBufSz()
2342         throws Exception
2343     {
2344         IOUtil.copy( nullReader(), nullOutputStream(), 1 );
2345     }
2346 
2347     @Test( expected = NullPointerException.class )
2348     public void copyNullReaderValidOutputStreamPosBufSz()
2349         throws Exception
2350     {
2351         IOUtil.copy( nullReader(), new DontCloseByteArrayOutputStream(), 1 );
2352     }
2353 
2354     @Test( expected = NullPointerException.class )
2355     public void copyEmptyReaderNullOutputStreamPosBufSz()
2356         throws Exception
2357     {
2358         IOUtil.copy( emptyReader(), nullOutputStream(), 1 );
2359     }
2360 
2361     @Test
2362     public void copyEmptyReaderValidOutputStreamPosBufSz()
2363         throws Exception
2364     {
2365         IOUtil.copy( emptyReader(), new DontCloseByteArrayOutputStream(), 1 );
2366     }
2367 
2368     @Test
2369     public void copyReaderValidOutputStreamPosBufSz()
2370         throws Exception
2371     {
2372         ByteArrayOutputStream outputStream = new DontCloseByteArrayOutputStream();
2373         String probe = "A string \u2345\u00ef";
2374         IOUtil.copy( new DontCloseStringReader( probe ), outputStream, 1 );
2375         assertThat( outputStream.toByteArray(), is( probe.getBytes()) );
2376     }
2377 
2378     /*
2379      * copy(byte[],Writer)
2380      */
2381 
2382     /*
2383      * copy(byte[],Writer,int)
2384      */
2385 
2386     /*
2387      * copy(byte[],Writer,String)
2388      */
2389 
2390     /*
2391      * copy(byte[],Writer,String,int)
2392      */
2393     /*
2394      * copy(byte[],Writer)
2395      */
2396 
2397     @Test( expected = NullPointerException.class )
2398     public void copyNullByteArrayNullWriter()
2399         throws Exception
2400     {
2401         IOUtil.copy( nullByteArray(), nullWriter() );
2402     }
2403 
2404     @Test( expected = NullPointerException.class )
2405     public void copyEmptyByteArrayNullWriter()
2406         throws Exception
2407     {
2408         IOUtil.copy( emptyByteArray(), nullWriter() );
2409     }
2410 
2411     @Test
2412     public void copyEmptyByteArrayValidWriter()
2413         throws Exception
2414     {
2415         StringWriter writer = new DontCloseStringWriter();
2416         IOUtil.copy( emptyByteArray(), writer );
2417         assertThat( writer.toString(), is( emptyString() ) );
2418     }
2419 
2420     @Test( expected = NullPointerException.class )
2421     public void copyByteArrayNullWriter()
2422         throws Exception
2423     {
2424         String probe = "A string \u2345\u00ef";
2425         IOUtil.copy( probe.getBytes(), nullWriter() );
2426     }
2427 
2428     @Test
2429     public void copyByteArrayValidWriter()
2430         throws Exception
2431     {
2432         String probe = "A string \u2345\u00ef";
2433         StringWriter writer = new DontCloseStringWriter();
2434         IOUtil.copy( probe.getBytes(), writer );
2435         assertThat( writer.toString().getBytes(), is( probe.getBytes() ) );
2436     }
2437 
2438     /*
2439      * copy(byte[],Writer,int)
2440      */
2441 
2442     @Test( expected = NullPointerException.class )
2443     public void copyNullByteArrayNullWriterNegBufSz()
2444         throws Exception
2445     {
2446         IOUtil.copy( nullByteArray(), nullWriter(), -1 );
2447     }
2448 
2449     @Test( expected = NegativeArraySizeException.class )
2450     public void copyEmptyByteArrayNullWriterNegBufSz()
2451         throws Exception
2452     {
2453         IOUtil.copy( emptyByteArray(), nullWriter(), -1 );
2454     }
2455 
2456     @Test( expected = NegativeArraySizeException.class )
2457     public void copyEmptyByteArrayValidWriterNegBufSz()
2458         throws Exception
2459     {
2460         IOUtil.copy( emptyByteArray(), new DontCloseStringWriter(), -1 );
2461     }
2462 
2463     @Test( expected = NegativeArraySizeException.class )
2464     public void copyByteArrayNullWriterNegBufSz()
2465         throws Exception
2466     {
2467         String probe = "A string \u2345\u00ef";
2468         IOUtil.copy( probe.getBytes(), nullWriter(), -1 );
2469     }
2470 
2471     @Test( expected = NegativeArraySizeException.class )
2472     public void copyByteArrayValidWriterNegBufSz()
2473         throws Exception
2474     {
2475         String probe = "A string \u2345\u00ef";
2476         StringWriter writer = new DontCloseStringWriter();
2477         IOUtil.copy( probe.getBytes(), writer, -1 );
2478         assertThat( writer.toString().getBytes(), is( probe.getBytes() ) );
2479     }
2480 
2481     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
2482     public void copyNullByteArrayNullWriterZeroBufSz()
2483         throws Exception
2484     {
2485         IOUtil.copy( nullByteArray(), nullWriter(), 0 );
2486     }
2487 
2488     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
2489     public void copyNullByteArrayValidWriterZeroBufSz()
2490         throws Exception
2491     {
2492         IOUtil.copy( nullByteArray(), new DontCloseStringWriter(), 0 );
2493     }
2494 
2495     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
2496     public void copyEmptyByteArrayNullWriterZeroBufSz()
2497         throws Exception
2498     {
2499         IOUtil.copy( emptyByteArray(), nullWriter(), 0 );
2500     }
2501 
2502     @Test( expected = NullPointerException.class )
2503     public void copyNullByteArrayNullWriterPosBufSz()
2504         throws Exception
2505     {
2506         IOUtil.copy( nullByteArray(), nullWriter(), 1 );
2507     }
2508 
2509     @Test( expected = NullPointerException.class )
2510     public void copyNullByteArrayValidWriterPosBufSz()
2511         throws Exception
2512     {
2513         IOUtil.copy( nullByteArray(), new DontCloseStringWriter(), 1 );
2514     }
2515 
2516     @Test( expected = NullPointerException.class )
2517     public void copyEmptyByteArrayNullWriterPosBufSz()
2518         throws Exception
2519     {
2520         IOUtil.copy( emptyByteArray(), nullWriter(), 1 );
2521     }
2522 
2523     @Test
2524     public void copyEmptyByteArrayValidWriterPosBufSz()
2525         throws Exception
2526     {
2527         StringWriter writer = new DontCloseStringWriter();
2528         IOUtil.copy( emptyByteArray(), writer, 1 );
2529         assertThat( writer.toString(), is( emptyString() ) );
2530     }
2531 
2532     @Test
2533     public void copyByteArrayValidWriterPosBufSz()
2534         throws Exception
2535     {
2536         String probe = "A string \u2345\u00ef";
2537         StringWriter writer = new DontCloseStringWriter();
2538         IOUtil.copy( probe.getBytes(), writer, 1 );
2539         assertThat( writer.toString().getBytes(), is( probe.getBytes() ) );
2540     }
2541 
2542     /*
2543      * copy(byte[],Writer,String)
2544      */
2545 
2546     @Test( expected = NullPointerException.class )
2547     public void copyNullByteArrayNullWriterNullEncoding()
2548         throws Exception
2549     {
2550         IOUtil.copy( nullByteArray(), nullWriter(), null );
2551     }
2552 
2553     @Test( expected = NullPointerException.class )
2554     public void copyNullByteArrayValidWriterNullEncoding()
2555         throws Exception
2556     {
2557         IOUtil.copy( nullByteArray(), new DontCloseStringWriter(), null );
2558     }
2559 
2560     @Test( expected = NullPointerException.class )
2561     public void copyEmptyByteArrayNullWriterNullEncoding()
2562         throws Exception
2563     {
2564         IOUtil.copy( emptyByteArray(), nullWriter(), null );
2565     }
2566 
2567     @Test( expected = NullPointerException.class )
2568     public void copyEmptyByteArrayValidWriterNullEncoding()
2569         throws Exception
2570     {
2571         IOUtil.copy( emptyByteArray(), new DontCloseStringWriter(), null );
2572     }
2573 
2574     @Test( expected = NullPointerException.class )
2575     public void copyByteArrayNullEncoding()
2576         throws Exception
2577     {
2578         String probe = "A string \u2345\u00ef";
2579         StringWriter writer = new DontCloseStringWriter();
2580         IOUtil.copy( probe.getBytes(), writer, null );
2581         assertThat( writer.toString().getBytes(), is( probe.getBytes() ) );
2582     }
2583 
2584     @Test( expected = NullPointerException.class )
2585     public void copyNullByteArrayNullWriterJunkEncoding()
2586         throws Exception
2587     {
2588         IOUtil.copy( nullByteArray(), nullWriter(), "junk" );
2589     }
2590 
2591     @Test( expected = NullPointerException.class )
2592     public void copyNullByteArrayValidWriterJunkEncoding()
2593         throws Exception
2594     {
2595         IOUtil.copy( nullByteArray(), new DontCloseStringWriter(), "junk" );
2596     }
2597 
2598     @Test( expected = UnsupportedEncodingException.class )
2599     public void copyEmptyByteArrayNullWriterJunkEncoding()
2600         throws Exception
2601     {
2602         IOUtil.copy( emptyByteArray(), nullWriter(), "junk" );
2603     }
2604 
2605     @Test( expected = UnsupportedEncodingException.class )
2606     public void copyEmptyByteArrayValidWriterJunkEncoding()
2607         throws Exception
2608     {
2609         IOUtil.copy( emptyByteArray(), new DontCloseStringWriter(), "junk" );
2610     }
2611 
2612     @Test( expected = UnsupportedEncodingException.class )
2613     public void copyByteArrayNullWriterJunkEncoding()
2614         throws Exception
2615     {
2616         String probe = "A string \u2345\u00ef";
2617         IOUtil.copy( probe.getBytes(), nullWriter(), "junk" );
2618     }
2619 
2620     @Test( expected = UnsupportedEncodingException.class )
2621     public void copyByteArrayValidWriterJunkEncoding()
2622         throws Exception
2623     {
2624         String probe = "A string \u2345\u00ef";
2625         StringWriter writer = new DontCloseStringWriter();
2626         IOUtil.copy( probe.getBytes(), writer, "junk" );
2627         assertThat( writer.toString().getBytes(), is( probe.getBytes() ) );
2628     }
2629 
2630     @Test( expected = NullPointerException.class )
2631     public void copyNullByteArrayNullWriterValidEncoding()
2632         throws Exception
2633     {
2634         IOUtil.copy( nullByteArray(), nullWriter(), "utf-16" );
2635     }
2636 
2637     @Test( expected = NullPointerException.class )
2638     public void copyEmptyByteArrayNullWriterValidEncoding()
2639         throws Exception
2640     {
2641         IOUtil.copy( emptyByteArray(), nullWriter(), "utf-16" );
2642     }
2643 
2644     @Test( expected = NullPointerException.class )
2645     public void copyNullByteArrayValidWriterValidEncoding()
2646         throws Exception
2647     {
2648         IOUtil.copy( nullByteArray(), new DontCloseStringWriter(), "utf-16" );
2649     }
2650 
2651     @Test
2652     public void copyEmptyByteArrayValidWriterValidEncoding()
2653         throws Exception
2654     {
2655         StringWriter writer = new DontCloseStringWriter();
2656         IOUtil.copy( emptyByteArray(), writer, "utf-16" );
2657         assertThat( writer.toString(), is( emptyString() ) );
2658     }
2659 
2660     @Test( expected = NullPointerException.class )
2661     public void copyByteArrayNullWriterValidEncoding()
2662         throws Exception
2663     {
2664         String probe = "A string \u2345\u00ef";
2665         IOUtil.copy( probe.getBytes( "utf-16" ), nullWriter(), "utf-16" );
2666     }
2667 
2668     @Test
2669     public void copyByteArrayValidWriterValidEncoding()
2670         throws Exception
2671     {
2672         String probe = "A string \u2345\u00ef";
2673         StringWriter writer = new DontCloseStringWriter();
2674         IOUtil.copy( probe.getBytes( "utf-16" ), writer, "utf-16" );
2675         assertThat( writer.toString().getBytes( "utf-8" ), is( probe.getBytes( "utf-8" ) ) );
2676     }
2677 
2678     /*
2679      * copy(byte[],Writer,String,int)
2680      */
2681 
2682     @Test( expected = NullPointerException.class )
2683     public void copyNullByteArrayNullWriterNullEncodingNegBufSz()
2684         throws Exception
2685     {
2686         IOUtil.copy( nullByteArray(), nullWriter(), null, -1 );
2687     }
2688 
2689     @Test( expected = NullPointerException.class )
2690     public void copyNullByteArrayValidWriterNullEncodingNegBufSz()
2691         throws Exception
2692     {
2693         IOUtil.copy( nullByteArray(), new DontCloseStringWriter(), null, -1 );
2694     }
2695 
2696     @Test( expected = NullPointerException.class )
2697     public void copyEmptyByteArrayNullWriterNullEncodingNegBufSz()
2698         throws Exception
2699     {
2700         IOUtil.copy( emptyByteArray(), nullWriter(), null, -1 );
2701     }
2702 
2703     @Test( expected = NullPointerException.class )
2704     public void copyEmptyByteArrayValidWriterNullEncodingNegBufSz()
2705         throws Exception
2706     {
2707         StringWriter writer = new DontCloseStringWriter();
2708         IOUtil.copy( emptyByteArray(), writer, null, -1 );
2709         assertThat( writer.toString(), is( emptyString() ) );
2710     }
2711 
2712     @Test( expected = NullPointerException.class )
2713     public void copyByteArrayNullWriterNullEncodingNegBufSz()
2714         throws Exception
2715     {
2716         String probe = "A string \u2345\u00ef";
2717         IOUtil.copy( probe.getBytes(), nullWriter(), null, -1 );
2718     }
2719 
2720     @Test( expected = NullPointerException.class )
2721     public void copyByteArrayValidWriterNullEncodingNegBufSz()
2722         throws Exception
2723     {
2724         String probe = "A string \u2345\u00ef";
2725         StringWriter writer = new DontCloseStringWriter();
2726         IOUtil.copy( probe.getBytes(), writer, null, -1 );
2727         assertThat( writer.toString().getBytes(), is( probe.getBytes() ) );
2728     }
2729 
2730     @Test( expected = NullPointerException.class )
2731     public void copyNullByteArrayNullWriterJunkEncodingNegBufSz()
2732         throws Exception
2733     {
2734         IOUtil.copy( nullByteArray(), nullWriter(), "junk", -1 );
2735     }
2736 
2737     @Test( expected = NullPointerException.class )
2738     public void copyNullByteArrayValidWriterJunkEncodingNegBufSz()
2739         throws Exception
2740     {
2741         IOUtil.copy( nullByteArray(), new DontCloseStringWriter(), "junk", -1 );
2742     }
2743 
2744     @Test( expected = UnsupportedEncodingException.class )
2745     public void copyEmptyByteArrayNullWriterJunkEncodingNegBufSz()
2746         throws Exception
2747     {
2748         IOUtil.copy( emptyByteArray(), nullWriter(), "junk", -1 );
2749     }
2750 
2751     @Test( expected = UnsupportedEncodingException.class )
2752     public void copyEmptyByteArrayJunkEncodingNegBufSz()
2753         throws Exception
2754     {
2755         StringWriter writer = new DontCloseStringWriter();
2756         IOUtil.copy( emptyByteArray(), writer, "junk", -1 );
2757         assertThat( writer.toString(), is( emptyString() ) );
2758     }
2759 
2760     @Test( expected = UnsupportedEncodingException.class )
2761     public void copyByteArrayNullWriterJunkEncodingNegBufSz()
2762         throws Exception
2763     {
2764         String probe = "A string \u2345\u00ef";
2765         IOUtil.copy( probe.getBytes(), nullWriter(), "junk", -1 );
2766     }
2767 
2768     @Test( expected = UnsupportedEncodingException.class )
2769     public void copyByteArrayValidWriterJunkEncodingNegBufSz()
2770         throws Exception
2771     {
2772         String probe = "A string \u2345\u00ef";
2773         StringWriter writer = new DontCloseStringWriter();
2774         IOUtil.copy( probe.getBytes(), writer, "junk", -1 );
2775         assertThat( writer.toString().getBytes(), is( probe.getBytes() ) );
2776     }
2777 
2778     @Test( expected = NullPointerException.class )
2779     public void copyNullByteArrayNullWriterValidEncodingNegBufSz()
2780         throws Exception
2781     {
2782         IOUtil.copy( nullByteArray(), nullWriter(), "utf-16", -1 );
2783     }
2784 
2785     @Test( expected = NullPointerException.class )
2786     public void copyNullByteArrayValidWriterValidEncodingNegBufSz()
2787         throws Exception
2788     {
2789         IOUtil.copy( nullByteArray(), new DontCloseStringWriter(), "utf-16", -1 );
2790     }
2791 
2792     @Test( expected = NegativeArraySizeException.class )
2793     public void copyEmptyByteArrayNullWriterValidEncodingNegBufSz()
2794         throws Exception
2795     {
2796         IOUtil.copy( emptyByteArray(), nullWriter(), "utf-16", -1 );
2797     }
2798 
2799     @Test( expected = NegativeArraySizeException.class )
2800     public void copyEmptyByteArrayValidWriterValidEncodingNegBufSz()
2801         throws Exception
2802     {
2803         StringWriter writer = new DontCloseStringWriter();
2804         IOUtil.copy( emptyByteArray(), writer, "utf-16", -1 );
2805         assertThat( writer.toString(), is( emptyString() ) );
2806     }
2807 
2808     @Test( expected = NegativeArraySizeException.class )
2809     public void copyByteArrayNullWriterValidEncodingNegBufSz()
2810         throws Exception
2811     {
2812         String probe = "A string \u2345\u00ef";
2813         IOUtil.copy( probe.getBytes( "utf-16" ), nullWriter(), -1 );
2814     }
2815 
2816     @Test( expected = NegativeArraySizeException.class )
2817     public void copyByteArrayValidEncodingNegBufSz()
2818         throws Exception
2819     {
2820         String probe = "A string \u2345\u00ef";
2821         StringWriter writer = new DontCloseStringWriter();
2822         IOUtil.copy( probe.getBytes( "utf-16" ), writer, "utf-16", -1 );
2823         assertThat( writer.toString().getBytes( "utf-8" ), is( probe.getBytes( "utf-8" ) ) );
2824     }
2825 
2826     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
2827     public void copyNullByteArrayNullWriterNullEncodingZeroBufSz()
2828         throws Exception
2829     {
2830         IOUtil.copy( nullByteArray(), nullWriter(), null, 0 );
2831     }
2832 
2833     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
2834     public void copyNullByteArrayValidWriterNullEncodingZeroBufSz()
2835         throws Exception
2836     {
2837         IOUtil.copy( nullByteArray(), new DontCloseStringWriter(), null, 0 );
2838     }
2839 
2840     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
2841     public void copyEmptyByteArrayNullWriterNullEncodingZeroBufSz()
2842         throws Exception
2843     {
2844         IOUtil.copy( emptyByteArray(), nullWriter(), null, 0 );
2845     }
2846 
2847     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
2848     public void copyEmptyByteArrayValidWriterNullEncodingZeroBufSz()
2849         throws Exception
2850     {
2851         StringWriter writer = new DontCloseStringWriter();
2852         IOUtil.copy( emptyByteArray(), writer, null, 0 );
2853         assertThat( writer.toString(), is( emptyString() ) );
2854     }
2855 
2856     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
2857     public void copyByteArrayNullWriterNullEncodingZeroBufSz()
2858         throws Exception
2859     {
2860         String probe = "A string \u2345\u00ef";
2861         IOUtil.copy( probe.getBytes(), nullWriter(), null, 0 );
2862     }
2863 
2864     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
2865     public void copyByteArrayValidWriterNullEncodingZeroBufSz()
2866         throws Exception
2867     {
2868         String probe = "A string \u2345\u00ef";
2869         StringWriter writer = new DontCloseStringWriter();
2870         IOUtil.copy( probe.getBytes(), writer, null, 0 );
2871         assertThat( writer.toString().getBytes(), is( probe.getBytes() ) );
2872     }
2873 
2874     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
2875     public void copyNullByteArrayNullWriterJunkEncodingZeroBufSz()
2876         throws Exception
2877     {
2878         IOUtil.copy( nullByteArray(), nullWriter(), "junk", 0 );
2879     }
2880 
2881     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
2882     public void copyNullByteArrayValidWriterJunkEncodingZeroBufSz()
2883         throws Exception
2884     {
2885         IOUtil.copy( nullByteArray(), new DontCloseStringWriter(), "junk", 0 );
2886     }
2887 
2888     @Test( expected = UnsupportedEncodingException.class, timeout = INFINITE_LOOP_TIMEOUT )
2889     public void copyEmptyByteArrayNullWriterJunkEncodingZeroBufSz()
2890         throws Exception
2891     {
2892         IOUtil.copy( emptyByteArray(), nullWriter(), "junk", 0 );
2893     }
2894 
2895     @Test( expected = UnsupportedEncodingException.class, timeout = INFINITE_LOOP_TIMEOUT )
2896     public void copyEmptyByteArrayValidWriterJunkEncodingZeroBufSz()
2897         throws Exception
2898     {
2899         StringWriter writer = new DontCloseStringWriter();
2900         IOUtil.copy( emptyByteArray(), writer, "junk", 0 );
2901         assertThat( writer.toString(), is( emptyString() ) );
2902     }
2903 
2904     @Test( expected = UnsupportedEncodingException.class, timeout = INFINITE_LOOP_TIMEOUT )
2905     public void copyByteArrayNullWriterJunkEncodingZeroBufSz()
2906         throws Exception
2907     {
2908         String probe = "A string \u2345\u00ef";
2909         IOUtil.copy( probe.getBytes(), nullWriter(), "junk", 0 );
2910     }
2911 
2912     @Test( expected = UnsupportedEncodingException.class, timeout = INFINITE_LOOP_TIMEOUT )
2913     public void copyByteArrayValidWriterJunkEncodingZeroBufSz()
2914         throws Exception
2915     {
2916         String probe = "A string \u2345\u00ef";
2917         StringWriter writer = new DontCloseStringWriter();
2918         IOUtil.copy( probe.getBytes(), writer, "junk", 0 );
2919         assertThat( writer.toString().getBytes(), is( probe.getBytes() ) );
2920     }
2921 
2922     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
2923     public void copyNullByteArrayNullWriterValidEncodingZeroBufSz()
2924         throws Exception
2925     {
2926         IOUtil.copy( nullByteArray(), nullWriter(), "utf-16", 0 );
2927     }
2928 
2929     @Test( expected = NullPointerException.class, timeout = INFINITE_LOOP_TIMEOUT )
2930     public void copyNullByteArrayValidWriterValidEncodingZeroBufSz()
2931         throws Exception
2932     {
2933         IOUtil.copy( nullByteArray(), new DontCloseStringWriter(), "utf-16", 0 );
2934     }
2935 
2936     /*
2937      * Utility methods
2938      */
2939     private static byte[] nullByteArray()
2940     {
2941         return null;
2942     }
2943 
2944     private static String nullString()
2945     {
2946         return null;
2947     }
2948 
2949     private static OutputStream nullOutputStream()
2950     {
2951         return null;
2952     }
2953 
2954     private static InputStream nullInputStream()
2955     {
2956         return null;
2957     }
2958 
2959     private static Writer nullWriter()
2960     {
2961         return null;
2962     }
2963 
2964     private static Reader nullReader()
2965     {
2966         return null;
2967     }
2968 
2969     private static ByteArrayInputStream emptyInputStream()
2970     {
2971         return new ByteArrayInputStream( emptyByteArray() );
2972     }
2973 
2974     private static Reader emptyReader()
2975     {
2976         return new StringReader( emptyString() );
2977     }
2978 
2979     private static String emptyString()
2980     {
2981         return "";
2982     }
2983 
2984     private static byte[] emptyByteArray()
2985     {
2986         return new byte[0];
2987     }
2988 
2989     private static class DontCloseStringWriter
2990         extends StringWriter
2991     {
2992         @Override
2993         public void close()
2994             throws IOException
2995         {
2996             throw new UnsupportedOperationException( "should not be called" );
2997         }
2998     }
2999 
3000     private static class DontCloseStringReader
3001         extends StringReader
3002     {
3003 
3004         public DontCloseStringReader( String s )
3005         {
3006             super( s );
3007         }
3008 
3009         @Override
3010         public void close()
3011         {
3012             throw new UnsupportedOperationException( "should not be called" );
3013         }
3014     }
3015 
3016     private static class DontCloseByteArrayInputStream
3017         extends ByteArrayInputStream
3018     {
3019         public DontCloseByteArrayInputStream( byte[] input )
3020         {
3021             super( input );
3022         }
3023 
3024         @Override
3025         public void close()
3026             throws IOException
3027         {
3028             throw new UnsupportedOperationException( "should not be called" );
3029         }
3030     }
3031 
3032     private static class DontCloseByteArrayOutputStream
3033         extends ByteArrayOutputStream
3034     {
3035         @Override
3036         public void close()
3037             throws IOException
3038         {
3039             throw new UnsupportedOperationException( "should not be called" );
3040         }
3041     }
3042 }