@Test public void testIsRegistered() throws Exception { boolean isRegistered; ChannelTarget channelTargetOne = Mockito.spy(new ChannelTarget()); isRegistered = bus.isRegistered(channelTargetOne, ChannelTarget.CHANNEL_ONE); assertTrue(!isRegistered); bus.register(channelTargetOne, ChannelTarget.CHANNEL_ONE, ChannelTarget.CHANNEL_TWO, ChannelTarget.CHANNEL_DEFAULT); isRegistered = bus.isRegistered(channelTargetOne, ChannelTarget.CHANNEL_ONE, ChannelTarget.CHANNEL_TWO); assertTrue(isRegistered); isRegistered = bus.isRegistered(channelTargetOne); assertTrue(isRegistered); bus.unregister(channelTargetOne, ChannelTarget.CHANNEL_ONE); isRegistered = bus.isRegistered(channelTargetOne, ChannelTarget.CHANNEL_ONE, ChannelTarget.CHANNEL_TWO); assertTrue(!isRegistered); bus.unregister(channelTargetOne, ChannelTarget.CHANNEL_DEFAULT); isRegistered = bus.isRegistered(channelTargetOne); assertTrue(!isRegistered); bus.register(channelTargetOne, ChannelTarget.CHANNEL_ONE); isRegistered = bus.isRegistered(channelTargetOne, ChannelTarget.CHANNEL_ONE, ChannelTarget.CHANNEL_TWO, ChannelTarget.CHANNEL_THREE); assertTrue(!isRegistered); }
@Test public void testChannelMultipleChannelMethod() throws Exception { MultipleChannelIDMethod multipleChannelIDMethod = Mockito.spy(new MultipleChannelIDMethod()); bus.register(multipleChannelIDMethod, ChannelTarget.CHANNEL_ONE, ChannelTarget.CHANNEL_TWO); bus.post("Message on One", ChannelTarget.CHANNEL_ONE); verify(multipleChannelIDMethod).onEventForTypeString("Message on One"); bus.post("Message on two", ChannelTarget.CHANNEL_TWO); verify(multipleChannelIDMethod).onEventForTypeString("Message on two"); bus.post("Message on default"); verify(multipleChannelIDMethod, never()).onEventForTypeString("Message on default"); bus.unregister(multipleChannelIDMethod, ChannelTarget.CHANNEL_ONE, ChannelTarget.CHANNEL_TWO); }
@Test public void testPostWithNoTargetOnParticularChannel() { ExceptionTarget exceptionTarget = Mockito.spy(new ExceptionTarget()); String eventString = "Method not found on channel ID Two"; bus.register(exceptionTarget, "one"); bus.post(eventString, "one"); bus.post(eventString, "two"); verify(logger).log("No method found for the event" + eventString.getClass() + " on channel ID" + "two"); }
@Before public void before() throws Exception { bus = NYBus.get(); bus.setSchedulerProvider(new TestSchedulerProvider()); bus.setLogger(logger); bus.enableLogging(); }
@Before public void before() throws Exception { bus = NYBus.get(); bus.setLogger(logger); bus.enableLogging(); }
@Test public void testUnregisterWithoutRegistrationOnChannelID() { ExceptionTarget exceptionTarget = Mockito.spy(new ExceptionTarget()); bus.register(exceptionTarget, "one"); bus.unregister(exceptionTarget); verify(logger).log(exceptionTarget.getClass() + " is either not subscribed(on some channel ID you wish to unregister " + "from) " + "or has " + "already been " + "unregistered"); }
@Override public void run() { for (int i = 0; i < 1000; i++) { bus.register(simpleTarget); latch.countDown(); } } }).start();
@Override public void run() { bus.post(event); latch.countDown(); } });
@Override public void run() { for (int i = 0; i < 1000; i++) { bus.unregister(simpleTarget); latch.countDown(); } } }).start();
@Before public void before() throws Exception { bus = NYBus.get(); bus.setSchedulerProvider(new TestSchedulerProvider()); bus.setLogger(logger); bus.enableLogging(); }
@Test public void testPostWithNoTarget() { ExceptionTarget exceptionTarget = Mockito.spy(new ExceptionTarget()); final NoTargetEvent eventObject = new NoTargetEvent(); bus.register(exceptionTarget, "one"); bus.post(eventObject); verify(logger).log("No target found for the event" + eventObject.getClass()); }
@Test public void testMultipleRegistrationOnSameChannelId() { ExceptionTarget exceptionTarget = Mockito.spy(new ExceptionTarget()); bus.register(exceptionTarget, "one"); bus.register(exceptionTarget, "one"); verify(logger).log(exceptionTarget.getClass() + " is already registered on same channel ids"); }
@Override public void run() { bus.post(event); latch.countDown(); } });
@Test public void testUnregisterWithoutRegistration() { ExceptionTarget exceptionTarget = Mockito.spy(new ExceptionTarget()); bus.unregister(exceptionTarget, "one"); verify(logger).log(exceptionTarget.getClass() + " is either not subscribed(on some channel ID you wish to unregister " + "from) " + "or has " + "already been " + "unregistered"); }
@Test public void testChannelTarget() throws Exception { ChannelTarget channelTargetOne = Mockito.spy(new ChannelTarget()); ChannelTarget channelTargetTwo = Mockito.spy(new ChannelTarget()); bus.register(channelTargetOne, ChannelTarget.CHANNEL_ONE); bus.register(channelTargetTwo, ChannelTarget.CHANNEL_TWO); bus.post("Message One", ChannelTarget.CHANNEL_ONE); verify(channelTargetOne).onEventForTypeOne("Message One"); verify(channelTargetOne, never()).onEventForTypeTwo("Message One"); bus.post("Message two", ChannelTarget.CHANNEL_TWO); verify(channelTargetTwo).onEventForTypeTwo("Message two"); verify(channelTargetTwo, never()).onEventForTypeOne("Message two"); bus.unregister(channelTargetOne, ChannelTarget.CHANNEL_ONE); bus.unregister(channelTargetTwo, ChannelTarget.CHANNEL_TWO); }
@Test public void testRegisterWithNoSubscribeMethods() { ExceptionTarget exceptionTarget = Mockito.spy(new ExceptionTarget()); bus.register(exceptionTarget, "two"); verify(logger).log("Subscriber " + exceptionTarget.getClass() + " and its super classes have no public methods with the @Subscribe annotation"); }
@Override public void run() { bus.post(event); latch.countDown(); } });
@Test public void testSubClassEvent() throws Exception { SubClassEventTarget subClassEventTarget = Mockito.spy(new SubClassEventTarget()); bus.register(subClassEventTarget); SubClassEvent event = new SubClassEvent(); bus.post(event); verify(subClassEventTarget).onEvent(event); verify(subClassEventTarget).onEventSubClass(event); bus.unregister(subClassEventTarget); }
@Test public void testRegisterWithSomeSubscribeMethods() { ExceptionTarget exceptionTarget = Mockito.spy(new ExceptionTarget()); bus.register(exceptionTarget, "one", "two"); verify(logger).log("Subscriber " + exceptionTarget.getClass() + " and its super classes have no public methods with the " + "@Subscribe annotation on ChannelID two"); }
@Test public void testInterfaceEvent() throws Exception { InterfaceEventTarget interfaceEventTarget = Mockito.spy(new InterfaceEventTarget()); bus.register(interfaceEventTarget); InterfaceEventImpl event = new InterfaceEventImpl(); bus.post(event); verify(interfaceEventTarget).onEventInterface(event); verify(interfaceEventTarget).onEventInterfaceImpl(event); bus.unregister(interfaceEventTarget); }