package com.areoit.guava.eventbus; import org.junit.Test; import static org.junit.Assert.assertEquals; import com.google.common.eventbus.EventBus; import com.google.common.eventbus.Subscribe; public class EventBusIntegerEventsTests { private static final String EVENT_BUS_ID = "numbers"; /** * Case: * + one listener * + listener subcribe for Integer event * + listener subcribe for Number event * + REMEMBER: Integer IS-A Number */ @Test public void oneIntegerEventTwoSubscribingMethod() { IntegerListener listener = new IntegerListener(); Integer integerEvent = Integer.MAX_VALUE; EventBus eventBus = new EventBus(EVENT_BUS_ID); eventBus.register(listener); eventBus.post(integerEvent); //check event counters int integerCount = listener.getIntegerEventCount(); int numberCount = listener.getNumberEventCount(); assertEquals(1, integerCount); assertEquals(1, numberCount); } class IntegerListener { private int integerEventCount = 0; private int numberEventCount = 0; @Subscribe public void onIntegerEvent(Integer event) { ++integerEventCount; } @Subscribe public void onNumberEvent(Number event) { ++numberEventCount; } public int getIntegerEventCount() { return integerEventCount; } public int getNumberEventCount() { return numberEventCount; } } }