You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by si...@apache.org on 2019/06/19 21:54:45 UTC

[sling-whiteboard] branch master updated: [feature-diff] added ExtensionsComparatorTest

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 08b7736  [feature-diff] added ExtensionsComparatorTest
08b7736 is described below

commit 08b773698a105534f1a1fa0407888d9896c02613
Author: Simo Tripodi <st...@adobe.com>
AuthorDate: Wed Jun 19 23:54:37 2019 +0200

    [feature-diff] added ExtensionsComparatorTest
---
 feature-diff/pom.xml                               |   6 +
 .../feature/diff/impl/ExtensionsComparator.java    |   6 +-
 .../diff/impl/ExtensionsComparatorTest.java        | 132 +++++++++++++++++++++
 3 files changed, 141 insertions(+), 3 deletions(-)

diff --git a/feature-diff/pom.xml b/feature-diff/pom.xml
index e88bc12..787ac08 100644
--- a/feature-diff/pom.xml
+++ b/feature-diff/pom.xml
@@ -94,6 +94,12 @@
       <artifactId>junit</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.apache.johnzon</groupId>
+      <artifactId>johnzon-core</artifactId>
+      <version>1.0.0</version>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 
   <build>
diff --git a/feature-diff/src/main/java/org/apache/sling/feature/diff/impl/ExtensionsComparator.java b/feature-diff/src/main/java/org/apache/sling/feature/diff/impl/ExtensionsComparator.java
index 303e488..dd3472d 100644
--- a/feature-diff/src/main/java/org/apache/sling/feature/diff/impl/ExtensionsComparator.java
+++ b/feature-diff/src/main/java/org/apache/sling/feature/diff/impl/ExtensionsComparator.java
@@ -43,7 +43,7 @@ public final class ExtensionsComparator extends AbstractFeatureElementComparator
         computeDiff(previous.getExtensions(), current.getExtensions(), target);
     }
 
