@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 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 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();
    }
  }
 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();
    }
  }