@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)); }
@Test public void testRemoveService() { // 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(serviceReference2); Assert.assertFalse(handler.wasOnActivateCalled()); Assert.assertTrue(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.assertFalse(handler.wasOnDeactivateCalled()); Pair<Class<?>, Object> removedService = handler.getServiceRemoved(); Assert.assertNotNull(removedService); Assert.assertEquals(String.class, removedService.getFirst()); Assert.assertEquals(serviceObject2, removedService.getSecond()); }
@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)); }
@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); }