Search in sources :

Example 1 with Tracked

use of org.apache.felix.dm.tracker.ServiceTracker.Tracked in project felix by apache.

the class TrackedTest method testSetInitialTrackAspects.

@Test
public void testSetInitialTrackAspects() {
    System.out.println("testSetInitialTrackAspects");
    TestCustomizer customizer = new TestCustomizer();
    ServiceTracker tracker = new TestTracker(customizer);
    tracker.open(false, true);
    Tracked tracked = tracker.getTracked();
    Object[] initialReferences = new Object[] { createServiceReference(1L), createServiceReference(2L, 1L, 10), createServiceReference(3L, 1L, 5) };
    tracked.setInitial(initialReferences);
    tracked.trackInitial();
    tracked.getExecutor().execute();
    assertArrayEquals(new Long[] { 1L, 2L, 3L }, customizer.getServiceReferenceIds());
}
Also used : Tracked(org.apache.felix.dm.tracker.ServiceTracker.Tracked) ServiceTracker(org.apache.felix.dm.tracker.ServiceTracker) Test(org.junit.Test)

Example 2 with Tracked

use of org.apache.felix.dm.tracker.ServiceTracker.Tracked in project felix by apache.

the class TrackedTest method testSetInitialHideAspects.

@Test
public void testSetInitialHideAspects() {
    System.out.println("testSetInitialHideAspects");
    TestCustomizer customizer = new TestCustomizer();
    ServiceTracker tracker = new TestTracker(customizer);
    tracker.open();
    Tracked tracked = tracker.getTracked();
    Object[] initialReferences = new Object[] { createServiceReference(1L), createServiceReference(2L, 1L, 10), createServiceReference(3L), createServiceReference(4L, 1L, 5), createServiceReference(5L, 3L, 5) };
    tracked.setInitial(initialReferences);
    tracked.trackInitial();
    tracked.getExecutor().execute();
    assertArrayEquals(new Long[] { 2L, 5L }, customizer.getServiceReferenceIds());
}
Also used : Tracked(org.apache.felix.dm.tracker.ServiceTracker.Tracked) ServiceTracker(org.apache.felix.dm.tracker.ServiceTracker) Test(org.junit.Test)

Example 3 with Tracked

use of org.apache.felix.dm.tracker.ServiceTracker.Tracked in project felix by apache.

the class TrackedTest method testUnHideAspect.

@Test
public void testUnHideAspect() {
    System.out.println("testUnhideAspect");
    TestCustomizer customizer = new TestCustomizer();
    ServiceTracker tracker = new TestTracker(customizer);
    tracker.open();
    Tracked tracked = tracker.getTracked();
    ServiceReference<?>[] initialReferences = new ServiceReference[] { createServiceReference(1L), createServiceReference(2L, 1L, 10), createServiceReference(3L), createServiceReference(4L, 1L, 5), createServiceReference(5L, 3L, 5) };
    tracked.setInitial(initialReferences);
    tracked.trackInitial();
    tracked.getExecutor().execute();
    assertArrayEquals(new Long[] { 2L, 5L }, customizer.getServiceReferenceIds());
    // create a service event that unregisters service with id 2, we would expect it to be swapped with 4.
    ServiceEvent event = new ServiceEvent(ServiceEvent.UNREGISTERING, initialReferences[1]);
    tracked.serviceChanged(event);
    assertArrayEquals(new Long[] { 5L, 4L }, customizer.getServiceReferenceIds());
    // create a service event that unregisters service with id 4, we would expect it to be swapped with 1.
    event = new ServiceEvent(ServiceEvent.UNREGISTERING, initialReferences[3]);
    tracked.serviceChanged(event);
    assertArrayEquals(new Long[] { 5L, 1L }, customizer.getServiceReferenceIds());
}
Also used : Tracked(org.apache.felix.dm.tracker.ServiceTracker.Tracked) ServiceTracker(org.apache.felix.dm.tracker.ServiceTracker) ServiceEvent(org.osgi.framework.ServiceEvent) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 4 with Tracked

use of org.apache.felix.dm.tracker.ServiceTracker.Tracked in project felix by apache.

the class TrackedTest method testHideAspect.

@Test
public void testHideAspect() {
    System.out.println("testHideAspect");
    TestCustomizer customizer = new TestCustomizer();
    ServiceTracker tracker = new TestTracker(customizer);
    tracker.open();
    Tracked tracked = tracker.getTracked();
    ServiceReference<?>[] initialReferences = new ServiceReference[] { createServiceReference(1L), createServiceReference(2L, 1L, 10), createServiceReference(3L), createServiceReference(4L, 1L, 5), createServiceReference(5L, 3L, 5) };
    tracked.setInitial(initialReferences);
    tracked.trackInitial();
    tracked.getExecutor().execute();
    assertArrayEquals(new Long[] { 2L, 5L }, customizer.getServiceReferenceIds());
    // create a service event that registers another but lower ranked aspect for service with id 1.
    ServiceReference<?> newReference = createServiceReference(6L, 1L, 8);
    ServiceEvent event = new ServiceEvent(ServiceEvent.REGISTERED, newReference);
    tracked.serviceChanged(event);
    assertArrayEquals(new Long[] { 2L, 5L }, customizer.getServiceReferenceIds());
    // create a service event that unregisters service with id 2, we would expect it to be swapped with 6.
    event = new ServiceEvent(ServiceEvent.UNREGISTERING, initialReferences[1]);
    tracked.serviceChanged(event);
    assertArrayEquals(new Long[] { 5L, 6L }, customizer.getServiceReferenceIds());
    // create a service event that unregisters service with id 6, we would expect it to be swapped with 4.
    event = new ServiceEvent(ServiceEvent.UNREGISTERING, newReference);
    tracked.serviceChanged(event);
    assertArrayEquals(new Long[] { 5L, 4L }, customizer.getServiceReferenceIds());
    // create a service event that registers a higher ranked aspect for service with id 1.
    ServiceReference<?> higherRankedReference = createServiceReference(7L, 1L, 15);
    ServiceEvent addHigherRankedEvent = new ServiceEvent(ServiceEvent.REGISTERED, higherRankedReference);
    tracked.serviceChanged(addHigherRankedEvent);
    assertArrayEquals(new Long[] { 5L, 7L }, customizer.getServiceReferenceIds());
}
Also used : Tracked(org.apache.felix.dm.tracker.ServiceTracker.Tracked) ServiceTracker(org.apache.felix.dm.tracker.ServiceTracker) ServiceEvent(org.osgi.framework.ServiceEvent) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Aggregations

ServiceTracker (org.apache.felix.dm.tracker.ServiceTracker)4 Tracked (org.apache.felix.dm.tracker.ServiceTracker.Tracked)4 Test (org.junit.Test)4 ServiceEvent (org.osgi.framework.ServiceEvent)2 ServiceReference (org.osgi.framework.ServiceReference)2