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.OutputStream;
022: import java.io.Reader;
023: import java.io.Writer;
024: import java.util.Arrays;
025:
026: import org.apache.commons.io.output.ByteArrayOutputStream;
027: import org.apache.commons.io.testtools.YellOnCloseInputStream;
028: import org.apache.commons.io.testtools.YellOnFlushAndCloseOutputStream;
029: import org.apache.commons.io.testtools.FileBasedTestCase;
030:
031: import junit.framework.Test;
032: import junit.framework.TestSuite;
033: import junit.textui.TestRunner;
034:
035: /**
036: * JUnit tests for CopyUtils.
037: *
038: * @author Jeff Turner
039: * @author Matthew Hawthorne
040: * @author <a href="mailto:jeremias@apache.org">Jeremias Maerki</a>
041: * @version $Id: CopyUtilsTest.java 437567 2006-08-28 06:39:07Z bayard $
042: * @see CopyUtils
043: */
044: public class CopyUtilsTest extends FileBasedTestCase {
045:
046: /*
047: * NOTE this is not particularly beautiful code. A better way to check for
048: * flush and close status would be to implement "trojan horse" wrapper
049: * implementations of the various stream classes, which set a flag when
050: * relevant methods are called. (JT)
051: */
052:
053: private static final int FILE_SIZE = 1024 * 4 + 1;
054:
055: private byte[] inData = generateTestData(FILE_SIZE);
056:
057: public static void main(String[] args) {
058: TestRunner.run(suite());
059: }
060:
061: public static Test suite() {
062: return new TestSuite(CopyUtilsTest.class);
063: }
064:
065: public CopyUtilsTest(String testName) {
066: super (testName);
067: }
068:
069: // ----------------------------------------------------------------
070: // Setup
071: // ----------------------------------------------------------------
072:
073: public void setUp() throws Exception {
074: }
075:
076: public void tearDown() throws Exception {
077: }
078:
079: // ----------------------------------------------------------------
080: // Tests
081: // ----------------------------------------------------------------
082:
083: public void testCopy_byteArrayToOutputStream() throws Exception {
084: ByteArrayOutputStream baout = new ByteArrayOutputStream();
085: OutputStream out = new YellOnFlushAndCloseOutputStream(baout,
086: false, true);
087:
088: CopyUtils.copy(inData, out);
089:
090: assertEquals("Sizes differ", inData.length, baout.size());
091: assertTrue("Content differs", Arrays.equals(inData, baout
092: .toByteArray()));
093: }
094:
095: public void testCopy_byteArrayToWriter() throws Exception {
096: ByteArrayOutputStream baout = new ByteArrayOutputStream();
097: OutputStream out = new YellOnFlushAndCloseOutputStream(baout,
098: false, true);
099: Writer writer = new java.io.OutputStreamWriter(baout,
100: "US-ASCII");
101:
102: CopyUtils.copy(inData, writer);
103: writer.flush();
104:
105: assertEquals("Sizes differ", inData.length, baout.size());
106: assertTrue("Content differs", Arrays.equals(inData, baout
107: .toByteArray()));
108: }
109:
110: public void testCopy_inputStreamToOutputStream() throws Exception {
111: InputStream in = new ByteArrayInputStream(inData);
112: in = new YellOnCloseInputStream(in);
113:
114: ByteArrayOutputStream baout = new ByteArrayOutputStream();
115: OutputStream out = new YellOnFlushAndCloseOutputStream(baout,
116: false, true);
117:
118: int count = CopyUtils.copy(in, out);
119:
120: assertTrue("Not all bytes were read", in.available() == 0);
121: assertEquals("Sizes differ", inData.length, baout.size());
122: assertTrue("Content differs", Arrays.equals(inData, baout
123: .toByteArray()));
124: }
125:
126: public void testCopy_inputStreamToWriter() throws Exception {
127: InputStream in = new ByteArrayInputStream(inData);
128: in = new YellOnCloseInputStream(in);
129:
130: ByteArrayOutputStream baout = new ByteArrayOutputStream();
131: OutputStream out = new YellOnFlushAndCloseOutputStream(baout,
132: false, true);
133: Writer writer = new java.io.OutputStreamWriter(baout,
134: "US-ASCII");
135:
136: CopyUtils.copy(in, writer);
137: writer.flush();
138:
139: assertTrue("Not all bytes were read", in.available() == 0);
140: assertEquals("Sizes differ", inData.length, baout.size());
141: assertTrue("Content differs", Arrays.equals(inData, baout
142: .toByteArray()));
143: }
144:
145: public void testCopy_readerToOutputStream() throws Exception {
146: InputStream in = new ByteArrayInputStream(inData);
147: in = new YellOnCloseInputStream(in);
148: Reader reader = new java.io.InputStreamReader(in, "US-ASCII");
149:
150: ByteArrayOutputStream baout = new ByteArrayOutputStream();
151: OutputStream out = new YellOnFlushAndCloseOutputStream(baout,
152: false, true);
153:
154: CopyUtils.copy(reader, out);
155: //Note: this method *does* flush. It is equivalent to:
156: // OutputStreamWriter _out = new OutputStreamWriter(fout);
157: // IOUtils.copy( fin, _out, 4096 ); // copy( Reader, Writer, int );
158: // _out.flush();
159: // out = fout;
160:
161: // Note: rely on the method to flush
162: assertEquals("Sizes differ", inData.length, baout.size());
163: assertTrue("Content differs", Arrays.equals(inData, baout
164: .toByteArray()));
165: }
166:
167: public void testCopy_readerToWriter() throws Exception {
168: InputStream in = new ByteArrayInputStream(inData);
169: in = new YellOnCloseInputStream(in);
170: Reader reader = new java.io.InputStreamReader(in, "US-ASCII");
171:
172: ByteArrayOutputStream baout = new ByteArrayOutputStream();
173: OutputStream out = new YellOnFlushAndCloseOutputStream(baout,
174: false, true);
175: Writer writer = new java.io.OutputStreamWriter(baout,
176: "US-ASCII");
177:
178: int count = CopyUtils.copy(reader, writer);
179: writer.flush();
180: assertEquals(
181: "The number of characters returned by copy is wrong",
182: inData.length, count);
183: assertEquals("Sizes differ", inData.length, baout.size());
184: assertTrue("Content differs", Arrays.equals(inData, baout
185: .toByteArray()));
186: }
187:
188: public void testCopy_stringToOutputStream() throws Exception {
189: String str = new String(inData, "US-ASCII");
190:
191: ByteArrayOutputStream baout = new ByteArrayOutputStream();
192: OutputStream out = new YellOnFlushAndCloseOutputStream(baout,
193: false, true);
194:
195: CopyUtils.copy(str, out);
196: //Note: this method *does* flush. It is equivalent to:
197: // OutputStreamWriter _out = new OutputStreamWriter(fout);
198: // IOUtils.copy( str, _out, 4096 ); // copy( Reader, Writer, int );
199: // _out.flush();
200: // out = fout;
201: // note: we don't flush here; this IOUtils method does it for us
202:
203: assertEquals("Sizes differ", inData.length, baout.size());
204: assertTrue("Content differs", Arrays.equals(inData, baout
205: .toByteArray()));
206: }
207:
208: public void testCopy_stringToWriter() throws Exception {
209: String str = new String(inData, "US-ASCII");
210:
211: ByteArrayOutputStream baout = new ByteArrayOutputStream();
212: OutputStream out = new YellOnFlushAndCloseOutputStream(baout,
213: false, true);
214: Writer writer = new java.io.OutputStreamWriter(baout,
215: "US-ASCII");
216:
217: CopyUtils.copy(str, writer);
218: writer.flush();
219:
220: assertEquals("Sizes differ", inData.length, baout.size());
221: assertTrue("Content differs", Arrays.equals(inData, baout
222: .toByteArray()));
223: }
224:
225: } // CopyUtilsTest
|