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.ByteArrayInputStream;
20  import java.io.InputStream;
21  import java.io.OutputStream;
22  import java.io.Reader;
23  import java.io.Writer;
24  import java.util.Arrays;
25  
26  import org.apache.commons.io.output.ByteArrayOutputStream;
27  import org.apache.commons.io.testtools.YellOnCloseInputStream;
28  import org.apache.commons.io.testtools.YellOnFlushAndCloseOutputStream;
29  import org.apache.commons.io.testtools.FileBasedTestCase;
30  
31  import junit.framework.Test;
32  import junit.framework.TestSuite;
33  import junit.textui.TestRunner;
34  
35  /**
36   * JUnit tests for CopyUtils.
37   * 
38   * @author Jeff Turner
39   * @author Matthew Hawthorne
40   * @author <a href="mailto:jeremias@apache.org">Jeremias Maerki</a>
41   * @version $Id: CopyUtilsTest.java 437567 2006-08-28 06:39:07Z bayard $
42   * @see CopyUtils
43   */
44  public class CopyUtilsTest extends FileBasedTestCase {
45  
46      /*
47       * NOTE this is not particularly beautiful code. A better way to check for
48       * flush and close status would be to implement "trojan horse" wrapper
49       * implementations of the various stream classes, which set a flag when
50       * relevant methods are called. (JT)
51       */
52  
53      private static final int FILE_SIZE = 1024 * 4 + 1;
54  
55  
56      private byte[] inData = generateTestData(FILE_SIZE);
57  
58      public static void main(String[] args) {
59          TestRunner.run(suite());
60      }
61  
62      public static Test suite() {
63          return new TestSuite(CopyUtilsTest.class);
64      }
65  
66      public CopyUtilsTest(String testName) {
67          super(testName);
68      }
69  
70      // ----------------------------------------------------------------
71      // Setup
72      // ----------------------------------------------------------------
73  
74      public void setUp() throws Exception {
75      }
76  
77      public void tearDown() throws Exception {
78      }
79  
80      // ----------------------------------------------------------------
81      // Tests
82      // ----------------------------------------------------------------
83  
84      public void testCopy_byteArrayToOutputStream() throws Exception {
85          ByteArrayOutputStream baout = new ByteArrayOutputStream();
86          OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
87          
88          CopyUtils.copy(inData, out);
89  
90          assertEquals("Sizes differ", inData.length, baout.size());
91          assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
92      }
93  
94      public void testCopy_byteArrayToWriter() throws Exception {
95          ByteArrayOutputStream baout = new ByteArrayOutputStream();
96          OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
97          Writer writer = new java.io.OutputStreamWriter(baout, "US-ASCII");
98          
99          CopyUtils.copy(inData, writer);
100         writer.flush();
101 
102         assertEquals("Sizes differ", inData.length, baout.size());
103         assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
104     }
105 
106     public void testCopy_inputStreamToOutputStream() throws Exception {
107         InputStream in = new ByteArrayInputStream(inData);
108         in = new YellOnCloseInputStream(in);
109 
110         ByteArrayOutputStream baout = new ByteArrayOutputStream();
111         OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
112 
113         int count = CopyUtils.copy(in, out);
114         
115         assertTrue("Not all bytes were read", in.available() == 0);
116         assertEquals("Sizes differ", inData.length, baout.size());
117         assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
118     }
119 
120     public void testCopy_inputStreamToWriter() throws Exception {
121         InputStream in = new ByteArrayInputStream(inData);
122         in = new YellOnCloseInputStream(in);
123 
124         ByteArrayOutputStream baout = new ByteArrayOutputStream();
125         OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
126         Writer writer = new java.io.OutputStreamWriter(baout, "US-ASCII");
127         
128         CopyUtils.copy(in, writer);
129         writer.flush();
130 
131         assertTrue("Not all bytes were read", in.available() == 0);
132         assertEquals("Sizes differ", inData.length, baout.size());
133         assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
134     }
135 
136     public void testCopy_readerToOutputStream() throws Exception {
137         InputStream in = new ByteArrayInputStream(inData);
138         in = new YellOnCloseInputStream(in);
139         Reader reader = new java.io.InputStreamReader(in, "US-ASCII");
140         
141         ByteArrayOutputStream baout = new ByteArrayOutputStream();
142         OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
143         
144         CopyUtils.copy(reader, out);
145         //Note: this method *does* flush. It is equivalent to:
146         //  OutputStreamWriter _out = new OutputStreamWriter(fout);
147         //  IOUtils.copy( fin, _out, 4096 ); // copy( Reader, Writer, int );
148         //  _out.flush();
149         //  out = fout;
150 
151         // Note: rely on the method to flush
152         assertEquals("Sizes differ", inData.length, baout.size());
153         assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
154     }
155 
156     public void testCopy_readerToWriter() throws Exception {
157         InputStream in = new ByteArrayInputStream(inData);
158         in = new YellOnCloseInputStream(in);
159         Reader reader = new java.io.InputStreamReader(in, "US-ASCII");
160 
161         ByteArrayOutputStream baout = new ByteArrayOutputStream();
162         OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
163         Writer writer = new java.io.OutputStreamWriter(baout, "US-ASCII");
164 
165         int count = CopyUtils.copy(reader, writer);
166         writer.flush();
167         assertEquals(
168             "The number of characters returned by copy is wrong",
169             inData.length,
170             count);
171         assertEquals("Sizes differ", inData.length, baout.size());
172         assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
173     }
174 
175     public void testCopy_stringToOutputStream() throws Exception {
176         String str = new String(inData, "US-ASCII");
177         
178         ByteArrayOutputStream baout = new ByteArrayOutputStream();
179         OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
180 
181         CopyUtils.copy(str, out);
182         //Note: this method *does* flush. It is equivalent to:
183         //  OutputStreamWriter _out = new OutputStreamWriter(fout);
184         //  IOUtils.copy( str, _out, 4096 ); // copy( Reader, Writer, int );
185         //  _out.flush();
186         //  out = fout;
187         // note: we don't flush here; this IOUtils method does it for us
188 
189         assertEquals("Sizes differ", inData.length, baout.size());
190         assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
191     }
192 
193     public void testCopy_stringToWriter() throws Exception {
194         String str = new String(inData, "US-ASCII");
195 
196         ByteArrayOutputStream baout = new ByteArrayOutputStream();
197         OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
198         Writer writer = new java.io.OutputStreamWriter(baout, "US-ASCII");
199 
200         CopyUtils.copy(str, writer);
201         writer.flush();
202 
203         assertEquals("Sizes differ", inData.length, baout.size());
204         assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
205     }
206 
207 } // CopyUtilsTest