001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.io;
018:
019: import java.io.ByteArrayInputStream;
020: import java.io.InputStream;
021: import java.io.InputStreamReader;
022: import java.io.OutputStream;
023: import java.io.OutputStreamWriter;
024: import java.io.IOException;
025: import java.io.Reader;
026: import java.io.Writer;
027: import java.util.Arrays;
028:
029: import junit.framework.Test;
030: import junit.framework.TestSuite;
031: import junit.textui.TestRunner;
032:
033: import org.apache.commons.io.input.NullInputStream;
034: import org.apache.commons.io.input.NullReader;
035: import org.apache.commons.io.output.ByteArrayOutputStream;
036: import org.apache.commons.io.output.NullOutputStream;
037: import org.apache.commons.io.output.NullWriter;
038: import org.apache.commons.io.testtools.FileBasedTestCase;
039: import org.apache.commons.io.testtools.YellOnCloseInputStream;
040: import org.apache.commons.io.testtools.YellOnFlushAndCloseOutputStream;
041:
042: /**
043: * JUnit tests for IOUtils copy methods.
044: *
045: * @author Jeff Turner
046: * @author Matthew Hawthorne
047: * @author Jeremias Maerki
048: * @author Stephen Colebourne
049: * @version $Id: IOUtilsCopyTestCase.java 481854 2006-12-03 18:30:07Z scolebourne $
050: * @see IOUtils
051: */
052: public class IOUtilsCopyTestCase extends FileBasedTestCase {
053:
054: /*
055: * NOTE this is not particularly beautiful code. A better way to check for
056: * flush and close status would be to implement "trojan horse" wrapper
057: * implementations of the various stream classes, which set a flag when
058: * relevant methods are called. (JT)
059: */
060:
061: private static final int FILE_SIZE = 1024 * 4 + 1;
062:
063: private byte[] inData = generateTestData(FILE_SIZE);
064:
065: public static void main(String[] args) {
066: TestRunner.run(suite());
067: }
068:
069: public static Test suite() {
070: return new TestSuite(IOUtilsCopyTestCase.class);
071: }
072:
073: public IOUtilsCopyTestCase(String testName) {
074: super (testName);
075: }
076:
077: // ----------------------------------------------------------------
078: // Setup
079: // ----------------------------------------------------------------
080:
081: public void setUp() throws Exception {
082: }
083:
084: public void tearDown() throws Exception {
085: }
086:
087: //-----------------------------------------------------------------------
088: public void testCopy_inputStreamToOutputStream() throws Exception {
089: InputStream in = new ByteArrayInputStream(inData);
090: in = new YellOnCloseInputStream(in);
091:
092: ByteArrayOutputStream baout = new ByteArrayOutputStream();
093: OutputStream out = new YellOnFlushAndCloseOutputStream(baout,
094: false, true);
095:
096: int count = IOUtils.copy(in, out);
097:
098: assertTrue("Not all bytes were read", in.available() == 0);
099: assertEquals("Sizes differ", inData.length, baout.size());
100: assertTrue("Content differs", Arrays.equals(inData, baout
101: .toByteArray()));
102: }
103:
104: public void testCopy_inputStreamToOutputStream_nullIn()
105: throws Exception {
106: OutputStream out = new ByteArrayOutputStream();
107: try {
108: IOUtils.copy((InputStream) null, out);
109: fail();
110: } catch (NullPointerException ex) {
111: }
112: }
113:
114: public void testCopy_inputStreamToOutputStream_nullOut()
115: throws Exception {
116: InputStream in = new ByteArrayInputStream(inData);
117: try {
118: IOUtils.copy(in, (OutputStream) null);
119: fail();
120: } catch (NullPointerException ex) {
121: }
122: }
123:
124: /**
125: * Test Copying file > 2GB - see issue# IO-84
126: */
127: public void testCopy_inputStreamToOutputStream_IO84()
128: throws Exception {
129: long size = (long) Integer.MAX_VALUE + (long) 1;
130: InputStream in = new NullInputStream(size);
131: OutputStream out = new NullOutputStream();
132:
133: // Test copy() method
134: assertEquals(-1, IOUtils.copy(in, out));
135:
136: // reset the input
137: in.close();
138:
139: // Test copyLarge() method
140: assertEquals("copyLarge()", size, IOUtils.copyLarge(in, out));
141: }
142:
143: //-----------------------------------------------------------------------
144: public void testCopy_inputStreamToWriter() throws Exception {
145: InputStream in = new ByteArrayInputStream(inData);
146: in = new YellOnCloseInputStream(in);
147:
148: ByteArrayOutputStream baout = new ByteArrayOutputStream();
149: YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(
150: baout, true, true);
151: Writer writer = new OutputStreamWriter(baout, "US-ASCII");
152:
153: IOUtils.copy(in, writer);
154: out.off();
155: writer.flush();
156:
157: assertTrue("Not all bytes were read", in.available() == 0);
158: assertEquals("Sizes differ", inData.length, baout.size());
159: assertTrue("Content differs", Arrays.equals(inData, baout
160: .toByteArray()));
161: }
162:
163: public void testCopy_inputStreamToWriter_nullIn() throws Exception {
164: ByteArrayOutputStream baout = new ByteArrayOutputStream();
165: OutputStream out = new YellOnFlushAndCloseOutputStream(baout,
166: true, true);
167: Writer writer = new OutputStreamWriter(baout, "US-ASCII");
168: try {
169: IOUtils.copy((InputStream) null, writer);
170: fail();
171: } catch (NullPointerException ex) {
172: }
173: }
174:
175: public void testCopy_inputStreamToWriter_nullOut() throws Exception {
176: InputStream in = new ByteArrayInputStream(inData);
177: try {
178: IOUtils.copy(in, (Writer) null);
179: fail();
180: } catch (NullPointerException ex) {
181: }
182: }
183:
184: //-----------------------------------------------------------------------
185: public void testCopy_inputStreamToWriter_Encoding()
186: throws Exception {
187: InputStream in = new ByteArrayInputStream(inData);
188: in = new YellOnCloseInputStream(in);
189:
190: ByteArrayOutputStream baout = new ByteArrayOutputStream();
191: YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(
192: baout, true, true);
193: Writer writer = new OutputStreamWriter(baout, "US-ASCII");
194:
195: IOUtils.copy(in, writer, "UTF8");
196: out.off();
197: writer.flush();
198:
199: assertTrue("Not all bytes were read", in.available() == 0);
200: byte[] bytes = baout.toByteArray();
201: bytes = new String(bytes, "UTF8").getBytes("US-ASCII");
202: assertTrue("Content differs", Arrays.equals(inData, bytes));
203: }
204:
205: public void testCopy_inputStreamToWriter_Encoding_nullIn()
206: throws Exception {
207: ByteArrayOutputStream baout = new ByteArrayOutputStream();
208: OutputStream out = new YellOnFlushAndCloseOutputStream(baout,
209: true, true);
210: Writer writer = new OutputStreamWriter(baout, "US-ASCII");
211: try {
212: IOUtils.copy((InputStream) null, writer, "UTF8");
213: fail();
214: } catch (NullPointerException ex) {
215: }
216: }
217:
218: public void testCopy_inputStreamToWriter_Encoding_nullOut()
219: throws Exception {
220: InputStream in = new ByteArrayInputStream(inData);
221: try {
222: IOUtils.copy(in, (Writer) null, "UTF8");
223: fail();
224: } catch (NullPointerException ex) {
225: }
226: }
227:
228: public void testCopy_inputStreamToWriter_Encoding_nullEncoding()
229: throws Exception {
230: InputStream in = new ByteArrayInputStream(inData);
231: in = new YellOnCloseInputStream(in);
232:
233: ByteArrayOutputStream baout = new ByteArrayOutputStream();
234: YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(
235: baout, true, true);
236: Writer writer = new OutputStreamWriter(baout, "US-ASCII");
237:
238: IOUtils.copy(in, writer, null);
239: out.off();
240: writer.flush();
241:
242: assertTrue("Not all bytes were read", in.available() == 0);
243: assertEquals("Sizes differ", inData.length, baout.size());
244: assertTrue("Content differs", Arrays.equals(inData, baout
245: .toByteArray()));
246: }
247:
248: //-----------------------------------------------------------------------
249: public void testCopy_readerToOutputStream() throws Exception {
250: InputStream in = new ByteArrayInputStream(inData);
251: in = new YellOnCloseInputStream(in);
252: Reader reader = new InputStreamReader(in, "US-ASCII");
253:
254: ByteArrayOutputStream baout = new ByteArrayOutputStream();
255: OutputStream out = new YellOnFlushAndCloseOutputStream(baout,
256: false, true);
257:
258: IOUtils.copy(reader, out);
259: //Note: this method *does* flush. It is equivalent to:
260: // OutputStreamWriter _out = new OutputStreamWriter(fout);
261: // IOUtils.copy( fin, _out, 4096 ); // copy( Reader, Writer, int );
262: // _out.flush();
263: // out = fout;
264:
265: // Note: rely on the method to flush
266: assertEquals("Sizes differ", inData.length, baout.size());
267: assertTrue("Content differs", Arrays.equals(inData, baout
268: .toByteArray()));
269: }
270:
271: public void testCopy_readerToOutputStream_nullIn() throws Exception {
272: ByteArrayOutputStream baout = new ByteArrayOutputStream();
273: OutputStream out = new YellOnFlushAndCloseOutputStream(baout,
274: true, true);
275: try {
276: IOUtils.copy((Reader) null, out);
277: fail();
278: } catch (NullPointerException ex) {
279: }
280: }
281:
282: public void testCopy_readerToOutputStream_nullOut()
283: throws Exception {
284: InputStream in = new ByteArrayInputStream(inData);
285: in = new YellOnCloseInputStream(in);
286: Reader reader = new InputStreamReader(in, "US-ASCII");
287: try {
288: IOUtils.copy(reader, (OutputStream) null);
289: fail();
290: } catch (NullPointerException ex) {
291: }
292: }
293:
294: //-----------------------------------------------------------------------
295: public void testCopy_readerToOutputStream_Encoding()
296: throws Exception {
297: InputStream in = new ByteArrayInputStream(inData);
298: in = new YellOnCloseInputStream(in);
299: Reader reader = new InputStreamReader(in, "US-ASCII");
300:
301: ByteArrayOutputStream baout = new ByteArrayOutputStream();
302: OutputStream out = new YellOnFlushAndCloseOutputStream(baout,
303: false, true);
304:
305: IOUtils.copy(reader, out, "UTF16");
306: // note: this method *does* flush.
307: // note: we don't flush here; this IOUtils method does it for us
308:
309: byte[] bytes = baout.toByteArray();
310: bytes = new String(bytes, "UTF16").getBytes("US-ASCII");
311: assertTrue("Content differs", Arrays.equals(inData, bytes));
312: }
313:
314: public void testCopy_readerToOutputStream_Encoding_nullIn()
315: throws Exception {
316: ByteArrayOutputStream baout = new ByteArrayOutputStream();
317: OutputStream out = new YellOnFlushAndCloseOutputStream(baout,
318: true, true);
319: try {
320: IOUtils.copy((Reader) null, out, "UTF16");
321: fail();
322: } catch (NullPointerException ex) {
323: }
324: }
325:
326: public void testCopy_readerToOutputStream_Encoding_nullOut()
327: throws Exception {
328: InputStream in = new ByteArrayInputStream(inData);
329: in = new YellOnCloseInputStream(in);
330: Reader reader = new InputStreamReader(in, "US-ASCII");
331: try {
332: IOUtils.copy(reader, (OutputStream) null, "UTF16");
333: fail();
334: } catch (NullPointerException ex) {
335: }
336: }
337:
338: public void testCopy_readerToOutputStream_Encoding_nullEncoding()
339: throws Exception {
340: InputStream in = new ByteArrayInputStream(inData);
341: in = new YellOnCloseInputStream(in);
342: Reader reader = new InputStreamReader(in, "US-ASCII");
343:
344: ByteArrayOutputStream baout = new ByteArrayOutputStream();
345: OutputStream out = new YellOnFlushAndCloseOutputStream(baout,
346: false, true);
347:
348: IOUtils.copy(reader, out, null);
349: // note: this method *does* flush.
350: // note: we don't flush here; this IOUtils method does it for us
351:
352: assertEquals("Sizes differ", inData.length, baout.size());
353: assertTrue("Content differs", Arrays.equals(inData, baout
354: .toByteArray()));
355: }
356:
357: //-----------------------------------------------------------------------
358: public void testCopy_readerToWriter() throws Exception {
359: InputStream in = new ByteArrayInputStream(inData);
360: in = new YellOnCloseInputStream(in);
361: Reader reader = new InputStreamReader(in, "US-ASCII");
362:
363: ByteArrayOutputStream baout = new ByteArrayOutputStream();
364: YellOnFlushAndCloseOutputStream out = new YellOnFlushAndCloseOutputStream(
365: baout, true, true);
366: Writer writer = new OutputStreamWriter(baout, "US-ASCII");
367:
368: int count = IOUtils.copy(reader, writer);
369: out.off();
370: writer.flush();
371: assertEquals(
372: "The number of characters returned by copy is wrong",
373: inData.length, count);
374: assertEquals("Sizes differ", inData.length, baout.size());
375: assertTrue("Content differs", Arrays.equals(inData, baout
376: .toByteArray()));
377: }
378:
379: public void testCopy_readerToWriter_nullIn() throws Exception {
380: ByteArrayOutputStream baout = new ByteArrayOutputStream();
381: OutputStream out = new YellOnFlushAndCloseOutputStream(baout,
382: true, true);
383: Writer writer = new OutputStreamWriter(baout, "US-ASCII");
384: try {
385: IOUtils.copy((Reader) null, writer);
386: fail();
387: } catch (NullPointerException ex) {
388: }
389: }
390:
391: public void testCopy_readerToWriter_nullOut() throws Exception {
392: InputStream in = new ByteArrayInputStream(inData);
393: in = new YellOnCloseInputStream(in);
394: Reader reader = new InputStreamReader(in, "US-ASCII");
395: try {
396: IOUtils.copy(reader, (Writer) null);
397: fail();
398: } catch (NullPointerException ex) {
399: }
400: }
401:
402: /**
403: * Test Copying file > 2GB - see issue# IO-84
404: */
405: public void testCopy_readerToWriter_IO84() throws Exception {
406: long size = (long) Integer.MAX_VALUE + (long) 1;
407: Reader reader = new NullReader(size);
408: Writer writer = new NullWriter();
409:
410: // Test copy() method
411: assertEquals(-1, IOUtils.copy(reader, writer));
412:
413: // reset the input
414: reader.close();
415:
416: // Test copyLarge() method
417: assertEquals("copyLarge()", size, IOUtils.copyLarge(reader,
418: writer));
419:
420: }
421:
422: }
|