You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by je...@apache.org on 2003/08/21 20:56:12 UTC
cvs commit: jakarta-commons-sandbox/io/src/test/org/apache/commons/io CopyUtilsTest.java FileUtilsTestCase.java IOTestSuite.java IOUtilsTestCase.java
jeremias 2003/08/21 11:56:12
Modified: io/src/test/org/apache/commons/io FileUtilsTestCase.java
IOTestSuite.java IOUtilsTestCase.java
Added: io/src/test/org/apache/commons/io CopyUtilsTest.java
Log:
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.
Revision Changes Path
1.2 +251 -172 jakarta-commons-sandbox/io/src/test/org/apache/commons/io/FileUtilsTestCase.java
Index: FileUtilsTestCase.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/io/src/test/org/apache/commons/io/FileUtilsTestCase.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- FileUtilsTestCase.java 25 Dec 2002 22:16:36 -0000 1.1
+++ FileUtilsTestCase.java 21 Aug 2003 18:56:12 -0000 1.2
@@ -7,7 +7,7 @@
*
* The Apache Software License, Version 1.1
*
- * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
+ * Copyright (c) 1999-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -56,217 +56,296 @@
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
- *
*/
-
package org.apache.commons.io;
-import java.io.BufferedOutputStream;
import java.io.File;
-import java.io.FileOutputStream;
import java.io.IOException;
+import java.net.URL;
+
+import org.apache.commons.io.testtools.*;
+
import junit.framework.Test;
-import junit.framework.TestCase;
import junit.framework.TestSuite;
+import junit.textui.TestRunner;
/**
* This is used to test FileUtils for correctness.
*
* @author <a href="mailto:peter@apache.org">Peter Donald</a>
*/
-public final class FileUtilsTestCase
- extends TestCase
-{
- private final int FILE1_SIZE = 1;
- private final int FILE2_SIZE = 1024 * 4 + 1;
+public final class FileUtilsTestCase extends FileBasedTestCase {
+
+ // Test data
+ private static final int FILE1_SIZE = 1;
+ private static final int FILE2_SIZE = 1024 * 4 + 1;
- private final File m_testDirectory;
private final File m_testFile1;
private final File m_testFile2;
- public FileUtilsTestCase( final String name )
- throws IOException
- {
- super( name );
-
- m_testDirectory = ( new File( "test/io/" ) ).getAbsoluteFile();
- if( !m_testDirectory.exists() )
- {
- m_testDirectory.mkdirs();
- }
+ public static void main(String[] args) {
+ TestRunner.run(suite());
+ }
- m_testFile1 = new File( m_testDirectory, "file1-test.txt" );
- m_testFile2 = new File( m_testDirectory, "file2-test.txt" );
+ public static Test suite() {
+ return new TestSuite(FileUtilsTestCase.class);
+ }
- createFile( m_testFile1, FILE1_SIZE );
- createFile( m_testFile2, FILE2_SIZE );
+ public FileUtilsTestCase(final String name) throws IOException {
+ super(name);
+
+ m_testFile1 = new File(getTestDirectory(), "file1-test.txt");
+ m_testFile2 = new File(getTestDirectory(), "file1a-test.txt");
}
- private void createFile( final File file, final long size )
- throws IOException
- {
- final BufferedOutputStream output =
- new BufferedOutputStream( new FileOutputStream( file ) );
-
- for( int i = 0; i < size; i++ )
- {
- output.write( (byte)'X' );
- }
+ /** @see junit.framework.TestCase#setUp() */
+ protected void setUp() throws Exception {
+ getTestDirectory().mkdirs();
+ createFile(m_testFile1, FILE1_SIZE);
+ createFile(m_testFile2, FILE2_SIZE);
+ FileUtils.deleteDirectory(getTestDirectory());
+ getTestDirectory().mkdirs();
+ createFile(m_testFile1, FILE1_SIZE);
+ createFile(m_testFile2, FILE2_SIZE);
+ }
+
+ /** @see junit.framework.TestCase#tearDown() */
+ protected void tearDown() throws Exception {
+ FileUtils.deleteDirectory(getTestDirectory());
+ }
+
+ public void testCopyFile1() throws Exception {
+ final File destination = new File(getTestDirectory(), "copy1.txt");
+ FileUtils.copyFile(m_testFile1, destination);
+ assertTrue("Check Exist", destination.exists());
+ assertTrue("Check Full copy", destination.length() == FILE1_SIZE);
+ }
- output.close();
+ public void testCopyFile2() throws Exception {
+ final File destination = new File(getTestDirectory(), "copy2.txt");
+ FileUtils.copyFile(m_testFile2, destination);
+ assertTrue("Check Exist", destination.exists());
+ assertTrue("Check Full copy", destination.length() == FILE2_SIZE);
}
- public static Test suite()
- throws IOException
- {
- final TestSuite suite = new TestSuite();
- suite.addTest( new FileUtilsTestCase( "testCopyFile1" ) );
- suite.addTest( new FileUtilsTestCase( "testCopyFile2" ) );
- suite.addTest( new FileUtilsTestCase( "testForceDeleteAFile1" ) );
- suite.addTest( new FileUtilsTestCase( "testForceDeleteAFile2" ) );
- suite.addTest( new FileUtilsTestCase( "testCopyFile1ToDir" ) );
- suite.addTest( new FileUtilsTestCase( "testCopyFile2ToDir" ) );
- suite.addTest( new FileUtilsTestCase( "testForceDeleteDir" ) );
- suite.addTest( new FileUtilsTestCase( "testResolveFileDotDot" ) );
- suite.addTest( new FileUtilsTestCase( "testResolveFileDot" ) );
- suite.addTest( new FileUtilsTestCase( "testNormalize" ) );
- return suite;
- }
-
- public void testCopyFile1()
- throws Exception
- {
- final File destination = new File( m_testDirectory, "copy1.txt" );
- FileUtils.copyFile( m_testFile1, destination );
- assertTrue( "Check Exist", destination.exists() );
- assertTrue( "Check Full copy", destination.length() == FILE1_SIZE );
- }
-
- public void testCopyFile2()
- throws Exception
- {
- final File destination = new File( m_testDirectory, "copy2.txt" );
- FileUtils.copyFile( m_testFile2, destination );
- assertTrue( "Check Exist", destination.exists() );
- assertTrue( "Check Full copy", destination.length() == FILE2_SIZE );
- }
-
- public void testForceDeleteAFile1()
- throws Exception
- {
- final File destination = new File( m_testDirectory, "copy1.txt" );
+ public void testForceDeleteAFile1() throws Exception {
+ final File destination = new File(getTestDirectory(), "copy1.txt");
destination.createNewFile();
- assertTrue( "Copy1.txt doesn't exist to delete", destination.exists() );
- FileUtils.forceDelete( destination );
- assertTrue( "Check No Exist", !destination.exists() );
+ assertTrue("Copy1.txt doesn't exist to delete", destination.exists());
+ FileUtils.forceDelete(destination);
+ assertTrue("Check No Exist", !destination.exists());
}
- public void testForceDeleteAFile2()
- throws Exception
- {
- final File destination = new File( m_testDirectory, "copy2.txt" );
+ public void testForceDeleteAFile2() throws Exception {
+ final File destination = new File(getTestDirectory(), "copy2.txt");
destination.createNewFile();
- assertTrue( "Copy2.txt doesn't exist to delete", destination.exists() );
- FileUtils.forceDelete( destination );
- assertTrue( "Check No Exist", !destination.exists() );
- }
-
- public void testCopyFile1ToDir()
- throws Exception
- {
- final File directory = new File( m_testDirectory, "subdir" );
- if( !directory.exists() ) directory.mkdirs();
- final File destination = new File( directory, "file1-test.txt" );
- FileUtils.copyFileToDirectory( m_testFile1, directory );
- assertTrue( "Check Exist", destination.exists() );
- assertTrue( "Check Full copy", destination.length() == FILE1_SIZE );
- }
-
- public void testCopyFile2ToDir()
- throws Exception
- {
- final File directory = new File( m_testDirectory, "subdir" );
- if( !directory.exists() ) directory.mkdirs();
- final File destination = new File( directory, "file2-test.txt" );
- FileUtils.copyFileToDirectory( m_testFile2, directory );
- assertTrue( "Check Exist", destination.exists() );
- assertTrue( "Check Full copy", destination.length() == FILE2_SIZE );
- }
-
- public void testForceDeleteDir()
- throws Exception
- {
- FileUtils.forceDelete( m_testDirectory.getParentFile() );
- assertTrue( "Check No Exist", !m_testDirectory.getParentFile().exists() );
- }
-
- public void testResolveFileDotDot()
- throws Exception
- {
- final File file = FileUtils.resolveFile( m_testDirectory, ".." );
- assertEquals( "Check .. operator", file, m_testDirectory.getParentFile() );
- }
-
- public void testResolveFileDot()
- throws Exception
- {
- final File file = FileUtils.resolveFile( m_testDirectory, "." );
- assertEquals( "Check . operator", file, m_testDirectory );
- }
-
- public void testNormalize()
- throws Exception
- {
+ assertTrue("Copy2.txt doesn't exist to delete", destination.exists());
+ FileUtils.forceDelete(destination);
+ assertTrue("Check No Exist", !destination.exists());
+ }
+
+ public void testCopyFile1ToDir() throws Exception {
+ final File directory = new File(getTestDirectory(), "subdir");
+ if (!directory.exists())
+ directory.mkdirs();
+ final File destination = new File(directory, m_testFile1.getName());
+ FileUtils.copyFileToDirectory(m_testFile1, directory);
+ assertTrue("Check Exist", destination.exists());
+ assertTrue("Check Full copy", destination.length() == FILE1_SIZE);
+ }
+
+ public void testCopyFile2ToDir() throws Exception {
+ final File directory = new File(getTestDirectory(), "subdir");
+ if (!directory.exists())
+ directory.mkdirs();
+ final File destination = new File(directory, m_testFile2.getName());
+ FileUtils.copyFileToDirectory(m_testFile2, directory);
+ assertTrue("Check Exist", destination.exists());
+ assertTrue("Check Full copy", destination.length() == FILE2_SIZE);
+ }
+
+ public void testForceDeleteDir() throws Exception {
+ FileUtils.forceDelete(getTestDirectory().getParentFile());
+ assertTrue("Check No Exist", !getTestDirectory().getParentFile().exists());
+ }
+
+ public void testResolveFileDotDot() throws Exception {
+ final File file = FileUtils.resolveFile(getTestDirectory(), "..");
+ assertEquals(
+ "Check .. operator",
+ file,
+ getTestDirectory().getParentFile());
+ }
+
+ public void testResolveFileDot() throws Exception {
+ final File file = FileUtils.resolveFile(getTestDirectory(), ".");
+ assertEquals("Check . operator", file, getTestDirectory());
+ }
+
+ public void testNormalize() throws Exception {
final String[] src =
{
- "", "/", "///", "/foo", "/foo//", "/./", "/foo/./", "/foo/./bar",
- "/foo/../bar", "/foo/../bar/../baz", "/foo/bar/../../baz", "/././",
- "/foo/./../bar", "/foo/.././bar/", "//foo//./bar", "/../",
- "/foo/../../"
- };
+ "",
+ "/",
+ "///",
+ "/foo",
+ "/foo//",
+ "/./",
+ "/foo/./",
+ "/foo/./bar",
+ "/foo/../bar",
+ "/foo/../bar/../baz",
+ "/foo/bar/../../baz",
+ "/././",
+ "/foo/./../bar",
+ "/foo/.././bar/",
+ "//foo//./bar",
+ "/../",
+ "/foo/../../" };
final String[] dest =
{
- "", "/", "/", "/foo", "/foo/", "/", "/foo/", "/foo/bar", "/bar",
- "/baz", "/baz", "/", "/bar", "/bar/", "/foo/bar", null, null
- };
-
- assertEquals( "Oops, test writer goofed", src.length, dest.length );
-
- for( int i = 0; i < src.length; i++ )
- {
- assertEquals( "Check if '" + src[ i ] + "' normalized to '" + dest[ i ] + "'",
- dest[ i ], FileUtils.normalize( src[ i ] ) );
+ "",
+ "/",
+ "/",
+ "/foo",
+ "/foo/",
+ "/",
+ "/foo/",
+ "/foo/bar",
+ "/bar",
+ "/baz",
+ "/baz",
+ "/",
+ "/bar",
+ "/bar/",
+ "/foo/bar",
+ null,
+ null };
+
+ assertEquals("Oops, test writer goofed", src.length, dest.length);
+
+ for (int i = 0; i < src.length; i++) {
+ assertEquals(
+ "Check if '" + src[i] + "' normalized to '" + dest[i] + "'",
+ dest[i],
+ FileUtils.normalize(src[i]));
+ }
+ }
+
+ private String replaceAll(String text, String lookFor, String replaceWith) {
+ StringBuffer sb = new StringBuffer(text);
+ while (true) {
+ int idx = sb.indexOf(lookFor);
+ if (idx < 0) {
+ break;
+ }
+ sb.replace(idx, idx + lookFor.length(), replaceWith);
}
+ return sb.toString();
}
/**
* Test the FileUtils implementation.
*/
/// Used to exist as IOTestCase class
- public void testFileUtils() {
- String filename = "src/test/org/apache/commons/io/test.txt";
- String filename2 = "src/test/org/apache/commons/io/test2.txt";
- assertTrue("test.txt extension == \"txt\"", FileUtils.extension(filename).equals("txt"));
- assertTrue("Test file exists", FileUtils.fileExists(filename));
- assertTrue("Second test file does not exist", !FileUtils.fileExists(filename2));
- try {
- FileUtils.fileWrite(filename2, filename);
- assertTrue("Second file was written", FileUtils.fileExists(filename2));
- String file2contents = FileUtils.fileRead(filename2);
- assertTrue("Second file's contents correct", FileUtils.fileRead(filename2).equals(file2contents));
- FileUtils.fileDelete(filename2);
- assertTrue("Second test file does not exist", !FileUtils.fileExists(filename2));
- } catch (Exception e) {
- fail("Error reading or writing second test file: " + filename);
+ public void testFileUtils() throws Exception {
+ // Loads file from classpath
+ final String path = "/test.txt";
+ final URL url = this.getClass().getResource(path);
+ assertNotNull(path + " was not found.", url);
+
+ String filename = url.getFile();
+ //The following line applies a fix for spaces in a path
+ filename = replaceAll(filename, "%20", " ");
+ final String filename2 = "test2.txt";
+
+ assertTrue(
+ "test.txt extension == \"txt\"",
+ FileUtils.getExtension(filename).equals("txt"));
+
+ assertTrue("Test file does not exist: " + filename, FileUtils.fileExists(filename));
+
+ assertTrue(
+ "Second test file does not exist",
+ !FileUtils.fileExists(filename2));
+
+ FileUtils.fileWrite(filename2, filename);
+ assertTrue("Second file was written", FileUtils.fileExists(filename2));
+
+ final String file2contents = FileUtils.fileRead(filename2);
+ assertTrue(
+ "Second file's contents correct",
+ FileUtils.fileRead(filename2).equals(file2contents));
+
+ FileUtils.fileDelete(filename2);
+ assertTrue(
+ "Second test file does not exist",
+ !FileUtils.fileExists(filename2));
+
+ final String contents = FileUtils.fileRead(filename);
+ assertTrue("FileUtils.fileRead()", contents.equals("This is a test"));
+
+ }
+
+ public void testGetExtension() {
+ final String[][] tests = {
+ {"filename.ext", "ext"},
+ {"README", ""},
+ {"domain.dot.com", "com"},
+ {"image.jpeg", "jpeg"}};
+ for (int i = 0; i < tests.length; i++) {
+ assertEquals(tests[i][1], FileUtils.getExtension(tests[i][0]));
+ //assertEquals(tests[i][1], FileUtils.extension(tests[i][0]));
+ }
+ }
+
+ /* TODO: Reenable this test */
+ public void DISABLED__testGetExtensionWithPaths() {
+ final String[][] testsWithPaths = {
+ {"/tmp/foo/filename.ext", "ext"},
+ {"C:\\temp\\foo\\filename.ext", "ext"},
+ {"/tmp/foo.bar/filename.ext", "ext"},
+ {"C:\\temp\\foo.bar\\filename.ext", "ext"},
+ {"/tmp/foo.bar/README", ""},
+ {"C:\\temp\\foo.bar\\README", ""},
+ {"../filename.ext", "ext"}};
+ for (int i = 0; i < testsWithPaths.length; i++) {
+ assertEquals(testsWithPaths[i][1], FileUtils.getExtension(testsWithPaths[i][0]));
+ //assertEquals(testsWithPaths[i][1], FileUtils.extension(testsWithPaths[i][0]));
}
+ }
- try {
- String contents = FileUtils.fileRead(filename);
- assertTrue("FileUtils.fileRead()", contents.equals("This is a test"));
- } catch (Exception e) {
- fail("Error loading file: " + filename);
+ public void testRemoveExtension() {
+ final String[][] tests = {
+ {"filename.ext", "filename"},
+ {"first.second.third.ext", "first.second.third"},
+ {"README", "README"},
+ {"domain.dot.com", "domain.dot"},
+ {"image.jpeg", "image"}};
+
+ for (int i = 0; i < tests.length; i++) {
+ assertEquals(tests[i][1], FileUtils.removeExtension(tests[i][0]));
+ //assertEquals(tests[i][1], FileUtils.basename(tests[i][0]));
+ }
+ }
+
+ /* TODO: Reenable this test */
+ public void DISABLED__testRemoveExtensionWithPaths() {
+ final String[][] testsWithPaths = {
+ {"/tmp/foo/filename.ext", "filename"},
+ {"C:\\temp\\foo\\filename.ext", "filename"},
+ {"/tmp/foo.bar/filename.ext", "filename"},
+ {"C:\\temp\\foo.bar\\filename.ext", "filename"},
+ {"/tmp/foo.bar/README", "README"},
+ {"C:\\temp\\foo.bar\\README", "README"},
+ {"../filename.ext", "filename"}};
+
+ for (int i = 0; i < testsWithPaths.length; i++) {
+ assertEquals(testsWithPaths[i][1], FileUtils.removeExtension(testsWithPaths[i][0]));
+ //assertEquals(testsWithPaths[i][1], FileUtils.basename(testsWithPaths[i][0]));
}
}
}
+
1.6 +5 -4 jakarta-commons-sandbox/io/src/test/org/apache/commons/io/IOTestSuite.java
Index: IOTestSuite.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/io/src/test/org/apache/commons/io/IOTestSuite.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- IOTestSuite.java 27 Jul 2003 17:10:18 -0000 1.5
+++ IOTestSuite.java 21 Aug 2003 18:56:12 -0000 1.6
@@ -83,8 +83,9 @@
public static Test suite()
{
final TestSuite suite = new TestSuite( "IO Utilities" );
- suite.addTest( new TestSuite( FileUtilsTestCase.class ) );
+ suite.addTest( new TestSuite( CopyUtilsTest.class ) );
suite.addTest( new TestSuite( IOUtilsTestCase.class ) );
+ suite.addTest( new TestSuite( FileUtilsTestCase.class ) );
suite.addTest( new TestSuite( FileFilterTestCase.class ) );
suite.addTest( new TestSuite( DemuxTestCase.class ) );
suite.addTest( new TestSuite( HexDumpTest.class ) );
1.2 +197 -256 jakarta-commons-sandbox/io/src/test/org/apache/commons/io/IOUtilsTestCase.java
Index: IOUtilsTestCase.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/io/src/test/org/apache/commons/io/IOUtilsTestCase.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- IOUtilsTestCase.java 27 Jul 2003 17:10:18 -0000 1.1
+++ IOUtilsTestCase.java 21 Aug 2003 18:56:12 -0000 1.2
@@ -61,20 +61,15 @@
package org.apache.commons.io;
-import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
-import java.io.OutputStream;
-import java.io.PrintStream;
-import java.io.PrintWriter;
-import java.io.Writer;
import java.util.Arrays;
-import junit.framework.AssertionFailedError;
-import junit.framework.TestCase;
+
+import org.apache.commons.io.testtools.*;
// Note: jdk1.2 dependency
@@ -92,9 +87,7 @@
*
* @author <a href="mailto:jefft@apache.org">Jeff Turner</a>
*/
-public final class IOUtilsTestCase
- extends TestCase
-{
+public final class IOUtilsTestCase extends FileBasedTestCase {
/*
* Note: this is not particularly beautiful code. A better way to check for
* flush and close status would be to implement "trojan horse" wrapper
@@ -104,26 +97,21 @@
private final int FILE_SIZE = 1024 * 4 + 1;
- private File m_testDirectory;
private File m_testFile;
public void setUp()
{
try
{
- m_testDirectory = ( new File( "test/io/" ) ).getAbsoluteFile();
- if( !m_testDirectory.exists() )
- {
- m_testDirectory.mkdirs();
- }
-
- m_testFile = new File( m_testDirectory, "file2-test.txt" );
+ getTestDirectory().mkdirs();
+ m_testFile = new File( getTestDirectory(), "file2-test.txt" );
createFile( m_testFile, FILE_SIZE );
}
catch( IOException ioe )
{
- throw new RuntimeException( "Can't run this test because environment could not be built" );
+ throw new RuntimeException( "Can't run this test because "
+ + "environment could not be built: " + ioe.getMessage());
}
}
@@ -131,7 +119,7 @@
{
try
{
- FileUtils.deleteDirectory( "test" );
+ FileUtils.deleteDirectory( getTestDirectory() );
}
catch( IOException ioe )
{
@@ -144,20 +132,6 @@
super( name );
}
- private void createFile( final File file, final long size )
- throws IOException
- {
- final BufferedOutputStream output =
- new BufferedOutputStream( new FileOutputStream( file ) );
-
- for( int i = 0; i < size; i++ )
- {
- output.write( (byte)( i % 128 ) ); // nice varied byte pattern compatible with Readers and Writers
- }
-
- output.close();
- }
-
/** Assert that the contents of two byte arrays are the same. */
private void assertEqualContent( final byte[] b0, final byte[] b1 )
throws IOException
@@ -165,62 +139,28 @@
assertTrue( "Content not equal according to java.util.Arrays#equals()", Arrays.equals( b0, b1 ) );
}
- /** Assert that the content of two files is the same. */
- private void assertEqualContent( final File f0, final File f1 )
- throws IOException
- {
- final FileInputStream is0 = new FileInputStream( f0 );
- final FileInputStream is1 = new FileInputStream( f1 );
- final byte[] buf0 = new byte[ FILE_SIZE ];
- final byte[] buf1 = new byte[ FILE_SIZE ];
- int n0 = 0;
- int n1 = 0;
-
- while( -1 != n0 )
- {
- n0 = is0.read( buf0 );
- n1 = is1.read( buf1 );
- assertTrue( "The files " + f0 + " and " + f1 +
- " have differing number of bytes available (" + n0 +
- " vs " + n1 + ")", ( n0 == n1 ) );
-
- assertTrue( "The files " + f0 + " and " + f1 +
- " have different content", Arrays.equals( buf0, buf1 ) );
- }
- }
-
- /** Assert that the content of a file is equal to that in a byte[]. */
- private void assertEqualContent( final byte[] b0, final File file )
- throws IOException
- {
- final FileInputStream is = new FileInputStream( file );
- byte[] b1 = new byte[ b0.length ];
- int numRead = is.read( b1 );
- assertTrue( "Different number of bytes", numRead == b0.length && is.available() == 0 );
- for( int i = 0;
- i < numRead;
- assertTrue( "Byte " + i + " differs (" + b0[ i ] + " != " + b1[ i ] + ")", b0[ i ] == b1[ i ] ), i++
- )
- ;
- }
-
public void testInputStreamToOutputStream()
throws Exception
{
final File destination = newFile( "copy1.txt" );
final FileInputStream fin = new FileInputStream( m_testFile );
- final FileOutputStream fout = new FileOutputStream( destination );
-
- int count = IOUtils.copy( fin, fout );
- assertTrue( "Not all bytes were read", fin.available() == 0 );
- assertEquals( "Number of bytes read should equal file size", m_testFile.length(), count );
- fout.flush();
-
- checkFile( destination );
- checkWrite( fout );
- fout.close();
- fin.close();
- deleteFile( destination );
+ try {
+ final FileOutputStream fout = new FileOutputStream( destination );
+ try {
+ int count = IOUtils.copy( fin, fout );
+ assertTrue( "Not all bytes were read", fin.available() == 0 );
+ assertEquals( "Number of bytes read should equal file size", m_testFile.length(), count );
+ fout.flush();
+
+ checkFile( destination, m_testFile );
+ checkWrite( fout );
+ } finally {
+ fout.close();
+ }
+ deleteFile( destination );
+ } finally {
+ fin.close();
+ }
}
public void testInputStreamToWriter()
@@ -228,30 +168,38 @@
{
final File destination = newFile( "copy2.txt" );
final FileInputStream fin = new FileInputStream( m_testFile );
- final FileWriter fout = new FileWriter( destination );
-
- IOUtils.copy( fin, fout );
-
- assertTrue( "Not all bytes were read", fin.available() == 0 );
- fout.flush();
-
- checkFile( destination );
- checkWrite( fout );
- fout.close();
- fin.close();
- deleteFile( destination );
+ try {
+ final FileWriter fout = new FileWriter( destination );
+ try {
+ IOUtils.copy( fin, fout );
+
+ assertTrue( "Not all bytes were read", fin.available() == 0 );
+ fout.flush();
+
+ checkFile( destination, m_testFile );
+ checkWrite( fout );
+ } finally {
+ fout.close();
+ }
+ deleteFile( destination );
+ } finally {
+ fin.close();
+ }
}
public void testInputStreamToString()
throws Exception
{
final FileInputStream fin = new FileInputStream( m_testFile );
- final String out = IOUtils.toString( fin );
- assertNotNull( out );
- assertTrue( "Not all bytes were read", fin.available() == 0 );
- assertTrue( "Wrong output size: out.length()=" + out.length() +
- "!=" + FILE_SIZE, out.length() == FILE_SIZE );
- fin.close();
+ try {
+ final String out = IOUtils.toString( fin );
+ assertNotNull( out );
+ assertTrue( "Not all bytes were read", fin.available() == 0 );
+ assertTrue( "Wrong output size: out.length()=" + out.length() +
+ "!=" + FILE_SIZE, out.length() == FILE_SIZE );
+ } finally {
+ fin.close();
+ }
}
public void testReaderToOutputStream()
@@ -259,20 +207,26 @@
{
final File destination = newFile( "copy3.txt" );
final FileReader fin = new FileReader( m_testFile );
- final FileOutputStream fout = new FileOutputStream( destination );
- IOUtils.copy( fin, fout );
- //Note: this method *does* flush. It is equivalent to:
- // final OutputStreamWriter _out = new OutputStreamWriter(fout);
- // IOUtils.copy( fin, _out, 4096 ); // copy( Reader, Writer, int );
- // _out.flush();
- // out = fout;
-
- // Note: rely on the method to flush
- checkFile( destination );
- checkWrite( fout );
- fout.close();
- fin.close();
- deleteFile( destination );
+ try {
+ final FileOutputStream fout = new FileOutputStream( destination );
+ try {
+ IOUtils.copy( fin, fout );
+ //Note: this method *does* flush. It is equivalent to:
+ // final OutputStreamWriter _out = new OutputStreamWriter(fout);
+ // IOUtils.copy( fin, _out, 4096 ); // copy( Reader, Writer, int );
+ // _out.flush();
+ // out = fout;
+
+ // Note: rely on the method to flush
+ checkFile( destination, m_testFile );
+ checkWrite( fout );
+ } finally {
+ fout.close();
+ }
+ deleteFile( destination );
+ } finally {
+ fin.close();
+ }
}
public void testReaderToWriter()
@@ -280,28 +234,37 @@
{
final File destination = newFile( "copy4.txt" );
final FileReader fin = new FileReader( m_testFile );
- final FileWriter fout = new FileWriter( destination );
- int count = IOUtils.copy( fin, fout );
- assertEquals( "The number of characters returned by copy is wrong", m_testFile.length(), count);
-
- fout.flush();
- checkFile( destination );
- checkWrite( fout );
- fout.close();
- fin.close();
- deleteFile( destination );
+ try {
+ final FileWriter fout = new FileWriter( destination );
+ try {
+ int count = IOUtils.copy( fin, fout );
+ assertEquals( "The number of characters returned by copy is wrong", m_testFile.length(), count);
+
+ fout.flush();
+ checkFile( destination, m_testFile );
+ checkWrite( fout );
+ } finally {
+ fout.close();
+ }
+ deleteFile( destination );
+ } finally {
+ fin.close();
+ }
}
public void testReaderToString()
throws Exception
{
final FileReader fin = new FileReader( m_testFile );
- final String out = IOUtils.toString( fin );
- assertNotNull( out );
- assertTrue( "Wrong output size: out.length()=" +
- out.length() + "!=" + FILE_SIZE,
- out.length() == FILE_SIZE );
- fin.close();
+ try {
+ final String out = IOUtils.toString( fin );
+ assertNotNull( out );
+ assertTrue( "Wrong output size: out.length()=" +
+ out.length() + "!=" + FILE_SIZE,
+ out.length() == FILE_SIZE );
+ } finally {
+ fin.close();
+ }
}
public void testStringToOutputStream()
@@ -309,21 +272,29 @@
{
final File destination = newFile( "copy5.txt" );
final FileReader fin = new FileReader( m_testFile );
- // Create our String. Rely on testReaderToString() to make sure this is valid.
- final String str = IOUtils.toString( fin );
+ final String str;
+ try {
+ // Create our String. Rely on testReaderToString() to make sure this is valid.
+ str = IOUtils.toString( fin );
+ } finally {
+ fin.close();
+ }
+
final FileOutputStream fout = new FileOutputStream( destination );
- IOUtils.copy( str, fout );
- //Note: this method *does* flush. It is equivalent to:
- // final OutputStreamWriter _out = new OutputStreamWriter(fout);
- // IOUtils.copy( str, _out, 4096 ); // copy( Reader, Writer, int );
- // _out.flush();
- // out = fout;
- // note: we don't flush here; this IOUtils method does it for us
-
- checkFile( destination );
- checkWrite( fout );
- fout.close();
- fin.close();
+ try {
+ IOUtils.copy( str, fout );
+ //Note: this method *does* flush. It is equivalent to:
+ // final OutputStreamWriter _out = new OutputStreamWriter(fout);
+ // IOUtils.copy( str, _out, 4096 ); // copy( Reader, Writer, int );
+ // _out.flush();
+ // out = fout;
+ // note: we don't flush here; this IOUtils method does it for us
+
+ checkFile( destination, m_testFile );
+ checkWrite( fout );
+ } finally {
+ fout.close();
+ }
deleteFile( destination );
}
@@ -332,17 +303,24 @@
{
final File destination = newFile( "copy6.txt" );
FileReader fin = new FileReader( m_testFile );
- // Create our String. Rely on testReaderToString() to make sure this is valid.
- final String str = IOUtils.toString( fin );
+ final String str;
+ try {
+ // Create our String. Rely on testReaderToString() to make sure this is valid.
+ str = IOUtils.toString( fin );
+ } finally {
+ fin.close();
+ }
+
final FileWriter fout = new FileWriter( destination );
- IOUtils.copy( str, fout );
- fout.flush();
-
- checkFile( destination );
- checkWrite( fout );
- fout.close();
- fin.close();
-
+ try {
+ IOUtils.copy( str, fout );
+ fout.flush();
+
+ checkFile( destination, m_testFile );
+ checkWrite( fout );
+ } finally {
+ fout.close();
+ }
deleteFile( destination );
}
@@ -350,43 +328,55 @@
throws Exception
{
final FileInputStream fin = new FileInputStream( m_testFile );
- final byte[] out = IOUtils.toByteArray( fin );
- assertNotNull( out );
- assertTrue( "Not all bytes were read", fin.available() == 0 );
- assertTrue( "Wrong output size: out.length=" + out.length +
- "!=" + FILE_SIZE, out.length == FILE_SIZE );
- assertEqualContent( out, m_testFile );
- fin.close();
+ try {
+ final byte[] out = IOUtils.toByteArray( fin );
+ assertNotNull( out );
+ assertTrue( "Not all bytes were read", fin.available() == 0 );
+ assertTrue( "Wrong output size: out.length=" + out.length +
+ "!=" + FILE_SIZE, out.length == FILE_SIZE );
+ assertEqualContent( out, m_testFile );
+ } finally {
+ fin.close();
+ }
}
public void testStringToByteArray()
throws Exception
{
final FileReader fin = new FileReader( m_testFile );
-
- // Create our String. Rely on testReaderToString() to make sure this is valid.
- final String str = IOUtils.toString( fin );
-
- final byte[] out = IOUtils.toByteArray( str );
- assertEqualContent( str.getBytes(), out );
- fin.close();
+ try {
+ // Create our String. Rely on testReaderToString() to make sure this is valid.
+ final String str = IOUtils.toString( fin );
+
+ final byte[] out = IOUtils.toByteArray( str );
+ assertEqualContent( str.getBytes(), out );
+ } finally {
+ fin.close();
+ }
}
public void testByteArrayToWriter()
throws Exception
{
final File destination = newFile( "copy7.txt" );
- final FileWriter fout = new FileWriter( destination );
final FileInputStream fin = new FileInputStream( m_testFile );
+ final byte[] in;
+ try {
+ // Create our byte[]. Rely on testInputStreamToByteArray() to make sure this is valid.
+ in = IOUtils.toByteArray( fin );
+ } finally {
+ fin.close();
+ }
- // Create our byte[]. Rely on testInputStreamToByteArray() to make sure this is valid.
- final byte[] in = IOUtils.toByteArray( fin );
- IOUtils.copy( in, fout );
- fout.flush();
- checkFile( destination );
- checkWrite( fout );
- fout.close();
- fin.close();
+ final FileWriter fout = new FileWriter( destination );
+ try {
+ IOUtils.copy( in, fout );
+ fout.flush();
+ checkFile( destination, m_testFile );
+ checkWrite( fout );
+ } finally {
+ fout.close();
+ }
deleteFile( destination );
}
@@ -394,92 +384,43 @@
throws Exception
{
final FileInputStream fin = new FileInputStream( m_testFile );
- final byte[] in = IOUtils.toByteArray( fin );
- // Create our byte[]. Rely on testInputStreamToByteArray() to make sure this is valid.
- String str = IOUtils.toString( in );
- assertEqualContent( in, str.getBytes() );
- fin.close();
+ try {
+ final byte[] in = IOUtils.toByteArray( fin );
+ // Create our byte[]. Rely on testInputStreamToByteArray() to make sure this is valid.
+ String str = IOUtils.toString( in );
+ assertEqualContent( in, str.getBytes() );
+ } finally {
+ fin.close();
+ }
}
public void testByteArrayToOutputStream()
throws Exception
{
final File destination = newFile( "copy8.txt" );
- final FileOutputStream fout = new FileOutputStream( destination );
final FileInputStream fin = new FileInputStream( m_testFile );
+ final byte[] in;
+ try {
+ // Create our byte[]. Rely on testInputStreamToByteArray() to make sure this is valid.
+ in = IOUtils.toByteArray( fin );
+ } finally {
+ fin.close();
+ }
- // Create our byte[]. Rely on testInputStreamToByteArray() to make sure this is valid.
- final byte[] in = IOUtils.toByteArray( fin );
-
- IOUtils.copy( in, fout );
-
- fout.flush();
-
- checkFile( destination );
- checkWrite( fout );
- fout.close();
- fin.close();
- deleteFile( destination );
- }
-
-
- //////////////////////////////////////////////////////
- // xxxxxxxxx
-
-
- private File newFile( String filename )
- throws Exception
- {
- final File destination = new File( m_testDirectory, filename );
- assertTrue( filename + "Test output data file shouldn't previously exist",
- !destination.exists() );
-
- return destination;
- }
-
- private void checkFile( final File file )
- throws Exception
- {
- assertTrue( "Check existence of output file", file.exists() );
- assertEqualContent( m_testFile, file );
- }
+ final FileOutputStream fout = new FileOutputStream( destination );
+ try {
+ IOUtils.copy( in, fout );
- private void checkWrite( final OutputStream output )
- throws Exception
- {
- try
- {
- new PrintStream( output ).write( 0 );
- }
- catch( final Throwable t )
- {
- throw new AssertionFailedError( "The copy() method closed the stream " +
- "when it shouldn't have. " + t.getMessage() );
- }
- }
+ fout.flush();
- private void checkWrite( final Writer output )
- throws Exception
- {
- try
- {
- new PrintWriter( output ).write( 'a' );
- }
- catch( final Throwable t )
- {
- throw new AssertionFailedError( "The copy() method closed the stream " +
- "when it shouldn't have. " + t.getMessage() );
+ checkFile( destination, m_testFile );
+ checkWrite( fout );
+ } finally {
+ fout.close();
}
+ deleteFile( destination );
}
- private void deleteFile( final File file )
- throws Exception
- {
- assertTrue( "Wrong output size: file.length()=" +
- file.length() + "!=" + FILE_SIZE + 1,
- file.length() == FILE_SIZE + 1 );
- //assertTrue( "File would not delete", (file.delete() || ( !file.exists() )));
- }
}
1.1 jakarta-commons-sandbox/io/src/test/org/apache/commons/io/CopyUtilsTest.java
Index: CopyUtilsTest.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.io;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.util.Arrays;
import org.apache.commons.io.testtools.YellOnCloseInputStream;
import org.apache.commons.io.testtools.YellOnFlushAndCloseOutputStream;
import org.apache.commons.io.testtools.FileBasedTestCase;
import junit.framework.Test;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
/**
* JUnit tests for CopyUtils.
*
* @author Jeff Turner
* @author Matthew Hawthorne
* @author <a href="mailto:jeremias@apache.org">Jeremias Maerki</a>
* @version $Id: CopyUtilsTest.java,v 1.1 2003/08/21 18:56:12 jeremias Exp $
* @see CopyUtils
*/
public class CopyUtilsTest extends FileBasedTestCase {
/*
* NOTE this is not particularly beautiful code. A better way to check for
* flush and close status would be to implement "trojan horse" wrapper
* implementations of the various stream classes, which set a flag when
* relevant methods are called. (JT)
*/
private final int FILE_SIZE = 1024 * 4 + 1;
private byte[] inData = generateTestData(FILE_SIZE);
public static void main(String[] args) {
TestRunner.run(suite());
}
public static Test suite() {
return new TestSuite(CopyUtilsTest.class);
}
public CopyUtilsTest(String testName) {
super(testName);
}
// ----------------------------------------------------------------
// Setup
// ----------------------------------------------------------------
public void setUp() throws Exception {
}
public void tearDown() throws Exception {
}
// ----------------------------------------------------------------
// Tests
// ----------------------------------------------------------------
public void testCopy_byteArrayToOutputStream() throws Exception {
ByteArrayOutputStream baout = new ByteArrayOutputStream();
OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
CopyUtils.copy(inData, out);
assertEquals("Sizes differ", inData.length, baout.size());
assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
}
public void testCopy_byteArrayToWriter() throws Exception {
ByteArrayOutputStream baout = new ByteArrayOutputStream();
OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
Writer writer = new java.io.OutputStreamWriter(baout, "US-ASCII");
CopyUtils.copy(inData, writer);
writer.flush();
assertEquals("Sizes differ", inData.length, baout.size());
assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
}
public void testCopy_inputStreamToOutputStream() throws Exception {
InputStream in = new ByteArrayInputStream(inData);
in = new YellOnCloseInputStream(in);
ByteArrayOutputStream baout = new ByteArrayOutputStream();
OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
int count = CopyUtils.copy(in, out);
assertTrue("Not all bytes were read", in.available() == 0);
assertEquals("Sizes differ", inData.length, baout.size());
assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
}
public void testCopy_inputStreamToWriter() throws Exception {
InputStream in = new ByteArrayInputStream(inData);
in = new YellOnCloseInputStream(in);
ByteArrayOutputStream baout = new ByteArrayOutputStream();
OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
Writer writer = new java.io.OutputStreamWriter(baout, "US-ASCII");
CopyUtils.copy(in, writer);
writer.flush();
assertTrue("Not all bytes were read", in.available() == 0);
assertEquals("Sizes differ", inData.length, baout.size());
assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
}
public void testCopy_readerToOutputStream() throws Exception {
InputStream in = new ByteArrayInputStream(inData);
in = new YellOnCloseInputStream(in);
Reader reader = new java.io.InputStreamReader(in, "US-ASCII");
ByteArrayOutputStream baout = new ByteArrayOutputStream();
OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
CopyUtils.copy(reader, out);
//Note: this method *does* flush. It is equivalent to:
// final OutputStreamWriter _out = new OutputStreamWriter(fout);
// IOUtils.copy( fin, _out, 4096 ); // copy( Reader, Writer, int );
// _out.flush();
// out = fout;
// Note: rely on the method to flush
assertEquals("Sizes differ", inData.length, baout.size());
assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
}
public void testCopy_readerToWriter() throws Exception {
InputStream in = new ByteArrayInputStream(inData);
in = new YellOnCloseInputStream(in);
Reader reader = new java.io.InputStreamReader(in, "US-ASCII");
ByteArrayOutputStream baout = new ByteArrayOutputStream();
OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
Writer writer = new java.io.OutputStreamWriter(baout, "US-ASCII");
int count = CopyUtils.copy(reader, writer);
writer.flush();
assertEquals(
"The number of characters returned by copy is wrong",
inData.length,
count);
assertEquals("Sizes differ", inData.length, baout.size());
assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
}
public void testCopy_stringToOutputStream() throws Exception {
final String str = new String(inData, "US-ASCII");
ByteArrayOutputStream baout = new ByteArrayOutputStream();
OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
CopyUtils.copy(str, out);
//Note: this method *does* flush. It is equivalent to:
// final OutputStreamWriter _out = new OutputStreamWriter(fout);
// IOUtils.copy( str, _out, 4096 ); // copy( Reader, Writer, int );
// _out.flush();
// out = fout;
// note: we don't flush here; this IOUtils method does it for us
assertEquals("Sizes differ", inData.length, baout.size());
assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
}
public void testCopy_stringToWriter() throws Exception {
final String str = new String(inData, "US-ASCII");
ByteArrayOutputStream baout = new ByteArrayOutputStream();
OutputStream out = new YellOnFlushAndCloseOutputStream(baout, false, true);
Writer writer = new java.io.OutputStreamWriter(baout, "US-ASCII");
CopyUtils.copy(str, writer);
writer.flush();
assertEquals("Sizes differ", inData.length, baout.size());
assertTrue("Content differs", Arrays.equals(inData, baout.toByteArray()));
}
} // CopyUtilsTest