use of org.apache.felix.framework.ServiceRegistrationImpl.ServiceReferenceImpl in project felix by apache.
the class ServiceRegistryTest method testGetService.
@SuppressWarnings("unchecked")
public void testGetService() {
ServiceRegistry sr = new ServiceRegistry(null, null);
String svc = "foo";
Bundle b = Mockito.mock(Bundle.class);
ServiceRegistrationImpl reg = Mockito.mock(ServiceRegistrationImpl.class);
Mockito.when(reg.isValid()).thenReturn(true);
Mockito.when(reg.getService(b)).thenReturn(svc);
ServiceReferenceImpl ref = Mockito.mock(ServiceReferenceImpl.class);
Mockito.when(ref.getRegistration()).thenReturn(reg);
assertSame(svc, sr.getService(b, ref, false));
}
use of org.apache.felix.framework.ServiceRegistrationImpl.ServiceReferenceImpl in project felix by apache.
the class ServiceRegistryTest method testGetServiceThreadMarking2.
@SuppressWarnings("unchecked")
public void testGetServiceThreadMarking2() throws Exception {
ServiceRegistry sr = new ServiceRegistry(null, null);
String svc = "bar";
Bundle b = Mockito.mock(Bundle.class);
ServiceRegistrationImpl reg = (ServiceRegistrationImpl) sr.registerService(b, new String[] { String.class.getName() }, svc, null);
ServiceReferenceImpl ref = Mockito.mock(ServiceReferenceImpl.class);
Mockito.when(ref.getRegistration()).thenReturn(reg);
reg.markCurrentThread();
try {
sr.getService(b, ref, false);
fail("Should have thrown an exception to signal reentrant behaviour");
} catch (ServiceException se) {
assertEquals(ServiceException.FACTORY_ERROR, se.getType());
}
}
use of org.apache.felix.framework.ServiceRegistrationImpl.ServiceReferenceImpl in project felix by apache.
the class ServiceRegistryTest method testGetServiceThrowsException.
@SuppressWarnings("unchecked")
public void testGetServiceThrowsException() throws Exception {
final ServiceRegistry sr = new ServiceRegistry(null, null);
final Bundle b = Mockito.mock(Bundle.class);
ServiceRegistrationImpl reg = Mockito.mock(ServiceRegistrationImpl.class);
Mockito.when(reg.isValid()).thenReturn(true);
Mockito.when(reg.getService(b)).thenAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
Thread.sleep(500);
throw new Exception("boo!");
}
});
final ServiceReferenceImpl ref = Mockito.mock(ServiceReferenceImpl.class);
Mockito.when(ref.getRegistration()).thenReturn(reg);
final StringBuffer sb = new StringBuffer();
Thread t = new Thread() {
@Override
public void run() {
try {
assertEquals("Should not yet have given the service to the other thread", "", sb.toString());
sr.getService(b, ref, false);
} catch (Exception e) {
// We expect an exception here.
}
}
};
t.start();
// Wait until the other thread has called getService();
Thread.sleep(250);
// This thread has waited long enough for the other thread to call getService()
// however the actual getService() call blocks long enough for this one to then
// concurrently call getService() while the other thread is in getService() of the
// factory. This thread will then end up in m_latch.await().
// The factory implementation of the other thread then throws an exception. This test
// ultimately checks that this thread here is not stuck waiting forwever.
assertNull(sr.getService(b, ref, false));
sb.append("Obtained service");
}
use of org.apache.felix.framework.ServiceRegistrationImpl.ServiceReferenceImpl in project felix by apache.
the class ServiceRegistryTest method testUngetService4.
@SuppressWarnings("unchecked")
public void testUngetService4() throws Exception {
ServiceRegistry sr = new ServiceRegistry(null, null);
Bundle b = Mockito.mock(Bundle.class);
ServiceRegistrationImpl reg = Mockito.mock(ServiceRegistrationImpl.class);
Mockito.when(reg.isValid()).thenReturn(false);
ServiceReferenceImpl ref = Mockito.mock(ServiceReferenceImpl.class);
Mockito.when(ref.getRegistration()).thenReturn(reg);
final ConcurrentMap<Bundle, UsageCount[]> inUseMap = (ConcurrentMap<Bundle, UsageCount[]>) getPrivateField(sr, "m_inUseMap");
UsageCount uc = new UsageCount(ref, false);
uc.m_svcHolderRef.set(new ServiceHolder());
uc.m_count.set(2);
inUseMap.put(b, new UsageCount[] { uc });
assertTrue(sr.ungetService(b, ref, null));
assertNull(uc.m_svcHolderRef.get());
Mockito.verify(reg, Mockito.never()).ungetService(Mockito.isA(Bundle.class), Mockito.any());
}
use of org.apache.felix.framework.ServiceRegistrationImpl.ServiceReferenceImpl in project felix by apache.
the class ServiceRegistryTest method testGetServicePrototype.
@SuppressWarnings("unchecked")
public void testGetServicePrototype() throws Exception {
ServiceRegistry sr = new ServiceRegistry(null, null);
String svc = "xyz";
Bundle b = Mockito.mock(Bundle.class);
ServiceRegistrationImpl reg = Mockito.mock(ServiceRegistrationImpl.class);
Mockito.when(reg.isValid()).thenReturn(true);
Mockito.when(reg.getService(b)).thenReturn(svc);
ServiceReferenceImpl ref = Mockito.mock(ServiceReferenceImpl.class);
Mockito.when(ref.getRegistration()).thenReturn(reg);
assertSame(svc, sr.getService(b, ref, true));
final ConcurrentMap<Bundle, UsageCount[]> inUseMap = (ConcurrentMap<Bundle, UsageCount[]>) getPrivateField(sr, "m_inUseMap");
UsageCount[] uca = inUseMap.get(b);
assertEquals(1, uca.length);
assertEquals(1, uca[0].m_serviceObjectsCount.get());
sr.getService(b, ref, true);
assertEquals(2, uca[0].m_serviceObjectsCount.get());
}
Aggregations