-    private void computeDiff(Extensions previousExtensions, Extensions currentExtensions, Feature target) {
+    protected void computeDiff(Extensions previousExtensions, Extensions currentExtensions, Feature target) {
         for (Extension previousExtension : previousExtensions) {
             Extension currentExtension = currentExtensions.getByName(previousExtension.getName());
 
@@ -63,7 +63,7 @@ public final class ExtensionsComparator extends AbstractFeatureElementComparator
         }
     }
 
-    public void computeDiff(Extension previousExtension, Extension currentExtension, Feature target) {
+    protected void computeDiff(Extension previousExtension, Extension currentExtension, Feature target) {
         switch (previousExtension.getType()) {
             case ARTIFACTS:
                 Extension targetExtension = new Extension(previousExtension.getType(), previousExtension.getName(), previousExtension.isRequired());
@@ -112,7 +112,7 @@ public final class ExtensionsComparator extends AbstractFeatureElementComparator
                     JsonValue previousNode = parseJSON(previousJSON);
                     JsonValue currentNode = parseJSON(currentJSON); 
 
-                    if (previousNode.equals(currentNode)) {
+                    if (!previousNode.equals(currentNode)) {
                         target.getExtensions().add(currentExtension);
                     }
                 } catch (Throwable t) {
diff --git a/feature-diff/src/test/java/org/apache/sling/feature/diff/impl/ExtensionsComparatorTest.java b/feature-diff/src/test/java/org/apache/sling/feature/diff/impl/ExtensionsComparatorTest.java
new file mode 100644
index 0000000..662e819
--- /dev/null
+++ b/feature-diff/src/test/java/org/apache/sling/feature/diff/impl/ExtensionsComparatorTest.java
@@ -0,0 +1,132 @@
+/*
+ * 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.diff.impl;
+
+import static org.junit.Assert.*;
+
+import java.util.List;
+import java.util.Map;
+
+import static org.apache.sling.feature.ExtensionType.ARTIFACTS;
+import static org.apache.sling.feature.ExtensionType.JSON;
+import static org.apache.sling.feature.ExtensionType.TEXT;
+
+import org.apache.sling.feature.Artifact;
+import org.apache.sling.feature.ArtifactId;
+import org.apache.sling.feature.Extension;
+import org.apache.sling.feature.Extensions;
+import org.junit.Test;
+
+public class ExtensionsComparatorTest extends AbstractComparatorTest<ExtensionsComparator> {
+
+    @Override
+    protected ExtensionsComparator newComparatorInstance() {
+        return new ExtensionsComparator();
+    }
+
+    @Test
+    public void checkTextExtensionRemoved() {
+        Extension removed = new Extension(TEXT, "removed-TEXT-extension", true);
+        removed.setText("This is just a test");
+        checkRemovedExtension(removed);
+    }
+
+    @Test
+    public void checkJSONExtensionRemoved() {
+        Extension removed = new Extension(JSON, "removed-JSON-extension", true);
+        removed.setJSON("[true, 100, null]");
+        checkRemovedExtension(removed);
+    }
+
+    @Test
+    public void checkArtifactsExtensionRemoved() {
+        Extension removed = new Extension(ARTIFACTS, "removed-ARTIFACTS-extension", true);
+        removed.getArtifacts().add(new Artifact(ArtifactId.parse("org.apache.sling:org.apache.sling.diff:1.0.0")));
+        checkRemovedExtension(removed);
+    }
+
+    private void checkRemovedExtension(Extension removedExtension) {
+        Extensions previous = new Extensions();
+        previous.add(removedExtension);
+
+        Extensions current = new Extensions();
+
+        comparator.computeDiff(previous, current, targetFeature);
+
+        assertTrue(targetFeature.getPrototype().getExtensionRemovals().contains(removedExtension.getName()));
+    }
+
+    @Test
+    public void checkRemovedArtifacts() {
+        Extension previousExtension = new Extension(ARTIFACTS, "content-packages", true);
+        ArtifactId removedId = ArtifactId.parse("org.apache.sling:org.apache.sling.diff:1.0.0");
+        previousExtension.getArtifacts().add(new Artifact(removedId));
+
+        Extension currentExtension = new Extension(ARTIFACTS, "content-packages", true);
+
+        comparator.computeDiff(previousExtension, currentExtension, targetFeature);
+
+        Map<String, List<ArtifactId>> artifactExtensionRemovals = targetFeature.getPrototype().getArtifactExtensionRemovals(); 
+        assertFalse(artifactExtensionRemovals.isEmpty());
+        assertTrue(artifactExtensionRemovals.containsKey("content-packages"));
+        assertTrue(artifactExtensionRemovals.get("content-packages").contains(removedId));
+    }
+
+    @Test
+    public void checkTextExtensionUpdated() {
+        Extension previous = new Extension(TEXT, "repoinit", true);
+        previous.setText("create path /content/example.com(mixin mix:referenceable)");
+
+        Extension current = new Extension(TEXT, "repoinit", true);
+        current.setText("create path /content/example.com(mixin mix:referenceable)\ncreate path (nt:unstructured) /var");
+
+        comparator.computeDiff(previous, current, targetFeature);
+
+        assertEquals(current.getText(), targetFeature.getExtensions().getByName(current.getName()).getText());
+    }
+
+    @Test
+    public void checkJSONExtensionUpdated() {
+        Extension previous = new Extension(JSON, "api-regions", true);
+        previous.setJSON("{\"name\": \"global\"}");
+
+        Extension current = new Extension(JSON, "api-regions", true);
+        current.setJSON("{\"name\": \"deprecated\"}");
+
+        comparator.computeDiff(previous, current, targetFeature);
+
+        assertEquals(current.getJSON(), targetFeature.getExtensions().getByName(current.getName()).getJSON());
+    }
+
+    @Test
+    public void checkArtifactsExtensionUpdated() {
+        Extension previous = new Extension(ARTIFACTS, "content-packages", true);
+        ArtifactId removedId = ArtifactId.parse("org.apache.sling:org.apache.sling.diff:1.0.0");
+        previous.getArtifacts().add(new Artifact(removedId));
+
+        Extension current = new Extension(ARTIFACTS, "content-packages", true);
+        ArtifactId updatedId = ArtifactId.parse("org.apache.sling:org.apache.sling.diff:2.0.0");
+        current.getArtifacts().add(new Artifact(updatedId));
+
+        comparator.computeDiff(previous, current, targetFeature);
+
+        Map<String, List<ArtifactId>> artifactExtensionRemovals = targetFeature.getPrototype().getArtifactExtensionRemovals();
+        assertTrue(artifactExtensionRemovals.get("content-packages").contains(removedId));
+        assertTrue(targetFeature.getExtensions().getByName(current.getName()).getArtifacts().contains(current.getArtifacts().iterator().next()));
+    }
+
+}