Search in sources :

Example 31 with IAnswer

use of org.easymock.IAnswer in project karaf by apache.

the class GuardProxyCatalogTest method testHandleServiceModified2.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testHandleServiceModified2() throws Exception {
    // no configuration used in this test...
    BundleContext bc = mockConfigAdminBundleContext();
    GuardProxyCatalog gpc = new GuardProxyCatalog(bc);
    // The service being proxied has these properties
    long serviceID = 1L;
    final Hashtable<String, Object> serviceProps = new Hashtable<>();
    serviceProps.put(Constants.OBJECTCLASS, new String[] { TestServiceAPI.class.getName() });
    serviceProps.put(Constants.SERVICE_ID, serviceID);
    BundleContext providerBC = EasyMock.createNiceMock(BundleContext.class);
    EasyMock.expect(providerBC.registerService(EasyMock.aryEq(new String[] { TestServiceAPI.class.getName() }), EasyMock.anyObject(), EasyMock.anyObject(Dictionary.class))).andAnswer((IAnswer) () -> {
        final Dictionary props = (Dictionary) EasyMock.getCurrentArguments()[2];
        assertEquals(Boolean.TRUE, props.get(GuardProxyCatalog.PROXY_SERVICE_KEY));
        ServiceRegistration reg = EasyMock.createMock(ServiceRegistration.class);
        ServiceReference sr = mockServiceReference(props);
        EasyMock.expect(reg.getReference()).andReturn(sr).anyTimes();
        reg.setProperties(EasyMock.isA(Dictionary.class));
        EasyMock.expectLastCall().andAnswer(() -> {
            ArrayList<String> oldKeys = Collections.list(props.keys());
            for (String key : oldKeys) {
                props.remove(key);
            }
            Dictionary<String, Object> newProps = (Dictionary<String, Object>) EasyMock.getCurrentArguments()[0];
            for (String key : Collections.list(newProps.keys())) {
                props.put(key, newProps.get(key));
            }
            return null;
        }).once();
        EasyMock.replay(reg);
        return reg;
    }).anyTimes();
    EasyMock.replay(providerBC);
    // In some cases the proxy-creating code is looking for a classloader (e.g. when run through
    // a coverage tool such as EclEmma). This will satisfy that.
    BundleWiring bw = EasyMock.createMock(BundleWiring.class);
    EasyMock.expect(bw.getClassLoader()).andReturn(getClass().getClassLoader()).anyTimes();
    EasyMock.replay(bw);
    // The mock bundle that provides the original service (and also the proxy is registered with this)
    Bundle providerBundle = EasyMock.createNiceMock(Bundle.class);
    EasyMock.expect(providerBundle.getBundleContext()).andReturn(providerBC).anyTimes();
    EasyMock.expect(providerBundle.adapt(BundleWiring.class)).andReturn(bw).anyTimes();
    EasyMock.replay(providerBundle);
    ServiceReference sr = mockServiceReference(providerBundle, serviceProps);
    gpc.proxyIfNotAlreadyProxied(sr);
    GuardProxyCatalog.CreateProxyRunnable runnable = gpc.createProxyQueue.take();
    runnable.run(getProxyManager());
    ServiceRegistrationHolder holder = gpc.proxyMap.get(serviceID);
    ServiceRegistration<?> reg = holder.registration;
    assertFalse("No roles defined for this service using configuration, so roles property should not be set", Arrays.asList(reg.getReference().getPropertyKeys()).contains(GuardProxyCatalog.SERVICE_GUARD_ROLES_PROPERTY));
    for (String key : serviceProps.keySet()) {
        assertEquals(serviceProps.get(key), reg.getReference().getProperty(key));
    }
    assertEquals(Boolean.TRUE, reg.getReference().getProperty(GuardProxyCatalog.PROXY_SERVICE_KEY));
    // now change the original service and let the proxy react
    serviceProps.put(GuardProxyCatalog.SERVICE_GUARD_ROLES_PROPERTY, "foobar");
    assertEquals("Precondition, the mocked reference should have picked up this change", "foobar", sr.getProperty(GuardProxyCatalog.SERVICE_GUARD_ROLES_PROPERTY));
    gpc.serviceChanged(new ServiceEvent(ServiceEvent.MODIFIED, sr));
    assertEquals("Changing the service should not change the number of proxies", 1, gpc.proxyMap.size());
    assertFalse("The roles property set on the modified service should have been removed", Arrays.asList(reg.getReference().getPropertyKeys()).contains(GuardProxyCatalog.SERVICE_GUARD_ROLES_PROPERTY));
    assertEquals(Boolean.TRUE, reg.getReference().getProperty(GuardProxyCatalog.PROXY_SERVICE_KEY));
}
Also used : Dictionary(java.util.Dictionary) CreateProxyRunnable(org.apache.karaf.service.guard.impl.GuardProxyCatalog.CreateProxyRunnable) Hashtable(java.util.Hashtable) Bundle(org.osgi.framework.Bundle) BundleWiring(org.osgi.framework.wiring.BundleWiring) ArrayList(java.util.ArrayList) ServiceRegistrationHolder(org.apache.karaf.service.guard.impl.GuardProxyCatalog.ServiceRegistrationHolder) ServiceReference(org.osgi.framework.ServiceReference) IAnswer(org.easymock.IAnswer) ServiceEvent(org.osgi.framework.ServiceEvent) BundleContext(org.osgi.framework.BundleContext) ServiceRegistration(org.osgi.framework.ServiceRegistration) Test(org.junit.Test)

Aggregations

IAnswer (org.easymock.IAnswer)31 Test (org.junit.Test)17 Bundle (org.osgi.framework.Bundle)11 BundleContext (org.osgi.framework.BundleContext)11 Hashtable (java.util.Hashtable)8 ArrayList (java.util.ArrayList)6 Dictionary (java.util.Dictionary)6 ServiceReference (org.osgi.framework.ServiceReference)6 BundleWiring (org.osgi.framework.wiring.BundleWiring)6 Firehose (io.druid.data.input.Firehose)5 InputRow (io.druid.data.input.InputRow)5 MapBasedInputRow (io.druid.data.input.MapBasedInputRow)5 URL (java.net.URL)5 URLClassLoader (java.net.URLClassLoader)5 HashMap (java.util.HashMap)5 ServiceRegistrationHolder (org.apache.karaf.service.guard.impl.GuardProxyCatalog.ServiceRegistrationHolder)5 ServiceRegistration (org.osgi.framework.ServiceRegistration)5 CreateProxyRunnable (org.apache.karaf.service.guard.impl.GuardProxyCatalog.CreateProxyRunnable)4 LinkedHashMap (java.util.LinkedHashMap)3 ProxyManager (org.apache.aries.proxy.ProxyManager)3