use of org.osgi.service.cm.SynchronousConfigurationListener in project felix by apache.
the class ConfigurationListenerTest method test_sync_listener.
@Test
public void test_sync_listener() throws IOException {
final String pid = "test_listener";
Configuration config = configure(pid, null, false);
// Synchronous listener expecting synchronous events being
// registered as a SynchronousConfigurationListener
final TestListener testListener = new SynchronousTestListener();
final ServiceRegistration listener = this.bundleContext.registerService(SynchronousConfigurationListener.class.getName(), testListener, null);
// Synchronous listener expecting asynchronous events being
// registered as a regular ConfigurationListener
final TestListener testListenerAsync = new SynchronousTestListener();
final ServiceRegistration listenerAsync = this.bundleContext.registerService(ConfigurationListener.class.getName(), testListenerAsync, null);
int eventCount = 0;
int eventCountAsync = 0;
try {
delay();
testListener.assertNoEvent();
testListenerAsync.assertNoEvent();
config.update(new Hashtable<String, Object>() {
{
put("x", "x");
}
});
delay();
testListener.assertEvent(ConfigurationEvent.CM_UPDATED, pid, null, false, ++eventCount);
testListenerAsync.assertEvent(ConfigurationEvent.CM_UPDATED, pid, null, true, ++eventCountAsync);
config.update(new Hashtable<String, Object>() {
{
put("x", "x");
}
});
delay();
testListener.assertEvent(ConfigurationEvent.CM_UPDATED, pid, null, false, ++eventCount);
testListenerAsync.assertEvent(ConfigurationEvent.CM_UPDATED, pid, null, true, ++eventCountAsync);
config.setBundleLocation("new_Location");
delay();
testListener.assertEvent(ConfigurationEvent.CM_LOCATION_CHANGED, pid, null, false, ++eventCount);
testListenerAsync.assertEvent(ConfigurationEvent.CM_LOCATION_CHANGED, pid, null, true, ++eventCountAsync);
config.update();
testListener.assertNoEvent();
testListenerAsync.assertNoEvent();
config.delete();
config = null;
delay();
testListener.assertEvent(ConfigurationEvent.CM_DELETED, pid, null, false, ++eventCount);
testListenerAsync.assertEvent(ConfigurationEvent.CM_DELETED, pid, null, true, ++eventCountAsync);
} finally {
if (config != null) {
try {
config.delete();
} catch (IOException ioe) {
// ignore
}
}
listener.unregister();
listenerAsync.unregister();
}
}
use of org.osgi.service.cm.SynchronousConfigurationListener in project felix by apache.
the class ConfigurationManagerTest method testEventsStartingBundle.
@Test
public void testEventsStartingBundle() throws Exception {
final Set<String> result = new HashSet<>();
SynchronousConfigurationListener syncListener1 = new SynchronousConfigurationListener() {
@Override
public void configurationEvent(ConfigurationEvent event) {
result.add("L1");
}
};
SynchronousConfigurationListener syncListener2 = new SynchronousConfigurationListener() {
@Override
public void configurationEvent(ConfigurationEvent event) {
result.add("L2");
}
};
SynchronousConfigurationListener syncListener3 = new SynchronousConfigurationListener() {
@Override
public void configurationEvent(ConfigurationEvent event) {
result.add("L3");
}
};
ServiceReference mockRef = Mockito.mock(ServiceReference.class);
ServiceRegistration mockReg = Mockito.mock(ServiceRegistration.class);
Mockito.when(mockReg.getReference()).thenReturn(mockRef);
ConfigurationManager configMgr = new ConfigurationManager(new PersistenceManagerProxy(new MockPersistenceManager()), null);
setServiceTrackerField(configMgr, "configurationListenerTracker");
ServiceReference[] refs = setServiceTrackerField(configMgr, "syncConfigurationListenerTracker", syncListener1, syncListener2, syncListener3);
for (int i = 0; i < refs.length; i++) {
Bundle mockBundle = Mockito.mock(Bundle.class);
switch(i) {
case 0:
Mockito.when(mockBundle.getState()).thenReturn(Bundle.ACTIVE);
break;
case 1:
Mockito.when(mockBundle.getState()).thenReturn(Bundle.STARTING);
break;
case 2:
Mockito.when(mockBundle.getState()).thenReturn(Bundle.STOPPING);
break;
}
Mockito.when(refs[i].getBundle()).thenReturn(mockBundle);
}
Field srField = configMgr.getClass().getDeclaredField("configurationAdminRegistration");
srField.setAccessible(true);
srField.set(configMgr, mockReg);
Field utField = configMgr.getClass().getDeclaredField("updateThread");
utField.setAccessible(true);
utField.set(configMgr, new UpdateThread(null, "Test updater"));
Dictionary<String, Object> props = new Hashtable<>();
props.put(Constants.SERVICE_PID, "org.acme.testpid");
ConfigurationImpl config = new ConfigurationImpl(configMgr, new MockPersistenceManager(), props);
configMgr.updated(config, true);
assertEquals("Both listeners should have been called, both in the STARTING and ACTIVE state, but not in the STOPPING state", 2, result.size());
}
Aggregations