From d1601d028d2d0bff231dbe624d44a1649015de21 Mon Sep 17 00:00:00 2001 From: Axel Hanikel Date: Fri, 23 Nov 2018 15:35:33 +0100 Subject: [PATCH] OAK-7878 - Add more unit tests for LoggingHook --- .../oak/segment/LoggingHookTest.java | 108 +++++++++++++++--- 1 file changed, 91 insertions(+), 17 deletions(-) diff --git a/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/LoggingHookTest.java b/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/LoggingHookTest.java index 92a99003c0..d5dcc980d4 100644 --- a/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/LoggingHookTest.java +++ b/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/LoggingHookTest.java @@ -18,26 +18,16 @@ package org.apache.jackrabbit.oak.segment; -import static org.apache.jackrabbit.oak.segment.file.FileStoreBuilder.fileStoreBuilder; 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 +35,100 @@ 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 { + public void testChildNodeAdded() throws Exception { + String result = + "n+ chi%25:ld\n" + + "n!\n" + + "n!\n"; + assertCommitProduces(result, root -> root.setChildNode("chi%:ld")); + } + + @Test + public void testChildNodeChanged() throws Exception { String result = + "n^ existing\n" + "n+ child\n" + - "n!\n" + - "n!\n"; - assertCommitProduces(result, root -> root.setChildNode("child")); + "n!\n" + + "n!\n" + + "n!\n"; + assertCommitProduces(result, root -> root.getChildNode("existing").setChildNode("child")); + } + + @Test + public void testChildNodeDeleted() throws Exception { + String result = + "n- existing\n" + + "n!\n"; + assertCommitProduces(result, root -> root.getChildNode("existing").remove()); + } + + @Test + public void testChildNodesAdded() throws Exception { + String result = + "n+ child\n" + + "n+ childchild\n" + + "n+ childchildchild\n" + + "n!\n" + + "n!\n" + + "n!\n" + + "n!\n"; + assertCommitProduces(result, root -> + root.setChildNode("child") + .setChildNode("childchild") + .setChildNode("childchildchild") + ); + } + + @Test + public void testNoChange() throws Exception { + assertCommitProduces("", root -> {}); + assertCommitProduces("n!\n", root -> { + root.setChildNode("a"); + root.getChildNode("a").remove(); + }); + } + + @Test + public void testPropertyAdded() throws Exception { + assertCommitProduces("p+ a+string = a+string/slash:colon%25percent%24dollar%5Cbackslash%0Anewline\nn!\n" + , root -> root.setProperty("a string", "a string/slash:colon%percent$dollar\\backslash\nnewline")); + assertCommitProduces("p+ strings = [a+string,another+string]\nn!\n" + , root -> root.setProperty("strings", Arrays.asList(new String[] {"a string", "another string"}), Type.STRINGS)); + assertCommitProduces("p+ a+long = 42\nn!\n" + , root -> root.setProperty("a long", 42L)); + assertCommitProduces("p+ longs = [42,99]\nn!\n" + , root -> root.setProperty("longs", Arrays.asList(new Long[] {42L,99L}), Type.LONGS)); + assertCommitProduces("p+ an+int = 42\nn!\n" + , root -> root.setProperty("an int", (int) 42)); + assertCommitProduces("p+ a+date = Jan+02+01:00:00+CET+1970\nn!\n" + , root -> root.setProperty("a date", "Jan 02 01:00:00 CET 1970", org.apache.jackrabbit.oak.api.Type.DATE)); + assertCommitProduces("p+ a+binary = 68656C6C6F\nn!\n" + , root -> root.setProperty("a binary", "hello".getBytes())); + } + + @Test + public void testPropertyChanged() throws Exception { + assertCommitProduces("p+ a+string = a+string\nn!\n" + , root -> root.setProperty("a string", "a string")); + assertCommitProduces("p^ a+string = a+different+string\nn!\n" + , root -> root.setProperty("a string", "a different string")); + } + + @Test + public void testPropertyDeleted() throws Exception { + assertCommitProduces("p+ a+string = a+string\nn!\n" + , root -> root.setProperty("a string", "a string")); + assertCommitProduces("p- a+string = a+string\nn!\n" + , root -> root.removeProperty("a string")); } private void assertCommitProduces(String expected, Consumer committer) throws Exception { @@ -70,6 +139,11 @@ 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); -- 2.17.1