Esempio n. 1
0
  @Test
  public void testAddServices() {
    // Register Two dependencies
    ServiceTracker stringTracker = serviceBinder.createTracker(String.class);
    Assert.assertNotNull(stringTracker);
    stringTracker.open();

    ServiceTracker intTracker = serviceBinder.createTracker(Integer.class);
    Assert.assertNotNull(intTracker);
    intTracker.open();

    handler.reset();
    stringTracker.addingService(serviceReference1);
    Assert.assertFalse(handler.wasOnActivateCalled());
    Assert.assertFalse(handler.wasOnServiceAddedCalled());
    Assert.assertFalse(handler.wasOnServiceRemovedCalled());
    Assert.assertFalse(handler.wasOnDeactivateCalled());

    intTracker.addingService(serviceReference2);
    Assert.assertTrue(handler.wasOnActivateCalled());
    Assert.assertFalse(handler.wasOnServiceAddedCalled());
    Assert.assertFalse(handler.wasOnServiceRemovedCalled());
    Assert.assertFalse(handler.wasOnDeactivateCalled());

    Map<Class<?>, Object> activated = handler.getOnActivateServices();
    Assert.assertEquals(2, activated.size());
    Assert.assertEquals(serviceObject1, activated.get(String.class));
    Assert.assertEquals(serviceObject2, activated.get(Integer.class));
  }
Esempio n. 2
0
  @Test
  public void testAddSameService() {
    // Register One Dependency
    ServiceTracker stringTracker = serviceBinder.createTracker(String.class);
    Assert.assertNotNull(stringTracker);
    stringTracker.open();

    stringTracker.addingService(serviceReference1);
    Assert.assertTrue(handler.wasOnActivateCalled());
    Assert.assertFalse(handler.wasOnServiceAddedCalled());
    Assert.assertFalse(handler.wasOnServiceRemovedCalled());
    Assert.assertFalse(handler.wasOnDeactivateCalled());

    handler.reset();
    stringTracker.addingService(serviceReference1);
    Assert.assertFalse(handler.wasOnActivateCalled());
    Assert.assertTrue(handler.wasOnServiceAddedCalled());
    Assert.assertFalse(handler.wasOnServiceRemovedCalled());
    Assert.assertFalse(handler.wasOnDeactivateCalled());

    Pair<Class<?>, Object> addedService = handler.getServiceAdded();
    Assert.assertNotNull(addedService);
    Assert.assertEquals(String.class, addedService.getFirst());
    Assert.assertEquals(serviceObject1, addedService.getSecond());
  }
Esempio n. 3
0
  @Test(expected = IllegalStateException.class)
  public void testRemoveSameService() {
    // Register One Dependency
    ServiceTracker stringTracker = serviceBinder.createTracker(String.class);
    Assert.assertNotNull(stringTracker);
    stringTracker.open();

    stringTracker.addingService(serviceReference2);
    Assert.assertTrue(handler.wasOnActivateCalled());
    Assert.assertFalse(handler.wasOnServiceAddedCalled());
    Assert.assertFalse(handler.wasOnServiceRemovedCalled());
    Assert.assertFalse(handler.wasOnDeactivateCalled());

    handler.reset();
    stringTracker.removedService(serviceReference2, serviceObject2);
    Assert.assertFalse(handler.wasOnActivateCalled());
    Assert.assertFalse(handler.wasOnServiceAddedCalled());
    Assert.assertTrue(handler.wasOnServiceRemovedCalled());
    Assert.assertTrue(handler.wasOnDeactivateCalled());

    // Remove Again
    handler.reset();
    stringTracker.removedService(serviceReference2, serviceObject2);
  }
Esempio n. 4
0
 @Before
 public void setupTest() {
   serviceMap = new ConcurrentHashMap<Class<?>, Collection<Object>>();
   serviceBinder = new MultiServiceBinder(serviceMap, context, handler);
   handler.reset();
 }
Esempio n. 5
0
  @Test
  public void testDeactivateFollowedByRemoveServiceBinder() {
    // Register 2 Dependencies
    ServiceTracker stringTracker = serviceBinder.createTracker(String.class);
    Assert.assertNotNull(stringTracker);
    stringTracker.open();

    ServiceTracker intTracker = serviceBinder.createTracker(Integer.class);
    Assert.assertNotNull(intTracker);
    intTracker.open();

    stringTracker.addingService(serviceReference1);
    intTracker.addingService(serviceReference2);

    handler.reset();
    stringTracker.removedService(serviceReference1, serviceObject1);
    Assert.assertFalse(handler.wasOnActivateCalled());
    Assert.assertFalse(handler.wasOnServiceAddedCalled());
    Assert.assertTrue(handler.wasOnServiceRemovedCalled());
    Assert.assertTrue(handler.wasOnDeactivateCalled());

    Pair<Class<?>, Object> removedService = handler.getServiceRemoved();
    Assert.assertNotNull(removedService);
    Assert.assertEquals(String.class, removedService.getFirst());
    Assert.assertEquals(serviceObject1, removedService.getSecond());

    handler.reset();
    intTracker.removedService(serviceReference2, serviceObject2);
    Assert.assertFalse(handler.wasOnActivateCalled());
    Assert.assertFalse(handler.wasOnServiceAddedCalled());
    Assert.assertTrue(handler.wasOnServiceRemovedCalled());
    Assert.assertTrue(handler.wasOnDeactivateCalled());

    removedService = handler.getServiceRemoved();
    Assert.assertNotNull(removedService);
    Assert.assertEquals(Integer.class, removedService.getFirst());
    Assert.assertEquals(serviceObject2, removedService.getSecond());
  }
Esempio n. 6
0
  @Test
  public void testReactivateServiceBinder() {
    // Register One Dependency
    ServiceTracker stringTracker = serviceBinder.createTracker(String.class);
    Assert.assertNotNull(stringTracker);
    stringTracker.open();

    stringTracker.addingService(serviceReference1);
    handler.reset();
    stringTracker.removedService(serviceReference1, serviceObject1);
    Assert.assertFalse(handler.wasOnActivateCalled());
    Assert.assertFalse(handler.wasOnServiceAddedCalled());
    Assert.assertTrue(handler.wasOnServiceRemovedCalled());
    Assert.assertTrue(handler.wasOnDeactivateCalled());

    handler.reset();

    // Add another dependency to check that onActivate is
    // not called when dependencies that might be managed by other binders are missing
    serviceMap.put(Boolean.class, new HashSet<Object>());

    stringTracker.addingService(serviceReference1);
    Assert.assertFalse(handler.wasOnActivateCalled());
    Assert.assertFalse(handler.wasOnServiceAddedCalled());
    Assert.assertFalse(handler.wasOnServiceRemovedCalled());
    Assert.assertFalse(handler.wasOnDeactivateCalled());

    serviceMap.remove(Boolean.class);
    stringTracker.addingService(serviceReference1);
    Assert.assertTrue(handler.wasOnActivateCalled());
    Assert.assertFalse(handler.wasOnServiceAddedCalled());
    Assert.assertFalse(handler.wasOnServiceRemovedCalled());
    Assert.assertFalse(handler.wasOnDeactivateCalled());

    Map<Class<?>, Object> activated = handler.getOnActivateServices();
    Assert.assertEquals(1, activated.size());
    Assert.assertEquals(serviceObject1, activated.get(String.class));
  }