Search in sources :

Example 1 with MockBundleContext

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

the class ConfigurationManagerTest method createConfigurationManager.

private static ConfigurationManager createConfigurationManager(final LogService logService) {
    ConfigurationManager configMgr = new ConfigurationManager();
    try {
        Field field = configMgr.getClass().getDeclaredField("logTracker");
        field.setAccessible(true);
        field.set(configMgr, new ServiceTracker(new MockBundleContext(), "", null) {

            @Override
            public Object getService() {
                return logService;
            }
        });
    } catch (Throwable ignore) {
        throw (IllegalArgumentException) new IllegalArgumentException("Cannot set logTracker field value").initCause(ignore);
    }
    return configMgr;
}
Also used : Field(java.lang.reflect.Field) ServiceTracker(org.osgi.util.tracker.ServiceTracker) MockBundleContext(org.apache.felix.cm.MockBundleContext)

Example 2 with MockBundleContext

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

the class ConfigurationManagerTest method setServiceTrackerField.

private static ServiceReference[] setServiceTrackerField(ConfigurationManager configMgr, String fieldName, Object... services) throws Exception {
    final Map<ServiceReference, Object> refMap = new HashMap<>();
    for (Object svc : services) {
        ServiceReference sref = Mockito.mock(ServiceReference.class);
        Mockito.when(sref.getProperty("objectClass")).thenReturn(new String[] { "TestService" });
        refMap.put(sref, svc);
    }
    Field field = configMgr.getClass().getDeclaredField(fieldName);
    field.setAccessible(true);
    field.set(configMgr, new ServiceTracker(new MockBundleContext(), "", null) {

        @Override
        public ServiceReference[] getServiceReferences() {
            return refMap.keySet().toArray(new ServiceReference[0]);
        }

        @Override
        public Object getService(ServiceReference reference) {
            return refMap.get(reference);
        }
    });
    return refMap.keySet().toArray(new ServiceReference[0]);
}
Also used : Field(java.lang.reflect.Field) HashMap(java.util.HashMap) ServiceTracker(org.osgi.util.tracker.ServiceTracker) MockBundleContext(org.apache.felix.cm.MockBundleContext) ServiceReference(org.osgi.framework.ServiceReference)

Example 3 with MockBundleContext

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

the class ConfigurationManagerTest method createConfigurationManagerAndLog.

private static ConfigurationManager createConfigurationManagerAndLog(final LogService logService) throws IOException {
    final PersistenceManager pm = Mockito.mock(PersistenceManager.class);
    ConfigurationManager configMgr = new ConfigurationManager(new CachingPersistenceManagerProxy(pm), null);
    try {
        Field field = Log.class.getDeclaredField("logTracker");
        field.setAccessible(true);
        field.set(Log.logger, new ServiceTracker(new MockBundleContext(), "", null) {

            @Override
            public Object getService() {
                return logService;
            }
        });
    } catch (Throwable ignore) {
        throw (IllegalArgumentException) new IllegalArgumentException("Cannot set logTracker field value").initCause(ignore);
    }
    return configMgr;
}
Also used : Field(java.lang.reflect.Field) PersistenceManager(org.apache.felix.cm.PersistenceManager) MockNotCachablePersistenceManager(org.apache.felix.cm.MockNotCachablePersistenceManager) MockPersistenceManager(org.apache.felix.cm.MockPersistenceManager) ServiceTracker(org.osgi.util.tracker.ServiceTracker) MockBundleContext(org.apache.felix.cm.MockBundleContext) CachingPersistenceManagerProxy(org.apache.felix.cm.impl.persistence.CachingPersistenceManagerProxy)

Example 4 with MockBundleContext

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

the class DynamicBindingsTest method test_no_bindings.

public void test_no_bindings() throws IOException {
    // ensure there is no file
    bindingsFile.delete();
    final BundleContext ctx = new MockBundleContext();
    final DynamicBindings dm = new DynamicBindings(ctx, persistenceManager);
    final Dictionary bindings = getBindings(dm);
    assertNotNull(bindings);
    assertTrue(bindings.isEmpty());
}
Also used : Dictionary(java.util.Dictionary) MockBundleContext(org.apache.felix.cm.MockBundleContext) BundleContext(org.osgi.framework.BundleContext) MockBundleContext(org.apache.felix.cm.MockBundleContext)

Example 5 with MockBundleContext

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

the class DynamicBindingsTest method test_store_and_load_bindings_with_cleanup.

public void test_store_and_load_bindings_with_cleanup() 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 DynamicBindings dm = new DynamicBindings(new MockBundleContext(), persistenceManager);
    // API check
    assertNull(dm.getLocation(PID1));
    // low level check
    final Dictionary bindings = getBindings(dm);
    assertNotNull(bindings);
    assertTrue(bindings.isEmpty());
}
Also used : Dictionary(java.util.Dictionary) 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