@Test public void testUnkeyedServiceReferencesBalanceRefCount() { RegistryWrapper registryWrapper = getRegistryWrapper(); try (ServiceTrackerMap<TrackedOne, TrackedOne> serviceTrackerMap = ServiceTrackerCollections.singleValueMap( TrackedOne.class, null, new ServiceReferenceMapper<TrackedOne, TrackedOne>() { @Override public void map( ServiceReference<TrackedOne> serviceReference, Emitter<TrackedOne> emitter) {} })) { serviceTrackerMap.open(); ServiceRegistration<TrackedOne> serviceRegistration1 = registerService(new TrackedOne()); ServiceRegistration<TrackedOne> serviceRegistration2 = registerService(new TrackedOne()); Map<ServiceReference<?>, AtomicInteger> serviceReferenceCountsMap = registryWrapper.getServiceReferenceCountsMap(); Collection<AtomicInteger> serviceReferenceCounts = serviceReferenceCountsMap.values(); Assert.assertEquals(0, serviceReferenceCounts.size()); serviceRegistration1.unregister(); serviceRegistration2.unregister(); Assert.assertEquals(0, serviceReferenceCounts.size()); } finally { RegistryUtil.setRegistry(registryWrapper.getWrappedRegistry()); } }
@Test public void testContainsKey() { try (ServiceTrackerMap<String, TrackedOne> serviceTrackerMap = createServiceTrackerMap()) { ServiceRegistration<TrackedOne> serviceRegistration1 = registerService(new TrackedOne(), "aTarget1"); ServiceRegistration<TrackedOne> serviceRegistration2 = registerService(new TrackedOne(), "aTarget1"); ServiceRegistration<TrackedOne> serviceRegistration3 = registerService(new TrackedOne(), "aTarget2"); ServiceRegistration<TrackedOne> serviceRegistration4 = registerService(new TrackedOne(), "aTarget2"); Assert.assertTrue(serviceTrackerMap.containsKey("aTarget1")); Assert.assertTrue(serviceTrackerMap.containsKey("aTarget2")); Assert.assertFalse(serviceTrackerMap.containsKey("aTarget3")); Assert.assertFalse(serviceTrackerMap.containsKey("")); try { serviceTrackerMap.containsKey(null); Assert.fail(); } catch (NullPointerException npe) { } serviceRegistration1.unregister(); serviceRegistration2.unregister(); serviceRegistration3.unregister(); serviceRegistration4.unregister(); } }
@Test public void testKeySet() { try (ServiceTrackerMap<String, TrackedOne> serviceTrackerMap = createServiceTrackerMap()) { ServiceRegistration<TrackedOne> serviceRegistration1 = registerService(new TrackedOne(), "aTarget1"); ServiceRegistration<TrackedOne> serviceRegistration2 = registerService(new TrackedOne(), "aTarget1"); ServiceRegistration<TrackedOne> serviceRegistration3 = registerService(new TrackedOne(), "aTarget2"); ServiceRegistration<TrackedOne> serviceRegistration4 = registerService(new TrackedOne(), "aTarget2"); Set<String> targets = new HashSet<>(); targets.add("aTarget1"); targets.add("aTarget2"); Assert.assertEquals(targets, serviceTrackerMap.keySet()); serviceRegistration1.unregister(); serviceRegistration2.unregister(); serviceRegistration3.unregister(); serviceRegistration4.unregister(); } }
@Test public void testKeySetReturnsUnmodifiableSet() { try (ServiceTrackerMap<String, TrackedOne> serviceTrackerMap = createServiceTrackerMap()) { ServiceRegistration<TrackedOne> serviceRegistration1 = registerService(new TrackedOne(), "aTarget1"); ServiceRegistration<TrackedOne> serviceRegistration2 = registerService(new TrackedOne(), "aTarget1"); ServiceRegistration<TrackedOne> serviceRegistration3 = registerService(new TrackedOne(), "aTarget2"); ServiceRegistration<TrackedOne> serviceRegistration4 = registerService(new TrackedOne(), "aTarget2"); Set<String> keySet = serviceTrackerMap.keySet(); try { keySet.remove("aTarget1"); Assert.fail(); } catch (UnsupportedOperationException uoe) { } serviceRegistration1.unregister(); serviceRegistration2.unregister(); serviceRegistration3.unregister(); serviceRegistration4.unregister(); } }
@Test public void testGetServiceWithCustomResolver() { try (ServiceTrackerMap<String, TrackedOne> serviceTrackerMap = ServiceTrackerCollections.singleValueMap( TrackedOne.class, "(&(other=*)(target=*))", new ServiceReferenceMapper<String, TrackedOne>() { @Override public void map(ServiceReference<TrackedOne> serviceReference, Emitter<String> keys) { keys.emit( serviceReference.getProperty("other") + " - " + serviceReference.getProperty("target")); } })) { serviceTrackerMap.open(); Dictionary<String, String> properties = new Hashtable<>(); properties.put("other", "aProperty"); properties.put("target", "aTarget"); ServiceRegistration<TrackedOne> serviceRegistration = _bundleContext.registerService(TrackedOne.class, new TrackedOne(), properties); Assert.assertNotNull(serviceTrackerMap.getService("aProperty - aTarget")); serviceRegistration.unregister(); } }
@Test public void testGetServiceWithIncorrectKey() { try (ServiceTrackerMap<String, TrackedOne> serviceTrackerMap = createServiceTrackerMap()) { registerService(new TrackedOne(), "anotherTarget"); Assert.assertNull(serviceTrackerMap.getService("aTarget")); } }
@Test public void testGetServiceWithServiceCustomizer() { final Registry registry = RegistryUtil.getRegistry(); try (ServiceTrackerMap<String, TrackedTwo> serviceTrackerMap = ServiceTrackerCollections.singleValueMap( TrackedOne.class, "target", new ServiceTrackerCustomizer<TrackedOne, TrackedTwo>() { @Override public TrackedTwo addingService(ServiceReference<TrackedOne> serviceReference) { return new TrackedTwo(registry.getService(serviceReference)); } @Override public void modifiedService( ServiceReference<TrackedOne> serviceReference, TrackedTwo service) { removedService(serviceReference, service); } @Override public void removedService( ServiceReference<TrackedOne> serviceReference, TrackedTwo service) { registry.ungetService(serviceReference); } })) { serviceTrackerMap.open(); TrackedOne trackedOne1 = new TrackedOne(); ServiceRegistration<TrackedOne> serviceRegistration1 = registerService(trackedOne1, "trackedOne1"); TrackedOne trackedOne2 = new TrackedOne(); ServiceRegistration<TrackedOne> serviceRegistration2 = registerService(trackedOne2, "trackedOne2"); TrackedTwo trackedTwo1 = serviceTrackerMap.getService("trackedOne1"); Assert.assertEquals(trackedOne1, trackedTwo1.getTrackedOne()); TrackedTwo trackedTwo2 = serviceTrackerMap.getService("trackedOne2"); Assert.assertEquals(trackedOne2, trackedTwo2.getTrackedOne()); serviceRegistration1.unregister(); serviceRegistration2.unregister(); } }
@Test public void testGetServiceWithSimpleRegistration() { try (ServiceTrackerMap<String, TrackedOne> serviceTrackerMap = createServiceTrackerMap()) { ServiceRegistration<TrackedOne> serviceRegistration = registerService(new TrackedOne()); Assert.assertNotNull(serviceTrackerMap.getService("aTarget")); serviceRegistration.unregister(); } }
@Test public void testGetServiceIsNullAfterDeregistration() { try (ServiceTrackerMap<String, TrackedOne> serviceTrackerMap = createServiceTrackerMap()) { ServiceRegistration<TrackedOne> serviceRegistration1 = registerService(new TrackedOne()); ServiceRegistration<TrackedOne> serviceRegistration2 = registerService(new TrackedOne()); ServiceRegistration<TrackedOne> serviceRegistration3 = registerService(new TrackedOne()); Assert.assertNotNull(serviceTrackerMap.getService("aTarget")); serviceRegistration1.unregister(); serviceRegistration2.unregister(); serviceRegistration3.unregister(); Assert.assertNull(serviceTrackerMap.getService("aTarget")); } }
@Test public void testGetServiceGetsReplacedAfterRemovalInverseOrder() { try (ServiceTrackerMap<String, TrackedOne> serviceTrackerMap = createServiceTrackerMap()) { TrackedOne trackedOne2 = new TrackedOne(); ServiceRegistration<TrackedOne> serviceRegistration1 = registerService(trackedOne2, 1); TrackedOne trackedOne1 = new TrackedOne(); ServiceRegistration<TrackedOne> serviceRegistration2 = registerService(trackedOne1, 2); Assert.assertEquals(trackedOne1, serviceTrackerMap.getService("aTarget")); serviceRegistration2.unregister(); Assert.assertEquals(trackedOne2, serviceTrackerMap.getService("aTarget")); serviceRegistration1.unregister(); } }
static { _serviceTrackerMap.open(); }
@Test public void testGetServiceWithServiceCustomizerAndServiceReferenceMapper() { final Registry registry = RegistryUtil.getRegistry(); try (ServiceTrackerMap<String, TrackedTwo> serviceTrackerMap = ServiceTrackerCollections.singleValueMap( TrackedOne.class, ("(target=*)"), new ServiceReferenceMapper<String, TrackedOne>() { @Override public void map( ServiceReference<TrackedOne> serviceReference, Emitter<String> emitter) { TrackedOne service = registry.getService(serviceReference); String targetProperty = (String) serviceReference.getProperty("target"); emitter.emit(targetProperty + "-" + service.getKey()); registry.ungetService(serviceReference); } }, new ServiceTrackerCustomizer<TrackedOne, TrackedTwo>() { @Override public TrackedTwo addingService(ServiceReference<TrackedOne> serviceReference) { return new TrackedTwo(registry.getService(serviceReference)); } @Override public void modifiedService( ServiceReference<TrackedOne> serviceReference, TrackedTwo service) { removedService(serviceReference, service); } @Override public void removedService( ServiceReference<TrackedOne> serviceReference, TrackedTwo service) { registry.ungetService(serviceReference); } })) { serviceTrackerMap.open(); TrackedOne trackedOne1 = new TrackedOne("1"); ServiceRegistration<TrackedOne> serviceRegistration1 = registerService(trackedOne1, "trackedOne1"); TrackedOne trackedOne2 = new TrackedOne("2"); ServiceRegistration<TrackedOne> serviceRegistration2 = registerService(trackedOne2, "trackedOne2"); TrackedTwo trackedTwo1 = serviceTrackerMap.getService("trackedOne1-1"); Assert.assertEquals(trackedOne1, trackedTwo1.getTrackedOne()); TrackedTwo trackedTwo2 = serviceTrackerMap.getService("trackedOne2-2"); Assert.assertEquals(trackedOne2, trackedTwo2.getTrackedOne()); serviceRegistration1.unregister(); serviceRegistration2.unregister(); } }