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 2025/02/24 19:11:01 UTC

(sling-org-apache-sling-jcr-repoinit) branch master updated: SLING-12668 Forced multivalue property with single value is not stored as multiple property (#61)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 1789cb9  SLING-12668 Forced multivalue property with single value is not stored as multiple property (#61)
1789cb9 is described below

commit 1789cb9a3dc73cfc895571ee53bdb3d61ccf7b5e
Author: Stefan Seifert <st...@users.noreply.github.com>
AuthorDate: Mon Feb 24 20:10:55 2025 +0100

    SLING-12668 Forced multivalue property with single value is not stored as multiple property (#61)
---
 pom.xml                                            |  2 +-
 .../jcr/repoinit/impl/NodePropertiesVisitor.java   |  2 +-
 .../sling/jcr/repoinit/it/RepoInitTextIT.java      | 53 ++++++++++++++++++++++
 src/test/resources/repoinit.txt                    |  9 ++++
 4 files changed, 64 insertions(+), 2 deletions(-)

diff --git a/pom.xml b/pom.xml
index 6bcaaf1..5e0b831 100644
--- a/pom.xml
+++ b/pom.xml
@@ -229,7 +229,7 @@
         <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.repoinit.parser</artifactId>
-            <version>1.9.0</version>
+            <version>1.9.1-SNAPSHOT</version>
             <scope>provided</scope>
         </dependency>
         <dependency>
diff --git a/src/main/java/org/apache/sling/jcr/repoinit/impl/NodePropertiesVisitor.java b/src/main/java/org/apache/sling/jcr/repoinit/impl/NodePropertiesVisitor.java
index b311bb4..1420e19 100644
--- a/src/main/java/org/apache/sling/jcr/repoinit/impl/NodePropertiesVisitor.java
+++ b/src/main/java/org/apache/sling/jcr/repoinit/impl/NodePropertiesVisitor.java
@@ -291,7 +291,7 @@ class NodePropertiesVisitor extends DoNothingVisitor {
                 // note: Node#setProperty() touches the node even if the property value is unchanged
                 //       and thus needs to be explicitly avoided
                 if (hasPropertyChange(oldProperty, newType, newValues)) {
-                    if (newValues.length == 1) {
+                    if (newValues.length == 1 && !pl.isMultiple()) {
                         n.setProperty(pName, newValues[0], newType);
                     } else {
                         n.setProperty(pName, newValues, newType);
diff --git a/src/test/java/org/apache/sling/jcr/repoinit/it/RepoInitTextIT.java b/src/test/java/org/apache/sling/jcr/repoinit/it/RepoInitTextIT.java
index baac1c9..c4c400d 100644
--- a/src/test/java/org/apache/sling/jcr/repoinit/it/RepoInitTextIT.java
+++ b/src/test/java/org/apache/sling/jcr/repoinit/it/RepoInitTextIT.java
@@ -19,13 +19,17 @@
 package org.apache.sling.jcr.repoinit.it;
 
 import javax.inject.Inject;
+import javax.jcr.Node;
+import javax.jcr.Property;
 import javax.jcr.PropertyType;
+import javax.jcr.RepositoryException;
 import javax.jcr.Value;
 import javax.jcr.ValueFactory;
 
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.util.UUID;
+import java.util.stream.Stream;
 
 import org.apache.jackrabbit.api.JackrabbitSession;
 import org.apache.jackrabbit.api.security.user.Authorizable;
@@ -40,6 +44,7 @@ import org.ops4j.pax.exam.junit.PaxExam;
 import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
 import org.ops4j.pax.exam.spi.reactors.PerClass;
 
+import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
@@ -407,4 +412,52 @@ public class RepoInitTextIT extends RepoInitTestSupport {
             }
         };
     }
+
+    @Test
+    public void forcedMultiValue() throws Exception {
+        new Retry() {
+            @Override
+            public Void call() throws Exception {
+                Node node = session.getNode("/forcedMultiValue");
+                assertMultiProperty(node, "multiValue", new String[] {"item1", "item2"});
+                assertMultiProperty(node, "singleMultiValue", new String[] {"single"});
+                assertMultiProperty(node, "emptyMultiValue", new String[0]);
+                assertLongMultiProperty(node, "longMultiValue", new Long[] {1243L, 5678L});
+                assertLongMultiProperty(node, "singleLongMultiValue", new Long[] {1243L});
+                assertLongMultiProperty(node, "emptyMultiValue", new Long[0]);
+                return null;
+            }
+        };
+    }
+
+    private static void assertMultiProperty(Node node, String propertyName, Object[] expectedValues) throws Exception {
+        Property prop = node.getProperty(propertyName);
+        assertTrue("Property " + propertyName + " is not multiple", prop.isMultiple());
+        Object[] actualValues = Stream.of(prop.getValues())
+                .map(value -> {
+                    try {
+                        return value.getString();
+                    } catch (IllegalStateException | RepositoryException e) {
+                        return null;
+                    }
+                })
+                .toArray();
+        assertArrayEquals("Unexpected values for property " + propertyName, expectedValues, actualValues);
+    }
+
+    private static void assertLongMultiProperty(Node node, String propertyName, Object[] expectedValues)
+            throws Exception {
+        Property prop = node.getProperty(propertyName);
+        assertTrue("Property " + propertyName + " is not multiple", prop.isMultiple());
+        Object[] actualValues = Stream.of(prop.getValues())
+                .map(value -> {
+                    try {
+                        return value.getLong();
+                    } catch (IllegalStateException | RepositoryException e) {
+                        return null;
+                    }
+                })
+                .toArray();
+        assertArrayEquals("Unexpected values for property " + propertyName, expectedValues, actualValues);
+    }
 }
diff --git a/src/test/resources/repoinit.txt b/src/test/resources/repoinit.txt
index 2dbbe24..a3fa817 100644
--- a/src/test/resources/repoinit.txt
+++ b/src/test/resources/repoinit.txt
@@ -112,3 +112,12 @@ set properties on authorizable(bob)/nested,authorizable(grpB)/nested
   set quotedMix to "quoted", non-quoted, "the last \" one"
   set nested/someInteger{Long} to 42
 end
+
+ensure nodes (nt:unstructured) /forcedMultiValue with properties
+  set multiValue{String[]} to "item1","item2"
+  set singleMultiValue{String[]} to "single"
+  set emptyMultiValue{String[]} to
+  set longMultiValue{Long[]} to 1243,5678
+  set singleLongMultiValue{Long[]} to 1243
+  set emptyLongMultiValue{Long[]} to
+end