jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mdue...@apache.org
Subject svn commit: r1338251 - in /jackrabbit/oak/trunk/oak-core/src: main/java/org/apache/jackrabbit/oak/kernel/KernelNodeStore.java test/java/org/apache/jackrabbit/oak/core/AbstractOakTest.java test/java/org/apache/jackrabbit/oak/kernel/KernelNodeStoreTest.java
Date Mon, 14 May 2012 15:17:14 GMT
Author: mduerig
Date: Mon May 14 15:17:14 2012
New Revision: 1338251

URL: http://svn.apache.org/viewvc?rev=1338251&view=rev
Log:
OAK-100: Proper CommitHook handling in NodeStore
- test case

Added:
    jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/kernel/KernelNodeStoreTest.java
Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/KernelNodeStore.java
    jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/core/AbstractOakTest.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/KernelNodeStore.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/KernelNodeStore.java?rev=1338251&r1=1338250&r2=1338251&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/KernelNodeStore.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/kernel/KernelNodeStore.java
Mon May 14 15:17:14 2012
@@ -98,7 +98,7 @@ public class KernelNodeStore extends Abs
     @Override
     public NodeStateBuilder getBuilder(NodeState base) {
         if (!(base instanceof KernelNodeState)) {
-            throw new IllegalArgumentException("Alien node state");
+            throw new IllegalArgumentException("Alien node state " + base.getClass() + ":
" + root);
         }
 
         NodeStateBuilderContext context = new NodeStateBuilderContext((KernelNodeState) base);
@@ -115,7 +115,7 @@ public class KernelNodeStore extends Abs
             committed = ((NodeStateBuilderContext.NodeDecorator) newRoot).applyChanges();
         }
         else {
-            throw new CommitFailedException("Alien node state: " + newRoot);
+            throw new CommitFailedException("Alien node state " + newRoot.getClass() + ":
" + newRoot);
         }
 
         commitHook.afterCommit(this, baseState, committed);

Modified: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/core/AbstractOakTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/core/AbstractOakTest.java?rev=1338251&r1=1338250&r2=1338251&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/core/AbstractOakTest.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/core/AbstractOakTest.java
Mon May 14 15:17:14 2012
@@ -20,6 +20,8 @@ import org.apache.jackrabbit.mk.api.Micr
 import org.apache.jackrabbit.mk.core.MicroKernelImpl;
 import org.apache.jackrabbit.oak.api.CoreValueFactory;
 import org.apache.jackrabbit.oak.kernel.KernelNodeStore;
+import org.apache.jackrabbit.oak.spi.commit.CommitHook;
+import org.apache.jackrabbit.oak.spi.commit.EmptyCommitHook;
 import org.apache.jackrabbit.oak.spi.state.NodeState;
 import org.apache.jackrabbit.oak.spi.state.NodeStore;
 import org.junit.Before;
@@ -45,12 +47,16 @@ public abstract class AbstractOakTest {
     @Before
     public void setUp() {
         MicroKernel microKernel = new MicroKernelImpl();
-        store = new KernelNodeStore(microKernel);
+        store = new KernelNodeStore(microKernel, createCommitHook());
         valueFactory = store.getValueFactory();
 
         state = createInitialState(microKernel);
     }
 
+    protected CommitHook createCommitHook() {
+        return new EmptyCommitHook();
+    }
+
     protected abstract NodeState createInitialState(MicroKernel microKernel);
 
 }
\ No newline at end of file

Added: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/kernel/KernelNodeStoreTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/kernel/KernelNodeStoreTest.java?rev=1338251&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/kernel/KernelNodeStoreTest.java
(added)
+++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/kernel/KernelNodeStoreTest.java
Mon May 14 15:17:14 2012
@@ -0,0 +1,158 @@
+package org.apache.jackrabbit.oak.kernel;
+
+import org.apache.jackrabbit.mk.api.MicroKernel;
+import org.apache.jackrabbit.oak.api.CommitFailedException;
+import org.apache.jackrabbit.oak.api.CoreValue;
+import org.apache.jackrabbit.oak.core.AbstractOakTest;
+import org.apache.jackrabbit.oak.spi.commit.CommitHook;
+import org.apache.jackrabbit.oak.spi.commit.EmptyCommitHook;
+import org.apache.jackrabbit.oak.spi.state.NodeState;
+import org.apache.jackrabbit.oak.spi.state.NodeStateBuilder;
+import org.apache.jackrabbit.oak.spi.state.NodeStore;
+import org.junit.Ignore;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
+public class KernelNodeStoreTest extends AbstractOakTest {
+    private final CommitHookDelegate commitHookDelegate = new CommitHookDelegate();
+
+    private NodeState root;
+
+    @Override
+    protected NodeState createInitialState(MicroKernel microKernel) {
+        String jsop =
+                "+\"test\":{\"a\":1,\"b\":2,\"c\":3,"
+                        + "\"x\":{},\"y\":{},\"z\":{}}";
+        microKernel.commit("/", jsop, null, "test data");
+        root = store.getRoot();
+        return root;
+    }
+
+    @Override
+    protected CommitHook createCommitHook() {
+        return commitHookDelegate;
+    }
+
+    @Test
+    public void getRoot() {
+        assertEquals(root, store.getRoot());
+        assertEquals(root.getChildNode("test"), store.getRoot().getChildNode("test"));
+        assertEquals(root.getChildNode("test").getChildNode("x"),
+                store.getRoot().getChildNode("test").getChildNode("x"));
+        assertEquals(root.getChildNode("test").getChildNode("any"),
+                store.getRoot().getChildNode("test").getChildNode("any"));
+        assertEquals(root.getChildNode("test").getProperty("a"),
+                store.getRoot().getChildNode("test").getProperty("a"));
+        assertEquals(root.getChildNode("test").getProperty("any"),
+                store.getRoot().getChildNode("test").getProperty("any"));
+    }
+
+    @Test
+    public void setRoot() throws CommitFailedException {
+        NodeState test = store.getRoot().getChildNode("test");
+        NodeStateBuilder testBuilder = store.getBuilder(test);
+
+        NodeStateBuilder newNodeBuilder = testBuilder.addNode("newNode");
+        testBuilder.removeNode("a");
+        CoreValue fortyTwo = store.getValueFactory().createValue(42);
+        newNodeBuilder.setProperty("n", fortyTwo);
+        NodeState testState = testBuilder.getNodeState();
+
+        store.setRoot(testBuilder.getNodeState());
+
+        assertNotNull(testState.getChildNode("newNode"));
+        assertNull(testState.getChildNode("a"));
+        assertEquals(fortyTwo, testState.getChildNode("newNode").getProperty("n").getValue());
+        assertEquals(testState, store.getRoot().getChildNode("test"));
+    }
+
+    @Test
+    public void afterCommitHook() throws CommitFailedException {
+        NodeState test = store.getRoot().getChildNode("test");
+        NodeStateBuilder testBuilder = store.getBuilder(test);
+
+        NodeStateBuilder newNodeBuilder = testBuilder.addNode("newNode");
+        testBuilder.removeNode("a");
+        final CoreValue fortyTwo = store.getValueFactory().createValue(42);
+        newNodeBuilder.setProperty("n", fortyTwo);
+        final NodeState testState = testBuilder.getNodeState();
+
+        commitWithHook(testBuilder.getNodeState(), new EmptyCommitHook() {
+            @Override
+            public void afterCommit(NodeStore store, NodeState before, NodeState after) {
+                assertNull(before.getChildNode("newNode"));
+                assertNotNull(after.getChildNode("newNode"));
+                assertNull(after.getChildNode("a"));
+                assertEquals(fortyTwo, after.getChildNode("newNode").getProperty("n").getValue());
+                assertEquals(testState, after);
+            }
+        });
+    }
+
+    @Test
+    @Ignore ("WIP") // TODO OAK-100: NodeStore.getBuilder() only works for KernelNodeState
type arguments
+    public void beforeCommitHook() throws CommitFailedException {
+        NodeState test = store.getRoot().getChildNode("test");
+        NodeStateBuilder testBuilder = store.getBuilder(test);
+
+        NodeStateBuilder newNodeBuilder = testBuilder.addNode("newNode");
+        testBuilder.removeNode("a");
+        final CoreValue fortyTwo = store.getValueFactory().createValue(42);
+        newNodeBuilder.setProperty("n", fortyTwo);
+        final NodeState testState = testBuilder.getNodeState();
+
+        commitWithHook(testBuilder.getNodeState(), new EmptyCommitHook() {
+            @Override
+            public NodeState beforeCommit(NodeStore store, NodeState before, NodeState after)
{
+                NodeStateBuilder afterBuilder = store.getBuilder(after);
+                afterBuilder.addNode("fromHook");
+                return afterBuilder.getNodeState();
+            }
+        });
+
+        assertNotNull(testState.getChildNode("newNode"));
+        assertNotNull(testState.getChildNode("fromHook"));
+        assertNull(testState.getChildNode("a"));
+        assertEquals(fortyTwo, testState.getChildNode("newNode").getProperty("n").getValue());
+        assertEquals(testState, store.getRoot().getChildNode("test"));
+
+    }
+
+    //------------------------------------------------------------< private >---
+
+    private void commitWithHook(NodeState nodeState, CommitHook commitHook)
+            throws CommitFailedException {
+
+        commitHookDelegate.set(commitHook);
+        try {
+            store.setRoot(nodeState);
+        }
+        finally {
+            commitHookDelegate.set(new EmptyCommitHook());
+        }
+    }
+
+    private static class CommitHookDelegate implements CommitHook {
+        private CommitHook delegate = new EmptyCommitHook();
+
+        public void set(CommitHook commitHook) {
+            delegate = commitHook;
+        }
+
+        @Override
+        public NodeState beforeCommit(NodeStore store, NodeState before, NodeState after)
+                throws CommitFailedException {
+
+            return delegate.beforeCommit(store, before, after);
+        }
+
+        @Override
+        public void afterCommit(NodeStore store, NodeState before, NodeState after) {
+            delegate.afterCommit(store, before, after);
+        }
+    }
+
+}



Mime
View raw message