Search in sources :

Example 6 with MockBundleContext

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

the class DynamicBindingsTest method test_store_bindings.

public void test_store_bindings() throws IOException {
    // ensure there is no file
    bindingsFile.delete();
    final BundleContext ctx = new MockBundleContext();
    final DynamicBindings dm = new DynamicBindings(ctx, persistenceManager);
    dm.putLocation(PID1, LOCATION1);
    assertEquals(LOCATION1, dm.getLocation(PID1));
    assertTrue(bindingsFile.exists());
    final Dictionary bindings = persistenceManager.load(DynamicBindings.BINDINGS_FILE_NAME);
    assertNotNull(bindings);
    assertEquals(1, bindings.size());
    assertEquals(LOCATION1, bindings.get(PID1));
}
Also used : Dictionary(java.util.Dictionary) MockBundleContext(org.apache.felix.cm.MockBundleContext) BundleContext(org.osgi.framework.BundleContext) MockBundleContext(org.apache.felix.cm.MockBundleContext)

Example 7 with MockBundleContext

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

the class ConfigurationManagerTest method test_factoryConfigurationCleanup.

public void test_factoryConfigurationCleanup() throws Exception {
    MockNotCachablePersistenceManager pm = new MockNotCachablePersistenceManager();
    ConfigurationManager configMgr = new ConfigurationManager(new CachingPersistenceManagerProxy(pm), null);
    final Field bcField = configMgr.getClass().getDeclaredField("bundleContext");
    bcField.setAccessible(true);
    bcField.set(configMgr, new MockBundleContext());
    setServiceTrackerField(configMgr, "persistenceManagerTracker");
    setServiceTrackerField(configMgr, "logTracker");
    setServiceTrackerField(configMgr, "configurationListenerTracker");
    setServiceTrackerField(configMgr, "syncConfigurationListenerTracker");
    final Field mstField = configMgr.getClass().getDeclaredField("managedServiceFactoryTracker");
    mstField.setAccessible(true);
    mstField.set(configMgr, new ManagedServiceFactoryTracker(configMgr) {

        @Override
        public void open() {
        }
    });
    final Field utField = configMgr.getClass().getDeclaredField("updateThread");
    utField.setAccessible(true);
    utField.set(configMgr, new UpdateThread(null, "Test updater") {

        @Override
        void schedule(Runnable update) {
            update.run();
        }
    });
    final String factoryPid = "my.factory";
    final Dictionary<String, Object> props = new Hashtable<>();
    props.put("hello", "world");
    final ConfigurationImpl c1 = configMgr.createFactoryConfiguration(factoryPid, null);
    c1.update(props);
    final ConfigurationImpl c2 = configMgr.createFactoryConfiguration(factoryPid, null);
    c2.update(props);
    final ConfigurationImpl c3 = configMgr.createFactoryConfiguration(factoryPid, null);
    c3.update(props);
    assertEquals(4, pm.getStored().size());
    c1.delete();
    assertEquals(3, pm.getStored().size());
    c2.delete();
    assertEquals(2, pm.getStored().size());
    c3.delete();
    assertEquals(0, pm.getStored().size());
}
Also used : ManagedServiceFactoryTracker(org.apache.felix.cm.impl.helper.ManagedServiceFactoryTracker) CachingPersistenceManagerProxy(org.apache.felix.cm.impl.persistence.CachingPersistenceManagerProxy) Hashtable(java.util.Hashtable) Field(java.lang.reflect.Field) MockBundleContext(org.apache.felix.cm.MockBundleContext) MockNotCachablePersistenceManager(org.apache.felix.cm.MockNotCachablePersistenceManager)

Example 8 with MockBundleContext

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

the class ConfigurationManagerTest method testLogSetup.

@Test
public void testLogSetup() throws IOException {
    final MockBundleContext bundleContext = new MockBundleContext();
    createConfigurationManagerAndLog(null);
    // ensure the configuration data goes to target
    bundleContext.setProperty("felix.cm.dir", "target/config");
    // default value is 2
    bundleContext.setProperty("felix.cm.loglevel", null);
    Log.logger.start(bundleContext);
    assertEquals(2, getLogLevel());
    Log.logger.stop();
    // illegal number yields default value
    bundleContext.setProperty("felix.cm.loglevel", "not-a-number");
    Log.logger.start(bundleContext);
    assertEquals(2, getLogLevel());
    Log.logger.stop();
    bundleContext.setProperty("felix.cm.loglevel", "-100");
    Log.logger.start(bundleContext);
    assertEquals(-100, getLogLevel());
    Log.logger.stop();
    bundleContext.setProperty("felix.cm.loglevel", "4");
    Log.logger.start(bundleContext);
    assertEquals(4, getLogLevel());
    Log.logger.stop();
}
Also used : MockBundleContext(org.apache.felix.cm.MockBundleContext) Test(org.junit.Test)

Example 9 with MockBundleContext

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

the class DynamicBindingsTest method test_store_and_load_bindings.

public void test_store_and_load_bindings() throws IOException {
    // ensure there is no file
    bindingsFile.delete();
    // preset bindings
    final DynamicBindings dm0 = new DynamicBindings(new MockBundleContext(), persistenceManager);
    dm0.putLocation(PID1, LOCATION1);
    // check bindings
    final BundleContext ctx = new DMTestMockBundleContext();
    final DynamicBindings dm = new DynamicBindings(ctx, persistenceManager);
    // API check
    assertEquals(LOCATION1, dm.getLocation(PID1));
    // low level check
    final Dictionary bindings = getBindings(dm);
    assertNotNull(bindings);
    assertEquals(1, bindings.size());
    assertEquals(LOCATION1, bindings.get(PID1));
}
Also used : Dictionary(java.util.Dictionary) MockBundleContext(org.apache.felix.cm.MockBundleContext) BundleContext(org.osgi.framework.BundleContext) MockBundleContext(org.apache.felix.cm.MockBundleContext)

Aggregations

MockBundleContext (org.apache.felix.cm.MockBundleContext)9 Field (java.lang.reflect.Field)4 Dictionary (java.util.Dictionary)4 BundleContext (org.osgi.framework.BundleContext)3 ServiceTracker (org.osgi.util.tracker.ServiceTracker)3 MockNotCachablePersistenceManager (org.apache.felix.cm.MockNotCachablePersistenceManager)2 CachingPersistenceManagerProxy (org.apache.felix.cm.impl.persistence.CachingPersistenceManagerProxy)2 HashMap (java.util.HashMap)1 Hashtable (java.util.Hashtable)1 MockPersistenceManager (org.apache.felix.cm.MockPersistenceManager)1 PersistenceManager (org.apache.felix.cm.PersistenceManager)1 ManagedServiceFactoryTracker (org.apache.felix.cm.impl.helper.ManagedServiceFactoryTracker)1 Test (org.junit.Test)1 ServiceReference (org.osgi.framework.ServiceReference)1