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

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-configuration.git


The following commit(s) were added to refs/heads/master by this push:
     new 3319b182 CONFIGURATION-846 restore previous behavior allowing spring 
to inject multiple values (#425)
3319b182 is described below

commit 3319b182093756b77f583a79c5ff866d4bdc32ac
Author: Andrea Bollini <andrea.boll...@4science.com>
AuthorDate: Thu May 30 13:22:48 2024 +0200

    CONFIGURATION-846 restore previous behavior allowing spring to inject 
multiple values (#425)
    
    * CONFIGURATION-846 restore previous behavior allowing spring to inject 
multiple values
    
    * CONFIGURATION-846 add test to demonstrate the wrong behavior with unset 
property as by community feedback
    
    * CONFIGURATION-846 fix wrong behavior with unset property reported as 
empty array
---
 .../spring/ConfigurationPropertySource.java        |  9 +++++-
 .../spring/TestConfigurationPropertySource.java    | 37 ++++++++++++++++++++--
 2 files changed, 42 insertions(+), 4 deletions(-)

diff --git 
a/src/main/java/org/apache/commons/configuration2/spring/ConfigurationPropertySource.java
 
b/src/main/java/org/apache/commons/configuration2/spring/ConfigurationPropertySource.java
index f12b0ccd..ad2931a0 100644
--- 
a/src/main/java/org/apache/commons/configuration2/spring/ConfigurationPropertySource.java
+++ 
b/src/main/java/org/apache/commons/configuration2/spring/ConfigurationPropertySource.java
@@ -39,7 +39,14 @@ public class ConfigurationPropertySource extends 
EnumerablePropertySource<Config
 
     @Override
     public Object getProperty(final String name) {
-        return source.getString(name);
+        final String[] propValue = source.getStringArray(name);
+        if (propValue == null || propValue.length == 0) {
+            return null;
+        } else if (propValue.length == 1) {
+            return propValue[0];
+        } else {
+            return propValue;
+        }
     }
 
     @Override
diff --git 
a/src/test/java/org/apache/commons/configuration2/spring/TestConfigurationPropertySource.java
 
b/src/test/java/org/apache/commons/configuration2/spring/TestConfigurationPropertySource.java
index a5f15ffd..902888b8 100644
--- 
a/src/test/java/org/apache/commons/configuration2/spring/TestConfigurationPropertySource.java
+++ 
b/src/test/java/org/apache/commons/configuration2/spring/TestConfigurationPropertySource.java
@@ -17,7 +17,9 @@
 
 package org.apache.commons.configuration2.spring;
 
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
 
 import org.apache.commons.configuration2.PropertiesConfiguration;
 import org.junit.jupiter.api.AfterAll;
@@ -59,20 +61,34 @@ public class TestConfigurationPropertySource {
 
     private static final String TEST_PROPERTY = "test.property";
 
+    private static final String TEST_LIST_PROPERTY = "test.list.property";
+
     private static final String TEST_SYSTEM_PROPERTY = "test.system.property";
 
+    private static final String TEST_NULL_PROPERTY = "test.null.property";
+
     private static final String TEST_VALUE = "testVALUE";
 
+    private static final String TEST_SYSTEM_VALUE = "testVALUEforSystemEnv";
+
+    private static final String TEST_SYSTEM_PROPERTY_VALUE = "${sys:" + 
TEST_SYSTEM_PROPERTY + "}";
+
+    private static final String[] TEST_LIST_PROPERTY_VALUE = new String[] 
{TEST_SYSTEM_PROPERTY_VALUE, TEST_VALUE};
+
+    private static final String[] TEST_LIST_VALUE = new String[] 
{TEST_SYSTEM_VALUE, TEST_VALUE};
+
     private static ConfigurationPropertySource createConfigPropertySource() {
         final PropertiesConfiguration propertiesConfiguration = new 
PropertiesConfiguration();
         propertiesConfiguration.addProperty(TEST_PROPERTY, TEST_VALUE);
-        propertiesConfiguration.addProperty(TEST_SYSTEM_PROPERTY, "${sys:" + 
TEST_SYSTEM_PROPERTY + "}");
+        propertiesConfiguration.addProperty(TEST_LIST_PROPERTY, 
TEST_LIST_PROPERTY_VALUE);
+        propertiesConfiguration.addProperty(TEST_SYSTEM_PROPERTY, 
TEST_SYSTEM_PROPERTY_VALUE);
+        propertiesConfiguration.addProperty(TEST_NULL_PROPERTY, null);
         return new ConfigurationPropertySource("test configuration", 
propertiesConfiguration);
     }
 
     @BeforeAll
     public static void setUp() {
-        System.setProperty(TEST_SYSTEM_PROPERTY, TEST_VALUE);
+        System.setProperty(TEST_SYSTEM_PROPERTY, TEST_SYSTEM_VALUE);
     }
 
     @AfterAll
@@ -83,12 +99,18 @@ public class TestConfigurationPropertySource {
     @Value("${" + TEST_PROPERTY + "}")
     private String value;
 
+    @Value("${" + TEST_LIST_PROPERTY + "}")
+    private String[] listValue;
+
     @Value("${" + TEST_SYSTEM_PROPERTY + "}")
     private String systemPropertyValue;
 
+    @Value("${" + TEST_NULL_PROPERTY + ":false}")
+    private boolean booleanNullValue;
+
     @Test
     public void testSystemPropertyValueInjection() {
-        assertEquals(TEST_VALUE, systemPropertyValue);
+        assertEquals(TEST_SYSTEM_VALUE, systemPropertyValue);
     }
 
     @Test
@@ -96,4 +118,13 @@ public class TestConfigurationPropertySource {
         assertEquals(TEST_VALUE, value);
     }
 
+    @Test
+    public void testListValueInjection() {
+        assertArrayEquals(TEST_LIST_VALUE, listValue);
+    }
+
+    @Test
+    public void testNullValueInjection() {
+        assertFalse(booleanNullValue);
+    }
 }

Reply via email to