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/14 12:28:22 UTC

[sling-org-apache-sling-jcr-resource] branch master updated: SLING-11567 add null checks (#36)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 1aa5b1d  SLING-11567 add null checks (#36)
1aa5b1d is described below

commit 1aa5b1dbcb0731a8abe3ba89cd438e16b091e6da
Author: Jörg Hoh <jo...@users.noreply.github.com>
AuthorDate: Wed Sep 14 14:28:16 2022 +0200

    SLING-11567 add null checks (#36)
    
    * SLING-11567 add null checks
---
 .../sling/jcr/resource/internal/JcrValueMap.java   | 30 +++++++++--
 .../jcr/resource/internal/JcrValueMapTest.java     | 63 ++++++++++++++++++++++
 2 files changed, 89 insertions(+), 4 deletions(-)

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..1b6ae65 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
@@ -85,11 +85,22 @@ public class JcrValueMap implements ValueMap {
 
     /**
      * @see org.apache.sling.api.resource.ValueMap#get(java.lang.String, java.lang.Class)
+     * 
+     * Note: The {@code type} parameter is marked as @NonNull in the API documentation, but
+     * https://issues.apache.org/jira/browse/SLING-11567 it got obvious that this assumption
+     * does not hold true (this change actually broke b/w compatibility).
+     * That means we still have to handle the case that {@code type} is null.
+     * 
+     * This is also recommended by the API documentation of this method.
+     * 
      */
     @Override
-    @SuppressWarnings("unchecked")
-    public <T> T get(final @NotNull String aKey, final @NotNull Class<T> type) {
+    @SuppressWarnings({"unchecked","java:S2583"})
+    public <T> T get(final @NotNull String aKey, @NotNull final Class<T> type) {
         final String key = checkKey(aKey);
+        if (type == null) {
+            return (T) get(key);
+        }
         final JcrPropertyMapCacheEntry entry = this.read(key);
         if (entry == null) {
             return null;
@@ -99,11 +110,22 @@ public class JcrValueMap implements ValueMap {
 
     /**
      * @see org.apache.sling.api.resource.ValueMap#get(java.lang.String, java.lang.Object)
+     * 
+     * Note: The {@code defaultValue} parameter is marked as @NonNull in the API documentation, but
+     * https://issues.apache.org/jira/browse/SLING-11567 it got obvious that this assumption
+     * does not hold true (this change actually broke b/w compatibility).
+     * That means we still have to handle the case that {@code defaultValue} is null.
+     * 
+     * This is also recommended by the API documentation of this method.
+     * 
      */
     @Override
-    @SuppressWarnings("unchecked")
-    public <T> @NotNull T get(final @NotNull String aKey, final @NotNull T defaultValue) {
+    @SuppressWarnings({"unchecked","java:S2583"})
+    public <T> @NotNull T get(final @NotNull String aKey, @NotNull final T defaultValue) {
         final String key = checkKey(aKey);
+        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)); 
+    }
+    
+   
+
+}