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 4bf5bfa  [feature-diff] added ArtifactsComparatorTest and refined 
other tests
4bf5bfa is described below

commit 4bf5bfa6eb03ffabe89d2fc0793d920293259bfd
Author: stripodi <[email protected]>
AuthorDate: Wed Apr 3 11:48:27 2019 +0200

    [feature-diff] added ArtifactsComparatorTest and refined other tests
---
 .../diff/AbstractFeatureElementComparator.java     | 20 +++--
 .../sling/feature/diff/ArtifactsComparator.java    |  6 +-
 .../feature/diff/FeatureElementComparator.java     |  6 --
 .../feature/diff/ArtifactsComparatorTest.java      | 87 ++++++++++++++++++++++
 .../apache/sling/feature/diff/DiffSectionTest.java | 22 ++----
 5 files changed, 112 insertions(+), 29 deletions(-)

diff --git 
a/feature-diff/src/main/java/org/apache/sling/feature/diff/AbstractFeatureElementComparator.java
 
b/feature-diff/src/main/java/org/apache/sling/feature/diff/AbstractFeatureElementComparator.java
index f83bfba..1e1a3d3 100644
--- 
a/feature-diff/src/main/java/org/apache/sling/feature/diff/AbstractFeatureElementComparator.java
+++ 
b/feature-diff/src/main/java/org/apache/sling/feature/diff/AbstractFeatureElementComparator.java
@@ -16,38 +16,46 @@
  */
 package org.apache.sling.feature.diff;
 
+import static java.util.Objects.requireNonNull;
+
 abstract class AbstractFeatureElementComparator<T, I extends Iterable<T>> 
