This is an automated email from the ASF dual-hosted git repository.
cziegeler 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 09856ab SLING-9817 : Send ServiceEvent.MODIFIED when service
properties are changed
09856ab is described below
commit 09856abccfb990badb371c3601e21cd8d58fb437
Author: Carsten Ziegeler <[email protected]>
AuthorDate: Tue Oct 13 12:06:47 2020 +0200
SLING-9817 : Send ServiceEvent.MODIFIED when service properties are changed
---
.../sling/testing/mock/osgi/MockBundleContext.java | 2 +-
.../testing/mock/osgi/MockServiceRegistration.java | 28 +++++++---
.../testing/mock/osgi/MockBundleContextTest.java | 60 ++++++++++++++++++++++
3 files changed, 82 insertions(+), 8 deletions(-)
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 e19e739..ba69124 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
@@ -357,7 +357,7 @@ class MockBundleContext implements BundleContext {
serviceListeners.remove(serviceListener);
}
- private void notifyServiceListeners(int eventType, ServiceReference
serviceReference) {
+ void notifyServiceListeners(int eventType, ServiceReference
serviceReference) {
final ServiceEvent event = new ServiceEvent(eventType,
serviceReference);
for ( Map.Entry<ServiceListener, Filter> entry :
serviceListeners.entrySet()) {
if ( entry.getValue() == null ||
entry.getValue().match(serviceReference)) {
diff --git
a/core/src/main/java/org/apache/sling/testing/mock/osgi/MockServiceRegistration.java
b/core/src/main/java/org/apache/sling/testing/mock/osgi/MockServiceRegistration.java
index bc1454a..79b433c 100644
---
a/core/src/main/java/org/apache/sling/testing/mock/osgi/MockServiceRegistration.java
+++
b/core/src/main/java/org/apache/sling/testing/mock/osgi/MockServiceRegistration.java
@@ -20,6 +20,7 @@ package org.apache.sling.testing.mock.osgi;
import java.util.Arrays;
import java.util.Dictionary;
+import java.util.Enumeration;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Set;
@@ -30,6 +31,7 @@ import
org.apache.sling.testing.mock.osgi.OsgiMetadataUtil.OsgiMetadata;
import org.osgi.framework.Bundle;
import org.osgi.framework.Constants;
import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceEvent;
import org.osgi.framework.ServiceFactory;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
@@ -44,7 +46,7 @@ class MockServiceRegistration<T> implements
ServiceRegistration<T>, Comparable<M
private final Long serviceId;
private final Set<String> clazzes;
private final T service;
- private Dictionary<String, Object> properties;
+ private Hashtable<String, Object> properties;
private final ServiceReference<T> serviceReference;
private final MockBundleContext bundleContext;
@@ -63,22 +65,34 @@ class MockServiceRegistration<T> implements
ServiceRegistration<T>, Comparable<M
readOsgiMetadata();
- this.properties = properties != null ? properties : new
Hashtable<String,Object>();
- this.properties.put(Constants.SERVICE_ID, this.serviceId);
- this.properties.put(Constants.OBJECTCLASS, this.clazzes.toArray(new
String[this.clazzes.size()]));
+ this.properties = new Hashtable<String,Object>();
+ this.updateProperties(properties);
this.serviceReference = new MockServiceReference<T>(bundle, this);
this.bundleContext = bundleContext;
}
+ private void updateProperties(final Dictionary<String, ?> newProps) {
+ this.properties.clear();
+ if ( newProps != null ) {
+ final Enumeration<String> names = newProps.keys();
+ while ( names.hasMoreElements() ) {
+ final String key = names.nextElement();
+ this.properties.put(key, newProps.get(key));
+ }
+ }
+ this.properties.put(Constants.SERVICE_ID, this.serviceId);
+ this.properties.put(Constants.OBJECTCLASS, this.clazzes.toArray(new
String[this.clazzes.size()]));
+ }
+
@Override
public ServiceReference<T> getReference() {
return this.serviceReference;
}
- @SuppressWarnings("unchecked")
@Override
- public void setProperties(final Dictionary properties) {
- this.properties = properties;
+ public void setProperties(final Dictionary<String, ?> newProps) {
+ this.updateProperties(newProps);
+ this.bundleContext.notifyServiceListeners(ServiceEvent.MODIFIED,
this.serviceReference);
}
@Override
diff --git
a/core/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextTest.java
b/core/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextTest.java
index dc425b0..9f9680a 100644
---
a/core/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextTest.java
+++
b/core/src/test/java/org/apache/sling/testing/mock/osgi/MockBundleContextTest.java
@@ -30,9 +30,11 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import java.io.File;
+import java.util.ArrayList;
import java.util.Collection;
import java.util.Dictionary;
import java.util.Hashtable;
+import java.util.List;
import org.junit.After;
import org.junit.Before;
@@ -127,6 +129,64 @@ public class MockBundleContextTest {
}
@Test
+ public void testModifyServiceRegistration() throws InvalidSyntaxException {
+ // register test services
+ String service1 = "service";
+ Dictionary<String, Object> properties1 = new Hashtable<>();
+ properties1.put("a", "1");
+ properties1.put("b", "2");
+
+ final List<ServiceEvent> events = new ArrayList<>();
+ final ServiceListener listener = new ServiceListener(){
+
+ @Override
+ public void serviceChanged(ServiceEvent event) {
+ events.add(event);
+ }
+ };
+ bundleContext.addServiceListener(listener);
+ ServiceRegistration<String> reg1 =
bundleContext.registerService(String.class, service1, properties1);
+
+ // check properties for registration
+ assertEquals(4, reg1.getReference().getPropertyKeys().length);
+ assertNotNull(reg1.getReference().getProperty(Constants.SERVICE_ID));
+ assertNotNull(reg1.getReference().getProperty(Constants.OBJECTCLASS));
+ assertEquals("1", reg1.getReference().getProperty("a"));
+ assertEquals("2", reg1.getReference().getProperty("b"));
+
+ // check for registered event
+ assertEquals(1, events.size());
+ assertEquals(ServiceEvent.REGISTERED, events.get(0).getType());
+ assertSame(reg1.getReference(), events.get(0).getServiceReference());
+
+ // update properties
+ Dictionary<String, Object> properties2 = new Hashtable<>();
+ properties2.put("a", "1");
+ properties2.put("c", "3");
+ reg1.setProperties(properties2);
+
+ // check properties
+ assertEquals(4, reg1.getReference().getPropertyKeys().length);
+ assertNotNull(reg1.getReference().getProperty(Constants.SERVICE_ID));
+ assertNotNull(reg1.getReference().getProperty(Constants.OBJECTCLASS));
+ assertEquals("1", reg1.getReference().getProperty("a"));
+ assertEquals("3", reg1.getReference().getProperty("c"));
+
+ // check for modified event
+ assertEquals(2, events.size());
+ assertEquals(ServiceEvent.MODIFIED, events.get(1).getType());
+ assertSame(reg1.getReference(), events.get(1).getServiceReference());
+
+ // unregister
+ reg1.unregister();
+
+ // check for unregister event
+ assertEquals(3, events.size());
+ assertEquals(ServiceEvent.UNREGISTERING, events.get(2).getType());
+ assertSame(reg1.getReference(), events.get(2).getServiceReference());
+ }
+
+ @Test
public void testServiceFactoryRegistration() throws InvalidSyntaxException
{
// prepare test services
Class<String> clazz = String.class;