Skip to content

Commit 1db3dd9

Browse files
committed
Bugzilla 22075: Copy copy methods from IOUtils to CopyUtils, deprecate old copy methods.
Bugzilla 22332: Deprecated FileUtils string methods, Code style-up Submitted by: Matthew Hawthorne <mhawthorne at alumni.pitt.edu> Testcases (and infrastructure) modified a bit by myself after applying Matthew's patches. CopyUtils testcases are done in memory now instead of using files. git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/io/trunk@140385 13f79535-47bb-0310-9956-ffa450edef68
1 parent ea08b91 commit 1db3dd9

4 files changed

Lines changed: 677 additions & 412 deletions

File tree

Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
/* ====================================================================
2+
* The Apache Software License, Version 1.1
3+
*
4+
* Copyright (c) 2001-2003 The Apache Software Foundation. All rights
5+
* reserved.
6+
*
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions
9+
* are met:
10+
*
11+
* 1. Redistributions of source code must retain the above copyright
12+
* notice, this list of conditions and the following disclaimer.
13+
*
14+
* 2. Redistributions in binary form must reproduce the above copyright
15+
* notice, this list of conditions and the following disclaimer in
16+
* the documentation and/or other materials provided with the
17+
* distribution.
18+
*
19+
* 3. The end-user documentation included with the redistribution, if
20+
* any, must include the following acknowlegement:
21+
* "This product includes software developed by the
22+
* Apache Software Foundation (http://www.apache.org/)."
23+
* Alternately, this acknowlegement may appear in the software itself,
24+
* if and wherever such third-party acknowlegements normally appear.
25+
*
26+
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
27+
* Foundation" must not be used to endorse or promote products derived
28+
* from this software without prior written permission. For written
29+
* permission, please contact apache@apache.org.
30+
*
31+
* 5. Products derived from this software may not be called "Apache"
32+
* nor may "Apache" appear in their names without prior written
33+
* permission of the Apache Software Foundation.
34+
*
35+
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36+
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37+
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38+
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39+
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42+
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43+
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45+
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46+
* SUCH DAMAGE.
47+
* ====================================================================
48+
*
49+
* This software consists of voluntary contributions made by many
50+
* individuals on behalf of the Apache Software Foundation. For more
51+
* information on the Apache Software Foundation, please see
52+
* <http://www.apache.org/>.
53+
*/
54+
package org.apache.commons.io;
55+
56+
import java.io.ByteArrayInputStream;
57+
import java.io.ByteArrayOutputStream;
58+
import java.io.InputStream;
59+
import java.io.OutputStream;
60+
import java.io.Reader;
61+
import java.io.Writer;
62+
import java.util.Arrays;
63+
64+
import org.apache.commons.io.testtools.YellOnCloseInputStream;
65+
import org.apache.commons.io.testtools.YellOnFlushAndCloseOutputStream;
66+
import org.apache.commons.io.testtools.FileBasedTestCase;
67+
68+
import junit.framework.Test;
69+
import junit.framework.TestSuite;
70+
import junit.textui.TestRunner;
71+
72+
/**
73+
* JUnit tests for CopyUtils.
74+
*
75+
* @author Jeff Turner
76+
* @author Matthew Hawthorne
77+
* @author <a href="mailto:jeremias@apache.org">Jeremias Maerki</a>
78+
* @version $Id: CopyUtilsTest.java,v 1.1 2003/08/21 18:56:12 jeremias Exp $
79+
* @see CopyUtils
80+
*/
81+
public class CopyUtilsTest extends FileBasedTestCase {
82+
83+
/*
84+
* NOTE this is not particularly beautiful code. A better way to check for
85+
* flush and close status would be to implement "trojan horse" wrapper
86+
* implementations of the various stream classes, which set a flag when
87+
* relevant methods are called. (JT)
88+
*/
89+
90+
private final int FILE_SIZE = 1024 * 4 + 1;
91+
92+
93+
private byte[] inData = generateTestData(FILE_SIZE);
94+
95+
public static void main(String[] args) {
96+
TestRunner.run(suite());
97+
}
98+
99+
public static Test suite() {
100+
return new TestSuite(CopyUtilsTest.class);
101+
}
102+
103+
public CopyUtilsTest(String testName) {
104+
super(testName);
105+
}
106+
107+
// ----------------------------------------------------------------
108+
// Setup
109+
// ----------------------------------------------------------------
110+
111+
public void setUp() throws Exception {
112+
}
113+
114+
public void tearDown() throws Exception {
115+
}
116+
117+
// ----------------------------------------------------------------
118+
// Tests
119+
// ----------------------------------------------------------------
120+
121+
public void testCopy_byteArrayToOutputStream() throws Exception {
122+
ByteArrayOutputStream baout = new ByteArrayOutputStream();
123+
OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
124+
125+
CopyUtils.copy(inData, out);
126+
127+
assertEquals("Sizes differ", inData.length, baout.size());
128+
assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
129+
}
130+
131+
public void testCopy_byteArrayToWriter() throws Exception {
132+
ByteArrayOutputStream baout = new ByteArrayOutputStream();
133+
OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
134+
Writer writer = new java.io.OutputStreamWriter(baout, "US-ASCII");
135+
136+
CopyUtils.copy(inData, writer);
137+
writer.flush();
138+
139+
assertEquals("Sizes differ", inData.length, baout.size());
140+
assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
141+
}
142+
143+
public void testCopy_inputStreamToOutputStream() throws Exception {
144+
InputStream in = new ByteArrayInputStream(inData);
145+
in = new YellOnCloseInputStream(in);
146+
147+
ByteArrayOutputStream baout = new ByteArrayOutputStream();
148+
OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
149+
150+
int count = CopyUtils.copy(in, out);
151+
152+
assertTrue("Not all bytes were read", in.available() == 0);
153+
assertEquals("Sizes differ", inData.length, baout.size());
154+
assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
155+
}
156+
157+
public void testCopy_inputStreamToWriter() throws Exception {
158+
InputStream in = new ByteArrayInputStream(inData);
159+
in = new YellOnCloseInputStream(in);
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+
CopyUtils.copy(in, writer);
166+
writer.flush();
167+
168+
assertTrue("Not all bytes were read", in.available() == 0);
169+
assertEquals("Sizes differ", inData.length, baout.size());
170+
assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
171+
}
172+
173+
public void testCopy_readerToOutputStream() throws Exception {
174+
InputStream in = new ByteArrayInputStream(inData);
175+
in = new YellOnCloseInputStream(in);
176+
Reader reader = new java.io.InputStreamReader(in, "US-ASCII");
177+
178+
ByteArrayOutputStream baout = new ByteArrayOutputStream();
179+
OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
180+
181+
CopyUtils.copy(reader, out);
182+
//Note: this method *does* flush. It is equivalent to:
183+
// final OutputStreamWriter _out = new OutputStreamWriter(fout);
184+
// IOUtils.copy( fin, _out, 4096 ); // copy( Reader, Writer, int );
185+
// _out.flush();
186+
// out = fout;
187+
188+
// Note: rely on the method to flush
189+
assertEquals("Sizes differ", inData.length, baout.size());
190+
assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
191+
}
192+
193+
public void testCopy_readerToWriter() throws Exception {
194+
InputStream in = new ByteArrayInputStream(inData);
195+
in = new YellOnCloseInputStream(in);
196+
Reader reader = new java.io.InputStreamReader(in, "US-ASCII");
197+
198+
ByteArrayOutputStream baout = new ByteArrayOutputStream();
199+
OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
200+
Writer writer = new java.io.OutputStreamWriter(baout, "US-ASCII");
201+
202+
int count = CopyUtils.copy(reader, writer);
203+
writer.flush();
204+
assertEquals(
205+
"The number of characters returned by copy is wrong",
206+
inData.length,
207+
count);
208+
assertEquals("Sizes differ", inData.length, baout.size());
209+
assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
210+
}
211+
212+
public void testCopy_stringToOutputStream() throws Exception {
213+
final String str = new String(inData, "US-ASCII");
214+
215+
ByteArrayOutputStream baout = new ByteArrayOutputStream();
216+
OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
217+
218+
CopyUtils.copy(str, out);
219+
//Note: this method *does* flush. It is equivalent to:
220+
// final OutputStreamWriter _out = new OutputStreamWriter(fout);
221+
// IOUtils.copy( str, _out, 4096 ); // copy( Reader, Writer, int );
222+
// _out.flush();
223+
// out = fout;
224+
// note: we don't flush here; this IOUtils method does it for us
225+
226+
assertEquals("Sizes differ", inData.length, baout.size());
227+
assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
228+
}
229+
230+
public void testCopy_stringToWriter() throws Exception {
231+
final String str = new String(inData, "US-ASCII");
232+
233+
ByteArrayOutputStream baout = new ByteArrayOutputStream();
234+
OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
235+
Writer writer = new java.io.OutputStreamWriter(baout, "US-ASCII");
236+
237+
CopyUtils.copy(str, writer);
238+
writer.flush();
239+
240+
assertEquals("Sizes differ", inData.length, baout.size());
241+
assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
242+
}
243+
244+
} // CopyUtilsTest

0 commit comments

Comments
 (0)