use of org.osgi.framework.ServiceReference in project sling by apache.
the class OSGiInjectionTest method testCollectionOSGiModelField.
@Test
public void testCollectionOSGiModelField() throws Exception {
ServiceReference ref1 = mock(ServiceReference.class);
ServiceInterface service1 = mock(ServiceInterface.class);
when(bundleContext.getService(ref1)).thenReturn(service1);
ServiceReference ref2 = mock(ServiceReference.class);
ServiceInterface service2 = mock(ServiceInterface.class);
when(bundleContext.getService(ref2)).thenReturn(service2);
when(bundleContext.getServiceReferences(ServiceInterface.class.getName(), null)).thenReturn(new ServiceReference[] { ref1, ref2 });
Resource res = mock(Resource.class);
CollectionOSGiModel model = factory.getAdapter(res, CollectionOSGiModel.class);
assertNotNull(model);
assertNotNull(model.getServices());
// the order on those is non deterministic as the ServiceReference.compareTo() is always returning 0
// the real order is tested in the IT
assertThat(model.getServices(), Matchers.containsInAnyOrder(service1, service2));
verifyNoMoreInteractions(res);
}
use of org.osgi.framework.ServiceReference in project sling by apache.
the class OSGiInjectionTest method testSimpleOSGiModelConstructor.
@Test
public void testSimpleOSGiModelConstructor() throws Exception {
ServiceReference ref = mock(ServiceReference.class);
ServiceInterface service = mock(ServiceInterface.class);
when(bundleContext.getServiceReferences(ServiceInterface.class.getName(), null)).thenReturn(new ServiceReference[] { ref });
when(bundleContext.getService(ref)).thenReturn(service);
Resource res = mock(Resource.class);
org.apache.sling.models.testmodels.classes.constructorinjection.SimpleOSGiModel model = factory.getAdapter(res, org.apache.sling.models.testmodels.classes.constructorinjection.SimpleOSGiModel.class);
assertNotNull(model);
assertNotNull(model.getService());
assertEquals(service, model.getService());
verifyNoMoreInteractions(res);
}
use of org.osgi.framework.ServiceReference in project sling by apache.
the class ServiceUserMappedBundleFilterTest method testFind.
@Test
public void testFind() {
List collection = new ArrayList<ServiceReference>();
ServiceReference serviceReference1 = mock(ServiceReference.class);
ServiceReference serviceReference2 = mock(ServiceReference.class);
collection.add(serviceReference1);
collection.add(serviceReference2);
when(serviceReference1.getProperty(Mapping.SERVICENAME)).thenReturn(BUNDLE1);
when(serviceReference1.getProperty(Constants.OBJECTCLASS)).thenReturn(new String[] { ServiceUserMappedImpl.SERVICEUSERMAPPED });
when(serviceReference2.getProperty(Mapping.SERVICENAME)).thenReturn(BUNDLE2);
when(serviceReference2.getProperty(Constants.OBJECTCLASS)).thenReturn(new String[] { ServiceUserMappedImpl.SERVICEUSERMAPPED });
FindHook findHook = new ServiceUserMappedBundleFilter();
findHook.find(bundleContext1, null, null, false, collection);
TestCase.assertEquals(1, collection.size());
TestCase.assertTrue(collection.contains(serviceReference1));
}
use of org.osgi.framework.ServiceReference in project sling by apache.
the class ServiceUserMappedBundleFilter method find.
@Override
public void find(BundleContext bundleContext, String name, String filter, boolean allServices, Collection references) {
String bundleServiceName = ServiceUserMapperImpl.getServiceName(bundleContext.getBundle());
Iterator<ServiceReference> it = references.iterator();
while (it.hasNext()) {
ServiceReference serviceReference = it.next();
if (isServiceMappingReference(serviceReference)) {
Object serviceName = serviceReference.getProperty(Mapping.SERVICENAME);
if (serviceName != null && !serviceName.equals(bundleServiceName)) {
it.remove();
}
}
}
}
use of org.osgi.framework.ServiceReference in project sling by apache.
the class ServiceUserMappedBundleFilter method event.
@Override
public void event(ServiceEvent serviceEvent, Map map) {
ServiceReference serviceReference = serviceEvent.getServiceReference();
if (isServiceMappingReference(serviceReference)) {
Object serviceName = serviceReference.getProperty(Mapping.SERVICENAME);
if (serviceName != null && serviceName instanceof String) {
Iterator<Map.Entry<BundleContext, Collection<ListenerHook.ListenerInfo>>> it = map.entrySet().iterator();
while (it.hasNext()) {
BundleContext ctx = it.next().getKey();
String bundleServiceName = ServiceUserMapperImpl.getServiceName(ctx.getBundle());
if (!serviceName.equals(bundleServiceName)) {
it.remove();
}
}
}
}
}
Aggregations