This is an automated email from the ASF dual-hosted git repository. davidb pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-feature-extension-content.git
commit f8ee6b974c123648212581502e1eba772bde590e Author: Dominik Suess <[email protected]> AuthorDate: Thu Nov 8 12:46:53 2018 +0100 SLING-8077 - adding testcoverage and processing of source & target even when being null --- pom.xml | 13 +++ .../content/ContentOrderMergeProcessor.java | 14 ++- .../content/ContentOrderMergeProcessorTest.java | 124 +++++++++++++++++++++ 3 files changed, 146 insertions(+), 5 deletions(-) diff --git a/pom.xml b/pom.xml index f5c121d..e467dcf 100644 --- a/pom.xml +++ b/pom.xml @@ -192,5 +192,18 @@ <version>1.3.1</version> <scope>compile</scope> </dependency> + <!-- Testing --> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-core</artifactId> + <version>2.23.0</version> + <scope>test</scope> + </dependency> </dependencies> </project> diff --git a/src/main/java/org/apache/sling/feature/extension/content/ContentOrderMergeProcessor.java b/src/main/java/org/apache/sling/feature/extension/content/ContentOrderMergeProcessor.java index 05b1133..9289e4c 100644 --- a/src/main/java/org/apache/sling/feature/extension/content/ContentOrderMergeProcessor.java +++ b/src/main/java/org/apache/sling/feature/extension/content/ContentOrderMergeProcessor.java @@ -24,13 +24,15 @@ import org.apache.sling.feature.FeatureConstants; import org.apache.sling.feature.KeyValueMap; import org.apache.sling.feature.builder.HandlerContext; import org.apache.sling.feature.builder.MergeHandler; -import org.apache.sling.feature.builder.PostProcessHandler; public class ContentOrderMergeProcessor implements MergeHandler { - private static final String DEFAULT_CONTENT_START_ORDER = "default.content.startorder"; + public static final String DEFAULT_CONTENT_START_ORDER = "default.content.startorder"; - private void processFeature(HandlerContext context, Feature feature, Extension extension) { + private void processFeature(Feature feature, Extension extension) { + if (feature == null) { + return; + } String defaultOrder = feature.getVariables().get(DEFAULT_CONTENT_START_ORDER); if (defaultOrder != null) { for (Artifact a : extension.getArtifacts()) { @@ -51,12 +53,14 @@ public class ContentOrderMergeProcessor implements MergeHandler { @Override public void merge(HandlerContext context, Feature target, Feature source, Extension targetEx, Extension sourceEx) { + + processFeature(target, targetEx); + processFeature(source, sourceEx); + if (targetEx == null) { target.getExtensions().add(sourceEx); return; } - processFeature(context, target, targetEx); - processFeature(context, source, sourceEx); for (final Artifact a : sourceEx.getArtifacts()) { boolean replace = true; final Artifact existing = targetEx.getArtifacts().getSame(a.getId()); diff --git a/src/test/java/org/apache/sling/feature/extension/content/ContentOrderMergeProcessorTest.java b/src/test/java/org/apache/sling/feature/extension/content/ContentOrderMergeProcessorTest.java new file mode 100644 index 0000000..15c5b8f --- /dev/null +++ b/src/test/java/org/apache/sling/feature/extension/content/ContentOrderMergeProcessorTest.java @@ -0,0 +1,124 @@ +/* + * 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.assertNull; +import static org.junit.Assert.assertTrue; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +import org.apache.sling.feature.Artifact; +import org.apache.sling.feature.ArtifactId; +import org.apache.sling.feature.Artifacts; +import org.apache.sling.feature.Extension; +import org.apache.sling.feature.ExtensionType; +import org.apache.sling.feature.Feature; +import org.apache.sling.feature.builder.HandlerContext; +import org.junit.Test; +import org.mockito.Mock; + +public class ContentOrderMergeProcessorTest { + + @Mock + HandlerContext handlerContext; + + @Test + public void testMergeDifferentStartOrders() { + final Artifacts targetArtifacts = new Artifacts(); + + final ArtifactId tid1 = ArtifactId.fromMvnId("sling:targetpack1:1"); + Artifact targetpack1 = new Artifact(tid1); + assertNull(targetpack1.getMetadata().get("start-order")); + targetArtifacts.add(targetpack1); + + final ArtifactId tid2 = ArtifactId.fromMvnId("sling:targetpack2:1"); + Artifact targetpack2 = new Artifact(tid2); + assertNull(targetpack2.getMetadata().get("start-order")); + targetArtifacts.add(targetpack2); + + final Artifacts sourceArtifacts = new Artifacts(); + + final ArtifactId sid1 = ArtifactId.fromMvnId("sling:sourcepack1:1"); + final Artifact sourcepack1 = new Artifact(sid1); + assertNull(sourcepack1.getMetadata().get("start-order")); + sourceArtifacts.add(sourcepack1); + + final ArtifactId sid2 = ArtifactId.fromMvnId("sling:sourcepack2:1"); + Artifact sourcepack2 = new Artifact(sid2); + assertNull(sourcepack2.getMetadata().get("start-order")); + sourceArtifacts.add(sourcepack2); + + final Extension targetEx = new Extension(ExtensionType.ARTIFACTS, "content-package", false); + targetEx.getArtifacts().addAll(targetArtifacts); + final Feature target = new Feature(ArtifactId.fromMvnId("sling:targettest:1")); + target.getExtensions().add(targetEx); + target.getVariables().put(ContentOrderMergeProcessor.DEFAULT_CONTENT_START_ORDER, "1"); + + final Extension sourceEx = new Extension(ExtensionType.ARTIFACTS, "content-package", false); + sourceEx.getArtifacts().addAll(sourceArtifacts); + final Feature source = new Feature(ArtifactId.fromMvnId("sling:sourcetest:1")); + source.getExtensions().add(sourceEx); + source.getVariables().put(ContentOrderMergeProcessor.DEFAULT_CONTENT_START_ORDER, "2"); + + final Set<Artifact> testArtifacts = new HashSet<>(Arrays.asList(targetpack1, targetpack2, sourcepack1, sourcepack2)); + + ContentOrderMergeProcessor comp = new ContentOrderMergeProcessor(); + comp.merge(handlerContext, target, source, targetEx, sourceEx); + + + Artifacts mergedArtifacts = targetEx.getArtifacts(); + assertTrue(mergedArtifacts.containsAll(testArtifacts)); + + assertEquals("1", mergedArtifacts.getSame(tid1).getMetadata().get("start-order")); + assertEquals("1", mergedArtifacts.getSame(tid2).getMetadata().get("start-order")); + assertEquals("2", mergedArtifacts.getSame(sid1).getMetadata().get("start-order")); + assertEquals("2", mergedArtifacts.getSame(sid2).getMetadata().get("start-order")); + + } + + + @Test + public void testEmptyTargetExtension() { + final Artifacts sourceArtifacts = new Artifacts(); + + final ArtifactId sid1 = ArtifactId.fromMvnId("sling:sourcepack1:1"); + final Artifact sourcepack1 = new Artifact(sid1); + assertNull(sourcepack1.getMetadata().get("start-order")); + sourceArtifacts.add(sourcepack1); + + final Feature target = new Feature(ArtifactId.fromMvnId("sling:targettest:1")); + + final Extension sourceEx = new Extension(ExtensionType.ARTIFACTS, "content-package", false); + sourceEx.getArtifacts().addAll(sourceArtifacts); + + final Feature source = new Feature(ArtifactId.fromMvnId("sling:sourcetest:1")); + source.getExtensions().add(sourceEx); + source.getVariables().put(ContentOrderMergeProcessor.DEFAULT_CONTENT_START_ORDER, "2"); + + ContentOrderMergeProcessor comp = new ContentOrderMergeProcessor(); + comp.merge(handlerContext, target, source, null, sourceEx); + + + Artifacts mergedArtifacts = target.getExtensions().getByName("content-package").getArtifacts(); + assertTrue(mergedArtifacts.contains(sourcepack1)); + assertEquals("2", mergedArtifacts.getSame(sid1).getMetadata().get("start-order")); + } + +}
