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/04/03 00:34:01 UTC

[sling-whiteboard] branch master updated: [feature-diff] started adding main entries unit tests

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 8c98539  [feature-diff] started adding main entries unit tests
8c98539 is described below

commit 8c98539c0563a81379dbb991e7c31ced396400e5
Author: stripodi <st...@192.168.1.111>
AuthorDate: Wed Apr 3 02:33:53 2019 +0200

    [feature-diff] started adding main entries unit tests
---
 .../org/apache/sling/feature/diff/DiffSection.java |   4 +-
 .../org/apache/sling/feature/diff/UpdatedItem.java |  31 +++++-
 .../apache/sling/feature/diff/DiffSectionTest.java | 105 +++++++++++++++++++++
 .../sling/feature/diff/UpdatedItemTest.java}       |  33 +++----
 4 files changed, 152 insertions(+), 21 deletions(-)

diff --git a/feature-diff/src/main/java/org/apache/sling/feature/diff/DiffSection.java b/feature-diff/src/main/java/org/apache/sling/feature/diff/DiffSection.java
index 2a1af1e..8ccc5db 100644
--- a/feature-diff/src/main/java/org/apache/sling/feature/diff/DiffSection.java
+++ b/feature-diff/src/main/java/org/apache/sling/feature/diff/DiffSection.java
@@ -60,8 +60,8 @@ public final class DiffSection {
     }
 
     protected <T> void markItemUpdated(String id, T previous, T current) {
-        T checkedPrevious = requireNonNull(previous);
-        T checkedCurrent = requireNonNull(current);
+        T checkedPrevious = previous;
+        T checkedCurrent = current;
         updatedItems.add(new UpdatedItem<T>(id, checkedPrevious, checkedCurrent));
     }
 
diff --git a/feature-diff/src/main/java/org/apache/sling/feature/diff/UpdatedItem.java b/feature-diff/src/main/java/org/apache/sling/feature/diff/UpdatedItem.java
index db623de..a65a024 100644
--- a/feature-diff/src/main/java/org/apache/sling/feature/diff/UpdatedItem.java
+++ b/feature-diff/src/main/java/org/apache/sling/feature/diff/UpdatedItem.java
@@ -16,6 +16,10 @@
  */
 package org.apache.sling.feature.diff;
 
