Search in sources :

Example 16 with BundleContext

use of org.osgi.framework.BundleContext in project karaf by apache.

the class GuardingFindHookTest method testNullFilter.

@Test
public void testNullFilter() throws Exception {
    BundleContext hookBC = mockBundleContext(5L);
    GuardProxyCatalog gpc = new GuardProxyCatalog(hookBC);
    GuardingFindHook gfh = new GuardingFindHook(hookBC, gpc, null);
    // should just do nothing
    gfh.find(null, null, null, true, null);
}
Also used : BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 17 with BundleContext

use of org.osgi.framework.BundleContext in project karaf by apache.

the class GuardProxyCatalogTest method testAssignRoles3.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testAssignRoles3() throws Exception {
    abstract class MyAbstractClass implements TestServiceAPI, TestServiceAPI2 {
    }
    Dictionary<String, Object> config = new Hashtable<>();
    config.put(Constants.SERVICE_PID, "foobar");
    config.put("service.guard", "(objectClass=" + TestServiceAPI2.class.getName() + ")");
    config.put("doit", "X");
    BundleContext bc = mockConfigAdminBundleContext(config);
    Map<ServiceReference, Object> serviceMap = new HashMap<>();
    testCreateProxy(bc, new Class[] { TestServiceAPI.class, TestServiceAPI2.class }, new MyAbstractClass() {

        @Override
        public String doit() {
            return null;
        }

        @Override
        public String doit(String s) {
            return null;
        }
    }, serviceMap);
    assertEquals(1, serviceMap.size());
    assertEquals(Collections.singleton("X"), serviceMap.keySet().iterator().next().getProperty(GuardProxyCatalog.SERVICE_GUARD_ROLES_PROPERTY));
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Hashtable(java.util.Hashtable) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 18 with BundleContext

use of org.osgi.framework.BundleContext in project karaf by apache.

the class GuardProxyCatalogTest method testAssignRoles4.

@SuppressWarnings("unchecked")
@Test
public void testAssignRoles4() throws Exception {
    Dictionary<String, Object> config = new Hashtable<>();
    config.put(Constants.SERVICE_PID, "foobar");
    config.put("service.guard", "(objectClass=" + TestServiceAPI.class.getName() + ")");
    config.put("somemethod", "b");
    config.put("someOtherMethod", "b");
    config.put("somethingelse", "*");
    BundleContext bc = mockConfigAdminBundleContext(config);
    Dictionary<String, Object> proxyProps = testCreateProxy(bc, TestServiceAPI.class, new TestService());
    Collection<String> result = (Collection<String>) proxyProps.get(GuardProxyCatalog.SERVICE_GUARD_ROLES_PROPERTY);
    assertEquals(1, result.size());
    assertEquals("b", result.iterator().next());
}
Also used : Hashtable(java.util.Hashtable) Collection(java.util.Collection) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 19 with BundleContext

use of org.osgi.framework.BundleContext in project karaf by apache.

the class GuardProxyCatalogTest method testGuardProxyCatalog.

@Test
public void testGuardProxyCatalog() throws Exception {
    Bundle b = EasyMock.createMock(Bundle.class);
    EasyMock.expect(b.getBundleId()).andReturn(9823L).anyTimes();
    EasyMock.replay(b);
    BundleContext bc = EasyMock.createNiceMock(BundleContext.class);
    EasyMock.expect(bc.getBundle()).andReturn(b).anyTimes();
    bc.addServiceListener(EasyMock.isA(ServiceListener.class));
    EasyMock.expectLastCall().once();
    String caFilter = "(&(objectClass=org.osgi.service.cm.ConfigurationAdmin)" + "(!(" + GuardProxyCatalog.PROXY_SERVICE_KEY + "=*)))";
    EasyMock.expect(bc.createFilter(caFilter)).andReturn(FrameworkUtil.createFilter(caFilter)).anyTimes();
    String pmFilter = "(&(objectClass=org.apache.aries.proxy.ProxyManager)" + "(!(" + GuardProxyCatalog.PROXY_SERVICE_KEY + "=*)))";
    EasyMock.expect(bc.createFilter(pmFilter)).andReturn(FrameworkUtil.createFilter(pmFilter)).anyTimes();
    EasyMock.replay(bc);
    GuardProxyCatalog gpc = new GuardProxyCatalog(bc);
    assertTrue("Service Tracker for ConfigAdmin should be opened", gpc.configAdminTracker.getTrackingCount() != -1);
    assertTrue("Service Tracker for ProxyManager should be opened", gpc.proxyManagerTracker.getTrackingCount() != -1);
    EasyMock.verify(bc);
    // Add some more behaviour checks to the bundle context
    EasyMock.reset(bc);
    bc.removeServiceListener(EasyMock.isA(ServiceListener.class));
    EasyMock.expectLastCall().once();
    EasyMock.replay(bc);
    gpc.close();
    assertEquals("Service Tracker for ConfigAdmin should be closed", -1, gpc.configAdminTracker.getTrackingCount());
    assertEquals("Service Tracker for ProxyManager should be closed", -1, gpc.proxyManagerTracker.getTrackingCount());
    EasyMock.verify(bc);
}
Also used : ServiceListener(org.osgi.framework.ServiceListener) Bundle(org.osgi.framework.Bundle) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 20 with BundleContext

use of org.osgi.framework.BundleContext in project karaf by apache.

the class ActivatorTest method testStartActivatorNoServicesSecured.

@Test
public void testStartActivatorNoServicesSecured() throws Exception {
    // keep the old properties. Note that the Properties 'copy constructor' new Properties(props)
    // doesn't actually copy, hence the awkward setup here...
    Properties oldProps = new Properties();
    oldProps.putAll(System.getProperties());
    try {
        Properties newProps = removeProperties(System.getProperties(), GuardProxyCatalog.KARAF_SECURED_SERVICES_SYSPROP);
        System.setProperties(newProps);
        BundleContext bc = EasyMock.createNiceMock(BundleContext.class);
        EasyMock.replay(bc);
        Activator a = new Activator();
        a.start(bc);
        assertNull(a.guardProxyCatalog);
    } finally {
        System.setProperties(oldProps);
    }
}
Also used : Properties(java.util.Properties) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Aggregations

BundleContext (org.osgi.framework.BundleContext)1052 Test (org.junit.Test)361 Bundle (org.osgi.framework.Bundle)298 ServiceReference (org.osgi.framework.ServiceReference)229 File (java.io.File)119 Hashtable (java.util.Hashtable)114 HashMap (java.util.HashMap)98 ArrayList (java.util.ArrayList)97 IOException (java.io.IOException)75 ServiceRegistration (org.osgi.framework.ServiceRegistration)68 BundleException (org.osgi.framework.BundleException)60 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)59 Equinox (org.eclipse.osgi.launch.Equinox)51 ComponentContext (org.osgi.service.component.ComponentContext)50 Dictionary (java.util.Dictionary)48 Before (org.junit.Before)48 Properties (java.util.Properties)47 LinkedHashMap (java.util.LinkedHashMap)46 URL (java.net.URL)43 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)40