Search in sources :

Example 1 with MockPersistenceManager

use of org.apache.felix.cm.MockPersistenceManager in project felix by apache.

the class CachingPersistenceManagerProxyTest method test_caching_is_applied.

@SuppressWarnings({ "rawtypes", "unchecked" })
public void test_caching_is_applied() throws Exception {
    String pid = "testDefaultPersistenceManager";
    SimpleFilter filter = SimpleFilter.parse("(&(service.pid=" + pid + ")(property1=value1))");
    PersistenceManager pm = new MockPersistenceManager();
    CachingPersistenceManagerProxy cpm = new CachingPersistenceManagerProxy(pm);
    Dictionary dictionary = new Hashtable();
    dictionary.put("property1", "value1");
    dictionary.put(Constants.SERVICE_PID, pid);
    pm.store(pid, dictionary);
    Enumeration dictionaries = cpm.getDictionaries(filter);
    List list = Collections.list(dictionaries);
    assertEquals(1, list.size());
    dictionary = new Hashtable();
    dictionary.put("property1", "value2");
    pid = "testDefaultPersistenceManager";
    dictionary.put(Constants.SERVICE_PID, pid);
    pm.store(pid, dictionary);
    dictionaries = cpm.getDictionaries(filter);
    list = Collections.list(dictionaries);
    assertEquals(1, list.size());
}
Also used : Dictionary(java.util.Dictionary) Enumeration(java.util.Enumeration) MockNotCachablePersistenceManager(org.apache.felix.cm.MockNotCachablePersistenceManager) MockPersistenceManager(org.apache.felix.cm.MockPersistenceManager) PersistenceManager(org.apache.felix.cm.PersistenceManager) Hashtable(java.util.Hashtable) List(java.util.List) MockPersistenceManager(org.apache.felix.cm.MockPersistenceManager)

Example 2 with MockPersistenceManager

use of org.apache.felix.cm.MockPersistenceManager in project felix by apache.

the class ConfigurationManagerTest method test_listConfigurations_cached.

@Test
public void test_listConfigurations_cached() throws Exception {
    String pid = "testDefaultPersistenceManager";
    PersistenceManager pm = new MockPersistenceManager();
    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 CachingPersistenceManagerProxy(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", "value2");
    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 property in the configurations cache was used
    assertEquals("value1", 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) CachingPersistenceManagerProxy(org.apache.felix.cm.impl.persistence.CachingPersistenceManagerProxy) MockPersistenceManager(org.apache.felix.cm.MockPersistenceManager) Test(org.junit.Test)

Example 3 with MockPersistenceManager

use of org.apache.felix.cm.MockPersistenceManager 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)

Example 4 with MockPersistenceManager

use of org.apache.felix.cm.MockPersistenceManager in project felix by apache.

the class CachingPersistenceManagerProxyTest method createAndPopulatePersistenceManager.

private PersistenceManager createAndPopulatePersistenceManager() throws IOException {
    final PersistenceManager pm = new MockPersistenceManager();
    pm.store(PID_A, createConfiguration(PID_A, null));
    pm.store(PID_B, createConfiguration(PID_B, null));
    pm.store(PID_C, createConfiguration(PID_C, null));
    pm.store(FA_PID_A, createConfiguration(FA_PID_A, FACTORY_PID_A));
    pm.store(FA_PID_B, createConfiguration(FA_PID_B, FACTORY_PID_A));
    pm.store(FA_PID_C, createConfiguration(FA_PID_C, FACTORY_PID_A));
    pm.store(FB_PID_A, createConfiguration(FB_PID_A, FACTORY_PID_B));
    pm.store(FB_PID_B, createConfiguration(FB_PID_B, FACTORY_PID_B));
    return pm;
}
Also used : MockPersistenceManager(org.apache.felix.cm.MockPersistenceManager) PersistenceManager(org.apache.felix.cm.PersistenceManager) MockPersistenceManager(org.apache.felix.cm.MockPersistenceManager)

Example 5 with MockPersistenceManager

use of org.apache.felix.cm.MockPersistenceManager in project felix by apache.

the class CachingPersistenceManagerProxyTest method test_caching_is_applied.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void test_caching_is_applied() throws Exception {
    String pid = "testDefaultPersistenceManager";
    SimpleFilter filter = SimpleFilter.parse("(&(service.pid=" + pid + ")(property1=value1))");
    PersistenceManager pm = new MockPersistenceManager();
    CachingPersistenceManagerProxy cpm = new CachingPersistenceManagerProxy(pm);
    Dictionary dictionary = new Hashtable();
    dictionary.put("property1", "value1");
    dictionary.put(Constants.SERVICE_PID, pid);
    pm.store(pid, dictionary);
    Collection<Dictionary> list = cpm.getDictionaries(filter);
    assertEquals(1, list.size());
    dictionary = new Hashtable();
    dictionary.put("property1", "value2");
    pid = "testDefaultPersistenceManager";
    dictionary.put(Constants.SERVICE_PID, pid);
    pm.store(pid, dictionary);
    list = cpm.getDictionaries(filter);
    assertEquals(1, list.size());
}
Also used : Dictionary(java.util.Dictionary) MockPersistenceManager(org.apache.felix.cm.MockPersistenceManager) PersistenceManager(org.apache.felix.cm.PersistenceManager) Hashtable(java.util.Hashtable) SimpleFilter(org.apache.felix.cm.impl.SimpleFilter) MockPersistenceManager(org.apache.felix.cm.MockPersistenceManager) Test(org.junit.Test)

Aggregations

MockPersistenceManager (org.apache.felix.cm.MockPersistenceManager)5 Hashtable (java.util.Hashtable)4 PersistenceManager (org.apache.felix.cm.PersistenceManager)4 Test (org.junit.Test)3 Dictionary (java.util.Dictionary)2 MockNotCachablePersistenceManager (org.apache.felix.cm.MockNotCachablePersistenceManager)2 CachingPersistenceManagerProxy (org.apache.felix.cm.impl.persistence.CachingPersistenceManagerProxy)2 Field (java.lang.reflect.Field)1 Enumeration (java.util.Enumeration)1 HashSet (java.util.HashSet)1 List (java.util.List)1 SimpleFilter (org.apache.felix.cm.impl.SimpleFilter)1 PersistenceManagerProxy (org.apache.felix.cm.impl.persistence.PersistenceManagerProxy)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