Search in sources :

Example 6 with ServiceReferenceImpl

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));
}
Also used : Bundle(org.osgi.framework.Bundle) ServiceReferenceImpl(org.apache.felix.framework.ServiceRegistrationImpl.ServiceReferenceImpl)

Example 7 with ServiceReferenceImpl

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());
    }
}
Also used : ServiceException(org.osgi.framework.ServiceException) Bundle(org.osgi.framework.Bundle) ServiceReferenceImpl(org.apache.felix.framework.ServiceRegistrationImpl.ServiceReferenceImpl)

Example 8 with ServiceReferenceImpl

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");
}
Also used : Answer(org.mockito.stubbing.Answer) Bundle(org.osgi.framework.Bundle) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ServiceException(org.osgi.framework.ServiceException) ServiceReferenceImpl(org.apache.felix.framework.ServiceRegistrationImpl.ServiceReferenceImpl)

Example 9 with ServiceReferenceImpl

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());
}
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 10 with ServiceReferenceImpl

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());
}
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

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