jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From thom...@apache.org
Subject svn commit: r1153105 - in /jackrabbit/sandbox/microkernel/src: main/java/org/apache/jackrabbit/mk/mem/MemoryDataStore.java main/java/org/apache/jackrabbit/mk/util/MicroKernelInputStream.java test/java/org/apache/jackrabbit/mk/DataStoreTest.java
Date Tue, 02 Aug 2011 12:01:09 GMT
Author: thomasm
Date: Tue Aug  2 12:01:09 2011
New Revision: 1153105

URL: http://svn.apache.org/viewvc?rev=1153105&view=rev
Log:
Improved data store tests, bug in memory data store.

Modified:
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/MemoryDataStore.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/MicroKernelInputStream.java
    jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/DataStoreTest.java

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/MemoryDataStore.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/MemoryDataStore.java?rev=1153105&r1=1153104&r2=1153105&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/MemoryDataStore.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/MemoryDataStore.java
Tue Aug  2 12:01:09 2011
@@ -62,6 +62,9 @@ public class MemoryDataStore {
     }
 
     public int read(String blobId, long pos, byte[] buff, int off, int length) {
+        if (length == 0) {
+            return 0;
+        }
         int end = off + length;
         int i = (int) pos * 2 + PREFIX.length();
         int count = 0;
@@ -70,7 +73,7 @@ public class MemoryDataStore {
             buff[off++] = (byte) x;
             count++;
         }
-        return count;
+        return count == 0 ? -1 : count;
     }
 
 }

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/MicroKernelInputStream.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/MicroKernelInputStream.java?rev=1153105&r1=1153104&r2=1153105&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/MicroKernelInputStream.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/MicroKernelInputStream.java
Tue Aug  2 12:01:09 2011
@@ -30,7 +30,7 @@ public class MicroKernelInputStream exte
     private long pos;
     private byte[] oneByteBuff;
 
-    MicroKernelInputStream(MicroKernel mk, String id) {
+    public MicroKernelInputStream(MicroKernel mk, String id) {
         this.mk = mk;
         this.id = id;
     }

Modified: jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/DataStoreTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/DataStoreTest.java?rev=1153105&r1=1153104&r2=1153105&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/DataStoreTest.java
(original)
+++ jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/DataStoreTest.java
Tue Aug  2 12:01:09 2011
@@ -17,6 +17,9 @@
 package org.apache.jackrabbit.mk;
 
 import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
 import java.util.Random;
 import junit.framework.TestCase;
 import org.apache.jackrabbit.mk.api.MicroKernel;
@@ -27,7 +30,7 @@ import org.apache.jackrabbit.mk.util.Mic
  */
 public class DataStoreTest extends TestCase {
 
-    // private static final String URL = "fs:{homeDir};clean";
+    // private static final String URL = "fs:{homeDir}/target;clean";
     private static final String URL = "mem:";
 
     private MicroKernel mk;
@@ -40,19 +43,19 @@ public class DataStoreTest extends TestC
         mk.dispose();
     }
 
-    public void testSmall() {
+    public void testSmall() throws IOException {
         doTest(10, 10);
     }
 
-    public void testMedium() {
+    public void testMedium() throws IOException {
         doTest(1000, 10);
     }
 
-    public void testLarge() {
+    public void testLarge() throws IOException {
         doTest(100000, 1);
     }
 
-    private void doTest(int maxLength, int count) {
+    private void doTest(int maxLength, int count) throws IOException {
         String[] s = new String[count * 2];
         Random r = new Random(0);
         for (int i = 0; i < s.length;) {
@@ -67,12 +70,60 @@ public class DataStoreTest extends TestC
             byte[] expectedData = new byte[expectedLen];
             r.nextBytes(expectedData);
             assertEquals(expectedLen, mk.getLength(s[i++]));
-            byte[] b = MicroKernelInputStream.readFully(mk, s[i++]);
-            assertEquals(expectedLen, b.length);
-            for (int j = 0; j < expectedData.length; j++) {
-                assertEquals(expectedData[j], b[j]);
+
+            String id = s[i++];
+            doTestReadFully(expectedData, expectedLen, id);
+            doTestRead(expectedData, expectedLen, id);
+        }
+    }
+
+    private void doTestReadFully(byte[] expectedData, int expectedLen, String id) {
+        byte[] got = MicroKernelInputStream.readFully(mk, id);
+        assertByteArrayEquals(expectedData, expectedLen, got);
+    }
+
+    private void assertByteArrayEquals(byte[] expected, int expectedLen, byte[] got) {
+        if (expectedLen != got.length) {
+            System.out.println("?");
+        }
+        assertEquals(expectedLen, got.length);
+        for (int j = 0; j < expectedLen; j++) {
+            assertEquals(expected[j], got[j]);
+        }
+    }
+
+    private void doTestRead(byte[] expectedData, int expectedLen, String id) throws IOException
{
+        InputStream in = new MicroKernelInputStream(mk, id);
+        Random r = new Random(1);
+        ByteArrayOutputStream buff = new ByteArrayOutputStream();
+        while (true) {
+            int op = r.nextInt(3);
+            if (op == 0) {
+                int x = in.read();
+                if (x < 0) {
+                    break;
+                }
+                buff.write(x);
+            } else if (op == 1) {
+                byte[] x = new byte[r.nextInt(100)];
+                int l = in.read(x);
+                if (l < 0) {
+                    break;
+                }
+                buff.write(x, 0, l);
+            } else {
+                int offset = r.nextInt(10);
+                int len = r.nextInt(20);
+                byte[] x = new byte[offset + len];
+                int l = in.read(x, offset, len);
+                if (l < 0) {
+                    break;
+                }
+                buff.write(x, offset, l);
             }
         }
+        byte[] got = buff.toByteArray();
+        assertByteArrayEquals(expectedData, expectedLen, got);
     }
 
 }



Mime
View raw message