From 4709b93d80d7f20b21b1a7d2eec95faf70ccd805 Mon Sep 17 00:00:00 2001 From: kske Date: Thu, 3 Sep 2020 09:52:00 +0200 Subject: [PATCH] Add simple unit test and fix event handler ordering --- src/main/java/dev/kske/eventbus/EventBus.java | 2 +- .../java/dev/kske/eventbus/EventHandler.java | 4 +- .../java/dev/kske/eventbus/EventBusTest.java | 39 +++++++++++++++++++ .../java/dev/kske/eventbus/SimpleEvent.java | 9 +++++ 4 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 src/test/java/dev/kske/eventbus/EventBusTest.java create mode 100644 src/test/java/dev/kske/eventbus/SimpleEvent.java diff --git a/src/main/java/dev/kske/eventbus/EventBus.java b/src/main/java/dev/kske/eventbus/EventBus.java index c44cb8d..ca14bc7 100644 --- a/src/main/java/dev/kske/eventbus/EventBus.java +++ b/src/main/java/dev/kske/eventbus/EventBus.java @@ -76,7 +76,7 @@ public final class EventBus { @SuppressWarnings("unchecked") var realParam = (Class) param; if (!bindings.containsKey(realParam)) - bindings.put(realParam, new HashSet<>()); + bindings.put(realParam, new TreeSet<>()); bindings.get(realParam).add(new EventHandler(listener, method, annotation)); } diff --git a/src/main/java/dev/kske/eventbus/EventHandler.java b/src/main/java/dev/kske/eventbus/EventHandler.java index 7619d72..fd5bebe 100644 --- a/src/main/java/dev/kske/eventbus/EventHandler.java +++ b/src/main/java/dev/kske/eventbus/EventHandler.java @@ -32,12 +32,14 @@ final class EventHandler implements Comparable { /** * Compares this to another event handler based on {@link Event#priority()}. In case of equal * priority a non-zero value based on hash codes is returned. + *

+ * This is used to retrieve event handlers in the correct order from a tree set. * * @since 0.0.1 */ @Override public int compareTo(EventHandler other) { - int priority = annotation.priority() - other.annotation.priority(); + int priority = other.annotation.priority() - annotation.priority(); if (priority == 0) priority = listener.hashCode() - other.listener.hashCode(); return priority == 0 ? hashCode() - other.hashCode() : priority; diff --git a/src/test/java/dev/kske/eventbus/EventBusTest.java b/src/test/java/dev/kske/eventbus/EventBusTest.java new file mode 100644 index 0000000..217f75d --- /dev/null +++ b/src/test/java/dev/kske/eventbus/EventBusTest.java @@ -0,0 +1,39 @@ +package dev.kske.eventbus; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +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 { + + public EventBus eventBus = new EventBus(); + int hits; + + @BeforeEach + public void registerListener() { + eventBus.registerListener(this); + } + + @Test + void testDispatch() { + eventBus.dispatch(new SimpleEvent()); + } + + @Event(priority = 50) + public void onSimpleEventSecond(SimpleEvent event) { + ++hits; + assertEquals(2, hits); + } + + @Event(priority = 150) + public void onSimpleEventFirst(SimpleEvent event) { + ++hits; + assertEquals(1, hits); + } +} diff --git a/src/test/java/dev/kske/eventbus/SimpleEvent.java b/src/test/java/dev/kske/eventbus/SimpleEvent.java new file mode 100644 index 0000000..a453acc --- /dev/null +++ b/src/test/java/dev/kske/eventbus/SimpleEvent.java @@ -0,0 +1,9 @@ +package dev.kske.eventbus; + +/** + * A simple event for testing purposes. + * + * @author Kai S. K. Engelbart + * @since 0.0.1 + */ +public class SimpleEvent implements IEvent {}