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-testing-osgi-mock.git


The following commit(s) were added to refs/heads/master by this push:
     new 907dbbd  SLING-12104 - Fix OSGi Configuration Injection of default 
value for an empty String
907dbbd is described below

commit 907dbbd5e6707b6be5f19f8af6dedcd6cdf2f87d
Author: Brouns, R <[email protected]>
AuthorDate: Wed Oct 18 11:22:27 2023 +0200

    SLING-12104 - Fix OSGi Configuration Injection of default value for an 
empty String
---
 .../sling/testing/mock/osgi/OsgiMetadataUtil.java  |  2 +-
 .../testing/mock/osgi/OsgiMetadataUtilTest.java    | 22 ++++++++++++++
 .../ServiceWithDefaultValues.java                  | 33 +++++++++++++++++++++
 .../ServiceWithDefaultValuesConfig.java            | 27 +++++++++++++++++
 .../ServiceWithDefaultValuesConstructor.java       | 34 ++++++++++++++++++++++
 5 files changed, 117 insertions(+), 1 deletion(-)

diff --git 
a/core/src/main/java/org/apache/sling/testing/mock/osgi/OsgiMetadataUtil.java 
b/core/src/main/java/org/apache/sling/testing/mock/osgi/OsgiMetadataUtil.java
index 2f6e752..4cd8dfc 100644
--- 
a/core/src/main/java/org/apache/sling/testing/mock/osgi/OsgiMetadataUtil.java
+++ 
b/core/src/main/java/org/apache/sling/testing/mock/osgi/OsgiMetadataUtil.java
@@ -289,7 +289,7 @@ final class OsgiMetadataUtil {
 
     private static Map<String, Object> getProperties(Class clazz, Document 
metadata) {
         Map<String, Object> props = new HashMap<String, Object>();
-        String query = getComponentXPathQuery(clazz) + "/property[@name!='' 
and @value!='']";
+        String query = getComponentXPathQuery(clazz) + "/property[@name!='' 
and boolean(@value)]";
         NodeList nodes = queryNodes(metadata, query);
         if (nodes != null) {
             for (int i = 0; i < nodes.getLength(); i++) {
diff --git 
a/core/src/test/java/org/apache/sling/testing/mock/osgi/OsgiMetadataUtilTest.java
 
b/core/src/test/java/org/apache/sling/testing/mock/osgi/OsgiMetadataUtilTest.java
index a1d64f7..94af49a 100644
--- 
a/core/src/test/java/org/apache/sling/testing/mock/osgi/OsgiMetadataUtilTest.java
+++ 
b/core/src/test/java/org/apache/sling/testing/mock/osgi/OsgiMetadataUtilTest.java
@@ -32,6 +32,8 @@ import 
org.apache.sling.testing.mock.osgi.OsgiMetadataUtil.Reference;
 import 
org.apache.sling.testing.mock.osgi.OsgiMetadataUtil.ReferenceCardinality;
 import org.apache.sling.testing.mock.osgi.testsvc.osgiserviceutil.Service3;
 import 
org.apache.sling.testing.mock.osgi.testsvc.osgiserviceutil.ServiceInterface2;
+import 
org.apache.sling.testing.mock.osgi.testsvc.osgiserviceutil.activatedeactivate.ServiceWithDefaultValues;
+import 
org.apache.sling.testing.mock.osgi.testsvc.osgiserviceutil.activatedeactivate.ServiceWithDefaultValuesConstructor;
 import org.junit.Test;
 import org.osgi.framework.Constants;
 
@@ -81,6 +83,26 @@ public class OsgiMetadataUtilTest {
         assertEquals("activate", metadata.getActivateMethodName());
     }
 
+    @Test
+    public void testWithDefaultValues() {
+        OsgiMetadata metadata = 
OsgiMetadataUtil.getMetadata(ServiceWithDefaultValues.class);
+
+        Map<String, Object> props = metadata.getProperties();
+        assertEquals(2, props.size());
+        assertEquals("", props.get("empty.string"));
+        assertEquals("value", props.get("value.string"));
+    }
+
+    @Test
+    public void testWithDefaultValuesViaConstructor() {
+        OsgiMetadata metadata = 
OsgiMetadataUtil.getMetadata(ServiceWithDefaultValuesConstructor.class);
+
+        Map<String, Object> props = metadata.getProperties();
+        assertEquals(2, props.size());
+        assertEquals("", props.get("empty.string"));
+        assertEquals("value", props.get("value.string"));
+    }
+
     static class ServiceWithMetadata {
         // empty class
     }
diff --git 
a/test-services/src/main/java/org/apache/sling/testing/mock/osgi/testsvc/osgiserviceutil/activatedeactivate/ServiceWithDefaultValues.java
 
b/test-services/src/main/java/org/apache/sling/testing/mock/osgi/testsvc/osgiserviceutil/activatedeactivate/ServiceWithDefaultValues.java
new file mode 100644
index 0000000..eecdf36
--- /dev/null
+++ 
b/test-services/src/main/java/org/apache/sling/testing/mock/osgi/testsvc/osgiserviceutil/activatedeactivate/ServiceWithDefaultValues.java
@@ -0,0 +1,33 @@
+/*
+ * 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.testing.mock.osgi.testsvc.osgiserviceutil.activatedeactivate;
+
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+
+@Component
+public class ServiceWithDefaultValues {
+
+    private ServiceWithDefaultValuesConfig config;
+
+    @Activate
+    private void activate(ServiceWithDefaultValuesConfig config) {
+        this.config = config;
+    }
+}
diff --git 
a/test-services/src/main/java/org/apache/sling/testing/mock/osgi/testsvc/osgiserviceutil/activatedeactivate/ServiceWithDefaultValuesConfig.java
 
b/test-services/src/main/java/org/apache/sling/testing/mock/osgi/testsvc/osgiserviceutil/activatedeactivate/ServiceWithDefaultValuesConfig.java
new file mode 100644
index 0000000..5b89f79
--- /dev/null
+++ 
b/test-services/src/main/java/org/apache/sling/testing/mock/osgi/testsvc/osgiserviceutil/activatedeactivate/ServiceWithDefaultValuesConfig.java
@@ -0,0 +1,27 @@
+/*
+ * 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.testing.mock.osgi.testsvc.osgiserviceutil.activatedeactivate;
+
+public @interface ServiceWithDefaultValuesConfig {
+
+    String value_string() default "value";
+    String empty_string() default "";
+    String null_string();
+
+}
diff --git 
a/test-services/src/main/java/org/apache/sling/testing/mock/osgi/testsvc/osgiserviceutil/activatedeactivate/ServiceWithDefaultValuesConstructor.java
 
b/test-services/src/main/java/org/apache/sling/testing/mock/osgi/testsvc/osgiserviceutil/activatedeactivate/ServiceWithDefaultValuesConstructor.java
new file mode 100644
index 0000000..e3bef73
--- /dev/null
+++ 
b/test-services/src/main/java/org/apache/sling/testing/mock/osgi/testsvc/osgiserviceutil/activatedeactivate/ServiceWithDefaultValuesConstructor.java
@@ -0,0 +1,34 @@
+/*
+ * 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.testing.mock.osgi.testsvc.osgiserviceutil.activatedeactivate;
+
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+
+@Component
+public class ServiceWithDefaultValuesConstructor {
+
+    private final ServiceWithDefaultValuesConfig config;
+
+    @Activate
+    public ServiceWithDefaultValuesConstructor(ServiceWithDefaultValuesConfig 
config) {
+        this.config = config;
+    }
+
+}

Reply via email to