2021-02-09 09:52:26 +01:00
|
|
|
package dev.kske.eventbus.core;
|
2020-11-26 08:14:11 +01:00
|
|
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
|
|
|
|
|
|
import org.junit.jupiter.api.*;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests the dispatching mechanism of the event bus.
|
|
|
|
*
|
|
|
|
* @author Kai S. K. Engelbart
|
|
|
|
* @since 0.0.1
|
|
|
|
*/
|
2021-02-15 14:43:34 +01:00
|
|
|
class DispatchTest {
|
2020-11-26 08:14:11 +01:00
|
|
|
|
|
|
|
EventBus bus;
|
|
|
|
static int hits;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs an event bus and registers this test instance as an event listener.
|
|
|
|
*
|
|
|
|
* @since 0.0.1
|
|
|
|
*/
|
|
|
|
@BeforeEach
|
|
|
|
void registerListener() {
|
|
|
|
bus = new EventBus();
|
|
|
|
bus.registerListener(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-02-20 21:40:48 +01:00
|
|
|
* Tests {@link EventBus#dispatch(Object)} with multiple handler priorities, a polymorphic
|
|
|
|
* handler and a static handler.
|
2020-11-26 08:14:11 +01:00
|
|
|
*
|
|
|
|
* @since 0.0.1
|
|
|
|
*/
|
|
|
|
@Test
|
|
|
|
void testDispatch() {
|
|
|
|
bus.dispatch(new SimpleEventSub());
|
|
|
|
bus.dispatch(new SimpleEvent());
|
|
|
|
}
|
|
|
|
|
2021-02-15 13:42:20 +01:00
|
|
|
@Event(SimpleEvent.class)
|
2021-02-15 12:06:33 +01:00
|
|
|
@Priority(200)
|
2021-02-15 10:55:30 +01:00
|
|
|
@Polymorphic
|
2020-11-26 08:14:11 +01:00
|
|
|
void onSimpleEventFirst() {
|
|
|
|
++hits;
|
|
|
|
assertTrue(hits == 1 || hits == 2);
|
|
|
|
}
|
|
|
|
|
2021-02-15 13:42:20 +01:00
|
|
|
@Event(SimpleEvent.class)
|
2021-02-15 12:06:33 +01:00
|
|
|
@Priority(150)
|
2020-11-26 08:14:11 +01:00
|
|
|
static void onSimpleEventSecond() {
|
|
|
|
++hits;
|
|
|
|
assertEquals(3, hits);
|
|
|
|
}
|
|
|
|
|
2021-02-15 12:06:33 +01:00
|
|
|
@Event
|
2020-11-26 08:14:11 +01:00
|
|
|
void onSimpleEventThird(SimpleEvent event) {
|
|
|
|
++hits;
|
|
|
|
assertEquals(4, hits);
|
|
|
|
}
|
|
|
|
}
|