コード例 #1
0
  @Test
  public void testDirectoryAgentListenerServiceTracker() throws Exception {
    UserAgent userAgent = SLP.newUserAgent(newSettings());
    StandardDirectoryAgentServer directoryAgentServer =
        StandardDirectoryAgentServer.newInstance(newSettings());
    final AtomicInteger counter = new AtomicInteger();

    userAgent.start();

    DirectoryAgentListenerServiceTracker tracker =
        new DirectoryAgentListenerServiceTracker(bundleContext, userAgent);
    tracker.open();

    ServiceRegistration serviceRegistration =
        bundleContext.registerService(
            DirectoryAgentListener.class.getName(),
            new DirectoryAgentListener() {
              public void directoryAgentBorn(DirectoryAgentEvent event) {
                counter.incrementAndGet();
              }

              public void directoryAgentDied(DirectoryAgentEvent event) {
                counter.decrementAndGet();
              }
            },
            null);

    directoryAgentServer.start();

    Thread.sleep(1000);

    Assert.assertEquals(1, tracker.size());
    Assert.assertEquals(1, counter.get());

    directoryAgentServer.stop();

    Thread.sleep(1000);

    Assert.assertEquals(1, tracker.size());
    Assert.assertEquals(0, counter.get());

    serviceRegistration.unregister();

    Thread.sleep(500);

    Assert.assertEquals(0, tracker.size());
    Assert.assertEquals(0, counter.get());

    tracker.close();

    directoryAgentServer.stop();
    userAgent.stop();
  }
コード例 #2
0
  @Test
  public void testDirectoryAgentListenerWithFilterServiceTracker() throws Exception {
    UserAgent userAgent = SLP.newUserAgent(newSettings());
    StandardDirectoryAgentServer directoryAgentServer =
        StandardDirectoryAgentServer.newInstance(newSettings());
    final AtomicInteger counter = new AtomicInteger();

    userAgent.start();

    DirectoryAgentListenerServiceTracker tracker =
        new DirectoryAgentListenerServiceTracker(
            bundleContext, bundleContext.createFilter("(&(foo=bar)(car=cdr))"), userAgent);
    tracker.open();

    Dictionary<String, String> dictionary = new Hashtable<String, String>();
    dictionary.put("foo", "bar");
    dictionary.put("car", "cdr");
    ServiceRegistration serviceRegistration =
        bundleContext.registerService(
            DirectoryAgentListener.class.getName(),
            new DirectoryAgentListener() {
              public void directoryAgentBorn(DirectoryAgentEvent event) {
                counter.incrementAndGet();
              }

              public void directoryAgentDied(DirectoryAgentEvent event) {
                counter.decrementAndGet();
              }
            },
            dictionary);

    directoryAgentServer.start();

    Thread.sleep(1000);

    Assert.assertEquals(1, tracker.size());
    Assert.assertEquals(1, counter.get());

    directoryAgentServer.stop();

    Thread.sleep(1000);

    Assert.assertEquals(1, tracker.size());
    Assert.assertEquals(0, counter.get());

    serviceRegistration.unregister();

    Thread.sleep(500);

    Assert.assertEquals(0, tracker.size());
    Assert.assertEquals(0, counter.get());

    dictionary.put("foo", "bad-value");
    serviceRegistration =
        bundleContext.registerService(
            DirectoryAgentListener.class.getName(),
            new DirectoryAgentListener() {
              public void directoryAgentBorn(DirectoryAgentEvent event) {
                counter.incrementAndGet();
              }

              public void directoryAgentDied(DirectoryAgentEvent event) {
                counter.decrementAndGet();
              }
            },
            dictionary);

    directoryAgentServer.start();

    Thread.sleep(1000);

    Assert.assertEquals(0, tracker.size());
    Assert.assertEquals(0, counter.get());

    directoryAgentServer.stop();

    Thread.sleep(1000);

    Assert.assertEquals(0, tracker.size());
    Assert.assertEquals(0, counter.get());

    serviceRegistration.unregister();

    Thread.sleep(500);

    Assert.assertEquals(0, tracker.size());
    Assert.assertEquals(0, counter.get());

    tracker.close();

    directoryAgentServer.stop();
    userAgent.stop();
  }