You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ad...@apache.org on 2003/01/21 03:42:29 UTC
cvs commit: jakarta-commons-sandbox/vfs/src/test/org/apache/commons/vfs/test ProviderReadTests.java AbstractProviderTestCase.java
adammurdoch 2003/01/20 18:42:29
Modified: vfs/src/test/org/apache/commons/vfs/test
ProviderReadTests.java
AbstractProviderTestCase.java
Log:
Moved ProviderReadTests.assertSameContent() up to AbstractProviderTestCase, so it is available
to other tests.
Revision Changes Path
1.5 +7 -54 jakarta-commons-sandbox/vfs/src/test/org/apache/commons/vfs/test/ProviderReadTests.java
Index: ProviderReadTests.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/test/org/apache/commons/vfs/test/ProviderReadTests.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- ProviderReadTests.java 25 Nov 2002 05:51:15 -0000 1.4
+++ ProviderReadTests.java 21 Jan 2003 02:42:29 -0000 1.5
@@ -55,10 +55,7 @@
*/
package org.apache.commons.vfs.test;
-import java.io.ByteArrayOutputStream;
-import java.io.InputStream;
import java.util.ArrayList;
-import java.util.Arrays;
import java.util.List;
import org.apache.commons.vfs.FileContent;
import org.apache.commons.vfs.FileName;
@@ -725,52 +722,11 @@
{
// Test non-empty file
FileObject file = getReadFolder().resolveFile( "file1.txt" );
- FileContent content = file.getContent();
- assertSameContent( FILE1_CONTENT, content );
+ assertSameContent( FILE1_CONTENT, file );
// Test empty file
file = getReadFolder().resolveFile( "empty.txt" );
- content = file.getContent();
- assertSameContent( "", content );
- }
-
- /**
- * Asserts that the content of a file is the same as expected. Checks the
- * length reported by getSize() is correct, then reads the content as
- * a byte stream and compares the result with the expected content.
- * Assumes files are encoded using UTF-8.
- */
- protected void assertSameContent( final String expected,
- final FileContent content )
- throws Exception
- {
- // Get file content as a binary stream
- final byte[] expectedBin = expected.getBytes( "utf-8" );
-
- // Check lengths
- assertEquals( "same content length", expectedBin.length, content.getSize() );
-
- // Read content into byte array
- final InputStream instr = content.getInputStream();
- final ByteArrayOutputStream outstr;
- try
- {
- outstr = new ByteArrayOutputStream();
- final byte[] buffer = new byte[ 256 ];
- int nread = 0;
- while ( nread >= 0 )
- {
- outstr.write( buffer, 0, nread );
- nread = instr.read( buffer );
- }
- }
- finally
- {
- instr.close();
- }
-
- // Compare
- assertTrue( "same binary content", Arrays.equals( expectedBin, outstr.toByteArray() ) );
+ assertSameContent( "", file );
}
/**
@@ -823,20 +779,17 @@
assertEquals( FileType.FILE, file.getType() );
// Get the file content
- FileContent content = file.getContent();
- assertSameContent( FILE1_CONTENT, content );
+ assertSameContent( FILE1_CONTENT, file );
// Read the content again
- content = file.getContent();
- assertSameContent( FILE1_CONTENT, content );
+ assertSameContent( FILE1_CONTENT, file );
// Close the content + file
- content.close();
+ file.getContent().close();
file.close();
// Read the content again
- content = file.getContent();
- assertSameContent( FILE1_CONTENT, content );
+ assertSameContent( FILE1_CONTENT, file );
}
/**
1.5 +47 -1 jakarta-commons-sandbox/vfs/src/test/org/apache/commons/vfs/test/AbstractProviderTestCase.java
Index: AbstractProviderTestCase.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/test/org/apache/commons/vfs/test/AbstractProviderTestCase.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- AbstractProviderTestCase.java 25 Nov 2002 05:46:18 -0000 1.4
+++ AbstractProviderTestCase.java 21 Jan 2003 02:42:29 -0000 1.5
@@ -66,6 +66,8 @@
import org.apache.commons.vfs.Capability;
import org.apache.commons.vfs.FileName;
import org.apache.commons.vfs.FileObject;
+import org.apache.commons.vfs.FileContent;
+import org.apache.commons.vfs.FileType;
import org.apache.commons.vfs.impl.DefaultFileReplicator;
import org.apache.commons.vfs.impl.DefaultFileSystemManager;
import org.apache.commons.vfs.impl.PrivilegedFileReplicator;
@@ -251,6 +253,50 @@
// Read content into byte array
final InputStream instr = connection.getInputStream();
+ final ByteArrayOutputStream outstr;
+ try
+ {
+ outstr = new ByteArrayOutputStream();
+ final byte[] buffer = new byte[ 256 ];
+ int nread = 0;
+ while ( nread >= 0 )
+ {
+ outstr.write( buffer, 0, nread );
+ nread = instr.read( buffer );
+ }
+ }
+ finally
+ {
+ instr.close();
+ }
+
+ // Compare
+ assertTrue( "same binary content", Arrays.equals( expectedBin, outstr.toByteArray() ) );
+ }
+
+ /**
+ * Asserts that the content of a file is the same as expected. Checks the
+ * length reported by getSize() is correct, then reads the content as
+ * a byte stream and compares the result with the expected content.
+ * Assumes files are encoded using UTF-8.
+ */
+ protected void assertSameContent( final String expected,
+ final FileObject file )
+ throws Exception
+ {
+ // Check the file exists, and is a file
+ assertTrue( file.exists() );
+ assertSame( FileType.FILE, file.getType() );
+
+ // Get file content as a binary stream
+ final byte[] expectedBin = expected.getBytes( "utf-8" );
+
+ // Check lengths
+ final FileContent content = file.getContent();
+ assertEquals( "same content length", expectedBin.length, content.getSize() );
+
+ // Read content into byte array
+ final InputStream instr = content.getInputStream();
final ByteArrayOutputStream outstr;
try
{
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>