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.assertEquals;
|
|
|
|
|
|
|
|
import org.junit.jupiter.api.*;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests the event cancellation mechanism of the event bus.
|
|
|
|
*
|
|
|
|
* @author Kai S. K. Engelbart
|
|
|
|
* @author Leon Hofmeister
|
|
|
|
* @since 0.1.0
|
|
|
|
*/
|
2021-11-24 12:52:59 +01:00
|
|
|
public class CancelTest {
|
2020-11-26 08:14:11 +01:00
|
|
|
|
|
|
|
EventBus bus;
|
|
|
|
int hits;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs an event bus and registers this test instance as an event listener.
|
|
|
|
*
|
|
|
|
* @since 0.1.0
|
|
|
|
*/
|
|
|
|
@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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests {@link EventBus#cancel()} with two event handlers, of which the first cancels the
|
|
|
|
* event.
|
|
|
|
*
|
|
|
|
* @since 0.1.0
|
|
|
|
*/
|
|
|
|
@Test
|
2021-11-24 12:52:59 +01:00
|
|
|
public void testCancellation() {
|
2020-11-26 08:14:11 +01:00
|
|
|
bus.dispatch(new SimpleEvent());
|
|
|
|
assertEquals(1, hits);
|
|
|
|
}
|
|
|
|
|
2021-02-15 13:42:20 +01:00
|
|
|
@Event(SimpleEvent.class)
|
2021-02-15 12:06:33 +01:00
|
|
|
@Priority(100)
|
2020-11-26 08:14:11 +01:00
|
|
|
void onSimpleFirst() {
|
|
|
|
++hits;
|
|
|
|
bus.cancel();
|
|
|
|
}
|
|
|
|
|
2021-02-15 13:42:20 +01:00
|
|
|
@Event(SimpleEvent.class)
|
2021-02-15 12:06:33 +01:00
|
|
|
@Priority(50)
|
2020-11-26 08:14:11 +01:00
|
|
|
void onSimpleSecond() {
|
|
|
|
++hits;
|
|
|
|
}
|
|
|
|
}
|