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