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 1db1301 SLING-13139 Use component name from metadata if present (#52)
1db1301 is described below
commit 1db130167123803f08836c2fc3c31274d9d8bd9b
Author: Stefan Seifert <[email protected]>
AuthorDate: Fri Mar 27 20:00:12 2026 +0100
SLING-13139 Use component name from metadata if present (#52)
---
.../sling/testing/mock/osgi/MapMergeUtil.java | 6 +++-
.../mock/osgi/context/OsgiContextImplTest.java | 8 +++++
.../osgiserviceutil/Service8CustomName.java | 40 ++++++++++++++++++++++
3 files changed, 53 insertions(+), 1 deletion(-)
diff --git
a/core/src/main/java/org/apache/sling/testing/mock/osgi/MapMergeUtil.java
b/core/src/main/java/org/apache/sling/testing/mock/osgi/MapMergeUtil.java
index 1a71916..34f915f 100644
--- a/core/src/main/java/org/apache/sling/testing/mock/osgi/MapMergeUtil.java
+++ b/core/src/main/java/org/apache/sling/testing/mock/osgi/MapMergeUtil.java
@@ -77,6 +77,7 @@ final class MapMergeUtil {
Map<String, Object> mergedProperties = new HashMap<>();
OsgiMetadata metadata = OsgiMetadataUtil.getMetadata(targetClass);
+ String componentName = targetClass.getName();
if (metadata != null) {
Map<String, Object> metadataProperties = metadata.getProperties();
if (metadataProperties != null) {
@@ -99,6 +100,9 @@ final class MapMergeUtil {
}
}
}
+ if (metadata.getName() != null) {
+ componentName = metadata.getName();
+ }
}
// merge with properties from calling unit test code
@@ -107,7 +111,7 @@ final class MapMergeUtil {
}
// add non overwritable auto-generated properties
- mergedProperties.put(ComponentConstants.COMPONENT_NAME,
targetClass.getName());
+ mergedProperties.put(ComponentConstants.COMPONENT_NAME, componentName);
mergedProperties.put(ComponentConstants.COMPONENT_ID,
COMPONENT_ID_COUNTER.getAndIncrement());
return mergedProperties;
}
diff --git
a/core/src/test/java/org/apache/sling/testing/mock/osgi/context/OsgiContextImplTest.java
b/core/src/test/java/org/apache/sling/testing/mock/osgi/context/OsgiContextImplTest.java
index 34b199e..6969299 100644
---
a/core/src/test/java/org/apache/sling/testing/mock/osgi/context/OsgiContextImplTest.java
+++
b/core/src/test/java/org/apache/sling/testing/mock/osgi/context/OsgiContextImplTest.java
@@ -28,6 +28,7 @@ import
org.apache.sling.testing.mock.osgi.NoScrMetadataException;
import org.apache.sling.testing.mock.osgi.testsvc.osgicontextimpl.MyComponent;
import org.apache.sling.testing.mock.osgi.testsvc.osgicontextimpl.MyService;
import org.apache.sling.testing.mock.osgi.testsvc.osgiserviceutil.Service3;
+import
org.apache.sling.testing.mock.osgi.testsvc.osgiserviceutil.Service8CustomName;
import
org.apache.sling.testing.mock.osgi.testsvc.osgiserviceutil.ServiceInterface1;
import
org.apache.sling.testing.mock.osgi.testsvc.osgiserviceutil.ServiceInterface2;
import
org.apache.sling.testing.mock.osgi.testsvc.osgiserviceutil.activatedeactivate.Service8;
@@ -143,6 +144,13 @@ public class OsgiContextImplTest {
assertNotNull(service.getConfig().get("component.id"));
}
+ @Test
+ public void testRegisterInjectActivate_Class_CustomComponentName() {
+ Service8CustomName service =
context.registerInjectActivateService(Service8CustomName.class);
+ assertEquals("Service8_this_is_a_custom_name",
service.getConfig().get("component.name"));
+ assertNotNull(service.getConfig().get("component.id"));
+ }
+
@Test
public void testRegisterInjectActivateWithProperties_Instance() {
context.registerService(ServiceInterface1.class,
mock(ServiceInterface1.class));
diff --git
a/test-services/src/main/java/org/apache/sling/testing/mock/osgi/testsvc/osgiserviceutil/Service8CustomName.java
b/test-services/src/main/java/org/apache/sling/testing/mock/osgi/testsvc/osgiserviceutil/Service8CustomName.java
new file mode 100644
index 0000000..4a3ac47
--- /dev/null
+++
b/test-services/src/main/java/org/apache/sling/testing/mock/osgi/testsvc/osgiserviceutil/Service8CustomName.java
@@ -0,0 +1,40 @@
+/*
+ * 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;
+
+import java.util.Map;
+
+import org.osgi.service.component.ComponentContext;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+
+@Component(name = "Service8_this_is_a_custom_name")
+public class Service8CustomName {
+
+ private Map<String, Object> config;
+
+ @Activate
+ private void activate(ComponentContext ctx) {
+ this.config = DictionaryTo.map(ctx.getProperties());
+ }
+
+ public Map<String, Object> getConfig() {
+ return config;
+ }
+}