You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by en...@apache.org on 2022/09/23 23:00:47 UTC
[sling-org-apache-sling-jcr-repoinit] branch master updated: SLING-11571 repoinit: allow add or remove mixin types (#38)
This is an automated email from the ASF dual-hosted git repository.
enorman pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-jcr-repoinit.git
The following commit(s) were added to refs/heads/master by this push:
new 9913b78 SLING-11571 repoinit: allow add or remove mixin types (#38)
9913b78 is described below
commit 9913b787574186a7a31d184480cec3862816438f
Author: Eric Norman <en...@apache.org>
AuthorDate: Fri Sep 23 16:00:43 2022 -0700
SLING-11571 repoinit: allow add or remove mixin types (#38)
Improve tests by incorporating feedback from a review
---
pom.xml | 6 +-
.../org/apache/sling/jcr/repoinit/LogCapture.java | 74 ++++++++++++++++++++++
.../org/apache/sling/jcr/repoinit/MixinsTest.java | 37 +++++++----
src/test/resources/log4j.properties | 24 -------
src/test/resources/logback-test.xml | 31 +++++++++
5 files changed, 134 insertions(+), 38 deletions(-)
diff --git a/pom.xml b/pom.xml
index d68db98..dcf0c3f 100644
--- a/pom.xml
+++ b/pom.xml
@@ -190,9 +190,9 @@
<scope>provided</scope>
</dependency>
<dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-log4j12</artifactId>
- <version>1.7.30</version>
+ <groupId>ch.qos.logback</groupId>
+ <artifactId>logback-classic</artifactId>
+ <version>1.2.3</version>
<scope>test</scope>
</dependency>
<dependency>
diff --git a/src/test/java/org/apache/sling/jcr/repoinit/LogCapture.java b/src/test/java/org/apache/sling/jcr/repoinit/LogCapture.java
new file mode 100644
index 0000000..8a4a5e4
--- /dev/null
+++ b/src/test/java/org/apache/sling/jcr/repoinit/LogCapture.java
@@ -0,0 +1,74 @@
+/*
+ * 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.sling.jcr.repoinit;
+
+import static org.junit.Assert.fail;
+
+import java.util.function.Predicate;
+import java.util.stream.Stream;
+
+import org.slf4j.LoggerFactory;
+
+import ch.qos.logback.classic.Level;
+import ch.qos.logback.classic.Logger;
+import ch.qos.logback.classic.LoggerContext;
+import ch.qos.logback.classic.spi.ILoggingEvent;
+import ch.qos.logback.core.read.ListAppender;
+
+/**
+ * Capture logs for testing
+ *
+ * Initially cloned from: https://github.com/apache/sling-org-apache-sling-graphql-core/blob/0b1c1dd72ed04324ea84d2227c3223ec65b0b21e/src/test/java/org/apache/sling/graphql/core/util/LogCapture.java
+ */
+class LogCapture extends ListAppender<ILoggingEvent> implements AutoCloseable {
+ private final boolean verboseFailure;
+ private Logger logger;
+
+ public LogCapture(String loggerName, boolean verboseFailure) {
+ this.verboseFailure = verboseFailure;
+ logger = (Logger) LoggerFactory.getLogger(loggerName);
+ logger.setLevel(Level.ALL);
+ setContext((LoggerContext) LoggerFactory.getILoggerFactory());
+ logger.addAppender(this);
+ start();
+ }
+
+ @Override
+ public void close() throws Exception {
+ if (logger != null) {
+ logger.detachAppender(this);
+ }
+ }
+
+ public boolean anyMatch(Predicate<ILoggingEvent> p) {
+ return this.list.stream().anyMatch(p);
+ }
+
+ public void assertContains(Level atLevel, String ... substrings) {
+ Stream.of(substrings).forEach(substring -> {
+ if(!anyMatch(event -> event.getLevel() == atLevel && event.getFormattedMessage().contains(substring))) {
+ if(verboseFailure) {
+ fail(String.format("No log message contains [%s] in log\n%s", substring, this.list.toString()));
+ } else {
+ fail(String.format("No log message contains [%s]", substring));
+ }
+ }
+ });
+ }
+}
diff --git a/src/test/java/org/apache/sling/jcr/repoinit/MixinsTest.java b/src/test/java/org/apache/sling/jcr/repoinit/MixinsTest.java
index 9137493..ff423c4 100644
--- a/src/test/java/org/apache/sling/jcr/repoinit/MixinsTest.java
+++ b/src/test/java/org/apache/sling/jcr/repoinit/MixinsTest.java
@@ -34,6 +34,8 @@ import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
+import ch.qos.logback.classic.Level;
+
/** Test the creation of paths with specific node types */
public class MixinsTest {
@@ -70,37 +72,50 @@ public class MixinsTest {
@Test
public void removeOneMixinFromOnePath() throws Exception {
- U.parseAndExecute("create path /removeOneMixinOnOnePath(nt:unstructured mixin mix:lockable)");
- U.assertNodeExists("/removeOneMixinOnOnePath", "nt:unstructured", Collections.singletonList("mix:lockable"));
+ U.parseAndExecute("create path /removeOneMixinOnOnePath(nt:unstructured mixin mix:lockable,mix:referenceable)");
+ U.assertNodeExists("/removeOneMixinOnOnePath", "nt:unstructured", Arrays.asList("mix:lockable","mix:referenceable"));
U.parseAndExecute("remove mixin mix:lockable from /removeOneMixinOnOnePath");
+ U.assertNodeExists("/removeOneMixinOnOnePath", "nt:unstructured", Collections.singletonList("mix:referenceable"));
+
+ U.parseAndExecute("remove mixin mix:referenceable from /removeOneMixinOnOnePath");
U.assertNodeExists("/removeOneMixinOnOnePath", "nt:unstructured", Collections.emptyList());
}
@Test
public void removeTwoMixinsFromTwoPaths() throws Exception {
- U.parseAndExecute("create path /removeTwoMixinsOnOnePath1(nt:unstructured mixin mix:lockable,mix:referenceable)\n"
+ U.parseAndExecute("create path /removeTwoMixinsOnOnePath1(nt:unstructured mixin mix:lockable,mix:referenceable,mix:lastModified)\n"
+ "create path /removeTwoMixinsOnOnePath2(nt:unstructured mixin mix:lockable,mix:referenceable)");
- U.assertNodeExists("/removeTwoMixinsOnOnePath1", "nt:unstructured", Arrays.asList("mix:lockable","mix:referenceable"));
+ U.assertNodeExists("/removeTwoMixinsOnOnePath1", "nt:unstructured", Arrays.asList("mix:lockable","mix:referenceable", "mix:lastModified"));
U.assertNodeExists("/removeTwoMixinsOnOnePath2", "nt:unstructured", Arrays.asList("mix:lockable","mix:referenceable"));
U.parseAndExecute("remove mixin mix:lockable,mix:referenceable from /removeTwoMixinsOnOnePath1,/removeTwoMixinsOnOnePath2");
- U.assertNodeExists("/removeTwoMixinsOnOnePath1", "nt:unstructured", Collections.emptyList());
+ U.assertNodeExists("/removeTwoMixinsOnOnePath1", "nt:unstructured", Collections.singletonList("mix:lastModified"));
U.assertNodeExists("/removeTwoMixinsOnOnePath2", "nt:unstructured", Collections.emptyList());
}
@Test
public void addMixinOnNotExistingPath() throws Exception {
- // this should just log a warning and continue
- U.parseAndExecute("add mixin mix:lockable to /addMixinOnNotExistingPath");
- assertNodeNotExists("/addMixinOnNotExistingPath");
+ try (LogCapture capture = new LogCapture("org.apache.sling.jcr.repoinit.impl.AclVisitor", true)) {
+ // this should just log a warning and continue
+ U.parseAndExecute("add mixin mix:lockable to /addMixinOnNotExistingPath");
+ assertNodeNotExists("/addMixinOnNotExistingPath");
+
+ // verify the warning was logged
+ capture.assertContains(Level.WARN, "Path does not exist, not adding mixins: /addMixinOnNotExistingPath");
+ }
}
@Test
public void removeMixinFromNotExistingPath() throws Exception {
- // this should just log a warning and continue
- U.parseAndExecute("remove mixin mix:lockable from /removeMixinFromNotExistingPath");
- assertNodeNotExists("/removeMixinFromNotExistingPath");
+ try (LogCapture capture = new LogCapture("org.apache.sling.jcr.repoinit.impl.AclVisitor", true)) {
+ // this should just log a warning and continue
+ U.parseAndExecute("remove mixin mix:lockable from /removeMixinFromNotExistingPath");
+ assertNodeNotExists("/removeMixinFromNotExistingPath");
+
+ // verify the warning was logged
+ capture.assertContains(Level.WARN, "Path does not exist, not removing mixins: /removeMixinFromNotExistingPath");
+ }
}
@Test
diff --git a/src/test/resources/log4j.properties b/src/test/resources/log4j.properties
deleted file mode 100644
index 3060198..0000000
--- a/src/test/resources/log4j.properties
+++ /dev/null
@@ -1,24 +0,0 @@
-# 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.
-
-log4j.rootLogger=INFO, file
-log4j.appender.file=org.apache.log4j.FileAppender
-log4j.appender.file.File=target/testing.log
-log4j.appender.file.layout=org.apache.log4j.PatternLayout
-log4j.appender.file.layout.ConversionPattern=%d{dd.MM.yyyy HH:mm:ss} *%-5p* [%t] %c{1}: %m (%F, line %L)\n
-
-log4j.logger.org.apache.jackrabbit=WARN
\ No newline at end of file
diff --git a/src/test/resources/logback-test.xml b/src/test/resources/logback-test.xml
new file mode 100644
index 0000000..94021e2
--- /dev/null
+++ b/src/test/resources/logback-test.xml
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ 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.
+-->
+<configuration>
+ <appender name="file" class="ch.qos.logback.core.FileAppender">
+ <File>target/testing.log</File>
+ <encoder>
+ <pattern>%d{dd.MM.yyyy HH:mm:ss} *%-5p* [%t] %c{1}: %m \(%F, line %L\)%n</pattern>
+ </encoder>
+ </appender>
+ <logger name="org.apache.jackrabbit" level="WARN"/>
+ <root level="INFO">
+ <appender-ref ref="file"/>
+ </root>
+</configuration>