コード例 #1
0
  @Test
  public void unregisterProducerInHandler() throws Exception {
    final Object producer =
        new Object() {
          private int calls = 0;

          @Produce
          public String produceString() {
            calls++;
            if (calls > 1) {
              fail("Should only have been called once, then unregistered and never called again.");
            }
            return "Please enjoy this hand-crafted String.";
          }
        };
    bus.register(producer);
    bus.register(
        new Object() {
          @Subscribe
          public void firstUnsubscribeTheProducer(String produced) {
            bus.unregister(producer);
          }

          @Subscribe
          public void shouldNeverBeCalled(String uhoh) {
            fail("Shouldn't receive events from an unregistered producer.");
          }
        });
  }
コード例 #2
0
  @Test
  public void unregisterInHandlerWhenEventProduced() throws Exception {
    UnregisteringStringCatcher catcher = new UnregisteringStringCatcher(bus);

    bus.register(new StringProducer());
    bus.register(catcher);
    assertEquals(Arrays.asList(StringProducer.VALUE), catcher.getEvents());

    bus.post(EVENT);
    bus.post(EVENT);
    assertEquals(
        "Shouldn't catch any more events when unregistered.",
        Arrays.asList(StringProducer.VALUE),
        catcher.getEvents());
  }
コード例 #3
0
  @Test
  public void unregisterInHandler() {
    UnregisteringStringCatcher catcher = new UnregisteringStringCatcher(bus);
    bus.register(catcher);
    bus.post(EVENT);

    assertEquals(
        "One correct event should be delivered.", Arrays.asList(EVENT), catcher.getEvents());

    bus.post(EVENT);
    bus.post(EVENT);
    assertEquals(
        "Shouldn't catch any more events when unregistered.",
        Arrays.asList(EVENT),
        catcher.getEvents());
  }