Search in sources :

Example 1 with ServiceInterface

use of org.apache.sling.models.testmodels.interfaces.ServiceInterface in project sling by apache.

the class OSGiInjectionTest method testArrayOSGiModelField.

@Test
public void testArrayOSGiModelField() throws Exception {
    ServiceReference ref1 = mock(ServiceReference.class);
    ServiceInterface service1 = mock(ServiceInterface.class);
    when(bundleContext.getService(ref1)).thenReturn(service1);
    ServiceReference ref2 = mock(ServiceReference.class);
    ServiceInterface service2 = mock(ServiceInterface.class);
    when(bundleContext.getService(ref2)).thenReturn(service2);
    when(bundleContext.getServiceReferences(ServiceInterface.class.getName(), null)).thenReturn(new ServiceReference[] { ref1, ref2 });
    Resource res = mock(Resource.class);
    ArrayOSGiModel model = factory.getAdapter(res, ArrayOSGiModel.class);
    assertNotNull(model);
    assertNotNull(model.getServices());
    // the order on those is non deterministic as the ServiceReference.compareTo() is always returning 0
    // the real order is tested in the IT
    assertThat(Arrays.asList(model.getServices()), Matchers.containsInAnyOrder(service1, service2));
    verifyNoMoreInteractions(res);
}
Also used : ArrayOSGiModel(org.apache.sling.models.testmodels.classes.ArrayOSGiModel) OptionalArrayOSGiModel(org.apache.sling.models.testmodels.classes.OptionalArrayOSGiModel) ServiceInterface(org.apache.sling.models.testmodels.interfaces.ServiceInterface) Resource(org.apache.sling.api.resource.Resource) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 2 with ServiceInterface

use of org.apache.sling.models.testmodels.interfaces.ServiceInterface in project sling by apache.

the class OSGiInjectionTest method testListOSGiModelField.

@Test
public void testListOSGiModelField() throws Exception {
    ServiceReference ref1 = mock(ServiceReference.class);
    ServiceInterface service1 = mock(ServiceInterface.class);
    when(bundleContext.getService(ref1)).thenReturn(service1);
    ServiceReference ref2 = mock(ServiceReference.class);
    ServiceInterface service2 = mock(ServiceInterface.class);
    when(bundleContext.getService(ref2)).thenReturn(service2);
    when(bundleContext.getServiceReferences(ServiceInterface.class.getName(), null)).thenReturn(new ServiceReference[] { ref1, ref2 });
    Resource res = mock(Resource.class);
    ListOSGiModel model = factory.getAdapter(res, ListOSGiModel.class);
    assertNotNull(model);
    assertNotNull(model.getServices());
    // the order on those is non deterministic as the ServiceReference.compareTo() is always returning 0
    // the real order is tested in the IT
    assertThat(model.getServices(), Matchers.containsInAnyOrder(service1, service2));
    verifyNoMoreInteractions(res);
}
Also used : ServiceInterface(org.apache.sling.models.testmodels.interfaces.ServiceInterface) Resource(org.apache.sling.api.resource.Resource) OptionalListOSGiModel(org.apache.sling.models.testmodels.classes.OptionalListOSGiModel) ListOSGiModel(org.apache.sling.models.testmodels.classes.ListOSGiModel) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 3 with ServiceInterface

use of org.apache.sling.models.testmodels.interfaces.ServiceInterface in project sling by apache.

the class OSGiInjectionTest method testSetOSGiModelField.

@Test
public void testSetOSGiModelField() throws Exception {
    ServiceReference ref1 = mock(ServiceReference.class);
    ServiceInterface service1 = mock(ServiceInterface.class);
    when(bundleContext.getService(ref1)).thenReturn(service1);
    ServiceReference ref2 = mock(ServiceReference.class);
    ServiceInterface service2 = mock(ServiceInterface.class);
    when(bundleContext.getService(ref2)).thenReturn(service2);
    when(bundleContext.getServiceReferences(ServiceInterface.class.getName(), null)).thenReturn(new ServiceReference[] { ref1, ref2 });
    Resource res = mock(Resource.class);
    SetOSGiModel model = factory.getAdapter(res, SetOSGiModel.class);
    assertNull(model);
    verify(bundleContext).registerService(eq(Runnable.class.getName()), eq(factory), any(Dictionary.class));
    verify(bundleContext).addBundleListener(any(BundleListener.class));
    verify(bundleContext).registerService(eq(Object.class.getName()), any(Object.class), any(Dictionary.class));
    verify(bundleContext).getBundles();
    verify(bundleContext).getBundle();
    verifyNoMoreInteractions(res, bundleContext);
}
Also used : Dictionary(java.util.Dictionary) SetOSGiModel(org.apache.sling.models.testmodels.classes.SetOSGiModel) ServiceInterface(org.apache.sling.models.testmodels.interfaces.ServiceInterface) Resource(org.apache.sling.api.resource.Resource) BundleListener(org.osgi.framework.BundleListener) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 4 with ServiceInterface

