You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2012/03/22 14:47:04 UTC

svn commit: r1303781 - in /commons/proper/vfs/trunk: core/src/test/java/org/apache/commons/vfs2/provider/ram/test/CustomRamProviderTest.java src/changes/changes.xml

Author: ggregory
Date: Thu Mar 22 13:47:03 2012
New Revision: 1303781

URL: http://svn.apache.org/viewvc?rev=1303781&view=rev
Log:
[VFS-407] reading a RAM FileSystem file fails because it never returns EOF -1.

Modified:
    commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/provider/ram/test/CustomRamProviderTest.java
    commons/proper/vfs/trunk/src/changes/changes.xml

Modified: commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/provider/ram/test/CustomRamProviderTest.java
URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/provider/ram/test/CustomRamProviderTest.java?rev=1303781&r1=1303780&r2=1303781&view=diff
==============================================================================
--- commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/provider/ram/test/CustomRamProviderTest.java (original)
+++ commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/provider/ram/test/CustomRamProviderTest.java Thu Mar 22 13:47:03 2012
@@ -17,10 +17,19 @@
 package org.apache.commons.vfs2.provider.ram.test;
 
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.fail;
 
+import java.io.Closeable;
+import java.io.IOException;
+import java.io.InputStream;
 import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
 
+import org.apache.commons.vfs2.FileContent;
 import org.apache.commons.vfs2.FileObject;
 import org.apache.commons.vfs2.FileSystemException;
 import org.apache.commons.vfs2.FileSystemOptions;
@@ -34,18 +43,53 @@ import org.junit.Test;
 /**
  * Custom tests
  *
- * @author edgar poce
- *
+ * @version $Id$
  */
 public class CustomRamProviderTest
 {
+    private static final byte[] NON_EMPTY_FILE_CONTENT = new byte[]{ 1, 2, 3 };
+
+    private List<Closeable> closeables = new ArrayList<Closeable>();
+
+    FileSystemOptions defaultRamFs = new FileSystemOptions();
+
     DefaultFileSystemManager manager;
 
+    FileSystemOptions smallSized = new FileSystemOptions();
+
     FileSystemOptions zeroSized = new FileSystemOptions();
 
-    FileSystemOptions smallSized = new FileSystemOptions();
+    /**
+     * Closes the given {@link Closeable} during the tearDown phase.
+     */
+    private <C extends Closeable> C closeOnTearDown(C closeable)
+    {
+        this.closeables.add(closeable);
+        return closeable;
+    }
 
-    FileSystemOptions defaultRamFs = new FileSystemOptions();
+    private InputStream createEmptyFile() throws FileSystemException, IOException
+    {
+        final FileObject root = manager.resolveFile("ram://file");
+        root.createFile();
+        return this.closeOnTearDown(root.getContent().getInputStream());
+    }
+
+    private InputStream createNonEmptyFile() throws FileSystemException, IOException
+    {
+        final FileObject root = manager.resolveFile("ram://file");
+        root.createFile();
+
+        final FileContent content = root.getContent();
+        final OutputStream output = this.closeOnTearDown(content.getOutputStream());
+        output.write(1);
+        output.write(2);
+        output.write(3);
+        output.flush();
+        output.close();
+
+        return this.closeOnTearDown(content.getInputStream());
+    }
 
     @Before
     public void setUp() throws Exception
@@ -62,26 +106,131 @@ public class CustomRamProviderTest
     @After
     public void tearDown() throws Exception
     {
+        for (Closeable closeable : this.closeables)
+        {
+            try
+            {
+                closeable.close();
+            } catch (final Exception e)
+            {
+                // ignore
+            }
+        }
         manager.close();
     }
 
     @Test
-    public void testSmallFS() throws Exception
+    public void testReadEmptyFileByteByByte() throws FileSystemException, IOException
+    {
+        final InputStream input = this.createEmptyFile();
+        assertEquals("Empty file didnt return EOF -1", -1, input.read());
+    }
+
+    @Test
+    public void testReadEmptyFileIntoBuffer() throws FileSystemException, IOException
+    {
+        final InputStream input = this.createEmptyFile();
+
+        final byte[] buffer = new byte[100];
+        assertEquals("Empty file didnt return when filling buffer", -1, input.read(buffer));
+        assertArrayEquals("Buffer was written too", new byte[100], buffer);
+    }
+
+    @Test
+    public void testReadEmptyFileIntoBufferWithOffsetAndLength() throws FileSystemException, IOException
+    {
+        final InputStream input = this.createEmptyFile();
+        final byte[] buffer = new byte[100];
+        assertEquals("Empty file didnt return when filling buffer", -1, input.read(buffer, 10, 90));
+        assertArrayEquals("Buffer was written too", new byte[100], buffer);
+    }
+
+    @Test
+    public void testReadNonEmptyFileByteByByte() throws FileSystemException, IOException
     {
+        final InputStream input = this.createNonEmptyFile();
 
+        assertEquals("Read 1st byte failed", 1, input.read());
+        assertEquals("Rread 2st byte failed", 2, input.read());
+        assertEquals("Read 3st byte failed", 3, input.read());
+        assertEquals("File should be empty", -1, input.read());
+    }
+
+    @Test
+    public void testReadNonEmptyFileIntoBuffer() throws FileSystemException, IOException
+    {
+        final InputStream input = this.createNonEmptyFile();
+
+        final byte[] buffer = new byte[100];
+        assertEquals("Filling buffer failed when file is not empty", NON_EMPTY_FILE_CONTENT.length, input.read(buffer));
+
+        final byte[] expectedBuffer = new byte[100];
+        System.arraycopy(NON_EMPTY_FILE_CONTENT, 0, expectedBuffer, 0, NON_EMPTY_FILE_CONTENT.length);
+        assertArrayEquals("Buffer not filled", expectedBuffer, buffer);
+
+        Arrays.fill(buffer, (byte) 0);
+        Arrays.fill(expectedBuffer, (byte) 0);
+
+        assertEquals("File should be empty after filling buffer", -1, input.read(buffer));
+        assertArrayEquals("Buffer was written when empty", expectedBuffer, buffer);
+    }
+
+    @Test
+    public void testReadNonEmptyFileIntoBufferWithOffsetAndLength() throws FileSystemException, IOException
+    {
+        final InputStream input = this.createNonEmptyFile();
+
+        final byte[] buffer = new byte[100];
+        final int offset = 10;
+        assertEquals("Filling buffer failed when file is not empty", NON_EMPTY_FILE_CONTENT.length,
+                input.read(buffer, offset, 100 - offset));
+
+        final byte[] expectedBuffer = new byte[100];
+        System.arraycopy(NON_EMPTY_FILE_CONTENT, 0, expectedBuffer, offset, NON_EMPTY_FILE_CONTENT.length);
+        assertArrayEquals("Buffer not filled", expectedBuffer, buffer);
+
+        Arrays.fill(buffer, (byte) 0);
+        Arrays.fill(expectedBuffer, (byte) 0);
+        assertEquals("File should be empty after filling buffer", -1, input.read(buffer, 10, 90));
+        assertArrayEquals("Buffer was written when empty", expectedBuffer, buffer);
+    }
+
+    /**
+     *
+     * Checks root folder exists
+     *
+     * @throws FileSystemException
+     */
+    @Test
+    public void testRootFolderExists() throws FileSystemException
+    {
+        FileObject root = manager.resolveFile("ram:///", defaultRamFs);
+        assertTrue(root.getType().hasChildren());
+
+        try
+        {
+            root.delete();
+            fail();
+        } catch (FileSystemException e)
+        {
+            // Expected
+        }
+
+    }
+
+    @Test
+    public void testSmallFS() throws Exception
+    {
         // Default FS
         FileObject fo1 = manager.resolveFile("ram:/");
         FileObject fo2 = manager.resolveFile("ram:/");
-        assertTrue("Both files should exist in the same fs instance.", fo1
-                .getFileSystem() == fo2.getFileSystem());
+        assertTrue("Both files should exist in the same fs instance.", fo1.getFileSystem() == fo2.getFileSystem());
 
         // Small FS
         FileObject fo3 = manager.resolveFile("ram:/fo3", smallSized);
         FileObject fo4 = manager.resolveFile("ram:/", smallSized);
-        assertTrue("Both files should exist in different fs instances.", fo3
-                .getFileSystem() == fo4.getFileSystem());
-        assertTrue("These file shouldn't be in the same file system.", fo1
-                .getFileSystem() != fo3.getFileSystem());
+        assertTrue("Both files should exist in different fs instances.", fo3.getFileSystem() == fo4.getFileSystem());
+        assertTrue("These file shouldn't be in the same file system.", fo1.getFileSystem() != fo3.getFileSystem());
 
         fo3.createFile();
         try
@@ -89,8 +238,7 @@ public class CustomRamProviderTest
             OutputStream os = fo3.getContent().getOutputStream();
             os.write(new byte[10]);
             os.close();
-        }
-        catch (FileSystemException e)
+        } catch (FileSystemException e)
         {
             fail("It shouldn't save such a small file");
         }
