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)
|
2022-01-09 09:37:27 +01:00
|
|
|
class DispatchTest {
|
2020-11-26 08:14:11 +01:00
|
|
|
|
2022-01-18 13:44:33 +01:00
|
|
|
EventBus bus;
|
2020-11-26 08:14:11 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs an event bus and registers this test instance as an event listener.
|
|
|
|
*
|
|
|
|
* @since 0.0.1
|
|
|
|
*/
|
|
|
|
@BeforeEach
|
2022-01-09 09:37:27 +01:00
|
|
|
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 -> {
|
2022-01-18 13:44:33 +01:00
|
|
|
e.increment();
|
|
|
|
assertEquals(3, e.getCounter());
|
2021-11-02 19:33:18 +01:00
|
|
|
});
|
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
|
2022-01-09 09:37:27 +01:00
|
|
|
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
|
2022-01-09 09:37:27 +01:00
|
|
|
void testDebugExecutionOrder() {
|
2021-11-01 21:36:24 +01:00
|
|
|
assertEquals(
|
|
|
|
"Event handler execution order for class dev.kske.eventbus.core.SimpleEvent (3 handler(s)):\n"
|
|
|
|
+ "==========================================================================================\n"
|
2022-01-18 13:44:33 +01:00
|
|
|
+ "ReflectiveEventHandler[eventType=class dev.kske.eventbus.core.SimpleEvent, polymorphic=true, priority=200, method=void dev.kske.eventbus.core.DispatchTest.onSimpleEventFirst(dev.kske.eventbus.core.SimpleEvent), useParameter=true]\n"
|
|
|
|
+ "ReflectiveEventHandler[eventType=class dev.kske.eventbus.core.SimpleEvent, polymorphic=false, priority=150, method=static void dev.kske.eventbus.core.DispatchTest.onSimpleEventSecond(dev.kske.eventbus.core.SimpleEvent), useParameter=true]\n"
|
2021-11-02 19:33:18 +01:00
|
|
|
+ "CallbackEventHandler[eventType=class dev.kske.eventbus.core.SimpleEvent, polymorphic=false, priority=100]\n"
|
2021-11-01 21:36:24 +01:00
|
|
|
+ "==========================================================================================",
|
2022-01-18 17:09:21 +01:00
|
|
|
bus.debugExecutionOrder(SimpleEvent.class));
|
2021-11-01 21:36:24 +01:00
|
|
|
}
|
|
|
|
|
2022-01-18 13:44:33 +01:00
|
|
|
/**
|
|
|
|
* Tests whether the handlers bound to an event type are correct when retrieved from the binding
|
|
|
|
* cache. On the second call of {@link EventBus#debugExecutionOrder(Class)} the cache is used.
|
|
|
|
*
|
|
|
|
* @since 1.3.0
|
|
|
|
*/
|
|
|
|
@Test
|
|
|
|
void testBindingCache() {
|
2022-01-18 17:09:21 +01:00
|
|
|
assertEquals(bus.debugExecutionOrder(SimpleEventSub.class),
|
|
|
|
bus.debugExecutionOrder(SimpleEventSub.class));
|
2022-01-18 13:44:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Event
|
2021-02-15 12:06:33 +01:00
|
|
|
@Priority(200)
|
2022-01-18 13:44:33 +01:00
|
|
|
void onSimpleEventFirst(SimpleEvent event) {
|
|
|
|
event.increment();
|
|
|
|
assertTrue(event.getCounter() == 1 || event.getCounter() == 2);
|
2020-11-26 08:14:11 +01:00
|
|
|
}
|
|
|
|
|
2022-01-18 13:44:33 +01:00
|
|
|
@Event
|
2021-03-14 11:18:11 +01:00
|
|
|
@Polymorphic(false)
|
2022-01-18 13:44:33 +01:00
|
|
|
static void onSimpleEventSecond(SimpleEvent event) {
|
|
|
|
event.increment();
|
|
|
|
assertEquals(2, event.getCounter());
|
2020-11-26 08:14:11 +01:00
|
|
|
}
|
|
|
|
}
|