Author: frm
Date: Thu Aug 10 07:24:54 2017
New Revision: 1804638

URL: http://svn.apache.org/viewvc?rev=1804638&view=rev
Log:
OAK-6528 - Add tests for reading and writing the binary references index

Added:
    
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/binaries/
    
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/binaries/BinaryReferencesIndexLoaderTest.java
   (with props)
    
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/binaries/BinaryReferencesIndexLoaderV1Test.java
   (with props)
    
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/binaries/BinaryReferencesIndexLoaderV2Test.java
   (with props)
    
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/binaries/BinaryReferencesIndexWriterTest.java
   (with props)
Modified:
    
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/binaries/BinaryReferencesIndexLoaderV1.java
    
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/binaries/BinaryReferencesIndexLoaderV2.java
    
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/binaries/Generation.java

Modified: 
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/binaries/BinaryReferencesIndexLoaderV1.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/binaries/BinaryReferencesIndexLoaderV1.java?rev=1804638&r1=1804637&r2=1804638&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/binaries/BinaryReferencesIndexLoaderV1.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/binaries/BinaryReferencesIndexLoaderV1.java
 Thu Aug 10 07:24:54 2017
@@ -33,7 +33,7 @@ class BinaryReferencesIndexLoaderV1 {
 
     static final int MAGIC = ('\n' << 24) + ('0' << 16) + ('B' << 8) + '\n';
 
-    private static final int FOOTER_SIZE = 16;
+    static final int FOOTER_SIZE = 16;
 
     static BinaryReferencesIndex loadBinaryReferencesIndex(ReaderAtEnd reader) 
throws IOException, InvalidBinaryReferencesIndexException {
         ByteBuffer meta = reader.readAtEnd(FOOTER_SIZE, FOOTER_SIZE);
@@ -56,11 +56,9 @@ class BinaryReferencesIndexLoaderV1 {
         ByteBuffer buffer = reader.readAtEnd(size, size - FOOTER_SIZE);
 
         CRC32 checksum = new CRC32();
-        byte[] data = new byte[size - FOOTER_SIZE];
         buffer.mark();
-        buffer.get(data);
+        checksum.update(buffer);
         buffer.reset();
-        checksum.update(data);
 
         if ((int) (checksum.getValue()) != crc32) {
             throw new InvalidBinaryReferencesIndexException("Invalid 
checksum");

Modified: 
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/binaries/BinaryReferencesIndexLoaderV2.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/binaries/BinaryReferencesIndexLoaderV2.java?rev=1804638&r1=1804637&r2=1804638&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/binaries/BinaryReferencesIndexLoaderV2.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/binaries/BinaryReferencesIndexLoaderV2.java
 Thu Aug 10 07:24:54 2017
@@ -33,7 +33,7 @@ class BinaryReferencesIndexLoaderV2 {
 
     static final int MAGIC = ('\n' << 24) + ('1' << 16) + ('B' << 8) + '\n';
 
-    private static final int FOOTER_SIZE = 16;
+    static final int FOOTER_SIZE = 16;
 
     static BinaryReferencesIndex loadBinaryReferencesIndex(ReaderAtEnd reader) 
throws IOException, InvalidBinaryReferencesIndexException {
         ByteBuffer meta = reader.readAtEnd(FOOTER_SIZE, FOOTER_SIZE);

Modified: 
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/binaries/Generation.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/binaries/Generation.java?rev=1804638&r1=1804637&r2=1804638&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/binaries/Generation.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-segment-tar/src/main/java/org/apache/jackrabbit/oak/segment/file/tar/binaries/Generation.java
 Thu Aug 10 07:24:54 2017
@@ -56,4 +56,14 @@ class Generation {
         return Objects.hash(generation, full, compacted);
     }
 
+    @Override
+    public String toString() {
+        return String.format(
+            "Generation{generation=%d, full=%d, compacted=%s}",
+            generation,
+            full,
+            compacted
+        );
+    }
+
 }

Added: 
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/binaries/BinaryReferencesIndexLoaderTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/binaries/BinaryReferencesIndexLoaderTest.java?rev=1804638&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/binaries/BinaryReferencesIndexLoaderTest.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/binaries/BinaryReferencesIndexLoaderTest.java
 Thu Aug 10 07:24:54 2017
@@ -0,0 +1,216 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.jackrabbit.oak.segment.file.tar.binaries;
+
+import static java.util.Arrays.asList;
+import static org.junit.Assert.assertEquals;
+
+import java.nio.ByteBuffer;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.UUID;
+import java.util.zip.CRC32;
+
+import com.google.common.base.Charsets;
+import org.junit.Test;
+
+public class BinaryReferencesIndexLoaderTest {
+
+    private static int length(String s) {
+        return bytes(s).length;
+    }
+
+    private static byte[] bytes(String s) {
+        return s.getBytes(Charsets.UTF_8);
+    }
+
+    private static int checksum(ByteBuffer buffer) {
+        CRC32 checksum = new CRC32();
+        int position = buffer.position();
+        checksum.update(buffer);
+        buffer.position(position);
+        return (int) checksum.getValue();
+    }
+
+    private static BinaryReferencesIndex loadIndex(ByteBuffer buffer) throws 
Exception {
+        return BinaryReferencesIndexLoader.loadBinaryReferencesIndex((whence, 
length) -> {
+            ByteBuffer slice = buffer.duplicate();
+            slice.position(slice.limit() - whence);
+            slice.limit(slice.position() + length);
+            return slice.slice();
+        });
+    }
+
+    @Test(expected = InvalidBinaryReferencesIndexException.class)
+    public void testUnrecognizedMagicNumber() throws Exception {
+        ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES);
+        try {
+            loadIndex(buffer);
+        } catch (InvalidBinaryReferencesIndexException e) {
+            assertEquals("Unrecognized magic number", e.getMessage());
+            throw e;
+        }
+    }
+
+    @Test
+    public void testLoadV1() throws Exception {
+        ByteBuffer entries = ByteBuffer.allocate(512)
+            // First generation
+            .putInt(1)
+            .putInt(2)
+            // First generation, first segment
+            .putLong(1).putLong(1)
+            .putInt(2)
+            .putInt(length("1.1.1")).put(bytes("1.1.1"))
+            .putInt(length("1.1.2")).put(bytes("1.1.2"))
+            // First generation, second segment
+            .putLong(1).putLong(2)
+            .putInt(2)
+            .putInt(length("1.2.1")).put(bytes("1.2.1"))
+            .putInt(length("1.2.2")).put(bytes("1.2.2"))
+            // Second generation
+            .putInt(2)
+            .putInt(2)
+            // Second generation, second segment
+            .putLong(2).putLong(1)
+            .putInt(2)
+            .putInt(length("2.1.1")).put(bytes("2.1.1"))
+            .putInt(length("2.1.2")).put(bytes("2.1.2"))
+            // Second generation, second segment
+            .putLong(2).putLong(2)
+            .putInt(2)
+            .putInt(length("2.2.1")).put(bytes("2.2.1"))
+            .putInt(length("2.2.2")).put(bytes("2.2.2"));
+        entries.flip();
+
+        ByteBuffer buffer = ByteBuffer.allocate(entries.remaining() + 
BinaryReferencesIndexLoaderV1.FOOTER_SIZE);
+        buffer.duplicate()
+            .put(entries.duplicate())
+            .putInt(checksum(entries))
+            .putInt(2)
+            .putInt(entries.remaining() + 
BinaryReferencesIndexLoaderV1.FOOTER_SIZE)
+            .putInt(BinaryReferencesIndexLoaderV1.MAGIC);
+
+        BinaryReferencesIndex index = loadIndex(buffer);
+
+        Generation g1 = new Generation(1, 0, false);
+        Generation g2 = new Generation(2, 0, false);
+
+        UUID s1 = new UUID(1, 1);
+        UUID s2 = new UUID(1, 2);
+        UUID s3 = new UUID(2, 1);
+        UUID s4 = new UUID(2, 2);
+
+        Map<Generation, Map<UUID, Set<String>>> expected = new HashMap<>();
+        expected.put(g1, new HashMap<>());
+        expected.put(g2, new HashMap<>());
+        expected.get(g1).put(s1, new HashSet<>());
+        expected.get(g1).put(s2, new HashSet<>());
+        expected.get(g2).put(s3, new HashSet<>());
+        expected.get(g2).put(s4, new HashSet<>());
+        expected.get(g1).get(s1).addAll(asList("1.1.1", "1.1.2"));
+        expected.get(g1).get(s2).addAll(asList("1.2.1", "1.2.2"));
+        expected.get(g2).get(s3).addAll(asList("2.1.1", "2.1.2"));
+        expected.get(g2).get(s4).addAll(asList("2.2.1", "2.2.2"));
+
+        Map<Generation, Map<UUID, Set<String>>> actual = new HashMap<>();
+        index.forEach((generation, full, compacted, id, reference) -> {
+            actual
+                .computeIfAbsent(new Generation(generation, full, compacted), 
k -> new HashMap<>())
+                .computeIfAbsent(id, k -> new HashSet<>())
+                .add(reference);
+        });
+
+        assertEquals(expected, actual);
+    }
+
+    @Test
+    public void testLoadV2() throws Exception {
+        ByteBuffer entries = ByteBuffer.allocate(512)
+            // First generation
+            .putInt(1).putInt(2).put((byte) 0)
+            .putInt(2)
+            // First generation, first segment
+            .putLong(1).putLong(1)
+            .putInt(2)
+            .putInt(length("1.1.1")).put(bytes("1.1.1"))
+            .putInt(length("1.1.2")).put(bytes("1.1.2"))
+            // First generation, second segment
+            .putLong(1).putLong(2)
+            .putInt(2)
+            .putInt(length("1.2.1")).put(bytes("1.2.1"))
+            .putInt(length("1.2.2")).put(bytes("1.2.2"))
+            // Second generation
+            .putInt(3).putInt(4).put((byte) 1)
+            .putInt(2)
+            // Second generation, second segment
+            .putLong(2).putLong(1)
+            .putInt(2)
+            .putInt(length("2.1.1")).put(bytes("2.1.1"))
+            .putInt(length("2.1.2")).put(bytes("2.1.2"))
+            // Second generation, second segment
+            .putLong(2).putLong(2)
+            .putInt(2)
+            .putInt(length("2.2.1")).put(bytes("2.2.1"))
+            .putInt(length("2.2.2")).put(bytes("2.2.2"));
+        entries.flip();
+
+        ByteBuffer buffer = ByteBuffer.allocate(entries.remaining() + 
BinaryReferencesIndexLoaderV2.FOOTER_SIZE);
+        buffer.duplicate()
+            .put(entries.duplicate())
+            .putInt(checksum(entries))
+            .putInt(2)
+            .putInt(entries.remaining() + 
BinaryReferencesIndexLoaderV2.FOOTER_SIZE)
+            .putInt(BinaryReferencesIndexLoaderV2.MAGIC);
+
+        BinaryReferencesIndex index = loadIndex(buffer);
+
+        Generation g1 = new Generation(1, 2, false);
+        Generation g2 = new Generation(3, 4, true);
+
+        UUID s1 = new UUID(1, 1);
+        UUID s2 = new UUID(1, 2);
+        UUID s3 = new UUID(2, 1);
+        UUID s4 = new UUID(2, 2);
+
+        Map<Generation, Map<UUID, Set<String>>> expected = new HashMap<>();
+        expected.put(g1, new HashMap<>());
+        expected.put(g2, new HashMap<>());
+        expected.get(g1).put(s1, new HashSet<>());
+        expected.get(g1).put(s2, new HashSet<>());
+        expected.get(g2).put(s3, new HashSet<>());
+        expected.get(g2).put(s4, new HashSet<>());
+        expected.get(g1).get(s1).addAll(asList("1.1.1", "1.1.2"));
+        expected.get(g1).get(s2).addAll(asList("1.2.1", "1.2.2"));
+        expected.get(g2).get(s3).addAll(asList("2.1.1", "2.1.2"));
+        expected.get(g2).get(s4).addAll(asList("2.2.1", "2.2.2"));
+
+        Map<Generation, Map<UUID, Set<String>>> actual = new HashMap<>();
+        index.forEach((generation, full, compacted, id, reference) -> {
+            actual
+                .computeIfAbsent(new Generation(generation, full, compacted), 
k -> new HashMap<>())
+                .computeIfAbsent(id, k -> new HashSet<>())
+                .add(reference);
+        });
+
+        assertEquals(expected, actual);
+    }
+
+}

Propchange: 
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/binaries/BinaryReferencesIndexLoaderTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/binaries/BinaryReferencesIndexLoaderV1Test.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/binaries/BinaryReferencesIndexLoaderV1Test.java?rev=1804638&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/binaries/BinaryReferencesIndexLoaderV1Test.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/binaries/BinaryReferencesIndexLoaderV1Test.java
 Thu Aug 10 07:24:54 2017
@@ -0,0 +1,179 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.jackrabbit.oak.segment.file.tar.binaries;
+
+import static 
org.apache.jackrabbit.oak.segment.file.tar.binaries.BinaryReferencesIndexLoaderV1.FOOTER_SIZE;
+import static 
org.apache.jackrabbit.oak.segment.file.tar.binaries.BinaryReferencesIndexLoaderV1.MAGIC;
+import static 
org.apache.jackrabbit.oak.segment.file.tar.binaries.BinaryReferencesIndexLoaderV1.loadBinaryReferencesIndex;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.nio.ByteBuffer;
+import java.util.zip.CRC32;
+
+import com.google.common.base.Charsets;
+import org.junit.Test;
+
+public class BinaryReferencesIndexLoaderV1Test {
+
+    private static int length(String s) {
+        return bytes(s).length;
+    }
+
+    private static byte[] bytes(String s) {
+        return s.getBytes(Charsets.UTF_8);
+    }
+
+    private static BinaryReferencesIndex loadIndex(ByteBuffer buffer) throws 
Exception {
+        return loadBinaryReferencesIndex((whence, length) -> {
+            ByteBuffer slice = buffer.duplicate();
+            slice.position(slice.limit() - whence);
+            slice.limit(slice.position() + length);
+            return slice.slice();
+        });
+    }
+
+    private static void assertInvalidBinaryReferencesIndexException(ByteBuffer 
buffer, String message) throws Exception {
+        try {
+            loadIndex(buffer);
+        } catch (InvalidBinaryReferencesIndexException e) {
+            assertEquals(message, e.getMessage());
+            throw e;
+        }
+    }
+
+    private static int checksum(ByteBuffer buffer) {
+        CRC32 checksum = new CRC32();
+        int position = buffer.position();
+        checksum.update(buffer);
+        buffer.position(position);
+        return (int) checksum.getValue();
+    }
+
+    @Test(expected = InvalidBinaryReferencesIndexException.class)
+    public void testInvalidMagicNumber() throws Exception {
+        ByteBuffer buffer = ByteBuffer.allocate(FOOTER_SIZE);
+        assertInvalidBinaryReferencesIndexException(buffer, "Invalid magic 
number");
+    }
+
+    @Test(expected = InvalidBinaryReferencesIndexException.class)
+    public void testInvalidCount() throws Exception {
+        ByteBuffer buffer = ByteBuffer.allocate(FOOTER_SIZE);
+        buffer.duplicate()
+            .putInt(0)
+            .putInt(-1)
+            .putInt(0)
+            .putInt(MAGIC);
+        assertInvalidBinaryReferencesIndexException(buffer, "Invalid count");
+    }
+
+    @Test(expected = InvalidBinaryReferencesIndexException.class)
+    public void testInvalidSize() throws Exception {
+        ByteBuffer buffer = ByteBuffer.allocate(FOOTER_SIZE);
+        buffer.duplicate()
+            .putInt(0)
+            .putInt(0)
+            .putInt(0)
+            .putInt(MAGIC);
+        assertInvalidBinaryReferencesIndexException(buffer, "Invalid size");
+    }
+
+    @Test(expected = InvalidBinaryReferencesIndexException.class)
+    public void testInvalidChecksum() throws Exception {
+        ByteBuffer entries = ByteBuffer.allocate(512)
+            // First generation
+            .putInt(1)
+            .putInt(2)
+            // First generation, first segment
+            .putLong(1).putLong(1)
+            .putInt(2)
+            .putInt(length("1.1.1")).put(bytes("1.1.1"))
+            .putInt(length("1.1.2")).put(bytes("1.1.2"))
+            // First generation, second segment
+            .putLong(1).putLong(2)
+            .putInt(2)
+            .putInt(length("1.2.1")).put(bytes("1.2.1"))
+            .putInt(length("1.2.2")).put(bytes("1.2.2"))
+            // Second generation
+            .putInt(2)
+            .putInt(2)
+            // Second generation, second segment
+            .putLong(1).putLong(1)
+            .putInt(2)
+            .putInt(length("2.1.1")).put(bytes("2.1.1"))
+            .putInt(length("2.1.2")).put(bytes("2.1.2"))
+            // Second generation, second segment
+            .putLong(1).putLong(2)
+            .putInt(2)
+            .putInt(length("2.2.1")).put(bytes("2.2.1"))
+            .putInt(length("2.2.2")).put(bytes("2.2.2"));
+        entries.flip();
+
+        ByteBuffer buffer = ByteBuffer.allocate(entries.remaining() + 
FOOTER_SIZE);
+        buffer.duplicate()
+            .put(entries.duplicate())
+            .putInt(checksum(entries) + 1)
+            .putInt(2)
+            .putInt(entries.remaining() + FOOTER_SIZE)
+            .putInt(MAGIC);
+
+        assertInvalidBinaryReferencesIndexException(buffer, "Invalid 
checksum");
+    }
+
+    @Test
+    public void testParse() throws Exception {
+        ByteBuffer entries = ByteBuffer.allocate(512)
+            // First generation
+            .putInt(1)
+            .putInt(2)
+            // First generation, first segment
+            .putLong(1).putLong(1)
+            .putInt(2)
+            .putInt(length("1.1.1")).put(bytes("1.1.1"))
+            .putInt(length("1.1.2")).put(bytes("1.1.2"))
+            // First generation, second segment
+            .putLong(1).putLong(2)
+            .putInt(2)
+            .putInt(length("1.2.1")).put(bytes("1.2.1"))
+            .putInt(length("1.2.2")).put(bytes("1.2.2"))
+            // Second generation
+            .putInt(2)
+            .putInt(2)
+            // Second generation, second segment
+            .putLong(1).putLong(1)
+            .putInt(2)
+            .putInt(length("2.1.1")).put(bytes("2.1.1"))
+            .putInt(length("2.1.2")).put(bytes("2.1.2"))
+            // Second generation, second segment
+            .putLong(1).putLong(2)
+            .putInt(2)
+            .putInt(length("2.2.1")).put(bytes("2.2.1"))
+            .putInt(length("2.2.2")).put(bytes("2.2.2"));
+        entries.flip();
+
+        ByteBuffer buffer = ByteBuffer.allocate(entries.remaining() + 
FOOTER_SIZE);
+        buffer.duplicate()
+            .put(entries.duplicate())
+            .putInt(checksum(entries))
+            .putInt(2)
+            .putInt(entries.remaining() + FOOTER_SIZE)
+            .putInt(MAGIC);
+
+        assertNotNull(loadIndex(buffer));
+    }
+}

Propchange: 
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/binaries/BinaryReferencesIndexLoaderV1Test.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/binaries/BinaryReferencesIndexLoaderV2Test.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/binaries/BinaryReferencesIndexLoaderV2Test.java?rev=1804638&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/binaries/BinaryReferencesIndexLoaderV2Test.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/binaries/BinaryReferencesIndexLoaderV2Test.java
 Thu Aug 10 07:24:54 2017
@@ -0,0 +1,180 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.jackrabbit.oak.segment.file.tar.binaries;
+
+import static 
org.apache.jackrabbit.oak.segment.file.tar.binaries.BinaryReferencesIndexLoaderV2.FOOTER_SIZE;
+import static 
org.apache.jackrabbit.oak.segment.file.tar.binaries.BinaryReferencesIndexLoaderV2.MAGIC;
+import static 
org.apache.jackrabbit.oak.segment.file.tar.binaries.BinaryReferencesIndexLoaderV2.loadBinaryReferencesIndex;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.nio.ByteBuffer;
+import java.util.zip.CRC32;
+
+import com.google.common.base.Charsets;
+import org.junit.Test;
+
+public class BinaryReferencesIndexLoaderV2Test {
+
+    private static int length(String s) {
+        return bytes(s).length;
+    }
+
+    private static byte[] bytes(String s) {
+        return s.getBytes(Charsets.UTF_8);
+    }
+
+    private static BinaryReferencesIndex loadIndex(ByteBuffer buffer) throws 
Exception {
+        return loadBinaryReferencesIndex((whence, length) -> {
+            ByteBuffer slice = buffer.duplicate();
+            slice.position(slice.limit() - whence);
+            slice.limit(slice.position() + length);
+            return slice.slice();
+        });
+    }
+
+    private static void assertInvalidBinaryReferencesIndexException(ByteBuffer 
buffer, String message) throws Exception {
+        try {
+            loadIndex(buffer);
+        } catch (InvalidBinaryReferencesIndexException e) {
+            assertEquals(message, e.getMessage());
+            throw e;
+        }
+    }
+
+    private static int checksum(ByteBuffer buffer) {
+        CRC32 checksum = new CRC32();
+        int position = buffer.position();
+        checksum.update(buffer);
+        buffer.position(position);
+        return (int) checksum.getValue();
+    }
+
+    @Test(expected = InvalidBinaryReferencesIndexException.class)
+    public void testInvalidMagicNumber() throws Exception {
+        ByteBuffer buffer = ByteBuffer.allocate(FOOTER_SIZE);
+        assertInvalidBinaryReferencesIndexException(buffer, "Invalid magic 
number");
+    }
+
+    @Test(expected = InvalidBinaryReferencesIndexException.class)
+    public void testInvalidCount() throws Exception {
+        ByteBuffer buffer = ByteBuffer.allocate(FOOTER_SIZE);
+        buffer.duplicate()
+            .putInt(0)
+            .putInt(-1)
+            .putInt(0)
+            .putInt(MAGIC);
+        assertInvalidBinaryReferencesIndexException(buffer, "Invalid count");
+    }
+
+    @Test(expected = InvalidBinaryReferencesIndexException.class)
+    public void testInvalidSize() throws Exception {
+        ByteBuffer buffer = ByteBuffer.allocate(FOOTER_SIZE);
+        buffer.duplicate()
+            .putInt(0)
+            .putInt(0)
+            .putInt(0)
+            .putInt(MAGIC);
+        assertInvalidBinaryReferencesIndexException(buffer, "Invalid size");
+    }
+
+    @Test(expected = InvalidBinaryReferencesIndexException.class)
+    public void testInvalidChecksum() throws Exception {
+        ByteBuffer entries = ByteBuffer.allocate(512)
+            // First generation
+            .putInt(1).putInt(2).put((byte) 0)
+            .putInt(2)
+            // First generation, first segment
+            .putLong(1).putLong(1)
+            .putInt(2)
+            .putInt(length("1.1.1")).put(bytes("1.1.1"))
+            .putInt(length("1.1.2")).put(bytes("1.1.2"))
+            // First generation, second segment
+            .putLong(1).putLong(2)
+            .putInt(2)
+            .putInt(length("1.2.1")).put(bytes("1.2.1"))
+            .putInt(length("1.2.2")).put(bytes("1.2.2"))
+            // Second generation
+            .putInt(3).putInt(4).put((byte) 1)
+            .putInt(2)
+            // Second generation, second segment
+            .putLong(1).putLong(1)
+            .putInt(2)
+            .putInt(length("2.1.1")).put(bytes("2.1.1"))
+            .putInt(length("2.1.2")).put(bytes("2.1.2"))
+            // Second generation, second segment
+            .putLong(1).putLong(2)
+            .putInt(2)
+            .putInt(length("2.2.1")).put(bytes("2.2.1"))
+            .putInt(length("2.2.2")).put(bytes("2.2.2"));
+        entries.flip();
+
+        ByteBuffer buffer = ByteBuffer.allocate(entries.remaining() + 
FOOTER_SIZE);
+        buffer.duplicate()
+            .put(entries.duplicate())
+            .putInt(checksum(entries) + 1)
+            .putInt(2)
+            .putInt(entries.remaining() + FOOTER_SIZE)
+            .putInt(MAGIC);
+
+        assertInvalidBinaryReferencesIndexException(buffer, "Invalid 
checksum");
+    }
+
+    @Test
+    public void testParse() throws Exception {
+        ByteBuffer entries = ByteBuffer.allocate(512)
+            // First generation
+            .putInt(1).putInt(2).put((byte) 0)
+            .putInt(2)
+            // First generation, first segment
+            .putLong(1).putLong(1)
+            .putInt(2)
+            .putInt(length("1.1.1")).put(bytes("1.1.1"))
+            .putInt(length("1.1.2")).put(bytes("1.1.2"))
+            // First generation, second segment
+            .putLong(1).putLong(2)
+            .putInt(2)
+            .putInt(length("1.2.1")).put(bytes("1.2.1"))
+            .putInt(length("1.2.2")).put(bytes("1.2.2"))
+            // Second generation
+            .putInt(3).putInt(4).put((byte) 1)
+            .putInt(2)
+            // Second generation, second segment
+            .putLong(1).putLong(1)
+            .putInt(2)
+            .putInt(length("2.1.1")).put(bytes("2.1.1"))
+            .putInt(length("2.1.2")).put(bytes("2.1.2"))
+            // Second generation, second segment
+            .putLong(1).putLong(2)
+            .putInt(2)
+            .putInt(length("2.2.1")).put(bytes("2.2.1"))
+            .putInt(length("2.2.2")).put(bytes("2.2.2"));
+        entries.flip();
+
+        ByteBuffer buffer = ByteBuffer.allocate(entries.remaining() + 
FOOTER_SIZE);
+        buffer.duplicate()
+            .put(entries.duplicate())
+            .putInt(checksum(entries))
+            .putInt(2)
+            .putInt(entries.remaining() + FOOTER_SIZE)
+            .putInt(MAGIC);
+
+        assertNotNull(loadIndex(buffer));
+    }
+
+}

Propchange: 
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/binaries/BinaryReferencesIndexLoaderV2Test.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/binaries/BinaryReferencesIndexWriterTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/binaries/BinaryReferencesIndexWriterTest.java?rev=1804638&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/binaries/BinaryReferencesIndexWriterTest.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/binaries/BinaryReferencesIndexWriterTest.java
 Thu Aug 10 07:24:54 2017
@@ -0,0 +1,82 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.jackrabbit.oak.segment.file.tar.binaries;
+
+import static java.util.Arrays.asList;
+import static 
org.apache.jackrabbit.oak.segment.file.tar.binaries.BinaryReferencesIndexLoader.loadBinaryReferencesIndex;
+import static 
org.apache.jackrabbit.oak.segment.file.tar.binaries.BinaryReferencesIndexWriter.newBinaryReferencesIndexWriter;
+import static org.junit.Assert.assertEquals;
+
+import java.nio.ByteBuffer;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.UUID;
+
+import org.junit.Test;
+
+public class BinaryReferencesIndexWriterTest {
+
+    @Test
+    public void testWrite() throws Exception {
+        UUID s1 = new UUID(1, 1);
+        UUID s2 = new UUID(1, 2);
+        UUID s3 = new UUID(2, 1);
+        UUID s4 = new UUID(2, 2);
+
+        BinaryReferencesIndexWriter writer = newBinaryReferencesIndexWriter();
+        writer.addEntry(1, 2, false, s1, "1.1.1");
+        writer.addEntry(1, 2, false, s1, "1.1.2");
+        writer.addEntry(1, 2, false, s2, "1.2.1");
+        writer.addEntry(1, 2, false, s2, "1.2.2");
+        writer.addEntry(3, 4, true, s3, "2.1.1");
+        writer.addEntry(3, 4, true, s3, "2.1.2");
+        writer.addEntry(3, 4, true, s4, "2.2.1");
+        writer.addEntry(3, 4, true, s4, "2.2.2");
+
+        byte[] data = writer.write();
+
+        BinaryReferencesIndex index = loadBinaryReferencesIndex((whence, 
length) -> ByteBuffer.wrap(data, data.length - whence, length));
+
+        Generation g1 = new Generation(1, 2, false);
+        Generation g2 = new Generation(3, 4, true);
+
+        Map<Generation, Map<UUID, Set<String>>> expected = new HashMap<>();
+        expected.put(g1, new HashMap<>());
+        expected.put(g2, new HashMap<>());
+        expected.get(g1).put(s1, new HashSet<>());
+        expected.get(g1).put(s2, new HashSet<>());
+        expected.get(g2).put(s3, new HashSet<>());
+        expected.get(g2).put(s4, new HashSet<>());
+        expected.get(g1).get(s1).addAll(asList("1.1.1", "1.1.2"));
+        expected.get(g1).get(s2).addAll(asList("1.2.1", "1.2.2"));
+        expected.get(g2).get(s3).addAll(asList("2.1.1", "2.1.2"));
+        expected.get(g2).get(s4).addAll(asList("2.2.1", "2.2.2"));
+
+        Map<Generation, Map<UUID, Set<String>>> actual = new HashMap<>();
+        index.forEach((generation, full, compacted, id, reference) -> {
+            actual
+                .computeIfAbsent(new Generation(generation, full, compacted), 
k -> new HashMap<>())
+                .computeIfAbsent(id, k -> new HashSet<>())
+                .add(reference);
+        });
+
+        assertEquals(expected, actual);
+    }
+}

Propchange: 
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/file/tar/binaries/BinaryReferencesIndexWriterTest.java
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to