implements FeatureElementComparator<T, I> {
 
     private final String id;
 
     public AbstractFeatureElementComparator(String id) {
-        this.id = id;
+        this.id = requireNonNull(id, "Null id can not be used to the create a 
new comparator");
     }
 
+    protected abstract String getId(T item);
+
+    protected abstract T find(T item, I collection);
+
+    protected abstract DiffSection compare(T previous, T current);
+
     @Override
     public final DiffSection apply(I previouses, I currents) {
         final DiffSection diffDsection = new DiffSection(id);
 
-        previouses.forEach(previous -> {
+        for (T previous : previouses) {
             T current = find(previous, currents);
 
             if (current == null) {
                 diffDsection.markRemoved(getId(previous));
             } else {
                 DiffSection updateSection = compare(previous, current);
-                if (!updateSection.isEmpty()) {
+                if (updateSection != null && !updateSection.isEmpty()) {
                     diffDsection.markUpdated(updateSection);
                 }
             }
-        });
+        }
 
-        currents.forEach(current -> {
+        for (T current : currents) {
             T previous = find(current, previouses);
 
             if (previous == null) {
                 diffDsection.markAdded(getId(current));
             }
-        });
+        };
 
         return diffDsection;
     }
diff --git 
a/feature-diff/src/main/java/org/apache/sling/feature/diff/ArtifactsComparator.java
 
b/feature-diff/src/main/java/org/apache/sling/feature/diff/ArtifactsComparator.java
index 5793f6e..dfd6136 100644
--- 
a/feature-diff/src/main/java/org/apache/sling/feature/diff/ArtifactsComparator.java
+++ 
b/feature-diff/src/main/java/org/apache/sling/feature/diff/ArtifactsComparator.java
@@ -21,7 +21,7 @@ import java.util.Objects;
 import org.apache.sling.feature.Artifact;
 import org.apache.sling.feature.Artifacts;
 
-public final class ArtifactsComparator extends 
AbstractFeatureElementComparator<Artifact, Artifacts> {
+final class ArtifactsComparator extends 
AbstractFeatureElementComparator<Artifact, Artifacts> {
 
     public ArtifactsComparator(String id) {
         super(id);
@@ -39,7 +39,7 @@ public final class ArtifactsComparator extends 
AbstractFeatureElementComparator<
 
     @Override
     public DiffSection compare(Artifact previous, Artifact current) {
-        DiffSection diffSection = new DiffSection(current.getId().toMvnId());
+        DiffSection diffSection = new DiffSection(getId(current));
 
         String previousVersion = previous.getId().getVersion();
         String currentVersion = current.getId().getVersion();
@@ -51,7 +51,7 @@ public final class ArtifactsComparator extends 
AbstractFeatureElementComparator<
             diffSection.markItemUpdated("start-order", 
previous.getStartOrder(), current.getStartOrder());
         }
 
-        return null;
+        return diffSection;
     }
 
 }
diff --git 
a/feature-diff/src/main/java/org/apache/sling/feature/diff/FeatureElementComparator.java
 
b/feature-diff/src/main/java/org/apache/sling/feature/diff/FeatureElementComparator.java
index f64f04e..6042d7f 100644
--- 
a/feature-diff/src/main/java/org/apache/sling/feature/diff/FeatureElementComparator.java
+++ 
b/feature-diff/src/main/java/org/apache/sling/feature/diff/FeatureElementComparator.java
@@ -20,10 +20,4 @@ import java.util.function.BiFunction;
 
 public interface FeatureElementComparator<T, I extends Iterable<T>> extends 
BiFunction<I, I, DiffSection> {
 
-    String getId(T item);
-
-    T find(T item, I collection);
-
-    DiffSection compare(T previous, T current);
-
 }
diff --git 
a/feature-diff/src/test/java/org/apache/sling/feature/diff/ArtifactsComparatorTest.java
 
b/feature-diff/src/test/java/org/apache/sling/feature/diff/ArtifactsComparatorTest.java
new file mode 100644
index 0000000..51c0b6e
--- /dev/null
+++ 
b/feature-diff/src/test/java/org/apache/sling/feature/diff/ArtifactsComparatorTest.java
@@ -0,0 +1,87 @@
+/*
+ * 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;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+
+import org.apache.sling.feature.Artifact;
+import org.apache.sling.feature.ArtifactId;
+import org.apache.sling.feature.Artifacts;
+import org.junit.Test;
+
+public class ArtifactsComparatorTest {
+
+    @Test(expected = NullPointerException.class)
+    public void nullIdNotAcceptedByTheConstructor() {
+        new ArtifactsComparator(null);
+    }
+
+    @Test
+    public void checkRemoved() {
+        Artifacts previousArtifacts = new Artifacts();
+        Artifact previousArtifact = new 
Artifact(ArtifactId.fromMvnId("org.apache.sling:org.apache.sling.feature.diff:1.0.0"));
+        previousArtifacts.add(previousArtifact);
+
+        Artifacts currentArtifacts = new Artifacts();
+
+        DiffSection artifactsDiff = new 
ArtifactsComparator("bundles").apply(previousArtifacts, currentArtifacts);
+        assertFalse(artifactsDiff.isEmpty());
+
+        assertEquals(artifactsDiff.getRemoved().iterator().next(), 
previousArtifact.getId().toMvnId());
+    }
+
+    @Test
+    public void checkAdded() {
+        Artifacts previousArtifacts = new Artifacts();
+
+        Artifacts currentArtifacts = new Artifacts();
+        Artifact currentArtifact = new 
Artifact(ArtifactId.fromMvnId("org.apache.sling:org.apache.sling.feature.diff:1.0.0"));
+        currentArtifacts.add(currentArtifact);
+
+        DiffSection artifactsDiff = new 
ArtifactsComparator("bundles").apply(previousArtifacts, currentArtifacts);
+        assertFalse(artifactsDiff.isEmpty());
+
+        assertEquals(artifactsDiff.getAdded().iterator().next(), 
currentArtifact.getId().toMvnId());
+    }
+
+    @Test
+    public void checkUpdated() {
+        Artifacts previousArtifacts = new Artifacts();
+        Artifact previousArtifact = new 
Artifact(ArtifactId.fromMvnId("org.apache.sling:org.apache.sling.feature.diff:0.0.1"));
+        previousArtifacts.add(previousArtifact);
+
+        Artifacts currentArtifacts = new Artifacts();
+        Artifact currentArtifact = new 
Artifact(ArtifactId.fromMvnId("org.apache.sling:org.apache.sling.feature.diff:1.0.0"));
+        currentArtifacts.add(currentArtifact);
+
+        DiffSection artifactsDiff = new 
ArtifactsComparator("bundles").apply(previousArtifacts, currentArtifacts);
+        assertFalse(artifactsDiff.isEmpty());
+
+        DiffSection artifactDiff = 
artifactsDiff.getUpdates().iterator().next();
+        for (UpdatedItem<?> updatedItem : artifactDiff.getUpdatedItems()) {
+            if ("version".equals(updatedItem.getId())) {
+                assertEquals(previousArtifact.getId().getVersion(), 
updatedItem.getPrevious());
+                assertEquals(currentArtifact.getId().getVersion(), 
updatedItem.getCurrent());
+            } else if ("start-order".equals(updatedItem.getId())) {
+                assertEquals(previousArtifact.getStartOrder(), 
updatedItem.getPrevious());
+                assertEquals(currentArtifact.getStartOrder(), 
updatedItem.getCurrent());
+            }
+        }
+    }
+
+}
diff --git 
a/feature-diff/src/test/java/org/apache/sling/feature/diff/DiffSectionTest.java 
b/feature-diff/src/test/java/org/apache/sling/feature/diff/DiffSectionTest.java
index dbb2423..2720381 100644
--- 
a/feature-diff/src/test/java/org/apache/sling/feature/diff/DiffSectionTest.java
+++ 
b/feature-diff/src/test/java/org/apache/sling/feature/diff/DiffSectionTest.java
@@ -16,7 +16,9 @@
  */
 package org.apache.sling.feature.diff;
 
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
 
 import org.junit.Test;
 
@@ -45,9 +47,7 @@ public class DiffSectionTest {
         addedDiff.markAdded(expectedAddedValue);
 
         assertFalse(addedDiff.isEmpty());
-        for (String currentAddedValue : addedDiff.getAdded()) {
-            assertEquals(expectedAddedValue, currentAddedValue);
-        }
+        assertEquals(addedDiff.getAdded().iterator().next(), 
expectedAddedValue);
     }
 
     @Test(expected = NullPointerException.class)
@@ -59,12 +59,10 @@ public class DiffSectionTest {
     public void validRemovedField() {
         String expectedRemovedValue = "removed";
         DiffSection removedDiff = new DiffSection("removed");
-        removedDiff.markAdded(expectedRemovedValue);
+        removedDiff.markRemoved(expectedRemovedValue);
 
         assertFalse(removedDiff.isEmpty());
-        for (String currentRemovedValue : removedDiff.getRemoved()) {
-            assertEquals(expectedRemovedValue, currentRemovedValue);
-        }
+        assertEquals(removedDiff.getRemoved().iterator().next(), 
expectedRemovedValue);
     }
 
     @Test(expected = NullPointerException.class)
@@ -80,9 +78,7 @@ public class DiffSectionTest {
         updatedDiff.markItemUpdated(expectedUpdatedItem.getId(), 
expectedUpdatedItem.getPrevious(), expectedUpdatedItem.getCurrent());
 
         assertFalse(updatedDiff.isEmpty());
-        for (UpdatedItem<?> currentUpdatedItem : 
updatedDiff.getUpdatedItems()) {
-            assertEquals(currentUpdatedItem, expectedUpdatedItem);
-        }
+        assertEquals(updatedDiff.getUpdatedItems().iterator().next(), 
expectedUpdatedItem);
     }
 
     @Test(expected = NullPointerException.class)
@@ -97,9 +93,7 @@ public class DiffSectionTest {
         mainDiff.markUpdated(childDiff);
 
         assertFalse(mainDiff.isEmpty());
-        for (DiffSection updatedDiff : mainDiff.getUpdates()) {
-            assertEquals(updatedDiff, childDiff);
-        }
+        assertEquals(mainDiff.getUpdates().iterator().next(), childDiff);
     }
 
 }

Reply via email to