expectedProxyProps.put(GuardProxyCatalog.PROXY_SERVICE_KEY, Boolean.TRUE);
// This will check that the right proxy is being registered.
EasyMock.expect(providerBC.registerService(
EasyMock.isA(String[].class),
EasyMock.anyObject(),
EasyMock.isA(Dictionary.class))).andAnswer(new IAnswer() {
@Override
public ServiceRegistration answer() throws Throwable {
if (!runningUnderCoverage) {
// Some of these checks don't work when running under coverage
assertArrayEquals(proxyRegClsMap.keySet().toArray(new String [] {}),
(String []) EasyMock.getCurrentArguments()[0]);
Object svc = EasyMock.getCurrentArguments()[1];
assertTrue(svc instanceof ServiceFactory);
}
Dictionary<String,Object> props = (Dictionary<String, Object>) EasyMock.getCurrentArguments()[2];
for (String key : expectedProxyProps.keySet()) {
if (GuardProxyCatalog.SERVICE_GUARD_ROLES_PROPERTY.equals(key)) {
assertTrue("The roles property should have been overwritten",
!Arrays.asList("everyone").equals(props.get(key)));
} else {
assertEquals(expectedProxyProps.get(key), props.get(key));
}
}
ServiceRegistration reg = EasyMock.createMock(ServiceRegistration.class);
ServiceReference sr = mockServiceReference(props);
EasyMock.expect(reg.getReference()).andReturn(sr).anyTimes();
reg.unregister();
EasyMock.expectLastCall().once();
EasyMock.replay(reg);
serviceMap.put(sr, EasyMock.getCurrentArguments()[1]);
return reg;
}
}).once();
EasyMock.expect(providerBC.getService(EasyMock.isA(ServiceReference.class))).andAnswer(new IAnswer<Object>() {
@Override
public Object answer() throws Throwable {
return serviceMap.get(EasyMock.getCurrentArguments()[0]);
}
}).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);
// The mock bundle context for the client bundle
BundleContext clientBC = EasyMock.createMock(BundleContext.class);
EasyMock.expect(clientBC.getService(sr)).andReturn(testService).anyTimes();
EasyMock.replay(clientBC);
// The mock bundle that consumes the service
Bundle clientBundle = EasyMock.createNiceMock(Bundle.class);
EasyMock.expect(clientBundle.getBundleId()).andReturn(2999L).anyTimes();
EasyMock.expect(clientBundle.getBundleContext()).andReturn(clientBC).anyTimes();
EasyMock.expect(clientBundle.loadClass(EasyMock.isA(String.class))).andAnswer(new IAnswer() {
@Override
public Class answer() throws Throwable {
return objClsMap.get(EasyMock.getCurrentArguments()[0]);
}
}).anyTimes();