Search in sources :

Example 1 with SynchronousConfigurationListener

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();
    }
}
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 SynchronousConfigurationListener

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());
}
Also used : ConfigurationEvent(org.osgi.service.cm.ConfigurationEvent) Bundle(org.osgi.framework.Bundle) Hashtable(java.util.Hashtable) PersistenceManagerProxy(org.apache.felix.cm.impl.persistence.PersistenceManagerProxy) CachingPersistenceManagerProxy(org.apache.felix.cm.impl.persistence.CachingPersistenceManagerProxy) SynchronousConfigurationListener(org.osgi.service.cm.SynchronousConfigurationListener) ServiceReference(org.osgi.framework.ServiceReference) Field(java.lang.reflect.Field) MockPersistenceManager(org.apache.felix.cm.MockPersistenceManager) HashSet(java.util.HashSet) ServiceRegistration(org.osgi.framework.ServiceRegistration) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)2 ServiceRegistration (org.osgi.framework.ServiceRegistration)2 SynchronousConfigurationListener (org.osgi.service.cm.SynchronousConfigurationListener)2 IOException (java.io.IOException)1 Field (java.lang.reflect.Field)1 HashSet (java.util.HashSet)1 Hashtable (java.util.Hashtable)1 MockPersistenceManager (org.apache.felix.cm.MockPersistenceManager)1 CachingPersistenceManagerProxy (org.apache.felix.cm.impl.persistence.CachingPersistenceManagerProxy)1 PersistenceManagerProxy (org.apache.felix.cm.impl.persistence.PersistenceManagerProxy)1 SynchronousTestListener (org.apache.felix.cm.integration.helper.SynchronousTestListener)1 TestListener (org.apache.felix.cm.integration.helper.TestListener)1 Bundle (org.osgi.framework.Bundle)1 ServiceReference (org.osgi.framework.ServiceReference)1 Configuration (org.osgi.service.cm.Configuration)1 ConfigurationEvent (org.osgi.service.cm.ConfigurationEvent)1 ConfigurationListener (org.osgi.service.cm.ConfigurationListener)1