You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by kw...@apache.org on 2021/11/14 19:55:35 UTC
[sling-org-apache-sling-testing-osgi-mock] 01/01: SLING-10919 fix generating component/service properties for DS
This is an automated email from the ASF dual-hosted git repository.
kwin pushed a commit to branch feature/SLING-10919
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-osgi-mock.git
commit d332e53fef6c18efc0ce6eedb7b8e63bc0d02b21
Author: Konrad Windszus <kw...@apache.org>
AuthorDate: Sun Nov 14 20:55:19 2021 +0100
SLING-10919 fix generating component/service properties for DS
WIP
---
.../sling/testing/mock/osgi/MapMergeUtil.java | 5 +++++
.../sling/testing/mock/osgi/MockBundleContext.java | 4 ++--
.../apache/sling/testing/mock/osgi/MockOsgi.java | 26 ++++++++++++++++++++++
.../testing/mock/osgi/context/OsgiContextImpl.java | 17 +++++++++++---
.../mock/osgi/MockServiceReferenceTest.java | 2 ++
5 files changed, 49 insertions(+), 5 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 b8bd56c..a284e1e 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
@@ -29,6 +29,7 @@ import java.util.Map;
import org.apache.sling.testing.mock.osgi.OsgiMetadataUtil.OsgiMetadata;
import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;
+import org.osgi.service.component.ComponentConstants;
/**
* Map util merge methods.
@@ -64,6 +65,7 @@ final class MapMergeUtil {
* @param configAdmin Configuration admin or null if none is registered
* @param properties Properties from unit test code or null if none where passed
* @return Merged properties
+ * @see <a href="http://docs.osgi.org/specification/osgi.cmpn/7.0.0/service.component.html#service.component-component.properties">Component Properties</a>
*/
static Map<String, Object> propertiesMergeWithOsgiMetadata(Class<?> targetClass,
ConfigurationAdmin configAdmin,
@@ -98,6 +100,9 @@ final class MapMergeUtil {
mergedProperties.putAll(properties);
}
+ // add non overwritable properties
+ mergedProperties.put(ComponentConstants.COMPONENT_NAME, targetClass.getName());
+ //mergedProperties.put(ComponentConstants.COMPONENT_ID, Long.randomValue());
return mergedProperties;
}
diff --git a/core/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java b/core/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java
index ed69b3d..624047d 100644
--- a/core/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java
+++ b/core/src/main/java/org/apache/sling/testing/mock/osgi/MockBundleContext.java
@@ -122,8 +122,8 @@ class MockBundleContext implements BundleContext {
@SuppressWarnings("unchecked")
@Override
public ServiceRegistration registerService(final String[] clazzes, final Object service, final Dictionary properties) {
- Dictionary<String, Object> mergedPropertes = MapMergeUtil.propertiesMergeWithOsgiMetadata(service.getClass(), configAdmin, properties);
- MockServiceRegistration<?> registration = new MockServiceRegistration<>(this.bundle, clazzes, service, mergedPropertes, this);
+ ///Dictionary<String, Object> mergedPropertes = MapMergeUtil.propertiesMergeWithOsgiMetadata(service.getClass(), configAdmin, properties);
+ MockServiceRegistration<?> registration = new MockServiceRegistration<>(this.bundle, clazzes, service, properties, this);
this.registeredServices.add(registration);
handleRefsUpdateOnRegister(registration, this);
notifyServiceListeners(ServiceEvent.REGISTERED, registration.getReference());
diff --git a/core/src/main/java/org/apache/sling/testing/mock/osgi/MockOsgi.java b/core/src/main/java/org/apache/sling/testing/mock/osgi/MockOsgi.java
index 5ffdf83..43a1d78 100644
--- a/core/src/main/java/org/apache/sling/testing/mock/osgi/MockOsgi.java
+++ b/core/src/main/java/org/apache/sling/testing/mock/osgi/MockOsgi.java
@@ -249,6 +249,32 @@ public final class MockOsgi {
return activate(target, bundleContext, toDictionary(properties));
}
+
+ /**
+ * Simulate activation of service instance. Invokes the @Activate annotated method.
+ * @param target Service instance.
+ * @param bundleContext Bundle context
+ * @param properties Properties
+ * @return the service properties to use for registering this service. This includes default values from the metatype, given properties and generated properties.
+ */
+ public static Dictionary<String, Object> activateAndReturnServiceProperties(@NotNull Object target, @NotNull BundleContext bundleContext, @Nullable Dictionary<String, Object> properties) {
+ Dictionary<String, Object> mergedProperties = propertiesMergeWithOsgiMetadata(target.getClass(), getConfigAdmin(bundleContext), properties);
+ ComponentContext componentContext = newComponentContext(bundleContext, mergedProperties);
+ OsgiServiceUtil.activateDeactivate(target, (MockComponentContext)componentContext, true);
+ return mergedProperties;
+ }
+
+ /**
+ * Simulate activation of service instance. Invokes the @Activate annotated method.
+ * @param target Service instance.
+ * @param bundleContext Bundle context
+ * @param properties Properties
+ * @return the service properties to use for registering this service. This includes default values from the metatype, given properties and generated properties.
+ */
+ public static Dictionary<String, Object> activateAndReturnServiceProperties(@NotNull Object target, @NotNull BundleContext bundleContext, @Nullable Map<String, Object> properties) {
+ return activateAndReturnServiceProperties(target, bundleContext, toDictionary(properties));
+ }
+
/**
* Simulate deactivation of service instance. Invokes the @Deactivate annotated method.
* @param target Service instance.
diff --git a/core/src/main/java/org/apache/sling/testing/mock/osgi/context/OsgiContextImpl.java b/core/src/main/java/org/apache/sling/testing/mock/osgi/context/OsgiContextImpl.java
index b288731..667dd78 100644
--- a/core/src/main/java/org/apache/sling/testing/mock/osgi/context/OsgiContextImpl.java
+++ b/core/src/main/java/org/apache/sling/testing/mock/osgi/context/OsgiContextImpl.java
@@ -116,7 +116,18 @@ public class OsgiContextImpl {
*/
public final @NotNull <T> T registerService(@Nullable final Class<T> serviceClass, @NotNull final T service, @Nullable final Map<String, Object> properties) {
Dictionary<String, Object> serviceProperties = MapUtil.toDictionary(properties);
- bundleContext().registerService(serviceClass != null ? serviceClass.getName() : null, service, serviceProperties);
+ return registerService(serviceClass, service, serviceProperties);
+ }
+ /**
+ * Registers a service in the mocked OSGi environment.
+ * @param <T> Service type
+ * @param serviceClass Service class
+ * @param service Service instance
+ * @param properties Service properties (optional)
+ * @return Registered service instance
+ */
+ public final @NotNull <T> T registerService(@Nullable final Class<T> serviceClass, @NotNull final T service, @Nullable final Dictionary<String, Object> properties) {
+ bundleContext().registerService(serviceClass != null ? serviceClass.getName() : null, service, properties);
return service;
}
@@ -151,8 +162,8 @@ public class OsgiContextImpl {
*/
public final @NotNull <T> T registerInjectActivateService(@NotNull final T service, @Nullable final Map<String, Object> properties) {
MockOsgi.injectServices(service, bundleContext(), properties);
- MockOsgi.activate(service, bundleContext(), properties);
- registerService(null, service, properties);
+ Dictionary<String, Object> mergedProperties = MockOsgi.activateAndReturnServiceProperties(service, bundleContext(), properties);
+ registerService(null, service, mergedProperties);
return service;
}
diff --git a/core/src/test/java/org/apache/sling/testing/mock/osgi/MockServiceReferenceTest.java b/core/src/test/java/org/apache/sling/testing/mock/osgi/MockServiceReferenceTest.java
index b938754..58b80d5 100644
--- a/core/src/test/java/org/apache/sling/testing/mock/osgi/MockServiceReferenceTest.java
+++ b/core/src/test/java/org/apache/sling/testing/mock/osgi/MockServiceReferenceTest.java
@@ -28,6 +28,7 @@ import java.util.Hashtable;
import org.apache.sling.testing.mock.osgi.OsgiMetadataUtilTest.ServiceWithMetadata;
import org.junit.Before;
+import org.junit.Ignore;
import org.junit.Test;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
@@ -72,6 +73,7 @@ public class MockServiceReferenceTest {
}
@Test
+ @Ignore //"BundleContext.registerService should not consider OSGi DS meta data"
public void testWithOsgiMetadata() {
ServiceWithMetadata serviceWithMetadata = new OsgiMetadataUtilTest.ServiceWithMetadata();
bundleContext.registerService((String) null, serviceWithMetadata, null);