You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ss...@apache.org on 2018/11/05 17:32:28 UTC

[sling-org-apache-sling-testing-jcr-mock] 01/01: SLING-8066 remove property if set to null (for all nullable property value types)

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

sseifert pushed a commit to branch feature/SLING-8066
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-jcr-mock.git

commit a47b2cd5277968ae83ea5b6e26b2fc3ea4af35bf
Author: sseifert <ss...@pro-vision.de>
AuthorDate: Mon Nov 5 18:31:37 2018 +0100

    SLING-8066 remove property if set to null (for all nullable property value types)
---
 .../apache/sling/testing/mock/jcr/MockNode.java    |  56 ++++++--
 .../sling/testing/mock/jcr/MockProperty.java       |  54 ++++++--
 .../sling/testing/mock/jcr/MockPropertyTest.java   | 143 +++++++++++++++++++++
 3 files changed, 233 insertions(+), 20 deletions(-)

diff --git a/src/main/java/org/apache/sling/testing/mock/jcr/MockNode.java b/src/main/java/org/apache/sling/testing/mock/jcr/MockNode.java
index db7bdc0..6767c2a 100644
--- a/src/main/java/org/apache/sling/testing/mock/jcr/MockNode.java
+++ b/src/main/java/org/apache/sling/testing/mock/jcr/MockNode.java
@@ -195,7 +195,12 @@ class MockNode extends AbstractItem implements Node {
         ItemData itemData = ItemData.newProperty(getPath() + "/" + name);
         Property property = new MockProperty(itemData, getSession());
         property.setValue(value);
-        getMockedSession().addItem(itemData);
+        if (value == null) {
+            getMockedSession().removeItem(itemData.getPath());
+        }
+        else {
+            getMockedSession().addItem(itemData);
+        }
         this.itemData.setIsChanged(true);
         return property;
     }
@@ -205,7 +210,12 @@ class MockNode extends AbstractItem implements Node {
         ItemData itemData = ItemData.newProperty(getPath() + "/" + name);
         Property property = new MockProperty(itemData, getSession());
         property.setValue(values);
-        getMockedSession().addItem(itemData);
+        if (values == null) {
+            getMockedSession().removeItem(itemData.getPath());
+        }
+        else {
+            getMockedSession().addItem(itemData);
+        }
         this.itemData.setIsChanged(true);
         return property;
     }
@@ -215,7 +225,12 @@ class MockNode extends AbstractItem implements Node {
         ItemData itemData = ItemData.newProperty(getPath() + "/" + name);
         Property property = new MockProperty(itemData, getSession());
         property.setValue(values);
-        getMockedSession().addItem(itemData);
+        if (values == null) {
+            getMockedSession().removeItem(itemData.getPath());
+        }
+        else {
+            getMockedSession().addItem(itemData);
+        }
         this.itemData.setIsChanged(true);
         return property;
     }
@@ -225,7 +240,12 @@ class MockNode extends AbstractItem implements Node {
         ItemData itemData = ItemData.newProperty(getPath() + "/" + name);
         Property property = new MockProperty(itemData, getSession());
         property.setValue(value);
-        getMockedSession().addItem(itemData);
+        if (value == null) {
+            getMockedSession().removeItem(itemData.getPath());
+        }
+        else {
+            getMockedSession().addItem(itemData);
+        }
         this.itemData.setIsChanged(true);
         return property;
     }
@@ -236,7 +256,12 @@ class MockNode extends AbstractItem implements Node {
         ItemData itemData = ItemData.newProperty(getPath() + "/" + name);
         Property property = new MockProperty(itemData, getSession());
         property.setValue(value);
-        getMockedSession().addItem(itemData);
+        if (value == null) {
+            getMockedSession().removeItem(itemData.getPath());
+        }
+        else {
+            getMockedSession().addItem(itemData);
+        }
         this.itemData.setIsChanged(true);
         return property;
     }
@@ -276,7 +301,12 @@ class MockNode extends AbstractItem implements Node {
         ItemData itemData = ItemData.newProperty(getPath() + "/" + name);
         Property property = new MockProperty(itemData, getSession());
         property.setValue(value);
-        getMockedSession().addItem(itemData);
+        if (value == null) {
+            getMockedSession().removeItem(itemData.getPath());
+        }
+        else {
+            getMockedSession().addItem(itemData);
+        }
         this.itemData.setIsChanged(true);
         return property;
     }
