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-03-14 11:18:11 +01:00
|
|
|
@Polymorphic
|
|
|
|
@Priority(150)
|
2021-11-24 12:52:59 +01:00
|
|
|
public 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
|
2021-11-24 12:52:59 +01:00
|
|
|
public void registerListener() {
|
2020-11-26 08:14:11 +01:00
|
|
|
bus = new EventBus();
|
|
|
|
bus.registerListener(this);
|
2021-11-02 19:33:18 +01:00
|
|
|
bus.registerListener(SimpleEvent.class, e -> {
|
|
|
|
++hits;
|
|
|
|
assertEquals(4, hits);
|
|
|
|
});
|
2020-11-26 08:14:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
2021-11-24 12:52:59 +01:00
|
|
|
public void testDispatch() {
|
2020-11-26 08:14:11 +01:00
|
|
|
bus.dispatch(new SimpleEventSub());
|
|
|
|
bus.dispatch(new SimpleEvent());
|
|
|
|
}
|
|
|
|
|
2021-11-01 21:36:24 +01:00
|
|
|
/**
|
2021-11-25 14:34:13 +01:00
|
|
|
* Tests {@link EventBus#debugExecutionOrder(Class)} based on the currently registered handlers.
|
2021-11-01 21:36:24 +01:00
|
|
|
*
|
|
|
|
* @since 1.2.0
|
|
|
|
*/
|
|
|
|
@Test
|
2021-11-25 14:36:07 +01:00
|
|
|
public void testDebugExecutionOrder() {
|
2021-11-25 14:34:13 +01:00
|
|
|
String executionOrder = bus.debugExecutionOrder(SimpleEvent.class);
|
2021-11-01 21:36:24 +01:00
|
|
|
System.out.println(executionOrder);
|
|
|
|
assertEquals(
|
|
|
|
"Event handler execution order for class dev.kske.eventbus.core.SimpleEvent (3 handler(s)):\n"
|
|
|
|
+ "==========================================================================================\n"
|
2021-11-02 19:33:18 +01:00
|
|
|
+ "ReflectiveEventHandler[eventType=class dev.kske.eventbus.core.SimpleEvent, polymorphic=true, priority=200, method=void dev.kske.eventbus.core.DispatchTest.onSimpleEventFirst(), useParameter=false]\n"
|
|
|
|
+ "ReflectiveEventHandler[eventType=class dev.kske.eventbus.core.SimpleEvent, polymorphic=false, priority=150, method=static void dev.kske.eventbus.core.DispatchTest.onSimpleEventSecond(), useParameter=false]\n"
|
|
|
|
+ "CallbackEventHandler[eventType=class dev.kske.eventbus.core.SimpleEvent, polymorphic=false, priority=100]\n"
|
2021-11-01 21:36:24 +01:00
|
|
|
+ "==========================================================================================",
|
|
|
|
executionOrder);
|
|
|
|
}
|
|
|
|
|
2021-02-15 13:42:20 +01:00
|
|
|
@Event(SimpleEvent.class)
|
2021-02-15 12:06:33 +01:00
|
|
|
@Priority(200)
|
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-03-14 11:18:11 +01:00
|
|
|
@Polymorphic(false)
|
2020-11-26 08:14:11 +01:00
|
|
|
static void onSimpleEventSecond() {
|
|
|
|
++hits;
|
|
|
|
assertEquals(3, hits);
|
|
|
|
}
|
|
|
|
}
|