bosschaert closed pull request #4: SLING-8085 - adjusting ContentHandler to 
properly incorporating layer…
URL: 
https://github.com/apache/sling-org-apache-sling-feature-extension-content/pull/4
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/pom.xml b/pom.xml
index e467dcf..a9d4c04 100644
--- a/pom.xml
+++ b/pom.xml
@@ -112,7 +112,7 @@
         <dependency>
             <groupId>org.apache.jackrabbit.vault</groupId>
             <artifactId>org.apache.jackrabbit.vault</artifactId>
-            <version>3.2.4</version>
+            <version>3.2.5-SNAPSHOT</version>
             <scope>compile</scope>
         </dependency>
            <dependency>
diff --git 
a/src/main/java/org/apache/sling/feature/extension/content/ContentHandler.java 
b/src/main/java/org/apache/sling/feature/extension/content/ContentHandler.java
index 1a57118..8f039b7 100644
--- 
a/src/main/java/org/apache/sling/feature/extension/content/ContentHandler.java
+++ 
b/src/main/java/org/apache/sling/feature/extension/content/ContentHandler.java
@@ -21,9 +21,12 @@
 import java.nio.file.Paths;
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.LinkedHashMap;
+import java.util.HashSet;
+
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
+import java.util.TreeMap;
 
 import org.apache.commons.collections.map.MultiValueMap;
 import org.apache.jackrabbit.vault.packaging.PackageId;
@@ -48,7 +51,7 @@
 
     private static final String REGISTRY_FOLDER = "packageregistry";
 
