use of org.apache.felix.cm.integration.helper.SynchronousTestListener 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.apache.felix.cm.integration.helper.SynchronousTestListener in project felix by apache.
the class FELIX2813_ConfigurationAdminStartupTest method testAddConfigurationWhenConfigurationAdminStarts.
@Test
public void testAddConfigurationWhenConfigurationAdminStarts() throws InvalidSyntaxException, BundleException {
List<Bundle> bundles = new ArrayList<Bundle>();
ServiceReference[] refs = configAdminTracker.getServiceReferences();
if (refs != null) {
for (ServiceReference ref : refs) {
bundles.add(ref.getBundle());
ref.getBundle().stop();
}
}
final TestListener listener = new TestListener();
bundleContext.registerService(ConfigurationListener.class.getName(), listener, null);
final TestListener syncListener = new SynchronousTestListener();
bundleContext.registerService(SynchronousConfigurationListener.class.getName(), syncListener, null);
final TestListener syncListenerAsync = new SynchronousTestListener();
bundleContext.registerService(ConfigurationListener.class.getName(), syncListenerAsync, null);
bundleContext.addServiceListener(this, "(" + Constants.OBJECTCLASS + "=" + ConfigurationAdmin.class.getName() + ")");
for (Bundle bundle : bundles) {
bundle.start();
}
/*
* Look at the console output for the following exception:
*
* *ERROR* Unexpected problem executing task
* java.lang.NullPointerException: reference and pid must not be null
* at org.osgi.service.cm.ConfigurationEvent.<init>(ConfigurationEvent.java:120)
* at org.apache.felix.cm.impl.ConfigurationManager$FireConfigurationEvent.run(ConfigurationManager.java:1818)
* at org.apache.felix.cm.impl.UpdateThread.run(UpdateThread.java:104)
* at java.lang.Thread.run(Thread.java:680)
*
* It is in fact the service reference that is still null, because the service registration
* has not been 'set' yet.
*
* This following code will ensure the situation did not occurr and the
* event has effectively been sent. The eventSeen flag is set by the
* configurationEvent method when the event for the test PID has been
* received. If the flag is not set, we wait at most 2 seconds for the
* event to arrive. If the event does not arrive by then, the test is
* assumed to have failed. This will rather generate false negatives
* (on slow machines) than false positives.
*/
delay();
listener.assertEvent(ConfigurationEvent.CM_UPDATED, "test", null, true, 1);
syncListener.assertEvent(ConfigurationEvent.CM_UPDATED, "test", null, false, 1);
syncListenerAsync.assertEvent(ConfigurationEvent.CM_UPDATED, "test", null, true, 1);
}
Aggregations