@@ -101,33 +249,10 @@ public class CustomRamProviderTest
             os.write(new byte[11]);
             os.close();
             fail("It shouldn't save such a big file");
-        }
-        catch (FileSystemException e)
+        } catch (FileSystemException e)
         {
-            // exception awaited
+            // Expected
         }
 
     }
-
-    /**
-     *
-     * Checks root folder exists
-     *
-     * @throws FileSystemException
-     */
-    @Test
-    public void testRootFolderExists() throws FileSystemException {
-        FileObject root = manager.resolveFile("ram:///", defaultRamFs);
-        assertTrue(root.getType().hasChildren());
-
-        try {
-            root.delete();
-            fail();
-        } catch (FileSystemException e) {
-
-        }
-
-    }
-
-
 }

Modified: commons/proper/vfs/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/src/changes/changes.xml?rev=1303781&r1=1303780&r2=1303781&view=diff
==============================================================================
--- commons/proper/vfs/trunk/src/changes/changes.xml (original)
+++ commons/proper/vfs/trunk/src/changes/changes.xml Thu Mar 22 13:47:03 2012
@@ -23,6 +23,9 @@
 
   <body>
     <release version="2.1" date="TBD" description="">
+      <action issue="VFS-407" dev="ggregory" type="fix" due-to="mp1">
+        [RAM] Reading a RAM FileSystem file fails because it never returns EOF -1.
+      </action>
       <action issue="VFS-404" dev="ggregory" type="update">
         [FTP][FTPS] Update Apache Commons Net to 3.1 from 3.0.1
       </action>