This is an automated email from the ASF dual-hosted git repository.

kwin pushed a commit to branch master
in repository 
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-feature-cpconverter.git


The following commit(s) were added to refs/heads/master by this push:
     new 7030446  SLING-10207 correctly extract CND files below jcr_root (#65)
7030446 is described below

commit 703044640a3720dfee9c5acd8dec82fd895c864e
Author: Konrad Windszus <[email protected]>
AuthorDate: Wed Mar 17 12:51:09 2021 +0100

    SLING-10207 correctly extract CND files below jcr_root (#65)
    
    default handler must match arbitrary cnd files below /META-INF/vault
---
 .../ContentPackage2FeatureModelConverter.java      |  2 +-
 .../handlers/NodeTypesEntryHandler.java            | 18 +++++++-
 .../handlers/NodeTypesEntryHandlerTest.java        | 54 ++++++++++++++++++++++
 3 files changed, 71 insertions(+), 3 deletions(-)

diff --git 
a/src/main/java/org/apache/sling/feature/cpconverter/ContentPackage2FeatureModelConverter.java
 
b/src/main/java/org/apache/sling/feature/cpconverter/ContentPackage2FeatureModelConverter.java
index ebd0c33..84bc7ac 100644
--- 
a/src/main/java/org/apache/sling/feature/cpconverter/ContentPackage2FeatureModelConverter.java
+++ 
b/src/main/java/org/apache/sling/feature/cpconverter/ContentPackage2FeatureModelConverter.java
@@ -429,7 +429,7 @@ public class ContentPackage2FeatureModelConverter extends 
BaseVaultPackageScanne
 
     @Override
     protected void addCdnPattern(@NotNull Pattern cndPattern) {
-        handlersManager.addEntryHandler(new NodeTypesEntryHandler(cndPattern));
+        
handlersManager.addEntryHandler(NodeTypesEntryHandler.forCndPattern(cndPattern));
     }
 
 }
diff --git 
a/src/main/java/org/apache/sling/feature/cpconverter/handlers/NodeTypesEntryHandler.java
 
b/src/main/java/org/apache/sling/feature/cpconverter/handlers/NodeTypesEntryHandler.java
index d653a2f..3d0d7c4 100644
--- 
a/src/main/java/org/apache/sling/feature/cpconverter/handlers/NodeTypesEntryHandler.java
+++ 
b/src/main/java/org/apache/sling/feature/cpconverter/handlers/NodeTypesEntryHandler.java
@@ -23,16 +23,30 @@ import java.util.regex.Pattern;
 
 import org.apache.jackrabbit.vault.fs.io.Archive;
 import org.apache.jackrabbit.vault.fs.io.Archive.Entry;
+import org.apache.jackrabbit.vault.util.Constants;
 import 
org.apache.sling.feature.cpconverter.ContentPackage2FeatureModelConverter;
 import org.jetbrains.annotations.NotNull;
 
 public class NodeTypesEntryHandler extends AbstractRegexEntryHandler {
 
     public NodeTypesEntryHandler() {
-        super("/META-INF/vault/nodetypes\\.cnd");
+        super("/META-INF/vault/.*\\.cnd");
     }
 
-    public NodeTypesEntryHandler(@NotNull Pattern pattern) {
+    public static NodeTypesEntryHandler forCndPattern(@NotNull Pattern 
pattern) {
+        // as the pattern refers to an absolute repository path, the prefix 
jcr_root needs to be manually added
+        // should work for most of the patterns
+        String originalCndRegex = pattern.pattern();
+        if (originalCndRegex.startsWith("^")) {
+            originalCndRegex = originalCndRegex.substring(1);
+        }
+        if (originalCndRegex.startsWith("/")) {
+            originalCndRegex = originalCndRegex.substring(1);
+        }
+        return new 
NodeTypesEntryHandler(Pattern.compile("/"+Constants.ROOT_DIR+"/"+originalCndRegex));
+    }
+
+    private NodeTypesEntryHandler(@NotNull Pattern pattern) {
         super(pattern);
     }
 
diff --git 
a/src/test/java/org/apache/sling/feature/cpconverter/handlers/NodeTypesEntryHandlerTest.java
 
b/src/test/java/org/apache/sling/feature/cpconverter/handlers/NodeTypesEntryHandlerTest.java
new file mode 100644
index 0000000..5d12bd3
--- /dev/null
+++ 
b/src/test/java/org/apache/sling/feature/cpconverter/handlers/NodeTypesEntryHandlerTest.java
@@ -0,0 +1,54 @@
+/*
+ * 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.feature.cpconverter.handlers;
+
+import java.util.regex.Pattern;
+
+import org.apache.jackrabbit.vault.fs.io.ImportOptions;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class NodeTypesEntryHandlerTest {
+
+    @Test
+    public void testForCndPatternWithDefaultPattern() {
+        // test with default pattern
+        NodeTypesEntryHandler handler = 
NodeTypesEntryHandler.forCndPattern(new ImportOptions().getCndPattern());
+        
Assert.assertTrue(handler.matches("/jcr_root/apps/myapp/nodetypes/test.cnd"));
+        // nodetype outside jcr_root
+        
Assert.assertFalse(handler.matches("/META-INF/apps/myapp/nodetypes/test.cnd"));
+        // invalid nodetypes folder
+        
Assert.assertFalse(handler.matches("/jcr_root/apps/myapp/nodetypes2/test.cnd"));
+        // invalid extension
+        
Assert.assertFalse(handler.matches("/jcr_root/apps/myapp/nodetypes/test.xml"));
+    }
+
+    @Test
+    public void testForCndPatternWithCustomPattern() {
+        // test with default pattern
+        NodeTypesEntryHandler handler = 
NodeTypesEntryHandler.forCndPattern(Pattern.compile(".*/mynts/.*\\.cnd"));
+        
Assert.assertTrue(handler.matches("/jcr_root/some/where/deep/mynts/test.cnd"));
+    }
+
+    @Test
+    public void testDefaultHandler() {
+        NodeTypesEntryHandler handler = new NodeTypesEntryHandler();
+        Assert.assertTrue(handler.matches("/META-INF/vault/nodetypes.cnd"));
+        Assert.assertTrue(handler.matches("/META-INF/vault/custom.cnd"));
+        Assert.assertTrue(handler.matches("/META-INF/vault/my/deep/nt.cnd"));
+    }
+}

Reply via email to