1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.io;
18
19 import java.io.ByteArrayInputStream;
20 import java.io.InputStream;
21 import java.io.OutputStream;
22 import java.io.Reader;
23 import java.io.Writer;
24 import java.util.Arrays;
25
26 import org.apache.commons.io.output.ByteArrayOutputStream;
27 import org.apache.commons.io.testtools.YellOnCloseInputStream;
28 import org.apache.commons.io.testtools.YellOnFlushAndCloseOutputStream;
29 import org.apache.commons.io.testtools.FileBasedTestCase;
30
31 import junit.framework.Test;
32 import junit.framework.TestSuite;
33 import junit.textui.TestRunner;
34
35
36
37
38
39
40
41
42
43
44 public class CopyUtilsTest extends FileBasedTestCase {
45
46
47
48
49
50
51
52
53 private static final int FILE_SIZE = 1024 * 4 + 1;
54
55
56 private byte[] inData = generateTestData(FILE_SIZE);
57
58 public static void main(String[] args) {
59 TestRunner.run(suite());
60 }
61
62 public static Test suite() {
63 return new TestSuite(CopyUtilsTest.class);
64 }
65
66 public CopyUtilsTest(String testName) {
67 super(testName);
68 }
69
70
71
72
73
74 public void setUp() throws Exception {
75 }
76
77 public void tearDown() throws Exception {
78 }
79
80
81
82
83
84 public void testCopy_byteArrayToOutputStream() throws Exception {
85 ByteArrayOutputStream baout = new ByteArrayOutputStream();
86 OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
87
88 CopyUtils.copy(inData, out);
89
90 assertEquals("Sizes differ", inData.length, baout.size());
91 assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
92 }
93
94 public void testCopy_byteArrayToWriter() throws Exception {
95 ByteArrayOutputStream baout = new ByteArrayOutputStream();
96 OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
97 Writer writer = new java.io.OutputStreamWriter(baout, "US-ASCII");
98
99 CopyUtils.copy(inData, writer);
100 writer.flush();
101
102 assertEquals("Sizes differ", inData.length, baout.size());
103 assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
104 }
105
106 public void testCopy_inputStreamToOutputStream() throws Exception {
107 InputStream in = new ByteArrayInputStream(inData);
108 in = new YellOnCloseInputStream(in);
109
110 ByteArrayOutputStream baout = new ByteArrayOutputStream();
111 OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
112
113 int count = CopyUtils.copy(in, out);
114
115 assertTrue("Not all bytes were read", in.available() == 0);
116 assertEquals("Sizes differ", inData.length, baout.size());
117 assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
118 }
119
120 public void testCopy_inputStreamToWriter() throws Exception {
121 InputStream in = new ByteArrayInputStream(inData);
122 in = new YellOnCloseInputStream(in);
123
124 ByteArrayOutputStream baout = new ByteArrayOutputStream();
125 OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
126 Writer writer = new java.io.OutputStreamWriter(baout, "US-ASCII");
127
128 CopyUtils.copy(in, writer);
129 writer.flush();
130
131 assertTrue("Not all bytes were read", in.available() == 0);
132 assertEquals("Sizes differ", inData.length, baout.size());
133 assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
134 }
135
136 public void testCopy_readerToOutputStream() throws Exception {
137 InputStream in = new ByteArrayInputStream(inData);
138 in = new YellOnCloseInputStream(in);
139 Reader reader = new java.io.InputStreamReader(in, "US-ASCII");
140
141 ByteArrayOutputStream baout = new ByteArrayOutputStream();
142 OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
143
144 CopyUtils.copy(reader, out);
145
146
147
148
149
150
151
152 assertEquals("Sizes differ", inData.length, baout.size());
153 assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
154 }
155
156 public void testCopy_readerToWriter() throws Exception {
157 InputStream in = new ByteArrayInputStream(inData);
158 in = new YellOnCloseInputStream(in);
159 Reader reader = new java.io.InputStreamReader(in, "US-ASCII");
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 int count = CopyUtils.copy(reader, writer);
166 writer.flush();
167 assertEquals(
168 "The number of characters returned by copy is wrong",
169 inData.length,
170 count);
171 assertEquals("Sizes differ", inData.length, baout.size());
172 assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
173 }
174
175 public void testCopy_stringToOutputStream() throws Exception {
176 String str = new String(inData, "US-ASCII");
177
178 ByteArrayOutputStream baout = new ByteArrayOutputStream();
179 OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
180
181 CopyUtils.copy(str, out);
182
183
184
185
186
187
188
189 assertEquals("Sizes differ", inData.length, baout.size());
190 assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
191 }
192
193 public void testCopy_stringToWriter() throws Exception {
194 String str = new String(inData, "US-ASCII");
195
196 ByteArrayOutputStream baout = new ByteArrayOutputStream();
197 OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
198 Writer writer = new java.io.OutputStreamWriter(baout, "US-ASCII");
199
200 CopyUtils.copy(str, writer);
201 writer.flush();
202
203 assertEquals("Sizes differ", inData.length, baout.size());
204 assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
205 }
206
207 }