1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.io;
18  
19  import java.io.File;
20  import java.io.FileInputStream;
21  import java.io.FileOutputStream;
22  import java.io.InputStream;
23  import java.io.InputStreamReader;
24  import java.io.OutputStream;
25  import java.io.OutputStreamWriter;
26  import java.io.Reader;
27  import java.io.StringWriter;
28  import java.io.Writer;
29  import java.util.Arrays;
30  import java.util.List;
31  
32  import junit.framework.Test;
33  import junit.framework.TestSuite;
34  import junit.textui.TestRunner;
35  
36  import org.apache.commons.io.output.ByteArrayOutputStream;
37  import org.apache.commons.io.testtools.FileBasedTestCase;
38  import org.apache.commons.io.testtools.YellOnFlushAndCloseOutputStream;
39  
40  /**
41   * JUnit tests for IOUtils write methods.
42   * 
43   * @author Jeff Turner
44   * @author Matthew Hawthorne
45   * @author Jeremias Maerki
46   * @author Stephen Colebourne
47   * @version $Id: IOUtilsWriteTestCase.java 437567 2006-08-28 06:39:07Z bayard $
48   * @see IOUtils
49   */
50  public class IOUtilsWriteTestCase extends FileBasedTestCase {
51  
52      private static final int FILE_SIZE = 1024 * 4 + 1;
53  
54  
55      private byte[] inData = generateTestData(FILE_SIZE);
56  
57      public static void main(String[] args) {
58          TestRunner.run(suite());
59      }
60  
61      public static Test suite() {
62          return new TestSuite(IOUtilsWriteTestCase.class);
63      }
64  
65      public IOUtilsWriteTestCase(String testName) {
66          super(testName);
67      }
68  
69      // ----------------------------------------------------------------
70      // Setup
71      // ----------------------------------------------------------------
72  
73      public void setUp() throws Exception {
74      }
75  
76      public void tearDown() throws Exception {
77      }
78  
79      // ----------------------------------------------------------------
80      // Tests
81      // ----------------------------------------------------------------
82  
83      //-----------------------------------------------------------------------
84      public void testWrite_byteArrayToOutputStream() throws Exception {
85          ByteArrayOutputStream baout = new ByteArrayOutputStream();
86          YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
87          
88          IOUtils.write(inData, out);
89          out.off();
90          out.flush();
91  
92          assertEquals("Sizes differ", inData.length, baout.size());
93          assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
94      }
95  
96      public void testWrite_byteArrayToOutputStream_nullData() throws Exception {
97          ByteArrayOutputStream baout = new ByteArrayOutputStream();
98          YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
99          
100         IOUtils.write((byte[]) null, out);
101         out.off();
102         out.flush();
103 
104         assertEquals("Sizes differ", 0, baout.size());
105     }
106 
107     public void testWrite_byteArrayToOutputStream_nullStream() throws Exception {
108         try {
109             IOUtils.write(inData, (OutputStream) null);
110             fail();
111         } catch (NullPointerException ex) {}
112     }
113 
114     //-----------------------------------------------------------------------
115     public void testWrite_byteArrayToWriter() throws Exception {
116         ByteArrayOutputStream baout = new ByteArrayOutputStream();
117         YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
118         Writer writer = new OutputStreamWriter(baout, "US-ASCII");
119         
120         IOUtils.write(inData, writer);
121         out.off();
122         writer.flush();
123 
124         assertEquals("Sizes differ", inData.length, baout.size());
125         assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
126     }
127 
128     public void testWrite_byteArrayToWriter_nullData() throws Exception {
129         ByteArrayOutputStream baout = new ByteArrayOutputStream();
130         YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
131         Writer writer = new OutputStreamWriter(baout, "US-ASCII");
132         
133         IOUtils.write((byte[]) null, writer);
134         out.off();
135         writer.flush();
136 
137         assertEquals("Sizes differ", 0, baout.size());
138     }
139 
140     public void testWrite_byteArrayToWriter_nullWriter() throws Exception {
141         try {
142             IOUtils.write(inData, (Writer) null);
143             fail();
144         } catch (NullPointerException ex) {}
145     }
146 
147     //-----------------------------------------------------------------------
148     public void testWrite_byteArrayToWriter_Encoding() throws Exception {
149         ByteArrayOutputStream baout = new ByteArrayOutputStream();
150         YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
151         Writer writer = new OutputStreamWriter(baout, "US-ASCII");
152         
153         IOUtils.write(inData, writer, "UTF8");
154         out.off();
155         writer.flush();
156 
157         byte[] bytes = baout.toByteArray();
158         bytes = new String(bytes, "UTF8").getBytes("US-ASCII");
159         assertTrue("Content differs", Arrays.equals(inData, bytes));
160     }
161 
162     public void testWrite_byteArrayToWriter_Encoding_nullData() throws Exception {
163         ByteArrayOutputStream baout = new ByteArrayOutputStream();
164         YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
165         Writer writer = new OutputStreamWriter(baout, "US-ASCII");
166         
167         IOUtils.write((byte[]) null, writer, "UTF8");
168         out.off();
169         writer.flush();
170 
171         assertEquals("Sizes differ", 0, baout.size());
172     }
173 
174     public void testWrite_byteArrayToWriter_Encoding_nullWriter() throws Exception {
175         try {
176             IOUtils.write(inData, (Writer) null, "UTF8");
177             fail();
178         } catch (NullPointerException ex) {}
179     }
180 
181     public void testWrite_byteArrayToWriter_Encoding_nullEncoding() throws Exception {
182         ByteArrayOutputStream baout = new ByteArrayOutputStream();
183         YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
184         Writer writer = new OutputStreamWriter(baout, "US-ASCII");
185         
186         IOUtils.write(inData, writer, null);
187         out.off();
188         writer.flush();
189         
190         assertEquals("Sizes differ", inData.length, baout.size());
191         assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
192     }
193 
194     //-----------------------------------------------------------------------
195     public void testWrite_stringToOutputStream() throws Exception {
196         String str = new String(inData, "US-ASCII");
197         
198         ByteArrayOutputStream baout = new ByteArrayOutputStream();
199         YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
200         
201         IOUtils.write(str, out);
202         out.off();
203         out.flush();
204         
205         assertEquals("Sizes differ", inData.length, baout.size());
206         assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
207     }
208 
209     public void testWrite_stringToOutputStream_nullData() throws Exception {
210         ByteArrayOutputStream baout = new ByteArrayOutputStream();
211         YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
212         
213         IOUtils.write((String) null, out);
214         out.off();
215         out.flush();
216 
217         assertEquals("Sizes differ", 0, baout.size());
218     }
219 
220     public void testWrite_stringToOutputStream_nullStream() throws Exception {
221         String str = new String(inData, "US-ASCII");
222         try {
223             IOUtils.write(str, (OutputStream) null);
224             fail();
225         } catch (NullPointerException ex) {}
226     }
227 
228     //-----------------------------------------------------------------------
229     public void testWrite_stringToOutputStream_Encoding() throws Exception {
230         String str = new String(inData, "US-ASCII");
231         
232         ByteArrayOutputStream baout = new ByteArrayOutputStream();
233         YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
234 
235         IOUtils.write(str, out, "UTF16");
236         out.off();
237         out.flush();
238         
239         byte[] bytes = baout.toByteArray();
240         bytes = new String(bytes, "UTF16").getBytes("US-ASCII");
241         assertTrue("Content differs", Arrays.equals(inData, bytes));
242     }
243 
244     public void testWrite_stringToOutputStream_Encoding_nullData() throws Exception {
245         ByteArrayOutputStream baout = new ByteArrayOutputStream();
246         YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
247         
248         IOUtils.write((String) null, out);
249         out.off();
250         out.flush();
251 
252         assertEquals("Sizes differ", 0, baout.size());
253     }
254 
255     public void testWrite_stringToOutputStream_Encoding_nullStream() throws Exception {
256         String str = new String(inData, "US-ASCII");
257         try {
258             IOUtils.write(str, (OutputStream) null);
259             fail();
260         } catch (NullPointerException ex) {}
261     }
262 
263     public void testWrite_stringToOutputStream_nullEncoding() throws Exception {
264         String str = new String(inData, "US-ASCII");
265         
266         ByteArrayOutputStream baout = new ByteArrayOutputStream();
267         YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
268 
269         IOUtils.write(str, out, null);
270         out.off();
271         out.flush();
272 
273         assertEquals("Sizes differ", inData.length, baout.size());
274         assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
275     }
276 
277     //-----------------------------------------------------------------------
278     public void testWrite_stringToWriter() throws Exception {
279         String str = new String(inData, "US-ASCII");
280 
281         ByteArrayOutputStream baout = new ByteArrayOutputStream();
282         YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
283         Writer writer = new OutputStreamWriter(baout, "US-ASCII");
284 
285         IOUtils.write(str, writer);
286         out.off();
287         writer.flush();
288 
289         assertEquals("Sizes differ", inData.length, baout.size());
290         assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
291     }
292 
293     public void testWrite_stringToWriter_Encoding_nullData() throws Exception {
294         ByteArrayOutputStream baout = new ByteArrayOutputStream();
295         YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
296         Writer writer = new OutputStreamWriter(baout, "US-ASCII");
297         
298         IOUtils.write((String) null, writer);
299         out.off();
300         writer.flush();
301 
302         assertEquals("Sizes differ", 0, baout.size());
303     }
304 
305     public void testWrite_stringToWriter_Encoding_nullStream() throws Exception {
306         String str = new String(inData, "US-ASCII");
307         try {
308             IOUtils.write(str, (Writer) null);
309             fail();
310         } catch (NullPointerException ex) {}
311     }
312 
313     //-----------------------------------------------------------------------
314     public void testWrite_charArrayToOutputStream() throws Exception {
315         String str = new String(inData, "US-ASCII");
316         
317         ByteArrayOutputStream baout = new ByteArrayOutputStream();
318         YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
319 
320         IOUtils.write(str.toCharArray(), out);
321         out.off();
322         out.flush();
323 
324         assertEquals("Sizes differ", inData.length, baout.size());
325         assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
326     }
327 
328     public void testWrite_charArrayToOutputStream_nullData() throws Exception {
329         ByteArrayOutputStream baout = new ByteArrayOutputStream();
330         YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
331         
332         IOUtils.write((char[]) null, out);
333         out.off();
334         out.flush();
335 
336         assertEquals("Sizes differ", 0, baout.size());
337     }
338 
339     public void testWrite_charArrayToOutputStream_nullStream() throws Exception {
340         String str = new String(inData, "US-ASCII");
341         try {
342             IOUtils.write(str.toCharArray(), (OutputStream) null);
343             fail();
344         } catch (NullPointerException ex) {}
345     }
346 
347     //-----------------------------------------------------------------------
348     public void testWrite_charArrayToOutputStream_Encoding() throws Exception {
349         String str = new String(inData, "US-ASCII");
350         
351         ByteArrayOutputStream baout = new ByteArrayOutputStream();
352         YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
353 
354         IOUtils.write(str.toCharArray(), out, "UTF16");
355         out.off();
356         out.flush();
357         
358         byte[] bytes = baout.toByteArray();
359         bytes = new String(bytes, "UTF16").getBytes("US-ASCII");
360         assertTrue("Content differs", Arrays.equals(inData, bytes));
361     }
362 
363     public void testWrite_charArrayToOutputStream_Encoding_nullData() throws Exception {
364         ByteArrayOutputStream baout = new ByteArrayOutputStream();
365         YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
366         
367         IOUtils.write((char[]) null, out);
368         out.off();
369         out.flush();
370 
371         assertEquals("Sizes differ", 0, baout.size());
372     }
373 
374     public void testWrite_charArrayToOutputStream_Encoding_nullStream() throws Exception {
375         String str = new String(inData, "US-ASCII");
376         try {
377             IOUtils.write(str.toCharArray(), (OutputStream) null);
378             fail();
379         } catch (NullPointerException ex) {}
380     }
381 
382     public void testWrite_charArrayToOutputStream_nullEncoding() throws Exception {
383         String str = new String(inData, "US-ASCII");
384         
385         ByteArrayOutputStream baout = new ByteArrayOutputStream();
386         YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
387 
388         IOUtils.write(str.toCharArray(), out, null);
389         out.off();
390         out.flush();
391 
392         assertEquals("Sizes differ", inData.length, baout.size());
393         assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
394     }
395 
396     //-----------------------------------------------------------------------
397     public void testWrite_charArrayToWriter() throws Exception {
398         String str = new String(inData, "US-ASCII");
399 
400         ByteArrayOutputStream baout = new ByteArrayOutputStream();
401         YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
402         Writer writer = new OutputStreamWriter(baout, "US-ASCII");
403 
404         IOUtils.write(str.toCharArray(), writer);
405         out.off();
406         writer.flush();
407 
408         assertEquals("Sizes differ", inData.length, baout.size());
409         assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
410     }
411 
412     public void testWrite_charArrayToWriter_Encoding_nullData() throws Exception {
413         ByteArrayOutputStream baout = new ByteArrayOutputStream();
414         YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
415         Writer writer = new OutputStreamWriter(baout, "US-ASCII");
416         
417         IOUtils.write((char[]) null, writer);
418         out.off();
419         writer.flush();
420 
421         assertEquals("Sizes differ", 0, baout.size());
422     }
423 
424     public void testWrite_charArrayToWriter_Encoding_nullStream() throws Exception {
425         String str = new String(inData, "US-ASCII");
426         try {
427             IOUtils.write(str.toCharArray(), (Writer) null);
428             fail();
429         } catch (NullPointerException ex) {}
430     }
431 
432     //-----------------------------------------------------------------------
433     public void testWriteLines_OutputStream() throws Exception {
434         Object[] data = new Object[] {
435             "hello", new StringBuffer("world"), "", "this is", null, "some text"};
436         List list = Arrays.asList(data);
437         
438         ByteArrayOutputStream baout = new ByteArrayOutputStream();
439         YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
440         
441         IOUtils.writeLines(list, "*", out);
442         
443         out.off();
444         out.flush();
445         
446         String expected = "hello*world**this is**some text*";
447         String actual = baout.toString();
448         assertEquals(expected, actual);
449     }
450 
451     public void testWriteLines_OutputStream_nullData() throws Exception {
452         ByteArrayOutputStream baout = new ByteArrayOutputStream();
453         YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
454         
455         IOUtils.writeLines((List) null, "*", out);
456         out.off();
457         out.flush();
458         
459         assertEquals("Sizes differ", 0, baout.size());
460     }
461 
462     public void testWriteLines_OutputStream_nullSeparator() throws Exception {
463         Object[] data = new Object[] {"hello", "world"};
464         List list = Arrays.asList(data);
465             
466         ByteArrayOutputStream baout = new ByteArrayOutputStream();
467         YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
468         
469         IOUtils.writeLines(list, (String) null, out);
470         out.off();
471         out.flush();
472         
473         String expected = "hello" + IOUtils.LINE_SEPARATOR + "world" + IOUtils.LINE_SEPARATOR;
474         String actual = baout.toString();
475         assertEquals(expected, actual);
476     }
477 
478     public void testWriteLines_OutputStream_nullStream() throws Exception {
479         Object[] data = new Object[] {"hello", "world"};
480         List list = Arrays.asList(data);
481         try {
482             IOUtils.writeLines(list, "*", (OutputStream) null);
483             fail();
484         } catch (NullPointerException ex) {}
485     }
486 
487     //-----------------------------------------------------------------------
488     public void testWriteLines_OutputStream_Encoding() throws Exception {
489         Object[] data = new Object[] {
490             "hello\u8364", new StringBuffer("world"), "", "this is", null, "some text"};
491         List list = Arrays.asList(data);
492         
493         ByteArrayOutputStream baout = new ByteArrayOutputStream();
494         YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
495         
496         IOUtils.writeLines(list, "*", out, "UTF-8");
497         
498         out.off();
499         out.flush();
500         
501         String expected = "hello\u8364*world**this is**some text*";
502         String actual = baout.toString("UTF-8");
503         assertEquals(expected, actual);
504     }
505 
506     public void testWriteLines_OutputStream_Encoding_nullData() throws Exception {
507         ByteArrayOutputStream baout = new ByteArrayOutputStream();
508         YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
509         
510         IOUtils.writeLines((List) null, "*", out, "US-ASCII");
511         out.off();
512         out.flush();
513         
514         assertEquals("Sizes differ", 0, baout.size());
515     }
516 
517     public void testWriteLines_OutputStream_Encoding_nullSeparator() throws Exception {
518         Object[] data = new Object[] {"hello", "world"};
519         List list = Arrays.asList(data);
520             
521         ByteArrayOutputStream baout = new ByteArrayOutputStream();
522         YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
523         
524         IOUtils.writeLines(list, (String) null, out, "US-ASCII");
525         out.off();
526         out.flush();
527         
528         String expected = "hello" + IOUtils.LINE_SEPARATOR + "world" + IOUtils.LINE_SEPARATOR;
529         String actual = baout.toString();
530         assertEquals(expected, actual);
531     }
532 
533     public void testWriteLines_OutputStream_Encoding_nullStream() throws Exception {
534         Object[] data = new Object[] {"hello", "world"};
535         List list = Arrays.asList(data);
536         try {
537             IOUtils.writeLines(list, "*", (OutputStream) null, "US-ASCII");
538             fail();
539         } catch (NullPointerException ex) {}
540     }
541 
542     public void testWriteLines_OutputStream_Encoding_nullEncoding() throws Exception {
543         Object[] data = new Object[] {
544             "hello", new StringBuffer("world"), "", "this is", null, "some text"};
545         List list = Arrays.asList(data);
546         
547         ByteArrayOutputStream baout = new ByteArrayOutputStream();
548         YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
549         
550         IOUtils.writeLines(list, "*", out, null);
551         
552         out.off();
553         out.flush();
554         
555         String expected = "hello*world**this is**some text*";
556         String actual = baout.toString();
557         assertEquals(expected, actual);
558     }
559 
560     //-----------------------------------------------------------------------
561     public void testWriteLines_Writer() throws Exception {
562         Object[] data = new Object[] {
563             "hello", new StringBuffer("world"), "", "this is", null, "some text"};
564         List list = Arrays.asList(data);
565         
566         ByteArrayOutputStream baout = new ByteArrayOutputStream();
567         YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
568         Writer writer = new OutputStreamWriter(baout, "US-ASCII");
569         
570         IOUtils.writeLines(list, "*", writer);
571         
572         out.off();
573         writer.flush();
574         
575         String expected = "hello*world**this is**some text*";
576         String actual = baout.toString();
577         assertEquals(expected, actual);
578     }
579 
580     public void testWriteLines_Writer_nullData() throws Exception {
581         ByteArrayOutputStream baout = new ByteArrayOutputStream();
582         YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
583         Writer writer = new OutputStreamWriter(baout, "US-ASCII");
584         
585         IOUtils.writeLines((List) null, "*", writer);
586         out.off();
587         writer.flush();
588         
589         assertEquals("Sizes differ", 0, baout.size());
590     }
591 
592     public void testWriteLines_Writer_nullSeparator() throws Exception {
593         Object[] data = new Object[] {"hello", "world"};
594         List list = Arrays.asList(data);
595             
596         ByteArrayOutputStream baout = new ByteArrayOutputStream();
597         YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(baout, true, true);
598         Writer writer = new OutputStreamWriter(baout, "US-ASCII");
599         
600         IOUtils.writeLines(list, (String) null, writer);
601         out.off();
602         writer.flush();
603         
604         String expected = "hello" + IOUtils.LINE_SEPARATOR + "world" + IOUtils.LINE_SEPARATOR;
605         String actual = baout.toString();
606         assertEquals(expected, actual);
607     }
608 
609     public void testWriteLines_Writer_nullStream() throws Exception {
610         Object[] data = new Object[] {"hello", "world"};
611         List list = Arrays.asList(data);
612         try {
613             IOUtils.writeLines(list, "*", (Writer) null);
614             fail();
615         } catch (NullPointerException ex) {}
616     }
617 
618 }