@@ -296,7 +326,12 @@ class MockNode extends AbstractItem implements Node {
         ItemData itemData = ItemData.newProperty(getPath() + "/" + name);
         Property property = new MockProperty(itemData, getSession());
         property.setValue(value);
-        getMockedSession().addItem(itemData);
+        if (value == null) {
+            getMockedSession().removeItem(itemData.getPath());
+        }
+        else {
+            getMockedSession().addItem(itemData);
+        }
         this.itemData.setIsChanged(true);
         return property;
     }
@@ -306,7 +341,12 @@ class MockNode extends AbstractItem implements Node {
         ItemData itemData = ItemData.newProperty(getPath() + "/" + name);
         Property property = new MockProperty(itemData, getSession());
         property.setValue(value);
-        getMockedSession().addItem(itemData);
+        if (value == null) {
+            getMockedSession().removeItem(itemData.getPath());
+        }
+        else {
+            getMockedSession().addItem(itemData);
+        }
         this.itemData.setIsChanged(true);
         return property;
     }
diff --git a/src/main/java/org/apache/sling/testing/mock/jcr/MockProperty.java b/src/main/java/org/apache/sling/testing/mock/jcr/MockProperty.java
index 5a5bb75..7012d5d 100644
--- a/src/main/java/org/apache/sling/testing/mock/jcr/MockProperty.java
+++ b/src/main/java/org/apache/sling/testing/mock/jcr/MockProperty.java
@@ -80,18 +80,23 @@ class MockProperty extends AbstractItem implements Property {
 
     @Override
     public void setValue(final Value newValue) throws RepositoryException {
+        if (newValue == null) {
+            remove();
+            return;
+        }
         this.itemData.setValues(new Value[] { newValue });
         this.itemData.setMultiple(false);
     }
 
     @Override
     public void setValue(final Value[] newValues) throws RepositoryException {
-        Value[] values = null;
-        if(newValues!=null){
-            values = new Value[newValues.length];
-            for (int i = 0; i < newValues.length; i++) {
-                values[i] = newValues[i];
-            }
+        if (newValues == null) {
+            remove();
+            return;
+        }
+        Value[] values = new Value[newValues.length];
+        for (int i = 0; i < newValues.length; i++) {
+            values[i] = newValues[i];
         }
         this.itemData.setValues(values);
         this.itemData.setMultiple(true);
@@ -99,18 +104,23 @@ class MockProperty extends AbstractItem implements Property {
 
     @Override
     public void setValue(final String newValue) throws RepositoryException {
+        if (newValue == null) {
+            remove();
+            return;
+        }
         this.itemData.setValues(new Value[] { getSession().getValueFactory().createValue(newValue) });
         this.itemData.setMultiple(false);
     }
 
     @Override
     public void setValue(final String[] newValues) throws RepositoryException {
-        Value[] values = null;
-        if(newValues!=null){
-            values = new Value[newValues.length];
-            for (int i = 0; i < newValues.length; i++) {
-                values[i] = getSession().getValueFactory().createValue(newValues[i]);
-            }
+        if (newValues == null) {
+            remove();
+            return;
+        }
+        Value[] values = new Value[newValues.length];
+        for (int i = 0; i < newValues.length; i++) {
+            values[i] = getSession().getValueFactory().createValue(newValues[i]);
         }
         this.itemData.setValues(values);
         this.itemData.setMultiple(true);
@@ -118,6 +128,10 @@ class MockProperty extends AbstractItem implements Property {
 
     @Override
     public void setValue(final InputStream newValue) throws RepositoryException {
+        if (newValue == null) {
+            remove();
+            return;
+        }
         this.itemData.setValues(new Value[] { new BinaryValue(newValue) });
         this.itemData.setMultiple(false);
     }
@@ -136,6 +150,10 @@ class MockProperty extends AbstractItem implements Property {
 
     @Override
     public void setValue(final Calendar newValue) throws RepositoryException {
+        if (newValue == null) {
+            remove();
+            return;
+        }
         this.itemData.setValues(new Value[] { getSession().getValueFactory().createValue(newValue) });
         this.itemData.setMultiple(false);
     }
@@ -148,18 +166,30 @@ class MockProperty extends AbstractItem implements Property {
 
     @Override
     public void setValue(final Node newValue) throws RepositoryException {
+        if (newValue == null) {
+            remove();
+            return;
+        }
         this.itemData.setValues(new Value[] { getSession().getValueFactory().createValue(newValue) });
         this.itemData.setMultiple(false);
     }
 
     @Override
     public void setValue(final Binary newValue) throws RepositoryException {
+        if (newValue == null) {
+            remove();
+            return;
+        }
         this.itemData.setValues(new Value[] { new BinaryValue(newValue) });
         this.itemData.setMultiple(false);
     }
 
     @Override
     public void setValue(final BigDecimal newValue) throws RepositoryException {
+        if (newValue == null) {
+            remove();
+            return;
+        }
         this.itemData.setValues(new Value[] { getSession().getValueFactory().createValue(newValue) });
         this.itemData.setMultiple(false);
     }
diff --git a/src/test/java/org/apache/sling/testing/mock/jcr/MockPropertyTest.java b/src/test/java/org/apache/sling/testing/mock/jcr/MockPropertyTest.java
index 949dbb3..640ff8c 100644
--- a/src/test/java/org/apache/sling/testing/mock/jcr/MockPropertyTest.java
+++ b/src/test/java/org/apache/sling/testing/mock/jcr/MockPropertyTest.java
@@ -25,6 +25,7 @@ import static org.junit.Assert.assertTrue;
 
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.math.BigDecimal;
 import java.util.Calendar;
 
@@ -87,6 +88,22 @@ public class MockPropertyTest {
     }
 
     @Test
+    public void testStringSetNullViaNode() throws RepositoryException {
+        this.node1.setProperty("prop1", "value1");
+
+        this.node1.setProperty("prop1", (String)null);
+        assertFalse(this.node1.hasProperty("prop1"));        
+    }
+    
+    @Test
+    public void testStringSetNullViaProp() throws RepositoryException {
+        this.node1.setProperty("prop1", "value1");
+
+        this.node1.getProperty("prop1").setValue((String)null);
+        assertFalse(this.node1.hasProperty("prop1"));        
+    }
+    
+    @Test
     public void testStringArray() throws RepositoryException {
         String[] value1 = new String[] { "aaa", "bbb" };
         this.node1.setProperty("prop1", value1);
@@ -110,6 +127,24 @@ public class MockPropertyTest {
     }
 
     @Test
+    public void testStringArraySetNullViaNode() throws RepositoryException {
+        String[] value1 = new String[] { "aaa", "bbb" };
+        this.node1.setProperty("prop1", value1);
+
+        this.node1.setProperty("prop1", (String[])null);
+        assertFalse(this.node1.hasProperty("prop1"));        
+    }
+
+    @Test
+    public void testStringArraySetNullViaProp() throws RepositoryException {
+        String[] value1 = new String[] { "aaa", "bbb" };
+        this.node1.setProperty("prop1", value1);
+
+        this.node1.getProperty("prop1").setValue((String[])null);
+        assertFalse(this.node1.hasProperty("prop1"));        
+    }
+
+    @Test
     public void testBoolean() throws RepositoryException {
         this.node1.setProperty("prop1", true);
         Property prop1 = this.node1.getProperty("prop1");
@@ -158,6 +193,22 @@ public class MockPropertyTest {
     }
 
     @Test
+    public void testBigDecimalSetNullViaNode() throws RepositoryException {
+        this.node1.setProperty("prop1", new BigDecimal("1.5"));
+
+        this.node1.setProperty("prop1", (BigDecimal)null);
+        assertFalse(this.node1.hasProperty("prop1"));        
+    }
+
+    @Test
+    public void testBigDecimalSetNullViaProp() throws RepositoryException {
+        this.node1.setProperty("prop1", new BigDecimal("1.5"));
+
+        this.node1.getProperty("prop1").setValue((BigDecimal)null);
+        assertFalse(this.node1.hasProperty("prop1"));        
+    }
+
+    @Test
     public void testCalendar() throws RepositoryException {
         Calendar value1 = Calendar.getInstance();
 
@@ -175,6 +226,24 @@ public class MockPropertyTest {
     }
 
     @Test
+    public void testCalendarSetNullViaNode() throws RepositoryException {
+        Calendar value1 = Calendar.getInstance();
+        this.node1.setProperty("prop1", value1);
+
+        this.node1.setProperty("prop1", (Calendar)null);
+        assertFalse(this.node1.hasProperty("prop1"));        
+    }
+
+    @Test
+    public void testCalendarSetNullViaProp() throws RepositoryException {
+        Calendar value1 = Calendar.getInstance();
+        this.node1.setProperty("prop1", value1);
+
+        this.node1.getProperty("prop1").setValue((Calendar)null);
+        assertFalse(this.node1.hasProperty("prop1"));        
+    }
+
+    @Test
     public void testBinary() throws RepositoryException, IOException {
         byte[] value1 = new byte[] { 0x01, 0x01, 0x03 };
 
@@ -190,6 +259,24 @@ public class MockPropertyTest {
         assertArrayEquals(value2, IOUtils.toByteArray(prop1.getValue().getBinary().getStream()));
     }
 
+    @Test
+    public void testBinarySetNullViaNode() throws RepositoryException {
+        byte[] value1 = new byte[] { 0x01, 0x01, 0x03 };
+        this.node1.setProperty("prop1", new BinaryValue(value1).getBinary());
+
+        this.node1.setProperty("prop1", (BinaryValue)null);
+        assertFalse(this.node1.hasProperty("prop1"));        
+    }
+
+    @Test
+    public void testBinarySetNullViaProp() throws RepositoryException {
+        byte[] value1 = new byte[] { 0x01, 0x01, 0x03 };
+        this.node1.setProperty("prop1", new BinaryValue(value1).getBinary());
+
+        this.node1.getProperty("prop1").setValue((BinaryValue)null);
+        assertFalse(this.node1.hasProperty("prop1"));        
+    }
+
     @SuppressWarnings("deprecation")
     @Test
     public void testInputStream() throws RepositoryException, IOException {
@@ -205,6 +292,26 @@ public class MockPropertyTest {
         assertArrayEquals(value2, IOUtils.toByteArray(prop1.getValue().getStream()));
     }
 
+    @SuppressWarnings("deprecation")
+    @Test
+    public void testInputStreamSetNullViaNode() throws RepositoryException {
+        byte[] value1 = new byte[] { 0x01, 0x01, 0x03 };
+        this.node1.setProperty("prop1", new ByteArrayInputStream(value1));
+
+        this.node1.setProperty("prop1", (InputStream)null);
+        assertFalse(this.node1.hasProperty("prop1"));        
+    }
+
+    @SuppressWarnings("deprecation")
+    @Test
+    public void testInputStreamSetNullViaProp() throws RepositoryException {
+        byte[] value1 = new byte[] { 0x01, 0x01, 0x03 };
+        this.node1.setProperty("prop1", new ByteArrayInputStream(value1));
+
+        this.node1.getProperty("prop1").setValue((InputStream)null);
+        assertFalse(this.node1.hasProperty("prop1"));        
+    }
+
     @Test
     public void testValue() throws RepositoryException {
         this.node1.setProperty("prop1", this.session.getValueFactory().createValue("value1"));
@@ -222,6 +329,22 @@ public class MockPropertyTest {
     }
 
     @Test
+    public void testValueSetNullViaNode() throws RepositoryException {
+        this.node1.setProperty("prop1", this.session.getValueFactory().createValue("value1"));
+
+        this.node1.setProperty("prop1", (Value)null);
+        assertFalse(this.node1.hasProperty("prop1"));        
+    }
+
+    @Test
+    public void testValueSetNullViaProp() throws RepositoryException {
+        this.node1.setProperty("prop1", this.session.getValueFactory().createValue("value1"));
+
+        this.node1.getProperty("prop1").setValue((Value)null);
+        assertFalse(this.node1.hasProperty("prop1"));        
+    }
+
+    @Test
     public void testValueArray() throws RepositoryException {
         Value[] value1 = new Value[] { this.session.getValueFactory().createValue("aaa"),
                 this.session.getValueFactory().createValue("bbb") };
@@ -246,6 +369,26 @@ public class MockPropertyTest {
     }
 
     @Test
+    public void testValueArraySetNullViaNode() throws RepositoryException {
+        Value[] value1 = new Value[] { this.session.getValueFactory().createValue("aaa"),
+                this.session.getValueFactory().createValue("bbb") };
+        this.node1.setProperty("prop1", value1);
+
+        this.node1.setProperty("prop1", (Value[])null);
+        assertFalse(this.node1.hasProperty("prop1"));        
+    }
+
+    @Test
+    public void testValueArraySetNullViaProp() throws RepositoryException {
+        Value[] value1 = new Value[] { this.session.getValueFactory().createValue("aaa"),
+                this.session.getValueFactory().createValue("bbb") };
+        this.node1.setProperty("prop1", value1);
+
+        this.node1.getProperty("prop1").setValue((Value[])null);
+        assertFalse(this.node1.hasProperty("prop1"));        
+    }
+
+    @Test
     public void testEmptyArrayGetType() throws RepositoryException {
         this.node1.setProperty("prop1", new Value[] {});
         Property prop1 = this.node1.getProperty("prop1");