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.File;
020: import java.io.FileInputStream;
021: import java.io.FileOutputStream;
022: import java.io.FileReader;
023: import java.io.FileWriter;
024: import java.io.IOException;
025: import java.io.InputStream;
026: import java.io.InputStreamReader;
027: import java.io.Reader;
028: import java.util.Arrays;
029: import java.util.List;
030:
031: import org.apache.commons.io.testtools.FileBasedTestCase;
032:
033: // Note: jdk1.2 dependency
034:
035: /**
036: * This is used to test IOUtils for correctness. The following checks are performed:
037: * <ul>
038: * <li>The return must not be null, must be the same type and equals() to the method's second arg</li>
039: * <li>All bytes must have been read from the source (available() == 0)</li>
040: * <li>The source and destination content must be identical (byte-wise comparison check)</li>
041: * <li>The output stream must not have been closed (a byte/char is written to test this, and
042: * subsequent size checked)</li>
043: * </ul>
044: * Due to interdependencies in IOUtils and IOUtilsTestlet, one bug may cause
045: * multiple tests to fail.
046: *
047: * @author <a href="mailto:jefft@apache.org">Jeff Turner</a>
048: * @author Gareth Davis
049: * @author Ian Springer
050: */
051: public class IOUtilsTestCase extends FileBasedTestCase {
052:
053: /** Determine if this is windows. */
054: private static final boolean WINDOWS = (File.separatorChar == '\\');
055: /*
056: * Note: this is not particularly beautiful code. A better way to check for
057: * flush and close status would be to implement "trojan horse" wrapper
058: * implementations of the various stream classes, which set a flag when
059: * relevant methods are called. (JT)
060: */
061:
062: private static final int FILE_SIZE = 1024 * 4 + 1;
063:
064: private File m_testFile;
065:
066: public void setUp() {
067: try {
068: getTestDirectory().mkdirs();
069: m_testFile = new File(getTestDirectory(), "file2-test.txt");
070:
071: createFile(m_testFile, FILE_SIZE);
072: } catch (IOException ioe) {
073: throw new RuntimeException("Can't run this test because "
074: + "environment could not be built: "
075: + ioe.getMessage());
076: }
077: }
078:
079: public void tearDown() {
080: try {
081: FileUtils.deleteDirectory(getTestDirectory());
082: } catch (IOException ioe) {
083: // Ignore, because by this time, it is too late.
084: }
085: }
086:
087: public IOUtilsTestCase(String name) {
088: super (name);
089: }
090:
091: //-----------------------------------------------------------------------
092: public void testConstants() throws Exception {
093: assertEquals('/', IOUtils.DIR_SEPARATOR_UNIX);
094: assertEquals('\\', IOUtils.DIR_SEPARATOR_WINDOWS);
095: assertEquals("\n", IOUtils.LINE_SEPARATOR_UNIX);
096: assertEquals("\r\n", IOUtils.LINE_SEPARATOR_WINDOWS);
097: if (WINDOWS) {
098: assertEquals('\\', IOUtils.DIR_SEPARATOR);
099: assertEquals("\r\n", IOUtils.LINE_SEPARATOR);
100: } else {
101: assertEquals('/', IOUtils.DIR_SEPARATOR);
102: assertEquals("\n", IOUtils.LINE_SEPARATOR);
103: }
104: }
105:
106: //-----------------------------------------------------------------------
107: /** Assert that the contents of two byte arrays are the same. */
108: private void assertEqualContent(byte[] b0, byte[] b1)
109: throws IOException {
110: assertTrue(
111: "Content not equal according to java.util.Arrays#equals()",
112: Arrays.equals(b0, b1));
113: }
114:
115: public void testInputStreamToString() throws Exception {
116: FileInputStream fin = new FileInputStream(m_testFile);
117: try {
118: String out = IOUtils.toString(fin);
119: assertNotNull(out);
120: assertTrue("Not all bytes were read", fin.available() == 0);
121: assertTrue("Wrong output size: out.length()="
122: + out.length() + "!=" + FILE_SIZE,
123: out.length() == FILE_SIZE);
124: } finally {
125: fin.close();
126: }
127: }
128:
129: public void testReaderToString() throws Exception {
130: FileReader fin = new FileReader(m_testFile);
131: try {
132: String out = IOUtils.toString(fin);
133: assertNotNull(out);
134: assertTrue("Wrong output size: out.length()="
135: + out.length() + "!=" + FILE_SIZE,
136: out.length() == FILE_SIZE);
137: } finally {
138: fin.close();
139: }
140: }
141:
142: public void testStringToOutputStream() throws Exception {
143: File destination = newFile("copy5.txt");
144: FileReader fin = new FileReader(m_testFile);
145: String str;
146: try {
147: // Create our String. Rely on testReaderToString() to make sure this is valid.
148: str = IOUtils.toString(fin);
149: } finally {
150: fin.close();
151: }
152:
153: FileOutputStream fout = new FileOutputStream(destination);
154: try {
155: CopyUtils.copy(str, fout);
156: //Note: this method *does* flush. It is equivalent to:
157: // OutputStreamWriter _out = new OutputStreamWriter(fout);
158: // CopyUtils.copy( str, _out, 4096 ); // copy( Reader, Writer, int );
159: // _out.flush();
160: // out = fout;
161: // note: we don't flush here; this IOUtils method does it for us
162:
163: checkFile(destination, m_testFile);
164: checkWrite(fout);
165: } finally {
166: fout.close();
167: }
168: deleteFile(destination);
169: }
170:
171: public void testStringToWriter() throws Exception {
172: File destination = newFile("copy6.txt");
173: FileReader fin = new FileReader(m_testFile);
174: String str;
175: try {
176: // Create our String. Rely on testReaderToString() to make sure this is valid.
177: str = IOUtils.toString(fin);
178: } finally {
179: fin.close();
180: }
181:
182: FileWriter fout = new FileWriter(destination);
183: try {
184: CopyUtils.copy(str, fout);
185: fout.flush();
186:
187: checkFile(destination, m_testFile);
188: checkWrite(fout);
189: } finally {
190: fout.close();
191: }
192: deleteFile(destination);
193: }
194:
195: public void testInputStreamToByteArray() throws Exception {
196: FileInputStream fin = new FileInputStream(m_testFile);
197: try {
198: byte[] out = IOUtils.toByteArray(fin);
199: assertNotNull(out);
200: assertTrue("Not all bytes were read", fin.available() == 0);
201: assertTrue("Wrong output size: out.length=" + out.length
202: + "!=" + FILE_SIZE, out.length == FILE_SIZE);
203: assertEqualContent(out, m_testFile);
204: } finally {
205: fin.close();
206: }
207: }
208:
209: public void testStringToByteArray() throws Exception {
210: FileReader fin = new FileReader(m_testFile);
211: try {
212: // Create our String. Rely on testReaderToString() to make sure this is valid.
213: String str = IOUtils.toString(fin);
214:
215: byte[] out = IOUtils.toByteArray(str);
216: assertEqualContent(str.getBytes(), out);
217: } finally {
218: fin.close();
219: }
220: }
221:
222: public void testByteArrayToWriter() throws Exception {
223: File destination = newFile("copy7.txt");
224: FileInputStream fin = new FileInputStream(m_testFile);
225: byte[] in;
226: try {
227: // Create our byte[]. Rely on testInputStreamToByteArray() to make sure this is valid.
228: in = IOUtils.toByteArray(fin);
229: } finally {
230: fin.close();
231: }
232:
233: FileWriter fout = new FileWriter(destination);
234: try {
235: CopyUtils.copy(in, fout);
236: fout.flush();
237: checkFile(destination, m_testFile);
238: checkWrite(fout);
239: } finally {
240: fout.close();
241: }
242: deleteFile(destination);
243: }
244:
245: public void testByteArrayToString() throws Exception {
246: FileInputStream fin = new FileInputStream(m_testFile);
247: try {
248: byte[] in = IOUtils.toByteArray(fin);
249: // Create our byte[]. Rely on testInputStreamToByteArray() to make sure this is valid.
250: String str = IOUtils.toString(in);
251: assertEqualContent(in, str.getBytes());
252: } finally {
253: fin.close();
254: }
255: }
256:
257: /**
258: * Test for {@link IOUtils#toInputStream(String)} and {@link IOUtils#toInputStream(String, String)}.
259: * Note, this test utilizes on {@link IOUtils#toByteArray(java.io.InputStream)} and so relies on
260: * {@link #testInputStreamToByteArray()} to ensure this method functions correctly.
261: *
262: * @throws Exception on error
263: */
264: public void testStringToInputStream() throws Exception {
265: String str = "Abc123Xyz!";
266: InputStream inStream = IOUtils.toInputStream(str);
267: byte[] bytes = IOUtils.toByteArray(inStream);
268: assertEqualContent(str.getBytes(), bytes);
269: inStream = IOUtils.toInputStream(str, null);
270: bytes = IOUtils.toByteArray(inStream);
271: assertEqualContent(str.getBytes(), bytes);
272: inStream = IOUtils.toInputStream(str, "UTF-8");
273: bytes = IOUtils.toByteArray(inStream);
274: assertEqualContent(str.getBytes("UTF-8"), bytes);
275: }
276:
277: public void testByteArrayToOutputStream() throws Exception {
278: File destination = newFile("copy8.txt");
279: FileInputStream fin = new FileInputStream(m_testFile);
280: byte[] in;
281: try {
282: // Create our byte[]. Rely on testInputStreamToByteArray() to make sure this is valid.
283: in = IOUtils.toByteArray(fin);
284: } finally {
285: fin.close();
286: }
287:
288: FileOutputStream fout = new FileOutputStream(destination);
289: try {
290: CopyUtils.copy(in, fout);
291:
292: fout.flush();
293:
294: checkFile(destination, m_testFile);
295: checkWrite(fout);
296: } finally {
297: fout.close();
298: }
299: deleteFile(destination);
300: }
301:
302: public void testInputStreamToCharArray() throws Exception {
303: FileInputStream fin = new FileInputStream(m_testFile);
304: try {
305: char[] out = IOUtils.toCharArray(fin);
306: assertNotNull(out);
307: assertTrue("Not all chars were read", fin.available() == 0);
308: assertTrue("Wrong output size: out.length=" + out.length
309: + "!=" + FILE_SIZE, out.length == FILE_SIZE);
310: assertEqualContent(out, m_testFile);
311: } finally {
312: fin.close();
313: }
314: }
315:
316: public void testInputStreamToCharArrayWithEncoding()
317: throws Exception {
318: FileInputStream fin = new FileInputStream(m_testFile);
319: try {
320: char[] out = IOUtils.toCharArray(fin, "UTF-8");
321: assertNotNull(out);
322: assertTrue("Not all chars were read", fin.available() == 0);
323: assertTrue("Wrong output size: out.length=" + out.length
324: + "!=" + FILE_SIZE, out.length == FILE_SIZE);
325: assertEqualContent(out, m_testFile);
326: } finally {
327: fin.close();
328: }
329: }
330:
331: public void testReaderToCharArray() throws Exception {
332: FileReader fr = new FileReader(m_testFile);
333: try {
334: char[] out = IOUtils.toCharArray(fr);
335: assertNotNull(out);
336: assertTrue("Wrong output size: out.length=" + out.length
337: + "!=" + FILE_SIZE, out.length == FILE_SIZE);
338: assertEqualContent(out, m_testFile);
339: } finally {
340: fr.close();
341: }
342: }
343:
344: //-----------------------------------------------------------------------
345: public void testReadLines_InputStream() throws Exception {
346: File file = newFile("lines.txt");
347: InputStream in = null;
348: try {
349: String[] data = new String[] { "hello", "world", "",
350: "this is", "some text" };
351: createLineBasedFile(file, data);
352:
353: in = new FileInputStream(file);
354: List lines = IOUtils.readLines(in);
355: assertEquals(Arrays.asList(data), lines);
356: assertEquals(-1, in.read());
357: } finally {
358: IOUtils.closeQuietly(in);
359: deleteFile(file);
360: }
361: }
362:
363: //-----------------------------------------------------------------------
364: public void testReadLines_InputStream_String() throws Exception {
365: File file = newFile("lines.txt");
366: InputStream in = null;
367: try {
368: String[] data = new String[] { "hello", "/u1234", "",
369: "this is", "some text" };
370: createLineBasedFile(file, data);
371:
372: in = new FileInputStream(file);
373: List lines = IOUtils.readLines(in, "UTF-8");
374: assertEquals(Arrays.asList(data), lines);
375: assertEquals(-1, in.read());
376: } finally {
377: IOUtils.closeQuietly(in);
378: deleteFile(file);
379: }
380: }
381:
382: //-----------------------------------------------------------------------
383: public void testReadLines_Reader() throws Exception {
384: File file = newFile("lines.txt");
385: Reader in = null;
386: try {
387: String[] data = new String[] { "hello", "/u1234", "",
388: "this is", "some text" };
389: createLineBasedFile(file, data);
390:
391: in = new InputStreamReader(new FileInputStream(file));
392: List lines = IOUtils.readLines(in);
393: assertEquals(Arrays.asList(data), lines);
394: assertEquals(-1, in.read());
395: } finally {
396: IOUtils.closeQuietly(in);
397: deleteFile(file);
398: }
399: }
400:
401: }
|