Search in sources :

Example 1 with PersistenceManagerProxy

use of org.apache.felix.cm.impl.persistence.PersistenceManagerProxy in project felix by apache.

the class ConfigurationManagerTest method test_listConfigurations_notcached.

@Test
public void test_listConfigurations_notcached() throws Exception {
    String pid = "testDefaultPersistenceManager";
    PersistenceManager pm = new MockNotCachablePersistenceManager();
    Dictionary<String, Object> dictionary = new Hashtable<>();
    dictionary.put("property1", "value1");
    dictionary.put(Constants.SERVICE_PID, pid);
    pm.store(pid, dictionary);
    ConfigurationManager configMgr = new ConfigurationManager(new PersistenceManagerProxy(pm), null);
    ConfigurationImpl[] conf = configMgr.listConfigurations(new ConfigurationAdminImpl(configMgr, null), null);
    assertEquals(1, conf.length);
    assertEquals(2, conf[0].getProperties(true).size());
    dictionary = new Hashtable<>();
    dictionary.put("property1", "valueNotCached");
    pid = "testDefaultPersistenceManager";
    dictionary.put(Constants.SERVICE_PID, pid);
    pm.store(pid, dictionary);
    conf = configMgr.listConfigurations(new ConfigurationAdminImpl(configMgr, null), null);
    assertEquals(1, conf.length);
    assertEquals(2, conf[0].getProperties(true).size());
    // verify that the value returned was not the one from the cache
    assertEquals("valueNotCached", conf[0].getProperties(true).get("property1"));
}
Also used : PersistenceManager(org.apache.felix.cm.PersistenceManager) MockNotCachablePersistenceManager(org.apache.felix.cm.MockNotCachablePersistenceManager) MockPersistenceManager(org.apache.felix.cm.MockPersistenceManager) Hashtable(java.util.Hashtable) PersistenceManagerProxy(org.apache.felix.cm.impl.persistence.PersistenceManagerProxy) CachingPersistenceManagerProxy(org.apache.felix.cm.impl.persistence.CachingPersistenceManagerProxy) MockNotCachablePersistenceManager(org.apache.felix.cm.MockNotCachablePersistenceManager) Test(org.junit.Test)

Example 2 with PersistenceManagerProxy

use of org.apache.felix.cm.impl.persistence.PersistenceManagerProxy 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

Hashtable (java.util.Hashtable)2 MockPersistenceManager (org.apache.felix.cm.MockPersistenceManager)2 CachingPersistenceManagerProxy (org.apache.felix.cm.impl.persistence.CachingPersistenceManagerProxy)2 PersistenceManagerProxy (org.apache.felix.cm.impl.persistence.PersistenceManagerProxy)2 Test (org.junit.Test)2 Field (java.lang.reflect.Field)1 HashSet (java.util.HashSet)1 MockNotCachablePersistenceManager (org.apache.felix.cm.MockNotCachablePersistenceManager)1 PersistenceManager (org.apache.felix.cm.PersistenceManager)1 Bundle (org.osgi.framework.Bundle)1 ServiceReference (org.osgi.framework.ServiceReference)1 ServiceRegistration (org.osgi.framework.ServiceRegistration)1 ConfigurationEvent (org.osgi.service.cm.ConfigurationEvent)1 SynchronousConfigurationListener (org.osgi.service.cm.SynchronousConfigurationListener)1