コード例 #1
0
  @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();
    }
  }
コード例 #2
0
  @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();
    }
  }