+import static java.util.Objects.requireNonNull;
+
+import java.util.Objects;
+
 public final class UpdatedItem<T> {
 
     private final String id;
@@ -25,7 +29,7 @@ public final class UpdatedItem<T> {
     private final T current;
 
     protected UpdatedItem(String id, T previous, T current) {
-        this.id = id;
+        this.id = requireNonNull(id);
         this.previous = previous;
         this.current = current;
     }
@@ -42,4 +46,29 @@ public final class UpdatedItem<T> {
         return current;
     }
 
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+
+        if (obj == null) {
+            return false;
+        }
+
+        if (getClass() != obj.getClass()) {
+            return false;
+        }
+
+        UpdatedItem<?> other = (UpdatedItem<?>) obj;
+        return Objects.equals(id, other.getId())
+                && Objects.equals(previous, other.getPrevious())
+                && Objects.equals(current, other.getCurrent());
+    }
+
+    @Override
+    public String toString() {
+        return "UpdatedItem [id=" + id + ", previous=" + previous + ", current=" + current + "]";
+    }
+
 }
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
new file mode 100644
index 0000000..dbb2423
--- /dev/null
+++ b/feature-diff/src/test/java/org/apache/sling/feature/diff/DiffSectionTest.java
@@ -0,0 +1,105 @@
+/*
+ * 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.*;
+
+import org.junit.Test;
+
+public class DiffSectionTest {
+
+    @Test(expected = NullPointerException.class)
+    public void requiresValidId() {
+        new DiffSection(null);
+    }
+
+    @Test
+    public void emptyCheck() {
+        DiffSection emptyDiff = new DiffSection("empty");
+        assertTrue(emptyDiff.isEmpty());
+    }
+
+    @Test(expected = NullPointerException.class)
+    public void unacceptedNullAdded() {
+        new DiffSection("added").markAdded(null);
+    }
+
+    @Test
+    public void validAddedField() {
+        String expectedAddedValue = "sample";
+        DiffSection addedDiff = new DiffSection("added");
+        addedDiff.markAdded(expectedAddedValue);
+
+        assertFalse(addedDiff.isEmpty());
+        for (String currentAddedValue : addedDiff.getAdded()) {
+            assertEquals(expectedAddedValue, currentAddedValue);
+        }
+    }
+
+    @Test(expected = NullPointerException.class)
+    public void unacceptedNullRemoved() {
+        new DiffSection("removed").markRemoved(null);
+    }
+
+    @Test
+    public void validRemovedField() {
+        String expectedRemovedValue = "removed";
+        DiffSection removedDiff = new DiffSection("removed");
+        removedDiff.markAdded(expectedRemovedValue);
+
+        assertFalse(removedDiff.isEmpty());
+        for (String currentRemovedValue : removedDiff.getRemoved()) {
+            assertEquals(expectedRemovedValue, currentRemovedValue);
+        }
+    }
+
+    @Test(expected = NullPointerException.class)
+    public void unacceptedNullUpdatedId() {
+        new DiffSection("updated").markItemUpdated(null, null, null);
+    }
+
+    @Test
+    public void validItemUpdated() {
+        UpdatedItem<String> expectedUpdatedItem = new UpdatedItem<String>("expected", "previous", "current");
+
+        DiffSection updatedDiff = new DiffSection("updated");
+        updatedDiff.markItemUpdated(expectedUpdatedItem.getId(), expectedUpdatedItem.getPrevious(), expectedUpdatedItem.getCurrent());
+
+        assertFalse(updatedDiff.isEmpty());
+        for (UpdatedItem<?> currentUpdatedItem : updatedDiff.getUpdatedItems()) {
+            assertEquals(currentUpdatedItem, expectedUpdatedItem);
+        }
+    }
+
+    @Test(expected = NullPointerException.class)
+    public void unacceptedNullUpdated() {
+        new DiffSection("updated").markUpdated(null);
+    }
+
+    @Test
+    public void validUpdatedSubDiff() {
+        DiffSection mainDiff = new DiffSection("main");
+        DiffSection childDiff = new DiffSection("child");
+        mainDiff.markUpdated(childDiff);
+
+        assertFalse(mainDiff.isEmpty());
+        for (DiffSection updatedDiff : mainDiff.getUpdates()) {
+            assertEquals(updatedDiff, childDiff);
+        }
+    }
+
+}
diff --git a/feature-diff/src/main/java/org/apache/sling/feature/diff/UpdatedItem.java b/feature-diff/src/test/java/org/apache/sling/feature/diff/UpdatedItemTest.java
similarity index 54%
copy from feature-diff/src/main/java/org/apache/sling/feature/diff/UpdatedItem.java
copy to feature-diff/src/test/java/org/apache/sling/feature/diff/UpdatedItemTest.java
index db623de..0e0fa6a 100644
--- a/feature-diff/src/main/java/org/apache/sling/feature/diff/UpdatedItem.java
+++ b/feature-diff/src/test/java/org/apache/sling/feature/diff/UpdatedItemTest.java
@@ -16,30 +16,27 @@
  */
 package org.apache.sling.feature.diff;
 
-public final class UpdatedItem<T> {
+import static org.junit.Assert.assertEquals;
 
-    private final String id;
+import org.junit.Test;
 
-    private final T previous;
+public class UpdatedItemTest {
 
-    private final T current;
-
-    protected UpdatedItem(String id, T previous, T current) {
-        this.id = id;
-        this.previous = previous;
-        this.current = current;
-    }
-
-    public String getId() {
-        return id;
+    @Test(expected = NullPointerException.class)
+    public void requiresIdNotNull() {
+        new UpdatedItem<Object>(null, null, null);
     }
 
-    public T getPrevious() {
-        return previous;
-    }
+    @Test
+    public void expectedFields() {
+        String expectedId = "expectedId";
+        String expectedPrevious = "expectedPrevious";
+        String expectedCurrent = "expectedCurrent";
+        UpdatedItem<String> item = new UpdatedItem<String>(expectedId, expectedPrevious, expectedCurrent);
 
-    public T getCurrent() {
-        return current;
+        assertEquals(expectedId, item.getId());
+        assertEquals(expectedPrevious, item.getPrevious());
+        assertEquals(expectedCurrent, item.getCurrent());
     }
 
 }