[Mulgara-svn] r1965 - trunk/src/jar/util/java/org/mulgara/util/io

pag at mulgara.org pag at mulgara.org
Sat Jul 3 02:14:59 UTC 2010


Author: pag
Date: 2010-07-03 02:14:59 +0000 (Sat, 03 Jul 2010)
New Revision: 1965

Added:
   trunk/src/jar/util/java/org/mulgara/util/io/LBufferedFileTest.java
   trunk/src/jar/util/java/org/mulgara/util/io/LIOBufferedFileTest.java
   trunk/src/jar/util/java/org/mulgara/util/io/LMappedBufferedFileTest.java
Log:
Full tests for read only file access

Added: trunk/src/jar/util/java/org/mulgara/util/io/LBufferedFileTest.java
===================================================================
--- trunk/src/jar/util/java/org/mulgara/util/io/LBufferedFileTest.java	                        (rev 0)
+++ trunk/src/jar/util/java/org/mulgara/util/io/LBufferedFileTest.java	2010-07-03 02:14:59 UTC (rev 1965)
@@ -0,0 +1,146 @@
+/**
+ * 
+ */
+package org.mulgara.util.io;
+
+import junit.framework.*;        // JUnit
+
+
+import java.io.File;
+import java.io.IOException;
+import java.io.RandomAccessFile;
+import java.nio.ByteBuffer;
+
+/**
+ * @author pag
+ *
+ */
+public abstract class LBufferedFileTest extends TestCase {
+
+  File f = new File("testFile");
+
+  RandomAccessFile braf;
+
+  RandomAccessFile raf;
+
+  LBufferedFile file;
+
+  /**
+   * Empty test suite to prevent the test runner from trying to instantiate
+   * an abstract class.
+   * @return An empty test suite.
+   */
+  public static Test suite() {
+    return new TestSuite();
+  }
+
+  /**
+   * @throws java.lang.Exception
+   */
+  public void setUp() throws Exception {
+    raf = new RandomAccessFile(f, "rw");
+    byte[] data = new byte[19];
+    for (byte i = 0; i < data.length; i++) data[i] = i;
+
+    raf.write(data);
+    System.setProperty(LMappedBufferedFile.PAGE_SIZE_PROP, "4");
+    braf = new RandomAccessFile(f, "r");
+    file = newFile(braf);
+  }
+
+  /**
+   * @throws java.lang.Exception
+   */
+  public void tearDown() throws Exception {
+    raf.close();
+    f.delete();
+  }
+
+  abstract LBufferedFile newFile(RandomAccessFile r) throws IOException;
+  
+  abstract boolean readOnly();
+  
+  /**
+   * Test method for {@link org.mulgara.util.io.LBufferedFile#read(long, int)}.
+   */
+  public void testRead() throws Exception {
+    // basic read of a whole block
+    ByteBuffer bb = file.read(0, 4);
+    assertArrEquals(new byte[] { 0, 1, 2, 3 }, bb);
+
+    // across boundaries
+    bb = file.read(2, 4);
+    assertArrEquals(new byte[] { 2, 3, 4, 5 }, bb);
+    
+    // read from the partial block
+    bb = file.read(16, 3);
+    assertArrEquals(new byte[] { 16, 17, 18 }, bb);
+
+    // across boundaries into the partial block
+    bb = file.read(14, 5);
+    assertArrEquals(new byte[] { 14, 15, 16, 17, 18 }, bb);
+    
+    // append to the file, and read the new data
+    raf.write(new byte[] { 19, 20, 21, 22 });
+
+    bb = file.read(20, 2);
+    assertArrEquals(new byte[] { 20, 21 }, bb);
+
+    // read the previous partial block
+    bb = file.read(15, 4);
+    assertArrEquals(new byte[] { 15, 16, 17, 18 }, bb);
+
+    // read across boundaries between the original block and the new block
+    bb = file.read(13, 6);
+    assertArrEquals(new byte[] { 13, 14, 15, 16, 17, 18 }, bb);
+
+    // append to the file, making the final block full
+    raf.write(new byte[] { 23 });
+
+    bb = file.read(20, 4);
+    assertArrEquals(new byte[] { 20, 21, 22, 23 }, bb);
+
+    // append to the file again, making the final block partial
+    raf.write(new byte[] { 24, 25 });
+
+    bb = file.read(22, 4);
+    assertArrEquals(new byte[] { 22, 23, 24, 25 }, bb);
+
+  }
+
+  /**
+   * Test method for {@link org.mulgara.util.io.LBufferedFile#allocate(long, int)}.
+   */
+  public void testAllocateLongInt() {
+    if (!readOnly()) {
+      // test
+    }
+  }
+
+  /**
+   * Test method for {@link org.mulgara.util.io.LBufferedFile#write(java.nio.ByteBuffer)}.
+   */
+  public void testWrite() {
+    if (!readOnly()) {
+      // test
+    }
+  }
+
+  /**
+   * Test method for {@link org.mulgara.util.io.LBufferedFile#seek(long)}.
+   */
+  public void testSeek() {
+    if (!readOnly()) {
+      // test
+    }
+  }
+
+  static void assertArrEquals(byte[] expected, ByteBuffer actual) {
+    if (expected.length != actual.limit()) throw new AssertionError("Arrays differ in length. Expected: " + expected.length + ", Actual: " + actual.limit());
+    for (int i = 0; i < expected.length; i++) {
+      byte e = expected[i];
+      byte a = actual.get(i);
+      assertEquals("Data differs at position " + i + ", expected: " + e + ", got: " + a, e, a);
+    }
+  }
+}

Added: trunk/src/jar/util/java/org/mulgara/util/io/LIOBufferedFileTest.java
===================================================================
--- trunk/src/jar/util/java/org/mulgara/util/io/LIOBufferedFileTest.java	                        (rev 0)
+++ trunk/src/jar/util/java/org/mulgara/util/io/LIOBufferedFileTest.java	2010-07-03 02:14:59 UTC (rev 1965)
@@ -0,0 +1,30 @@
+/**
+ * 
+ */
+package org.mulgara.util.io;
+
+import java.io.IOException;
+import java.io.RandomAccessFile;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+/**
+ * @author pag
+ *
+ */
+public class LIOBufferedFileTest extends LBufferedFileTest {
+
+  public static Test suite() {
+    return new TestSuite(LIOBufferedFileTest.class);
+  }
+
+
+  LBufferedFile newFile(RandomAccessFile r) throws IOException {
+    return new LReadOnlyIOBufferedFile(r);
+  }
+  
+  boolean readOnly() {
+    return true;
+  }
+}

Added: trunk/src/jar/util/java/org/mulgara/util/io/LMappedBufferedFileTest.java
===================================================================
--- trunk/src/jar/util/java/org/mulgara/util/io/LMappedBufferedFileTest.java	                        (rev 0)
+++ trunk/src/jar/util/java/org/mulgara/util/io/LMappedBufferedFileTest.java	2010-07-03 02:14:59 UTC (rev 1965)
@@ -0,0 +1,29 @@
+/**
+ * 
+ */
+package org.mulgara.util.io;
+
+import java.io.IOException;
+import java.io.RandomAccessFile;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+/**
+ * @author pag
+ *
+ */
+public class LMappedBufferedFileTest extends LBufferedFileTest {
+
+  public static Test suite() {
+    return new TestSuite(LMappedBufferedFileTest.class);
+  }
+
+  LBufferedFile newFile(RandomAccessFile r) throws IOException {
+    return new LMappedBufferedFile(r);
+  }
+  
+  boolean readOnly() {
+    return true;
+  }
+}



More information about the Mulgara-svn mailing list