You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ra...@apache.org on 2018/01/19 13:38:30 UTC

[sling-org-apache-sling-scripting-sightly-compiler-java] branch issue/SLING-7404 created (now 02bd032)

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

radu pushed a change to branch issue/SLING-7404
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-scripting-sightly-compiler-java.git.


      at 02bd032  SLING-7404 - ObjectModel and similar classes need consistent null checks

This branch includes the following new commits:

     new 02bd032  SLING-7404 - ObjectModel and similar classes need consistent null checks

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


-- 
To stop receiving notification emails like this one, please contact
['"commits@sling.apache.org" <co...@sling.apache.org>'].

[sling-org-apache-sling-scripting-sightly-compiler-java] 01/01: SLING-7404 - ObjectModel and similar classes need consistent null checks

Posted by ra...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

radu pushed a commit to branch issue/SLING-7404
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-scripting-sightly-compiler-java.git

commit 02bd0320128d42e8d7a9c2a57ca4716fabe71410
Author: Radu Cotescu <ra...@apache.org>
AuthorDate: Fri Jan 19 14:37:18 2018 +0100

    SLING-7404 - ObjectModel and similar classes need consistent null checks
---
 .../sightly/render/AbstractRuntimeObjectModel.java |   9 ++
 .../render/AbstractRuntimeObjectModelTest.java     | 116 +++++++++++++++++++++
 2 files changed, 125 insertions(+)

