Search in sources :

Example 1 with ServiceReferenceImpl

use of org.apache.felix.framework.ServiceRegistrationImpl.ServiceReferenceImpl in project felix by apache.

the class ServiceRegistryTest method testUngetService5.

@SuppressWarnings("unchecked")
public void testUngetService5() throws Exception {
    ServiceRegistry sr = new ServiceRegistry(null, null);
    Bundle b = Mockito.mock(Bundle.class);
    ServiceRegistrationImpl reg = Mockito.mock(ServiceRegistrationImpl.class);
    Mockito.doThrow(new RuntimeException("Test!")).when(reg).ungetService(Mockito.isA(Bundle.class), Mockito.any());
    ServiceReferenceImpl ref = Mockito.mock(ServiceReferenceImpl.class);
    Mockito.when(ref.getRegistration()).thenReturn(reg);
    final ConcurrentMap<Bundle, UsageCount[]> inUseMap = (ConcurrentMap<Bundle, UsageCount[]>) getPrivateField(sr, "m_inUseMap");
    String svc = "myService";
    UsageCount uc = new UsageCount(ref, false);
    ServiceHolder sh = new ServiceHolder();
    sh.m_service = svc;
    sh.m_latch.countDown();
    uc.m_svcHolderRef.set(sh);
    uc.m_count.set(1);
    inUseMap.put(b, new UsageCount[] { uc });
    try {
        assertTrue(sr.ungetService(b, ref, null));
        fail("Should have propagated the runtime exception");
    } catch (RuntimeException re) {
        assertEquals("Test!", re.getMessage());
    }
    assertNull(uc.m_svcHolderRef.get());
    Mockito.verify(reg, Mockito.times(1)).ungetService(b, svc);
}
Also used : Bundle(org.osgi.framework.Bundle) ConcurrentMap(java.util.concurrent.ConcurrentMap) ServiceHolder(org.apache.felix.framework.ServiceRegistry.ServiceHolder) UsageCount(org.apache.felix.framework.ServiceRegistry.UsageCount) ServiceReferenceImpl(org.apache.felix.framework.ServiceRegistrationImpl.ServiceReferenceImpl)

Example 2 with ServiceReferenceImpl

use of org.apache.felix.framework.ServiceRegistrationImpl.ServiceReferenceImpl in project felix by apache.

the class ServiceRegistryTest method testUngetServiceThreadMarking2.

public void testUngetServiceThreadMarking2() {
    ServiceRegistry sr = new ServiceRegistry(null, null);
    Bundle b = Mockito.mock(Bundle.class);
    ServiceRegistrationImpl reg = Mockito.mock(ServiceRegistrationImpl.class);
    Mockito.when(reg.currentThreadMarked()).thenReturn(true);
    ServiceReferenceImpl ref = Mockito.mock(ServiceReferenceImpl.class);
    Mockito.when(ref.getRegistration()).thenReturn(reg);
    try {
        sr.ungetService(b, ref, null);
        fail("The thread should be observed as marked and hence throw an exception");
    } catch (IllegalStateException ise) {
    // good
    }
}
Also used : Bundle(org.osgi.framework.Bundle) ServiceReferenceImpl(org.apache.felix.framework.ServiceRegistrationImpl.ServiceReferenceImpl)

Example 3 with ServiceReferenceImpl

use of org.apache.felix.framework.ServiceRegistrationImpl.ServiceReferenceImpl in project felix by apache.

the class ServiceRegistryTest method testUngetService2.

@SuppressWarnings("unchecked")
public void testUngetService2() throws Exception {
    ServiceRegistry sr = new ServiceRegistry(null, null);
    Bundle b = Mockito.mock(Bundle.class);
    ServiceRegistrationImpl reg = Mockito.mock(ServiceRegistrationImpl.class);
    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);
    ServiceHolder sh = new ServiceHolder();
    Object svc = new Object();
    sh.m_service = svc;
    uc.m_svcHolderRef.set(sh);
    uc.m_count.incrementAndGet();
    Mockito.verify(reg, Mockito.never()).ungetService(Mockito.isA(Bundle.class), Mockito.any());
    inUseMap.put(b, new UsageCount[] { uc });
    assertTrue(sr.ungetService(b, ref, null));
    assertNull(uc.m_svcHolderRef.get());
    Mockito.verify(reg, Mockito.times(1)).ungetService(Mockito.isA(Bundle.class), Mockito.eq(svc));
}
Also used : Bundle(org.osgi.framework.Bundle) ConcurrentMap(java.util.concurrent.ConcurrentMap) ServiceHolder(org.apache.felix.framework.ServiceRegistry.ServiceHolder) UsageCount(org.apache.felix.framework.ServiceRegistry.UsageCount) ServiceReferenceImpl(org.apache.felix.framework.ServiceRegistrationImpl.ServiceReferenceImpl)

Example 4 with ServiceReferenceImpl

use of org.apache.felix.framework.ServiceRegistrationImpl.ServiceReferenceImpl in project felix by apache.

the class ServiceRegistryTest method testUngetService3.

@SuppressWarnings("unchecked")
public void testUngetService3() 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(true);
    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));
    assertNotNull(uc.m_svcHolderRef.get());
    assertNotNull(inUseMap.get(b));
    Mockito.verify(reg, Mockito.never()).ungetService(Mockito.isA(Bundle.class), Mockito.any());
}
Also used : Bundle(org.osgi.framework.Bundle) ConcurrentMap(java.util.concurrent.ConcurrentMap) ServiceHolder(org.apache.felix.framework.ServiceRegistry.ServiceHolder) UsageCount(org.apache.felix.framework.ServiceRegistry.UsageCount) ServiceReferenceImpl(org.apache.felix.framework.ServiceRegistrationImpl.ServiceReferenceImpl)

Example 5 with ServiceReferenceImpl

use of org.apache.felix.framework.ServiceRegistrationImpl.ServiceReferenceImpl in project felix by apache.

the class ServiceRegistryTest method testGetServiceThreadMarking.

@SuppressWarnings("unchecked")
public void testGetServiceThreadMarking() throws Exception {
    ServiceRegistry sr = new ServiceRegistry(null, null);
    Bundle b = Mockito.mock(Bundle.class);
    ServiceRegistrationImpl reg = Mockito.mock(ServiceRegistrationImpl.class);
    ServiceReferenceImpl ref = Mockito.mock(ServiceReferenceImpl.class);
    Mockito.when(ref.getRegistration()).thenReturn(reg);
    sr.getService(b, ref, false);
    InOrder inOrder = Mockito.inOrder(reg);
    inOrder.verify(reg, Mockito.times(1)).currentThreadMarked();
    inOrder.verify(reg, Mockito.times(1)).markCurrentThread();
    inOrder.verify(reg, Mockito.times(1)).unmarkCurrentThread();
}
Also used : InOrder(org.mockito.InOrder) Bundle(org.osgi.framework.Bundle) ServiceReferenceImpl(org.apache.felix.framework.ServiceRegistrationImpl.ServiceReferenceImpl)

Aggregations

ServiceReferenceImpl (org.apache.felix.framework.ServiceRegistrationImpl.ServiceReferenceImpl)13 Bundle (org.osgi.framework.Bundle)13 UsageCount (org.apache.felix.framework.ServiceRegistry.UsageCount)7 ConcurrentMap (java.util.concurrent.ConcurrentMap)6 ServiceHolder (org.apache.felix.framework.ServiceRegistry.ServiceHolder)6 InOrder (org.mockito.InOrder)2 ServiceException (org.osgi.framework.ServiceException)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 Answer (org.mockito.stubbing.Answer)1