Search in sources :

Example 1 with SynchronousTestListener

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();
    }
}
Also used : SynchronousConfigurationListener(org.osgi.service.cm.SynchronousConfigurationListener) ConfigurationListener(org.osgi.service.cm.ConfigurationListener) Configuration(org.osgi.service.cm.Configuration) SynchronousConfigurationListener(org.osgi.service.cm.SynchronousConfigurationListener) SynchronousTestListener(org.apache.felix.cm.integration.helper.SynchronousTestListener) TestListener(org.apache.felix.cm.integration.helper.TestListener) SynchronousTestListener(org.apache.felix.cm.integration.helper.SynchronousTestListener) IOException(java.io.IOException) ServiceRegistration(org.osgi.framework.ServiceRegistration) Test(org.junit.Test)

Example 2 with SynchronousTestListener

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);
}
Also used : SynchronousConfigurationListener(org.osgi.service.cm.SynchronousConfigurationListener) ConfigurationListener(org.osgi.service.cm.ConfigurationListener) Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) SynchronousConfigurationListener(org.osgi.service.cm.SynchronousConfigurationListener) SynchronousTestListener(org.apache.felix.cm.integration.helper.SynchronousTestListener) TestListener(org.apache.felix.cm.integration.helper.TestListener) SynchronousTestListener(org.apache.felix.cm.integration.helper.SynchronousTestListener) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Aggregations

SynchronousTestListener (org.apache.felix.cm.integration.helper.SynchronousTestListener)2 TestListener (org.apache.felix.cm.integration.helper.TestListener)2 Test (org.junit.Test)2 ConfigurationListener (org.osgi.service.cm.ConfigurationListener)2 SynchronousConfigurationListener (org.osgi.service.cm.SynchronousConfigurationListener)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Bundle (org.osgi.framework.Bundle)1 ServiceReference (org.osgi.framework.ServiceReference)1 ServiceRegistration (org.osgi.framework.ServiceRegistration)1 Configuration (org.osgi.service.cm.Configuration)1 ConfigurationAdmin (org.osgi.service.cm.ConfigurationAdmin)1