diff --git a/src/test/java/dev/kske/eventbus/CancelTest.java b/src/test/java/dev/kske/eventbus/CancelTest.java new file mode 100644 index 0000000..8584f48 --- /dev/null +++ b/src/test/java/dev/kske/eventbus/CancelTest.java @@ -0,0 +1,52 @@ +package dev.kske.eventbus; + +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 + */ +class CancelTest implements EventListener { + + EventBus bus; + int hits; + + /** + * Constructs an event bus and registers this test instance as an event listener. + * + * @since 0.1.0 + */ + @BeforeEach + void registerListener() { + 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 + void testCancellation() { + bus.dispatch(new SimpleEvent()); + assertEquals(1, hits); + } + + @Event(eventType = SimpleEvent.class, priority = 100) + void onSimpleFirst() { + ++hits; + bus.cancel(); + } + + @Event(eventType = SimpleEvent.class, priority = 50) + void onSimpleSecond() { + ++hits; + } +} diff --git a/src/test/java/dev/kske/eventbus/DispatchTest.java b/src/test/java/dev/kske/eventbus/DispatchTest.java new file mode 100644 index 0000000..83c99e7 --- /dev/null +++ b/src/test/java/dev/kske/eventbus/DispatchTest.java @@ -0,0 +1,58 @@ +package dev.kske.eventbus; + +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 + */ +class DispatchTest implements EventListener { + + EventBus bus; + static int hits; + + /** + * Constructs an event bus and registers this test instance as an event listener. + * + * @since 0.0.1 + */ + @BeforeEach + void registerListener() { + bus = new EventBus(); + bus.registerListener(this); + } + + /** + * Tests {@link EventBus#dispatch(IEvent)} with multiple handler priorities, a subtype handler + * and a static handler. + * + * @since 0.0.1 + */ + @Test + void testDispatch() { + bus.dispatch(new SimpleEventSub()); + bus.dispatch(new SimpleEvent()); + } + + @Event(eventType = SimpleEvent.class, includeSubtypes = true, priority = 200) + void onSimpleEventFirst() { + ++hits; + assertTrue(hits == 1 || hits == 2); + } + + @Event(eventType = SimpleEvent.class, priority = 150) + static void onSimpleEventSecond() { + ++hits; + assertEquals(3, hits); + } + + @Event(priority = 100) + void onSimpleEventThird(SimpleEvent event) { + ++hits; + assertEquals(4, hits); + } +} diff --git a/src/test/java/dev/kske/eventbus/EventBusTest.java b/src/test/java/dev/kske/eventbus/EventBusTest.java deleted file mode 100644 index bbf8317..0000000 --- a/src/test/java/dev/kske/eventbus/EventBusTest.java +++ /dev/null @@ -1,63 +0,0 @@ -package dev.kske.eventbus; - -import static org.junit.jupiter.api.Assertions.*; - -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 { - - int hits, cancelledHits; - - @BeforeEach - public void registerListener() { - EventBus.getInstance().registerListener(this); - } - - @Test - void testDispatch() { - EventBus.getInstance().dispatch(new SimpleEventSub()); - EventBus.getInstance().dispatch(new SimpleEvent()); - } - - @Test - void testCancellation() { - EventBus.getInstance().dispatch(new SimpleCancelEvent()); - assertTrue(cancelledHits == 1); - } - - @Event(eventType = SimpleEvent.class, includeSubtypes = true, priority = 200) - private void onSimpleEventFirst() { - ++hits; - assertTrue(hits == 1 || hits == 2); - } - - @Event(eventType = SimpleEvent.class, priority = 150) - private void onSimpleEventSecond() { - ++hits; - assertEquals(3, hits); - } - - @Event(priority = 50) - private void onSimpleEventThird(SimpleEvent event) { - ++hits; - assertEquals(4, hits); - } - - @Event(eventType = SimpleCancelEvent.class, priority = 500) - private void onSimpleCancelFirst() { - ++cancelledHits; - assertTrue(cancelledHits == 1); - EventBus.getInstance().cancel(); - } - - @Event(eventType = SimpleCancelEvent.class, priority = 200) - private void onSimpleCancelSecond() { - fail(); - } -} diff --git a/src/test/java/dev/kske/eventbus/SimpleCancelEvent.java b/src/test/java/dev/kske/eventbus/SimpleCancelEvent.java deleted file mode 100644 index 63eed2b..0000000 --- a/src/test/java/dev/kske/eventbus/SimpleCancelEvent.java +++ /dev/null @@ -1,9 +0,0 @@ -package dev.kske.eventbus; - -/** - * A simple event for testing purposes that will get cancelled during propagation. - * - * @author Leon Hofmeister - * @since 0.1 - */ -public class SimpleCancelEvent implements IEvent {}