Author: frm
Date: Wed Nov 28 13:07:40 2018
New Revision: 1847637

URL: http://svn.apache.org/viewvc?rev=1847637&view=rev
Log:
OAK-7878 - Add unit test for LoggingHook

Modified:
    
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/LoggingHookTest.java

Modified: 
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/LoggingHookTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/LoggingHookTest.java?rev=1847637&r1=1847636&r2=1847637&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/LoggingHookTest.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/LoggingHookTest.java
 Wed Nov 28 13:07:40 2018
@@ -18,26 +18,19 @@
 
 package org.apache.jackrabbit.oak.segment;
 
-import static 
org.apache.jackrabbit.oak.segment.file.FileStoreBuilder.fileStoreBuilder;
+import static org.apache.jackrabbit.oak.api.Type.DATE;
+import static org.apache.jackrabbit.oak.api.Type.LONGS;
+import static org.apache.jackrabbit.oak.api.Type.STRINGS;
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
 
 import java.io.File;
-import java.io.IOException;
-import java.time.Instant;
-import java.util.Date;
+import java.util.Arrays;
 import java.util.function.Consumer;
-import java.util.regex.Pattern;
 
-import org.apache.jackrabbit.oak.api.CommitFailedException;
-import org.apache.jackrabbit.oak.api.Type;
-import org.apache.jackrabbit.oak.segment.file.FileStore;
-import org.apache.jackrabbit.oak.segment.file.InvalidFileStoreVersionException;
 import org.apache.jackrabbit.oak.segment.test.TemporaryFileStore;
 import org.apache.jackrabbit.oak.spi.commit.CommitInfo;
 import org.apache.jackrabbit.oak.spi.commit.EmptyHook;
 import org.apache.jackrabbit.oak.spi.state.NodeBuilder;
-import org.apache.jackrabbit.oak.spi.state.NodeState;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.RuleChain;
@@ -45,21 +38,172 @@ import org.junit.rules.TemporaryFolder;
 
 public class LoggingHookTest {
 
-    private TemporaryFolder folder = new TemporaryFolder(new File("target"));
+    private final TemporaryFolder folder = new TemporaryFolder(new 
File("target"));
 
-    private TemporaryFileStore fileStore = new TemporaryFileStore(folder, 
false);
+    private final TemporaryFileStore fileStore = new 
TemporaryFileStore(folder, false);
 
     @Rule
     public RuleChain chain = RuleChain.outerRule(folder)
         .around(fileStore);
 
     @Test
-    public void testChildNode() throws Exception {
-        String result =
-            "n+ child\n" +
-                "n!\n" +
-                "n!\n";
-        assertCommitProduces(result, root -> root.setChildNode("child"));
+    public void testChildNodeAdded() throws Exception {
+        assertCommitProduces(
+            lines(
+                "n+ chi%25:ld",
+                "n!",
+                "n!"
+            ),
+            root -> root.setChildNode("chi%:ld")
+        );
+    }
+
+    @Test
+    public void testChildNodeChanged() throws Exception {
+        assertCommitProduces(
+            lines(
+                "n^ existing",
+                "n+ child",
+                "n!",
+                "n!",
+                "n!"
+            ),
+            root -> root.getChildNode("existing").setChildNode("child")
+        );
+    }
+
+    @Test
+    public void testChildNodeDeleted() throws Exception {
+        assertCommitProduces(
+            lines(
+                "n- existing",
+                "n!"
+            ),
+            root -> root.getChildNode("existing").remove()
+        );
+    }
+
+    @Test
+    public void testChildNodesAdded() throws Exception {
+        assertCommitProduces(
+            lines(
+                "n+ child",
+                "n+ childchild",
+                "n+ childchildchild",
+                "n!",
+                "n!",
+                "n!",
+                "n!"
+            ),
+            root ->
+                root.setChildNode("child")
+                    .setChildNode("childchild")
+                    .setChildNode("childchildchild")
+        );
+    }
+
+    @Test
+    public void testNoChange() throws Exception {
+        assertCommitProduces("", root -> {
+            // Do nothing
+        });
+        assertCommitProduces(
+            lines(
+                "n!"
+            ),
+            root -> {
+                root.setChildNode("a");
+                root.getChildNode("a").remove();
+            }
+        );
+    }
+
+    @Test
+    public void testPropertyAdded() throws Exception {
+        assertCommitProduces(
+            lines(
+                "p+ a+string <STRING> = 
a+string/slash:colon%25percent%24dollar%5Cbackslash%0Anewline",
+                "n!"
+            ),
+            root -> root.setProperty("a string", "a 
string/slash:colon%percent$dollar\\backslash\nnewline")
+        );
+        assertCommitProduces(
+            lines(
+                "p+ strings <STRINGS> = [a+string,another+string]",
+                "n!"
+            ),
+            root -> root.setProperty("strings", Arrays.asList("a string", 
"another string"), STRINGS)
+        );
+        assertCommitProduces(
+            lines(
+                "p+ a+long <LONG> = 42",
+                "n!"
+            ),
+            root -> root.setProperty("a long", 42L)
+        );
+        assertCommitProduces(
+            lines(
+                "p+ longs <LONGS> = [42,99]",
+                "n!"
+            ),
+            root -> root.setProperty("longs", Arrays.asList(42L, 99L), LONGS)
+        );
+        assertCommitProduces(
+            lines(
+                "p+ an+int <LONG> = 42",
+                "n!"
+            ),
+            root -> root.setProperty("an int", 42)
+        );
+        assertCommitProduces(
+            lines(
+                "p+ a+date <DATE> = Jan+02+01:00:00+CET+1970",
+                "n!"
+            ),
+            root -> root.setProperty("a date", "Jan 02 01:00:00 CET 1970", 
DATE)
+        );
+        assertCommitProduces(
+            lines(
+                "p+ a+binary <BINARY> = 68656C6C6F",
+                "n!"
+            ),
+            root -> root.setProperty("a binary", "hello".getBytes())
+        );
+    }
+
+    @Test
+    public void testPropertyChanged() throws Exception {
+        assertCommitProduces(
+            lines(
+                "p+ a+string <STRING> = a+string",
+                "n!"
+            ),
+            root -> root.setProperty("a string", "a string")
+        );
+        assertCommitProduces(
+            lines("p^ a+string <STRING> = a+different+string",
+                "n!"
+            ),
+            root -> root.setProperty("a string", "a different string")
+        );
+    }
+
+    @Test
+    public void testPropertyDeleted() throws Exception {
+        assertCommitProduces(
+            lines(
+                "p+ a+string <STRING> = a+string",
+                "n!"
+            ),
+            root -> root.setProperty("a string", "a string")
+        );
+        assertCommitProduces(
+            lines(
+                "p- a+string <STRING> = a+string",
+                "n!"
+            ),
+            root -> root.removeProperty("a string")
+        );
     }
 
     private void assertCommitProduces(String expected, Consumer<NodeBuilder> 
committer) throws Exception {
@@ -70,10 +214,19 @@ public class LoggingHookTest {
             .build();
 
         NodeBuilder root = store.getRoot().builder();
+        root.setChildNode("existing");
+        store.merge(root, EmptyHook.INSTANCE, CommitInfo.EMPTY);
+
+        result.delete(0, result.length());
+
         committer.accept(root);
         store.merge(root, EmptyHook.INSTANCE, CommitInfo.EMPTY);
 
         assertEquals(expected, result.toString());
     }
 
+    private static String lines(String... lines) {
+        return String.join("\n", lines) + "\n";
+    }
+
 }


Reply via email to