You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by an...@apache.org on 2021/08/31 16:12:55 UTC

[sling-org-apache-sling-feature-cpconverter] branch SLING-10769 created (now 4371edc)

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

angela pushed a change to branch SLING-10769
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-feature-cpconverter.git.


      at 4371edc  SLING-10769 : VaultPackageAssembler should not implement EntryHandler

This branch includes the following new commits:

     new 4371edc  SLING-10769 : VaultPackageAssembler should not implement EntryHandler

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


[sling-org-apache-sling-feature-cpconverter] 01/01: SLING-10769 : VaultPackageAssembler should not implement EntryHandler

Posted by an...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 4371edceca6b57dbfb3a76c92bfaac7e42e8d054
Author: angela <an...@adobe.com>
AuthorDate: Tue Aug 31 18:12:27 2021 +0200

    SLING-10769 : VaultPackageAssembler should not implement EntryHandler
---
 .../ContentPackage2FeatureModelConverter.java      |  3 +-
 .../cpconverter/handlers/DefaultHandler.java       | 57 ++++++++++++++
 .../cpconverter/vltpkg/VaultPackageAssembler.java  | 28 +------
 .../cpconverter/handlers/DefaultHandlerTest.java   | 90 ++++++++++++++++++++++
 .../vltpkg/VaultPackageAssemblerTest.java          |  5 --
 5 files changed, 153 insertions(+), 30 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 4249863..d571d18 100644
--- a/src/main/java/org/apache/sling/feature/cpconverter/ContentPackage2FeatureModelConverter.java
+++ b/src/main/java/org/apache/sling/feature/cpconverter/ContentPackage2FeatureModelConverter.java
@@ -52,6 +52,7 @@ import org.apache.sling.feature.cpconverter.artifacts.ArtifactsDeployer;
 import org.apache.sling.feature.cpconverter.artifacts.FileArtifactWriter;
 import org.apache.sling.feature.cpconverter.features.FeaturesManager;
 import org.apache.sling.feature.cpconverter.filtering.ResourceFilter;
+import org.apache.sling.feature.cpconverter.handlers.DefaultHandler;
 import org.apache.sling.feature.cpconverter.handlers.EntryHandler;
 import org.apache.sling.feature.cpconverter.handlers.EntryHandlersManager;
 import org.apache.sling.feature.cpconverter.handlers.NodeTypesEntryHandler;
