Search in sources :

Example 11 with UsageCount

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

the class ServiceRegistryTest method testFlushUsageCountNull.

public void testFlushUsageCountNull() 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);
    Bundle b2 = Mockito.mock(Bundle.class);
    ServiceReference<?> ref = Mockito.mock(ServiceReference.class);
    UsageCount uc = new UsageCount(ref, false);
    ServiceReference<?> ref2 = Mockito.mock(ServiceReference.class);
    UsageCount uc2 = new UsageCount(ref2, true);
    ServiceReference<?> ref3 = Mockito.mock(ServiceReference.class);
    UsageCount uc3 = new UsageCount(ref3, true);
    inUseMap.put(b, new UsageCount[] { uc2, uc });
    inUseMap.put(b2, new UsageCount[] { uc3 });
    assertEquals("Precondition", 2, inUseMap.size());
    sr.flushUsageCount(b, ref, null);
    assertEquals(2, inUseMap.size());
    sr.flushUsageCount(b, ref2, null);
    assertEquals(1, inUseMap.size());
}
Also used : Bundle(org.osgi.framework.Bundle) ConcurrentMap(java.util.concurrent.ConcurrentMap) UsageCount(org.apache.felix.framework.ServiceRegistry.UsageCount)

Example 12 with UsageCount

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

the class ServiceRegistryTest method testFlushUsageCountAlienObject.

public void testFlushUsageCountAlienObject() 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 = new UsageCount(ref, false);
    inUseMap.put(b, new UsageCount[] { uc });
    assertEquals("Precondition", 1, inUseMap.size());
    assertEquals("Precondition", 1, inUseMap.values().iterator().next().length);
    UsageCount uc2 = new UsageCount(Mockito.mock(ServiceReference.class), false);
    sr.flushUsageCount(b, ref, uc2);
    assertEquals("Should be no changes", 1, inUseMap.size());
    assertEquals("Should be no changes", 1, inUseMap.values().iterator().next().length);
}
Also used : Bundle(org.osgi.framework.Bundle) ConcurrentMap(java.util.concurrent.ConcurrentMap) UsageCount(org.apache.felix.framework.ServiceRegistry.UsageCount) ServiceReference(org.osgi.framework.ServiceReference)

Example 13 with UsageCount

use of org.apache.felix.framework.ServiceRegistry.UsageCount 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());
}
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 14 with UsageCount

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

the class ServiceRegistryTest method testObtainUsageCount.

public void testObtainUsageCount() throws Exception {
    ServiceRegistry sr = new ServiceRegistry(null, null);
    @SuppressWarnings("unchecked") ConcurrentMap<Bundle, UsageCount[]> inUseMap = (ConcurrentMap<Bundle, UsageCount[]>) getPrivateField(sr, "m_inUseMap");
    assertEquals("Precondition", 0, inUseMap.size());
    Bundle b = Mockito.mock(Bundle.class);
    ServiceReference<?> ref = Mockito.mock(ServiceReference.class);
    UsageCount uc = sr.obtainUsageCount(b, ref, null, false);
    assertEquals(1, inUseMap.size());
    assertEquals(1, inUseMap.get(b).length);
    assertSame(uc, inUseMap.get(b)[0]);
    assertSame(ref, uc.m_ref);
    assertFalse(uc.m_prototype);
    UsageCount uc2 = sr.obtainUsageCount(b, ref, null, false);
    assertSame(uc, uc2);
    ServiceReference<?> ref2 = Mockito.mock(ServiceReference.class);
    UsageCount uc3 = sr.obtainUsageCount(b, ref2, null, false);
    assertNotSame(uc3, uc2);
    assertSame(ref2, uc3.m_ref);
}
Also used : Bundle(org.osgi.framework.Bundle) ConcurrentMap(java.util.concurrent.ConcurrentMap) UsageCount(org.apache.felix.framework.ServiceRegistry.UsageCount)

Example 15 with UsageCount

use of org.apache.felix.framework.ServiceRegistry.UsageCount 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());
}
Also used : Bundle(org.osgi.framework.Bundle) ConcurrentMap(java.util.concurrent.ConcurrentMap) 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