From 097566a2640a43623d4e5030f23995a35fcbd01e Mon Sep 17 00:00:00 2001 From: Axel Hanikel Date: Fri, 2 Nov 2018 13:45:52 +0100 Subject: [PATCH] OAK-7878 - Add LoggingHookTest --- .../oak/segment/LoggingHookTest.java | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/LoggingHookTest.java 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 new file mode 100644 index 0000000000..1b7833e19b --- /dev/null +++ b/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/LoggingHookTest.java @@ -0,0 +1,71 @@ +/* + * 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; + +import java.io.File; +import java.io.IOException; +import java.time.Instant; +import java.util.Date; +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.spi.commit.EmptyHook; +import org.apache.jackrabbit.oak.spi.state.NodeBuilder; +import org.apache.jackrabbit.oak.spi.state.NodeState; +import static org.apache.jackrabbit.oak.segment.file.FileStoreBuilder.fileStoreBuilder; +import org.apache.jackrabbit.oak.segment.file.InvalidFileStoreVersionException; +import org.apache.jackrabbit.oak.spi.commit.CommitInfo; +import static org.junit.Assert.assertEquals; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +public class LoggingHookTest { + + @Rule + public TemporaryFolder folder = new TemporaryFolder(new File("target")); + + @Test + public void testLoggingHook() throws InvalidFileStoreVersionException, IOException, CommitFailedException { + final String nodePattern = "n([!]|[+-^] [^ ]+)$"; + final String propPattern = "p[+-] [^ ]+ <[A-Z]+> = [^ ]*$"; + final Pattern expected = Pattern.compile("^[0-9]+ [^ ]+ (" + nodePattern + "|" + propPattern + ")"); + final Consumer check = s -> assertEquals("Checking " + s, true, expected.matcher(s).matches()); + + try (FileStore fileStore = fileStoreBuilder(folder.getRoot()).build()) { + final SegmentNodeStore ns = SegmentNodeStoreBuilders.builder(fileStore).withLoggingHook(check).build(); + final NodeState currentHead = ns.getRoot(); + NodeBuilder root = currentHead.builder(); + root.setChildNode("1a"); + root.setChildNode("1b").setProperty("p1", "p1val"); + NodeBuilder builder = root.setChildNode("1c"); + + builder.setChildNode("2d"); + builder.setChildNode("2e").setProperty("p2", 42); + builder = builder.setChildNode("2f"); + + builder.setChildNode("3g"); + builder.setChildNode("3h").setProperty("p3", Date.from(Instant.now()).toString(), Type.DATE); + builder.setChildNode("3i").setChildNode("4j"); + ns.merge(root, EmptyHook.INSTANCE, CommitInfo.EMPTY); + } + } +} -- 2.17.1