@@ -476,7 +477,7 @@ public class ContentPackage2FeatureModelConverter extends BaseVaultPackageScanne
 
         EntryHandler entryHandler = handlersManager.getEntryHandlerByEntryPath(entryPath);
         if (entryHandler == null) {
-            entryHandler = getMainPackageAssembler();
+            entryHandler = new DefaultHandler(getMainPackageAssembler(), removeInstallHooks);
         }
 
         if (entry == null) {
diff --git a/src/main/java/org/apache/sling/feature/cpconverter/handlers/DefaultHandler.java b/src/main/java/org/apache/sling/feature/cpconverter/handlers/DefaultHandler.java
new file mode 100644
index 0000000..5ffff3e
--- /dev/null
+++ b/src/main/java/org/apache/sling/feature/cpconverter/handlers/DefaultHandler.java
@@ -0,0 +1,57 @@
+/*
+ * 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 org.apache.jackrabbit.vault.fs.io.Archive;
+import org.apache.jackrabbit.vault.util.Constants;
+import org.apache.sling.feature.cpconverter.ContentPackage2FeatureModelConverter;
+import org.apache.sling.feature.cpconverter.vltpkg.VaultPackageAssembler;
+import org.jetbrains.annotations.NotNull;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+
+public class DefaultHandler implements EntryHandler {
+
+    private static final Logger log = LoggerFactory.getLogger(DefaultHandler.class);
+    
+    private static final String INSTALL_HOOK_ROOT = "/" + Constants.META_DIR + "/" + Constants.HOOKS_DIR;
+
+    private final VaultPackageAssembler mainPackageAssembler;
+    private final boolean removeInstallHooks;
+    
+    public DefaultHandler(@NotNull VaultPackageAssembler mainPackageAssembler, boolean removeInstallHooks) {
+        this.mainPackageAssembler = mainPackageAssembler;
+        this.removeInstallHooks = removeInstallHooks;
+    }
+    
+    @Override
+    public boolean matches(@NotNull String path) {
+        return true;
+    }
+
+    @Override
+    public void handle(@NotNull String path, @NotNull Archive archive, @NotNull Archive.Entry entry, @NotNull ContentPackage2FeatureModelConverter converter)
+            throws IOException {
+        if (removeInstallHooks && path.startsWith(INSTALL_HOOK_ROOT)) {
+            log.info("Skipping install hook {} from original package", path);
+        } else {
+            mainPackageAssembler.addEntry(path, archive, entry);
+        }
+    }
+}
\ No newline at end of file
diff --git a/src/main/java/org/apache/sling/feature/cpconverter/vltpkg/VaultPackageAssembler.java b/src/main/java/org/apache/sling/feature/cpconverter/vltpkg/VaultPackageAssembler.java
index cc3d112..7f04523 100644
--- a/src/main/java/org/apache/sling/feature/cpconverter/vltpkg/VaultPackageAssembler.java
+++ b/src/main/java/org/apache/sling/feature/cpconverter/vltpkg/VaultPackageAssembler.java
@@ -29,12 +29,9 @@ import org.apache.jackrabbit.vault.packaging.PackageId;
 import org.apache.jackrabbit.vault.packaging.PackageProperties;
 import org.apache.jackrabbit.vault.packaging.PackageType;
 import org.apache.jackrabbit.vault.packaging.VaultPackage;
-import org.apache.jackrabbit.vault.util.Constants;
 import org.apache.jackrabbit.vault.util.PlatformNameFormat;
 import org.apache.sling.feature.cpconverter.ContentPackage2FeatureModelConverter;
-import org.apache.sling.feature.cpconverter.ConverterException;
 import org.apache.sling.feature.cpconverter.handlers.DefaultEntryParser;
-import org.apache.sling.feature.cpconverter.handlers.EntryHandler;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 import org.slf4j.Logger;
@@ -71,7 +68,7 @@ import static org.apache.sling.feature.cpconverter.vltpkg.VaultPackageUtils.getD
 import static org.apache.sling.feature.cpconverter.vltpkg.VaultPackageUtils.setDependencies;
 import static org.apache.sling.feature.cpconverter.vltpkg.VaultPackageUtils.toRepositoryPath;
 
-public class VaultPackageAssembler implements EntryHandler {
+public class VaultPackageAssembler {
 
     private static final Pattern OSGI_BUNDLE_PATTERN = Pattern.compile("(jcr_root)?/apps/[^/]+/install(\\.([^/]+))?/.+\\.jar");
     
@@ -87,18 +84,16 @@ public class VaultPackageAssembler implements EntryHandler {
     private final File storingDirectory;
     private final Properties properties;
     private final File tmpDir;
-    private final boolean removeInstallHooks;
     
     /**
      * This class can not be instantiated from outside
      */
     private VaultPackageAssembler(@NotNull File tempDir, @NotNull File storingDirectory, @NotNull Properties properties, 
-                                  @NotNull Set<Dependency> dependencies, boolean removeInstallHooks) {
+                                  @NotNull Set<Dependency> dependencies) {
         this.storingDirectory = storingDirectory;
         this.properties = properties;
         this.dependencies = dependencies;
         this.tmpDir = tempDir;
-        this.removeInstallHooks = removeInstallHooks;
     }
     
     /**
@@ -134,7 +129,7 @@ public class VaultPackageAssembler implements EntryHandler {
 
         Set<Dependency> dependencies = getDependencies(vaultPackage);
 
-        VaultPackageAssembler assembler = new VaultPackageAssembler(tempDir, storingDirectory, properties, dependencies, removeInstallHooks);
+        VaultPackageAssembler assembler = new VaultPackageAssembler(tempDir, storingDirectory, properties, dependencies);
         assembler.mergeFilters(Objects.requireNonNull(vaultPackage.getMetaInf().getFilter()));
         return assembler;
     }
@@ -157,7 +152,7 @@ public class VaultPackageAssembler implements EntryHandler {
         props.put(PackageProperties.NAME_VERSION, packageId.getVersionString() + VERSION_SUFFIX);
 
         props.put(PackageProperties.NAME_DESCRIPTION, description);
-        return new VaultPackageAssembler(tempDir, storingDirectory, props, new HashSet<>(), false);
+        return new VaultPackageAssembler(tempDir, storingDirectory, props, new HashSet<>());
     }
 
     private static @NotNull File initStoringDirectory(PackageId packageId, @NotNull File tempDir) {
@@ -182,21 +177,6 @@ public class VaultPackageAssembler implements EntryHandler {
         return this.tmpDir;
     }
 
-    @Override
-    public boolean matches(@NotNull String path) {
-        return true;
-    }
-
-    @Override
-    public void handle(@NotNull String path, @NotNull Archive archive, @NotNull Entry entry, @NotNull ContentPackage2FeatureModelConverter converter)
-            throws IOException, ConverterException {
-        if (removeInstallHooks && path.startsWith("/" + Constants.META_DIR + "/" + Constants.HOOKS_DIR)) {
-            log.info("Skipping install hook {} from original package", path);
-        } else {
-            addEntry(path, archive, entry);
-        }
-    }
-
     public @NotNull Properties getPackageProperties() {
         return this.properties;
     }
diff --git a/src/test/java/org/apache/sling/feature/cpconverter/handlers/DefaultHandlerTest.java b/src/test/java/org/apache/sling/feature/cpconverter/handlers/DefaultHandlerTest.java
new file mode 100644
index 0000000..bd3f9a0
--- /dev/null
+++ b/src/test/java/org/apache/sling/feature/cpconverter/handlers/DefaultHandlerTest.java
@@ -0,0 +1,90 @@
+/*
+ * 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 org.apache.jackrabbit.vault.fs.io.Archive;
+import org.apache.jackrabbit.vault.util.Constants;
+import org.apache.sling.feature.cpconverter.ContentPackage2FeatureModelConverter;
+import org.apache.sling.feature.cpconverter.vltpkg.VaultPackageAssembler;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoInteractions;
+
+public class DefaultHandlerTest {
+
+    private static final Logger log = LoggerFactory.getLogger(DefaultHandlerTest.class);
+    
+    private final VaultPackageAssembler assembler = mock(VaultPackageAssembler.class);
+    
+    @Test
+    public void testMatch() {
+        DefaultHandler handler = new DefaultHandler(assembler, true);
+        assertTrue(handler.matches(""));
+        assertTrue(handler.matches("/"));
+        assertTrue(handler.matches("/jcr_root/.content.xml"));
+        assertTrue(handler.matches("/jcr_root/content"));
+        assertTrue(handler.matches("/jcr_root/apps"));
+        assertTrue(handler.matches("/jcr_root/content/_rep_policy.xml"));
+
+        verifyNoInteractions(assembler);
+    }
+    
+    @Test
+    public void testHandleInstallHooksTrue() throws Exception {
+        Archive archive = mock(Archive.class);
+        Archive.Entry entry = mock(Archive.Entry.class);
+        ContentPackage2FeatureModelConverter converter = mock(ContentPackage2FeatureModelConverter.class);
+
+        DefaultHandler handler = new DefaultHandler(assembler, true);
+        handler.handle("/" + Constants.META_DIR + "/" + Constants.HOOKS_DIR, archive, entry, converter);
+        
+        verifyNoInteractions(assembler, archive, entry, converter);
+    }
+
+    @Test
+    public void testHandleInstallHooksFalse() throws Exception {
+        Archive archive = mock(Archive.class);
+        Archive.Entry entry = mock(Archive.Entry.class);
+        ContentPackage2FeatureModelConverter converter = mock(ContentPackage2FeatureModelConverter.class);
+        String path = "/" + Constants.META_DIR + "/" + Constants.HOOKS_DIR + "/subdir";
+
+        DefaultHandler handler = new DefaultHandler(assembler, false);
+        handler.handle(path, archive, entry, converter);
+
+        verifyNoInteractions(archive, entry, converter);
+        verify(assembler).addEntry(path, archive, entry);
+    }
+
+    @Test
+    public void testHandleRegularPath() throws Exception {
+        Archive archive = mock(Archive.class);
+        Archive.Entry entry = mock(Archive.Entry.class);
+        ContentPackage2FeatureModelConverter converter = mock(ContentPackage2FeatureModelConverter.class);
+        String path = "/" + Constants.ROOT_DIR + "/content" + Constants.DOT_CONTENT_XML;
+
+        DefaultHandler handler = new DefaultHandler(assembler, true);
+        handler.handle(path, archive, entry, converter);
+
+        verifyNoInteractions(archive, entry, converter);
+        verify(assembler).addEntry(path, archive, entry);
+    }
+}
\ No newline at end of file
diff --git a/src/test/java/org/apache/sling/feature/cpconverter/vltpkg/VaultPackageAssemblerTest.java b/src/test/java/org/apache/sling/feature/cpconverter/vltpkg/VaultPackageAssemblerTest.java
index efc96f5..ae794cf 100644
--- a/src/test/java/org/apache/sling/feature/cpconverter/vltpkg/VaultPackageAssemblerTest.java
+++ b/src/test/java/org/apache/sling/feature/cpconverter/vltpkg/VaultPackageAssemblerTest.java
@@ -75,11 +75,6 @@ public class VaultPackageAssemblerTest {
     }
     
     @Test
-    public void matchAll() {
-        assembler.matches(resourceLocation);
-    }
-
-    @Test
     public void packageResource() throws Exception {
         if (resourceLocation != null) {
             assembler.addEntry(resourceLocation, getClass().getResourceAsStream("../handlers" + resourceLocation));