You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cassandra.apache.org by jb...@apache.org on 2010/06/24 17:12:34 UTC

svn commit: r957578 - /cassandra/trunk/test/unit/org/apache/cassandra/io/util/BufferedRandomAccessFileTest.java

Author: jbellis
Date: Thu Jun 24 15:12:34 2010
New Revision: 957578

URL: http://svn.apache.org/viewvc?rev=957578&view=rev
Log:
add test for BRAF EOFException.  patch by mdennis; reviewed by jbellis for CASSANDRA-1200

Modified:
    cassandra/trunk/test/unit/org/apache/cassandra/io/util/BufferedRandomAccessFileTest.java

Modified: cassandra/trunk/test/unit/org/apache/cassandra/io/util/BufferedRandomAccessFileTest.java
URL: http://svn.apache.org/viewvc/cassandra/trunk/test/unit/org/apache/cassandra/io/util/BufferedRandomAccessFileTest.java?rev=957578&r1=957577&r2=957578&view=diff
==============================================================================
--- cassandra/trunk/test/unit/org/apache/cassandra/io/util/BufferedRandomAccessFileTest.java (original)
+++ cassandra/trunk/test/unit/org/apache/cassandra/io/util/BufferedRandomAccessFileTest.java Thu Jun 24 15:12:34 2010
@@ -21,13 +21,16 @@ package org.apache.cassandra.io.util;
  */
 
 
-import static org.junit.Assert.*;
-
+import java.io.EOFException;
 import java.io.File;
+import java.io.FileOutputStream;
 import java.io.IOException;
+import java.util.Arrays;
 
 import org.junit.Test;
 
+import static org.junit.Assert.assertEquals;
+
 public class BufferedRandomAccessFileTest
 {
 
@@ -66,4 +69,45 @@ public class BufferedRandomAccessFileTes
         r.close();
     }
 
+    protected void expectException(int size, int offset, int len, BufferedRandomAccessFile braf)
+    {
+        boolean threw = false;
+        try
+        {
+            braf.readFully(new byte[size], offset, len);
+        }
+        catch(Throwable t)
+        {
+            assert t.getClass().equals(EOFException.class) : t.getClass().getName() + " is not " + EOFException.class.getName();
+            threw = true;
+        }
+        assert threw : EOFException.class.getName() + " not received";
+    }
+
+    @Test
+    public void testEOF() throws Exception
+    {
+        for (String mode : Arrays.asList("r", "rw")) // read, read+write
+        {
+            for (int buf : Arrays.asList(8, 16, 32, 0))  // smaller, equal, bigger, zero
+            {
+                for (int off : Arrays.asList(0, 8))
+                {
+                    expectException(32, off, 17, new BufferedRandomAccessFile(writeTemporaryFile(new byte[16]), mode, buf));
+                }
+            }
+        }
+    }
+
+    protected File writeTemporaryFile(byte[] data) throws Exception
+    {
+        File f = File.createTempFile("BRAFTestFile", null);
+        f.deleteOnExit();
+        FileOutputStream fout = new FileOutputStream(f);
+        fout.write(data);
+        fout.getFD().sync();
+        fout.close();
+        return f;
+    }
+
 }