-    private static ExecutionPlanBuilder 
buildExecutionPlan(Collection<Artifact> artifacts, LauncherPrepareContext 
prepareContext, File registryHome) throws Exception {
+    private static ExecutionPlanBuilder 
buildExecutionPlan(Collection<Artifact> artifacts, Set<PackageId> 
satisfiedPackages, LauncherPrepareContext prepareContext, File registryHome) 
throws Exception {
 
         List<File> packageReferences = new ArrayList<File>();
 
@@ -67,6 +70,7 @@ private static ExecutionPlanBuilder 
buildExecutionPlan(Collection<Artifact> arti
         FSPackageRegistry registry = new FSPackageRegistry(registryHome);
 
         ExecutionPlanBuilder builder = registry.createExecutionPlan();
+        builder.with(satisfiedPackages);
 
         for (File pkgFile : packageReferences) {
             PackageId pid = registry.registerExternal(pkgFile, true);
@@ -83,6 +87,7 @@ private static ExecutionPlanBuilder 
buildExecutionPlan(Collection<Artifact> arti
             builder.addTask().with(pid).with(Type.EXTRACT);
         }
         builder.validate();
+        satisfiedPackages.addAll(builder.preview());
         return builder;
 
     }
@@ -93,7 +98,7 @@ public boolean handle(Extension extension, 
LauncherPrepareContext prepareContext
         File registryHome = getRegistryHomeDir(installationContext);
         if (extension.getType() == ExtensionType.ARTIFACTS
                 && 
extension.getName().equals(FeatureConstants.EXTENSION_NAME_CONTENT_PACKAGES)) {
-            MultiValueMap orderedArtifacts = MultiValueMap.decorate(new 
LinkedHashMap<Integer, Collection<Artifact>>());
+            MultiValueMap orderedArtifacts = MultiValueMap.decorate(new 
TreeMap<Integer, Collection<Artifact>>());
             for (final Artifact a : extension.getArtifacts()) {
                 int order;
                 // content-packages without explicit start-order to be 
installed last
@@ -105,10 +110,11 @@ public boolean handle(Extension extension, 
LauncherPrepareContext prepareContext
                 orderedArtifacts.put(order, a);
             }
             List<String> executionPlans = new ArrayList<String>();
+            Set<PackageId> satisfiedPackages = new HashSet<>();
             for (Object key : orderedArtifacts.keySet()) {
                 @SuppressWarnings("unchecked")
                 Collection<Artifact> artifacts = 
orderedArtifacts.getCollection(key);
-                ExecutionPlanBuilder builder = buildExecutionPlan(artifacts, 
prepareContext, registryHome);
+                ExecutionPlanBuilder builder = buildExecutionPlan(artifacts, 
satisfiedPackages,  prepareContext, registryHome);
                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
                 builder.save(baos);
                 executionPlans.add(baos.toString("UTF-8"));
diff --git 
a/src/test/java/org/apache/sling/feature/extension/content/ContentHandlerTest.java
 
b/src/test/java/org/apache/sling/feature/extension/content/ContentHandlerTest.java
new file mode 100644
index 0000000..3e97c18
--- /dev/null
+++ 
b/src/test/java/org/apache/sling/feature/extension/content/ContentHandlerTest.java
@@ -0,0 +1,129 @@
+/*
+ * 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.extension.content;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.eq;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.io.File;
+import java.net.URL;
+import java.util.Dictionary;
+import java.util.Iterator;
+
+import org.apache.sling.feature.Artifact;
+import org.apache.sling.feature.ArtifactId;
+import org.apache.sling.feature.Extension;
+import org.apache.sling.feature.ExtensionType;
+import org.apache.sling.feature.launcher.spi.LauncherPrepareContext;
+import 
org.apache.sling.feature.launcher.spi.extensions.ExtensionInstallationContext;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.junit.runner.RunWith;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+
+@RunWith(MockitoJUnitRunner.class)
+public class ContentHandlerTest {
+    
+
+    @Rule
+    public TemporaryFolder testFolder = new TemporaryFolder();
+    
+    @Mock
+    LauncherPrepareContext prepareContext;
+    
+    @Mock
+    ExtensionInstallationContext installationContext;
+    
+    /**
+     * Test package A-1.0. Depends on B and C-1.X
+     */
+    private static final String COORDINATES_TEST_PACKAGE_A_10 = 
"my_packages:test_a:1.0";
+    private static String TEST_PACKAGE_A_10 = "testpackages/test_a-1.0.zip";
+    private static ArtifactId TEST_PACKAGE_AID_A_10 = 
ArtifactId.fromMvnId(COORDINATES_TEST_PACKAGE_A_10);
+
+    /**
+     * Test package B-1.0. Depends on C
+     */
+    private static final String COORDINATES_TEST_PACKAGE_B_10 = 
"my_packages:test_b:1.0";
+    private static String TEST_PACKAGE_B_10 = "testpackages/test_b-1.0.zip";
+    private static ArtifactId TEST_PACKAGE_AID_B_10 = 
ArtifactId.fromMvnId(COORDINATES_TEST_PACKAGE_B_10);
+
+    /**
+     * Test package C-1.0
+     */
+    private static final String COORDINATES_TEST_PACKAGE_C_10 = 
"my_packages:test_c:1.0";
+    private static String TEST_PACKAGE_C_10 = "testpackages/test_c-1.0.zip";
+    private static ArtifactId TEST_PACKAGE_AID_C_10 = 
ArtifactId.fromMvnId(COORDINATES_TEST_PACKAGE_C_10);
+
+    @Before
+    public void setUp() throws Exception {
+        URL test_a = this.getClass().getResource(TEST_PACKAGE_A_10);
+        
when(prepareContext.getArtifactFile(TEST_PACKAGE_AID_A_10)).thenReturn(new 
File(test_a.getFile()));
+        URL test_b = this.getClass().getResource(TEST_PACKAGE_B_10);
+        
when(prepareContext.getArtifactFile(TEST_PACKAGE_AID_B_10)).thenReturn(new 
File(test_b.getFile()));
+        URL test_c = this.getClass().getResource(TEST_PACKAGE_C_10);
+        
when(prepareContext.getArtifactFile(TEST_PACKAGE_AID_C_10)).thenReturn(new 
File(test_c.getFile()));
+    }
+
+    @Test
+    public void testMultipleStartOrders() throws Exception {
+        ContentHandler ch = new ContentHandler();
+        System.setProperty(ContentHandler.PACKAGEREGISTRY_HOME, 
testFolder.getRoot().toString());
+        Extension ext = new Extension(ExtensionType.ARTIFACTS, 
"content-packages", false);
+        Artifact artifact_a = new Artifact(TEST_PACKAGE_AID_A_10);
+        Artifact artifact_b = new Artifact(TEST_PACKAGE_AID_B_10);
+        Artifact artifact_c = new Artifact(TEST_PACKAGE_AID_C_10);
+        artifact_a.getMetadata().put("start-order", "2");
+        artifact_b.getMetadata().put("start-order", "1");
+        artifact_c.getMetadata().put("start-order", "1");
+        ext.getArtifacts().add(artifact_a);
+        ext.getArtifacts().add(artifact_b);
+        ext.getArtifacts().add(artifact_c);
+        @SuppressWarnings("unchecked")
+        ArgumentCaptor<Dictionary<String, Object>> executionPlanCaptor = 
ArgumentCaptor.forClass(Dictionary.class);
+        
+        ch.handle(ext, prepareContext, installationContext);
+        
verify(installationContext).addConfiguration(eq("org.apache.sling.jcr.packageinit.impl.ExecutionPlanRepoInitializer"),
 any(), executionPlanCaptor.capture());
+        
verify(installationContext).addConfiguration(eq("org.apache.jackrabbit.vault.packaging.registry.impl.FSPackageRegistry"),
 any(), any());
+        Iterator<Dictionary<String, Object>> dictIt = 
executionPlanCaptor.getAllValues().iterator();
+        final String[] executionplans = (String[]) 
dictIt.next().get("executionplans");
+        final String expected_0 =
+                "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
+                        "<executionPlan version=\"1.0\">\n" +
+                        "    <task cmd=\"extract\" 
packageId=\"my_packages:test_c:1.0\"/>\n" +
+                        "    <task cmd=\"extract\" 
packageId=\"my_packages:test_b:1.0\"/>\n" +
+                        "</executionPlan>\n";
+
+        assertEquals(expected_0, executionplans[0]);
+        final String expected_1 =
+                "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
+                        "<executionPlan version=\"1.0\">\n" +
+                        "    <task cmd=\"extract\" 
packageId=\"my_packages:test_a:1.0\"/>\n" +
+                        "</executionPlan>\n";
+
+        assertEquals(expected_1, executionplans[1]);
+        assertFalse(dictIt.hasNext());
+    }
+}
diff --git 
a/src/test/resources/org/apache/sling/feature/extension/content/testpackages/test_a-1.0.zip
 
b/src/test/resources/org/apache/sling/feature/extension/content/testpackages/test_a-1.0.zip
new file mode 100644
index 0000000..08df03a
Binary files /dev/null and 
b/src/test/resources/org/apache/sling/feature/extension/content/testpackages/test_a-1.0.zip
 differ
diff --git 
a/src/test/resources/org/apache/sling/feature/extension/content/testpackages/test_b-1.0.zip
 
b/src/test/resources/org/apache/sling/feature/extension/content/testpackages/test_b-1.0.zip
new file mode 100644
index 0000000..85fac13
Binary files /dev/null and 
b/src/test/resources/org/apache/sling/feature/extension/content/testpackages/test_b-1.0.zip
 differ
diff --git 
a/src/test/resources/org/apache/sling/feature/extension/content/testpackages/test_c-1.0.zip
 
b/src/test/resources/org/apache/sling/feature/extension/content/testpackages/test_c-1.0.zip
new file mode 100644
index 0000000..245291a
Binary files /dev/null and 
b/src/test/resources/org/apache/sling/feature/extension/content/testpackages/test_c-1.0.zip
 differ


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to