Search in sources :

Example 1 with UsageCount

use of org.apache.felix.framework.ServiceRegistry.UsageCount 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 UsageCount

use of org.apache.felix.framework.ServiceRegistry.UsageCount in project felix by apache.

the class ServiceRegistryTest method testObtainUsageCountPrototype.

public void testObtainUsageCountPrototype() throws Exception {
    ServiceRegistry sr = new ServiceRegistry(null, null);
    @SuppressWarnings("unchecked") ConcurrentMap<Bundle, UsageCount[]> inUseMap = (ConcurrentMap<Bundle, UsageCount[]>) getPrivateField(sr, "m_inUseMap");
    Bundle b = Mockito.mock(Bundle.class);
    ServiceReference<?> ref = Mockito.mock(ServiceReference.class);
    UsageCount uc = sr.obtainUsageCount(b, ref, null, true);
    assertEquals(1, inUseMap.size());
    assertEquals(1, inUseMap.values().iterator().next().length);
    ServiceReference<?> ref2 = Mockito.mock(ServiceReference.class);
    UsageCount uc2 = sr.obtainUsageCount(b, ref2, null, true);
    assertEquals(1, inUseMap.size());
    assertEquals(2, inUseMap.values().iterator().next().length);
    List<UsageCount> ucl = Arrays.asList(inUseMap.get(b));
    assertTrue(ucl.contains(uc));
    assertTrue(ucl.contains(uc2));
}
Also used : Bundle(org.osgi.framework.Bundle) ConcurrentMap(java.util.concurrent.ConcurrentMap) UsageCount(org.apache.felix.framework.ServiceRegistry.UsageCount)

Example 3 with UsageCount

use of org.apache.felix.framework.ServiceRegistry.UsageCount in project felix by apache.

the class ServiceRegistryTest method testObtainUsageCountRetry2.

@SuppressWarnings("unchecked")
public void testObtainUsageCountRetry2() throws Exception {
    ServiceRegistry sr = new ServiceRegistry(null, null);
    final Bundle b = Mockito.mock(Bundle.class);
    final ConcurrentMap<Bundle, UsageCount[]> orgInUseMap = (ConcurrentMap<Bundle, UsageCount[]>) getPrivateField(sr, "m_inUseMap");
    orgInUseMap.put(b, new UsageCount[] { new UsageCount(Mockito.mock(ServiceReference.class), false) });
    ConcurrentMap<Bundle, UsageCount[]> inUseMap = Mockito.mock(ConcurrentMap.class, AdditionalAnswers.delegatesTo(orgInUseMap));
    Mockito.doAnswer(new Answer<Boolean>() {

        @Override
        public Boolean answer(InvocationOnMock invocation) throws Throwable {
            orgInUseMap.remove(b);
            return false;
        }
    }).when(inUseMap).replace(Mockito.any(Bundle.class), Mockito.any(UsageCount[].class), Mockito.any(UsageCount[].class));
    setPrivateField(sr, "m_inUseMap", inUseMap);
    ServiceReference<?> ref = Mockito.mock(ServiceReference.class);
    assertEquals("Precondition", 1, inUseMap.size());
    assertEquals("Precondition", 1, inUseMap.values().iterator().next().length);
    assertNotSame("Precondition", ref, inUseMap.get(b)[0].m_ref);
    sr.obtainUsageCount(b, ref, null, false);
    assertEquals(1, inUseMap.size());
    assertEquals(1, inUseMap.values().iterator().next().length);
    assertSame("The old usage count should have been removed by the mock and this one should have been added", ref, inUseMap.get(b)[0].m_ref);
}
Also used : Bundle(org.osgi.framework.Bundle) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ConcurrentMap(java.util.concurrent.ConcurrentMap) UsageCount(org.apache.felix.framework.ServiceRegistry.UsageCount) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Example 4 with UsageCount

use of org.apache.felix.framework.ServiceRegistry.UsageCount 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 5 with UsageCount

use of org.apache.felix.framework.ServiceRegistry.UsageCount 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)

Aggregations

UsageCount (org.apache.felix.framework.ServiceRegistry.UsageCount)20 Bundle (org.osgi.framework.Bundle)20 ConcurrentMap (java.util.concurrent.ConcurrentMap)19 ServiceReferenceImpl (org.apache.felix.framework.ServiceRegistrationImpl.ServiceReferenceImpl)7 ServiceHolder (org.apache.felix.framework.ServiceRegistry.ServiceHolder)7 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 InvocationOnMock (org.mockito.invocation.InvocationOnMock)4 ArrayList (java.util.ArrayList)1 ServiceReference (org.osgi.framework.ServiceReference)1