2020-09-03 09:52:00 +02:00
|
|
|
package dev.kske.eventbus;
|
|
|
|
|
2020-09-20 12:20:29 +02:00
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
2020-09-03 09:52:00 +02:00
|
|
|
|
|
|
|
import org.junit.jupiter.api.*;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests the of the event bus library.
|
|
|
|
*
|
|
|
|
* @author Kai S. K. Engelbart
|
|
|
|
* @since 0.0.1
|
|
|
|
*/
|
|
|
|
class EventBusTest implements EventListener {
|
|
|
|
|
2020-11-23 23:42:17 +01:00
|
|
|
int hits;
|
|
|
|
static int canceledHits;
|
2020-09-03 09:52:00 +02:00
|
|
|
|
|
|
|
@BeforeEach
|
|
|
|
public void registerListener() {
|
2020-09-08 09:38:46 +02:00
|
|
|
EventBus.getInstance().registerListener(this);
|
2020-09-03 09:52:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testDispatch() {
|
2020-09-20 12:20:29 +02:00
|
|
|
EventBus.getInstance().dispatch(new SimpleEventSub());
|
2020-09-08 09:38:46 +02:00
|
|
|
EventBus.getInstance().dispatch(new SimpleEvent());
|
2020-09-03 09:52:00 +02:00
|
|
|
}
|
|
|
|
|
2020-11-23 23:42:17 +01:00
|
|
|
@Test
|
|
|
|
void testCancellation() {
|
|
|
|
var test2 = new EventBusTest();
|
|
|
|
test2.registerListener();
|
|
|
|
EventBus.getInstance().dispatch(new SimpleCancelEvent());
|
|
|
|
assertTrue(canceledHits == 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Event(eventType = SimpleEvent.class, includeSubtypes = true, priority = 200)
|
2020-09-20 12:20:29 +02:00
|
|
|
private void onSimpleEventFirst() {
|
2020-09-03 09:52:00 +02:00
|
|
|
++hits;
|
2020-09-20 12:20:29 +02:00
|
|
|
assertTrue(hits == 1 || hits == 2);
|
2020-09-03 09:52:00 +02:00
|
|
|
}
|
|
|
|
|
2020-09-08 19:47:21 +02:00
|
|
|
@Event(eventType = SimpleEvent.class, priority = 150)
|
2020-09-20 12:20:29 +02:00
|
|
|
private void onSimpleEventSecond() {
|
|
|
|
++hits;
|
|
|
|
assertEquals(3, hits);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Event(priority = 50)
|
|
|
|
private void onSimpleEventThird(SimpleEvent event) {
|
2020-09-03 09:52:00 +02:00
|
|
|
++hits;
|
2020-09-20 12:20:29 +02:00
|
|
|
assertEquals(4, hits);
|
2020-09-03 09:52:00 +02:00
|
|
|
}
|
2020-11-23 23:42:17 +01:00
|
|
|
|
|
|
|
@Event(eventType = SimpleCancelEvent.class, priority = 500)
|
|
|
|
private void onSimpleCancelFirst() {
|
|
|
|
++canceledHits;
|
|
|
|
assertTrue(canceledHits == 1);
|
|
|
|
EventBus.getInstance().cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Event(eventType = SimpleCancelEvent.class, priority = 200)
|
|
|
|
private void onSimpleCancelSecond() {
|
|
|
|
fail();
|
|
|
|
}
|
2020-09-03 09:52:00 +02:00
|
|
|
}
|