event-bus/core/src/test/java/dev/kske/eventbus/core/DispatchTest.java

89 lines
2.6 KiB
Java
Raw Normal View History

package dev.kske.eventbus.core;
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 {
2022-01-18 13:44:33 +01:00
EventBus bus;
/**
* 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() {
bus = new EventBus();
bus.registerListener(this);
bus.registerListener(SimpleEvent.class, e -> {
2022-01-18 13:44:33 +01:00
e.increment();
assertEquals(3, e.getCounter());
});
}
/**
2021-02-20 21:40:48 +01:00
* Tests {@link EventBus#dispatch(Object)} with multiple handler priorities, a polymorphic
* handler and a static handler.
*
* @since 0.0.1
*/
@Test
2022-01-09 09:37:27 +01:00
void testDispatch() {
bus.dispatch(new SimpleEventSub());
bus.dispatch(new SimpleEvent());
}
/**
* Tests {@link EventBus#debugExecutionOrder(Class)} based on the currently registered handlers.
*
* @since 1.2.0
*/
@Test
2022-01-09 09:37:27 +01:00
void testDebugExecutionOrder() {
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"
+ "CallbackEventHandler[eventType=class dev.kske.eventbus.core.SimpleEvent, polymorphic=false, priority=100]\n"
+ "==========================================================================================",
2022-01-18 17:09:21 +01:00
bus.debugExecutionOrder(SimpleEvent.class));
}
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
@Priority(200)
2022-01-18 13:44:33 +01:00
void onSimpleEventFirst(SimpleEvent event) {
event.increment();
assertTrue(event.getCounter() == 1 || event.getCounter() == 2);
}
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());
}
}