KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > io > CopyUtilsTest


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.io;
17
18 import java.io.ByteArrayInputStream JavaDoc;
19 import java.io.InputStream JavaDoc;
20 import java.io.OutputStream JavaDoc;
21 import java.io.Reader JavaDoc;
22 import java.io.Writer JavaDoc;
23 import java.util.Arrays JavaDoc;
24
25 import org.apache.commons.io.output.ByteArrayOutputStream;
26 import org.apache.commons.io.testtools.YellOnCloseInputStream;
27 import org.apache.commons.io.testtools.YellOnFlushAndCloseOutputStream;
28 import org.apache.commons.io.testtools.FileBasedTestCase;
29
30 import junit.framework.Test;
31 import junit.framework.TestSuite;
32 import junit.textui.TestRunner;
33
34 /**
35  * JUnit tests for CopyUtils.
36  *
37  * @author Jeff Turner
38  * @author Matthew Hawthorne
39  * @author <a HREF="mailto:jeremias@apache.org">Jeremias Maerki</a>
40  * @version $Id: CopyUtilsTest.java,v 1.6 2004/02/23 05:02:25 bayard Exp $
41  * @see CopyUtils
42  */

43 public class CopyUtilsTest extends FileBasedTestCase {
44
45     /*
46      * NOTE this is not particularly beautiful code. A better way to check for
47      * flush and close status would be to implement "trojan horse" wrapper
48      * implementations of the various stream classes, which set a flag when
49      * relevant methods are called. (JT)
50      */

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 JavaDoc[] args) {
58         TestRunner.run(suite());
59     }
60
61     public static Test suite() {
62         return new TestSuite(CopyUtilsTest.class);
63     }
64
65     public CopyUtilsTest(String JavaDoc testName) {
66         super(testName);
67     }
68
69     // ----------------------------------------------------------------
70
// Setup
71
// ----------------------------------------------------------------
72

73     public void setUp() throws Exception JavaDoc {
74     }
75
76     public void tearDown() throws Exception JavaDoc {
77     }
78
79     // ----------------------------------------------------------------
80
// Tests
81
// ----------------------------------------------------------------
82

