You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by jo...@apache.org on 2022/09/05 18:35:18 UTC

[sling-org-apache-sling-jcr-resource] 01/01: SLING-11567 add null checks

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

joerghoh pushed a commit to branch SLING-11567-add-nullchecks
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-jcr-resource.git

commit 70a4c7b93fcab2fbe1376fc7ffeb1e704265714d
Author: Joerg Hoh <jo...@apache.org>
AuthorDate: Mon Sep 5 20:35:00 2022 +0200

    SLING-11567 add null checks
---
 .../sling/jcr/resource/internal/JcrValueMap.java   |  8 +++
 .../jcr/resource/internal/JcrValueMapTest.java     | 63 ++++++++++++++++++++++
 2 files changed, 71 insertions(+)

diff --git a/src/main/java/org/apache/sling/jcr/resource/internal/JcrValueMap.java b/src/main/java/org/apache/sling/jcr/resource/internal/JcrValueMap.java
index 5baed74..e799f23 100644
--- a/src/main/java/org/apache/sling/jcr/resource/internal/JcrValueMap.java
+++ b/src/main/java/org/apache/sling/jcr/resource/internal/JcrValueMap.java
@@ -90,6 +90,10 @@ public class JcrValueMap implements ValueMap {
     @SuppressWarnings("unchecked")
     public <T> T get(final @NotNull String aKey, final @NotNull Class<T> type) {
         final String key = checkKey(aKey);
+        // See https://issues.apache.org/jira/browse/SLING-11567
+        if (type == null) {
+            return (T) get(key);
+        }
         final JcrPropertyMapCacheEntry entry = this.read(key);
         if (entry == null) {
             return null;
@@ -104,6 +108,10 @@ public class JcrValueMap implements ValueMap {
     @SuppressWarnings("unchecked")
     public <T> @NotNull T get(final @NotNull String aKey, final @NotNull T defaultValue) {
         final String key = checkKey(aKey);
+        // See https://issues.apache.org/jira/browse/SLING-11567
+        if (defaultValue == null) {
+            return (T) get(key);
+        }
 
         // special handling in case the default value implements one
         // of the interface types supported by the convertToType method
diff --git a/src/test/java/org/apache/sling/jcr/resource/internal/JcrValueMapTest.java b/src/test/java/org/apache/sling/jcr/resource/internal/JcrValueMapTest.java
new file mode 100644
index 0000000..07d5f57
--- /dev/null
+++ b/src/test/java/org/apache/sling/jcr/resource/internal/JcrValueMapTest.java
@@ -0,0 +1,63 @@
+/*
+ * 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.jcr.resource.internal;
+
+import javax.jcr.Node;
+
+import org.apache.sling.jcr.resource.internal.helper.jcr.SlingRepositoryTestBase;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+public class JcrValueMapTest extends SlingRepositoryTestBase {
+    
+    private Node rootNode;
+    HelperData helperData;
+    
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        String rootPath = "/test_" + System.currentTimeMillis();
+        rootNode = getSession().getRootNode().addNode(rootPath.substring(1),
+                "nt:unstructured");
+        rootNode.setProperty("string", "test");
+        getSession().save();
+        helperData = Mockito.mock(HelperData.class);
+    }
+    
+    
+    //  Tests with null as default value and class must pass, see https://issues.apache.org/jira/browse/SLING-11567
+    
+    @Test
+    public void testGetWithDefaultValue() {
+       JcrValueMap vm = new JcrValueMap(rootNode, helperData);
+       assertEquals("test", vm.get("string","default"));
+       assertEquals("default", vm.get("nonexistent","default"));
+       assertNull(vm.get("nonexistent",null));
+    }
+    
+    @Test
+    public void testGetWithClass() {
+        JcrValueMap vm = new JcrValueMap(rootNode, helperData);
+        assertEquals("test", vm.get("string",String.class));
+        assertEquals("test", vm.get("string",null)); 
+    }
+    
+   
+
+}