diff --git a/src/main/java/org/apache/sling/scripting/sightly/render/AbstractRuntimeObjectModel.java b/src/main/java/org/apache/sling/scripting/sightly/render/AbstractRuntimeObjectModel.java
index 7235cfa..2e77d46 100644
--- a/src/main/java/org/apache/sling/scripting/sightly/render/AbstractRuntimeObjectModel.java
+++ b/src/main/java/org/apache/sling/scripting/sightly/render/AbstractRuntimeObjectModel.java
@@ -73,6 +73,9 @@ public abstract class AbstractRuntimeObjectModel implements RuntimeObjectModel {
 
     @Override
     public Object resolveProperty(Object target, Object property) {
+        if (target == null || property == null) {
+            return null;
+        }
         Object resolved = null;
         if (property instanceof Number) {
             resolved = ObjectModel.getIndex(target, ((Number) property).intValue());
@@ -133,6 +136,9 @@ public abstract class AbstractRuntimeObjectModel implements RuntimeObjectModel {
     }
 
     protected Object getProperty(Object target, Object propertyObj) {
+        if (target == null || propertyObj == null) {
+            return null;
+        }
         String property = ObjectModel.toString(propertyObj);
         Object result = null;
         if (target instanceof Record) {
@@ -205,6 +211,9 @@ public abstract class AbstractRuntimeObjectModel implements RuntimeObjectModel {
      */
     @Deprecated
     protected Object getMapProperty(Map map, String property) {
+        if (map == null) {
+            return null;
+        }
         return map.get(property);
     }
 
diff --git a/src/test/java/org/apache/sling/scripting/sightly/render/AbstractRuntimeObjectModelTest.java b/src/test/java/org/apache/sling/scripting/sightly/render/AbstractRuntimeObjectModelTest.java
index 12bd65c..87f14a0 100644
--- a/src/test/java/org/apache/sling/scripting/sightly/render/AbstractRuntimeObjectModelTest.java
+++ b/src/test/java/org/apache/sling/scripting/sightly/render/AbstractRuntimeObjectModelTest.java
@@ -17,11 +17,16 @@
 package org.apache.sling.scripting.sightly.render;
 
 import java.util.Arrays;
+import java.util.Calendar;
+import java.util.Collection;
 import java.util.Collections;
+import java.util.Date;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
+import org.apache.sling.scripting.sightly.Record;
 import org.junit.Test;
 
 import static org.junit.Assert.*;
@@ -32,6 +37,9 @@ public class AbstractRuntimeObjectModelTest {
 
     @Test
     public void testResolveProperty() {
+        assertNull(runtimeObjectModel.resolveProperty(null, null));
+        assertNull(runtimeObjectModel.resolveProperty(this, null));
+        assertNull(runtimeObjectModel.resolveProperty(this, ""));
         assertEquals(0, runtimeObjectModel.resolveProperty(Collections.EMPTY_LIST, "size"));
         assertNull(runtimeObjectModel.resolveProperty(null, null));
         int[] ints = new int[] {1, 2, 3};
@@ -58,6 +66,114 @@ public class AbstractRuntimeObjectModelTest {
         }};
         assertEquals("one", runtimeObjectModel.resolveProperty(stringMap, 1));
         assertEquals("two", runtimeObjectModel.resolveProperty(stringMap, 2));
+        Map<String, String> strings = new HashMap<String, String>(){{
+            put("a", "one");
+            put("b", "two");
+        }};
+        Record<String> record = new Record<String>() {
+            @Override
+            public String getProperty(String name) {
+                return strings.get(name);
+            }
+
+            @Override
+            public Set<String> getPropertyNames() {
+                return strings.keySet();
+            }
+        };
+        assertEquals("one", runtimeObjectModel.resolveProperty(record, "a"));
+    }
+
+    @Test
+    public void testGetMapProperty() {
+        assertNull(runtimeObjectModel.getMapProperty(null, null));
+        Map<String, Integer> map = new HashMap<String, Integer>() {{
+            put("one", 1);
+            put("two", 2);
+        }};
+        assertEquals(1, runtimeObjectModel.getMapProperty(map, "one"));
+        assertNull(runtimeObjectModel.getMapProperty(map, null));
+    }
+
+    @Test
+    public void testToDate() {
+        assertNull(runtimeObjectModel.toDate(null));
+        Date testDate = new Date();
+        assertEquals(testDate, runtimeObjectModel.toDate(testDate));
+        Calendar testCalendar = Calendar.getInstance();
+        assertEquals(testCalendar.getTime(), runtimeObjectModel.toDate(testCalendar));
+    }
+
+    @Test
+    public void testGetPropertyNullChecks() {
+        assertNull(runtimeObjectModel.getProperty(null, null));
+        assertNull(runtimeObjectModel.getProperty(this, null));
+        assertNull(runtimeObjectModel.getProperty(this, ""));
+    }
+
+    @Test
+    public void testIsDate() {
+        assertFalse(runtimeObjectModel.isDate(null));
+        assertTrue(runtimeObjectModel.isDate(new Date()));
+        assertTrue(runtimeObjectModel.isDate(Calendar.getInstance()));
+    }
+
+    @Test
+    public void testIsNumber() {
+        assertFalse(runtimeObjectModel.isNumber(null));
+        assertFalse(runtimeObjectModel.isNumber(""));
+        assertTrue(runtimeObjectModel.isNumber(0));
+        assertTrue(runtimeObjectModel.isNumber(0.5));
+        assertTrue(runtimeObjectModel.isNumber("0"));
+        assertTrue(runtimeObjectModel.isNumber("0.5"));
+    }
+
+    @Test
+    public void testToCollection() {
+        assertTrue(runtimeObjectModel.toCollection(null).isEmpty());
+        Record<String> record = new Record<String>() {
+
+            private Map<String, String> properties = new HashMap<String, String>() {{
+                put("a", "1");
+                put("b", "2");
+            }};
+
+            @Override
+            public String getProperty(String name) {
+                return properties.get(name);
+            }
+
+            @Override
+            public Set<String> getPropertyNames() {
+                return properties.keySet();
+            }
+        };
+        Collection testCollection = runtimeObjectModel.toCollection(record);
+        assertEquals(2, testCollection.size());
+        assertTrue(testCollection.contains("a"));
+        assertTrue(testCollection.contains("b"));
+    }
+
+    @Test
+    public void testToMap() {
+        final Map<String, String> properties = new HashMap<String, String>() {{
+            put("a", "1");
+            put("b", "2");
+        }};
+        assertEquals(properties, runtimeObjectModel.toMap(properties));
+        Record<String> record = new Record<String>() {
+            @Override
+            public String getProperty(String name) {
+                return properties.get(name);
+            }
+
+            @Override
+            public Set<String> getPropertyNames() {
+                return properties.keySet();
+            }
+        };
+        assertEquals(properties, runtimeObjectModel.toMap(record));
+        assertTrue(runtimeObjectModel.toMap(null).isEmpty());
     }
 
 }

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.