83     public void testCopy_byteArrayToOutputStream() throws Exception JavaDoc {
84         ByteArrayOutputStream baout = new ByteArrayOutputStream();
85         OutputStream JavaDoc out = new YellOnFlushAndCloseOutputStream(baout, false, true);
86         
87         CopyUtils.copy(inData, out);
88
89         assertEquals("Sizes differ", inData.length, baout.size());
90         assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
91     }
92
93     public void testCopy_byteArrayToWriter() throws Exception JavaDoc {
94         ByteArrayOutputStream baout = new ByteArrayOutputStream();
95         OutputStream JavaDoc out = new YellOnFlushAndCloseOutputStream(baout, false, true);
96         Writer JavaDoc writer = new java.io.OutputStreamWriter JavaDoc(baout, "US-ASCII");
97         
98         CopyUtils.copy(inData, writer);
99         writer.flush();
100
101         assertEquals("Sizes differ", inData.length, baout.size());
102         assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
103     }
104
105     public void testCopy_inputStreamToOutputStream() throws Exception JavaDoc {
106         InputStream JavaDoc in = new ByteArrayInputStream JavaDoc(inData);
107         in = new YellOnCloseInputStream(in);
108
109         ByteArrayOutputStream baout = new ByteArrayOutputStream();
110         OutputStream JavaDoc out = new YellOnFlushAndCloseOutputStream(baout, false, true);
111
112         int count = CopyUtils.copy(in, out);
113         
114         assertTrue("Not all bytes were read", in.available() == 0);
115         assertEquals("Sizes differ", inData.length, baout.size());
116         assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
117     }
118
119     public void testCopy_inputStreamToWriter() throws Exception JavaDoc {
120         InputStream JavaDoc in = new ByteArrayInputStream JavaDoc(inData);
121         in = new YellOnCloseInputStream(in);
122
123         ByteArrayOutputStream baout = new ByteArrayOutputStream();
124         OutputStream JavaDoc out = new YellOnFlushAndCloseOutputStream(baout, false, true);
125         Writer JavaDoc writer = new java.io.OutputStreamWriter JavaDoc(baout, "US-ASCII");
126         
127         CopyUtils.copy(in, writer);
128         writer.flush();
129
130         assertTrue("Not all bytes were read", in.available() == 0);
131         assertEquals("Sizes differ", inData.length, baout.size());
132         assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
133     }
134
135     public void testCopy_readerToOutputStream() throws Exception JavaDoc {
136         InputStream JavaDoc in = new ByteArrayInputStream JavaDoc(inData);
137         in = new YellOnCloseInputStream(in);
138         Reader JavaDoc reader = new java.io.InputStreamReader JavaDoc(in, "US-ASCII");
139         
140         ByteArrayOutputStream baout = new ByteArrayOutputStream();
141         OutputStream JavaDoc out = new YellOnFlushAndCloseOutputStream(baout, false, true);
142         
143         CopyUtils.copy(reader, out);
144         //Note: this method *does* flush. It is equivalent to:
145
// OutputStreamWriter _out = new OutputStreamWriter(fout);
146
// IOUtils.copy( fin, _out, 4096 ); // copy( Reader, Writer, int );
147
// _out.flush();
148
// out = fout;
149

150         // Note: rely on the method to flush
151
assertEquals("Sizes differ", inData.length, baout.size());
152         assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
153     }
154
155     public void testCopy_readerToWriter() throws Exception JavaDoc {
156         InputStream JavaDoc in = new ByteArrayInputStream JavaDoc(inData);
157         in = new YellOnCloseInputStream(in);
158         Reader JavaDoc reader = new java.io.InputStreamReader JavaDoc(in, "US-ASCII");
159
160         ByteArrayOutputStream baout = new ByteArrayOutputStream();
161         OutputStream JavaDoc out = new YellOnFlushAndCloseOutputStream(baout, false, true);
162         Writer JavaDoc writer = new java.io.OutputStreamWriter JavaDoc(baout, "US-ASCII");
163
164         int count = CopyUtils.copy(reader, writer);
165         writer.flush();
166         assertEquals(
167             "The number of characters returned by copy is wrong",
168             inData.length,
169             count);
170         assertEquals("Sizes differ", inData.length, baout.size());
171         assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
172     }
173
174     public void testCopy_stringToOutputStream() throws Exception JavaDoc {
175         String JavaDoc str = new String JavaDoc(inData, "US-ASCII");
176         
177         ByteArrayOutputStream baout = new ByteArrayOutputStream();
178         OutputStream JavaDoc out = new YellOnFlushAndCloseOutputStream(baout, false, true);
179
180         CopyUtils.copy(str, out);
181         //Note: this method *does* flush. It is equivalent to:
182
// OutputStreamWriter _out = new OutputStreamWriter(fout);
183
// IOUtils.copy( str, _out, 4096 ); // copy( Reader, Writer, int );
184
// _out.flush();
185
// out = fout;
186
// note: we don't flush here; this IOUtils method does it for us
187

188         assertEquals("Sizes differ", inData.length, baout.size());
189         assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
190     }
191
192     public void testCopy_stringToWriter() throws Exception JavaDoc {
193         String JavaDoc str = new String JavaDoc(inData, "US-ASCII");
194
195         ByteArrayOutputStream baout = new ByteArrayOutputStream();
196         OutputStream JavaDoc out = new YellOnFlushAndCloseOutputStream(baout, false, true);
197         Writer JavaDoc writer = new java.io.OutputStreamWriter JavaDoc(baout, "US-ASCII");
198
199         CopyUtils.copy(str, writer);
200         writer.flush();
201
202         assertEquals("Sizes differ", inData.length, baout.size());
203         assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
204     }
205
206 } // CopyUtilsTest
207
Popular Tags