use of org.apache.sling.models.testmodels.interfaces.ServiceInterface in project sling by apache.

the class OSGiInjectionTest method testCollectionOSGiModelField.

@Test
public void testCollectionOSGiModelField() throws Exception {
    ServiceReference ref1 = mock(ServiceReference.class);
    ServiceInterface service1 = mock(ServiceInterface.class);
    when(bundleContext.getService(ref1)).thenReturn(service1);
    ServiceReference ref2 = mock(ServiceReference.class);
    ServiceInterface service2 = mock(ServiceInterface.class);
    when(bundleContext.getService(ref2)).thenReturn(service2);
    when(bundleContext.getServiceReferences(ServiceInterface.class.getName(), null)).thenReturn(new ServiceReference[] { ref1, ref2 });
    Resource res = mock(Resource.class);
    CollectionOSGiModel model = factory.getAdapter(res, CollectionOSGiModel.class);
    assertNotNull(model);
    assertNotNull(model.getServices());
    // the order on those is non deterministic as the ServiceReference.compareTo() is always returning 0
    // the real order is tested in the IT
    assertThat(model.getServices(), Matchers.containsInAnyOrder(service1, service2));
    verifyNoMoreInteractions(res);
}
Also used : ServiceInterface(org.apache.sling.models.testmodels.interfaces.ServiceInterface) Resource(org.apache.sling.api.resource.Resource) CollectionOSGiModel(org.apache.sling.models.testmodels.classes.CollectionOSGiModel) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 5 with ServiceInterface

use of org.apache.sling.models.testmodels.interfaces.ServiceInterface in project sling by apache.

the class OSGiInjectionTest method testSimpleOSGiModelConstructor.

@Test
public void testSimpleOSGiModelConstructor() throws Exception {
    ServiceReference ref = mock(ServiceReference.class);
    ServiceInterface service = mock(ServiceInterface.class);
    when(bundleContext.getServiceReferences(ServiceInterface.class.getName(), null)).thenReturn(new ServiceReference[] { ref });
    when(bundleContext.getService(ref)).thenReturn(service);
    Resource res = mock(Resource.class);
    org.apache.sling.models.testmodels.classes.constructorinjection.SimpleOSGiModel model = factory.getAdapter(res, org.apache.sling.models.testmodels.classes.constructorinjection.SimpleOSGiModel.class);
    assertNotNull(model);
    assertNotNull(model.getService());
    assertEquals(service, model.getService());
    verifyNoMoreInteractions(res);
}
Also used : ServiceInterface(org.apache.sling.models.testmodels.interfaces.ServiceInterface) Resource(org.apache.sling.api.resource.Resource) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Aggregations

Resource (org.apache.sling.api.resource.Resource)7 ServiceInterface (org.apache.sling.models.testmodels.interfaces.ServiceInterface)7 Test (org.junit.Test)7 ServiceReference (org.osgi.framework.ServiceReference)7 Dictionary (java.util.Dictionary)1 ArrayOSGiModel (org.apache.sling.models.testmodels.classes.ArrayOSGiModel)1 CollectionOSGiModel (org.apache.sling.models.testmodels.classes.CollectionOSGiModel)1 ListOSGiModel (org.apache.sling.models.testmodels.classes.ListOSGiModel)1 OptionalArrayOSGiModel (org.apache.sling.models.testmodels.classes.OptionalArrayOSGiModel)1 OptionalListOSGiModel (org.apache.sling.models.testmodels.classes.OptionalListOSGiModel)1 SetOSGiModel (org.apache.sling.models.testmodels.classes.SetOSGiModel)1 SimpleOSGiModel (org.apache.sling.models.testmodels.classes.SimpleOSGiModel)1 BundleListener (org.osgi.framework.BundleListener)1