This is an automated email from the ASF dual-hosted git repository. sseifert pushed a commit to branch feature/SLING-13139-component-name in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-osgi-mock.git
commit 3ba41fae4faf21a828e1306bbff49fe77c28f956 Author: Stefan Seifert <[email protected]> AuthorDate: Fri Mar 27 19:40:06 2026 +0100 SLING-13139 Use component name from metadata if present --- .../sling/testing/mock/osgi/MapMergeUtil.java | 6 +++- .../mock/osgi/context/OsgiContextImplTest.java | 11 ++++-- .../osgiserviceutil/Service8CustomName.java | 40 ++++++++++++++++++++++ 3 files changed, 54 insertions(+), 3 deletions(-) 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..f3f6bd9 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,9 +28,9 @@ 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; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -143,6 +143,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)); @@ -198,7 +205,7 @@ public class OsgiContextImplTest { @Test public void testRegisterInjectActivateWithAdditionalManualServiceRegistration() { - context.registerInjectActivateService(new Service8()); + context.registerInjectActivateService(new Service8CustomName()); } @Test(expected = RuntimeException.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; + } +}
