Shorten module names
This commit is contained in:
54
core/src/test/java/dev/kske/eventbus/core/CancelTest.java
Normal file
54
core/src/test/java/dev/kske/eventbus/core/CancelTest.java
Normal file
@ -0,0 +1,54 @@
|
||||
package dev.kske.eventbus.core;
|
||||
|
||||
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
|
||||
*/
|
||||
public class CancelTest {
|
||||
|
||||
EventBus bus;
|
||||
int hits;
|
||||
|
||||
/**
|
||||
* Constructs an event bus and registers this test instance as an event listener.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
@BeforeEach
|
||||
public 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
|
||||
public void testCancellation() {
|
||||
bus.dispatch(new SimpleEvent());
|
||||
assertEquals(1, hits);
|
||||
}
|
||||
|
||||
@Event(SimpleEvent.class)
|
||||
@Priority(100)
|
||||
void onSimpleFirst() {
|
||||
++hits;
|
||||
bus.cancel();
|
||||
}
|
||||
|
||||
@Event(SimpleEvent.class)
|
||||
@Priority(50)
|
||||
void onSimpleSecond() {
|
||||
++hits;
|
||||
}
|
||||
}
|
49
core/src/test/java/dev/kske/eventbus/core/DeadTest.java
Normal file
49
core/src/test/java/dev/kske/eventbus/core/DeadTest.java
Normal file
@ -0,0 +1,49 @@
|
||||
package dev.kske.eventbus.core;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Tests the dispatching of a dead event if an event could not be delivered.
|
||||
*
|
||||
* @author Kai S. K. Engelbart
|
||||
* @since 1.1.0
|
||||
*/
|
||||
public class DeadTest {
|
||||
|
||||
EventBus bus = new EventBus();
|
||||
String event = "This event has no handler";
|
||||
boolean deadEventHandled;
|
||||
|
||||
/**
|
||||
* Tests dead event delivery.
|
||||
*
|
||||
* @since 1.1.0
|
||||
*/
|
||||
@Test
|
||||
public void testDeadEvent() {
|
||||
bus.registerListener(this);
|
||||
bus.dispatch(event);
|
||||
assertTrue(deadEventHandled);
|
||||
bus.removeListener(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests how the event bus reacts to an unhandled dead event. This should not lead to an
|
||||
* exception or an endless recursion and should be logged instead.
|
||||
*
|
||||
* @since 1.1.0
|
||||
*/
|
||||
@Test
|
||||
public void testUnhandledDeadEvent() {
|
||||
bus.dispatch(event);
|
||||
}
|
||||
|
||||
@Event
|
||||
void onDeadEvent(DeadEvent deadEvent) {
|
||||
assertEquals(bus, deadEvent.getEventBus());
|
||||
assertEquals(event, deadEvent.getEvent());
|
||||
deadEventHandled = true;
|
||||
}
|
||||
}
|
79
core/src/test/java/dev/kske/eventbus/core/DispatchTest.java
Normal file
79
core/src/test/java/dev/kske/eventbus/core/DispatchTest.java
Normal file
@ -0,0 +1,79 @@
|
||||
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
|
||||
*/
|
||||
@Polymorphic
|
||||
@Priority(150)
|
||||
public class DispatchTest {
|
||||
|
||||
EventBus bus;
|
||||
static int hits;
|
||||
|
||||
/**
|
||||
* Constructs an event bus and registers this test instance as an event listener.
|
||||
*
|
||||
* @since 0.0.1
|
||||
*/
|
||||
@BeforeEach
|
||||
public void registerListener() {
|
||||
bus = new EventBus();
|
||||
bus.registerListener(this);
|
||||
bus.registerListener(SimpleEvent.class, e -> {
|
||||
++hits;
|
||||
assertEquals(4, hits);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests {@link EventBus#dispatch(Object)} with multiple handler priorities, a polymorphic
|
||||
* handler and a static handler.
|
||||
*
|
||||
* @since 0.0.1
|
||||
*/
|
||||
@Test
|
||||
public 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
|
||||
public void testDebugExecutionOrder() {
|
||||
String executionOrder = bus.debugExecutionOrder(SimpleEvent.class);
|
||||
System.out.println(executionOrder);
|
||||
assertEquals(
|
||||
"Event handler execution order for class dev.kske.eventbus.core.SimpleEvent (3 handler(s)):\n"
|
||||
+ "==========================================================================================\n"
|
||||
+ "ReflectiveEventHandler[eventType=class dev.kske.eventbus.core.SimpleEvent, polymorphic=true, priority=200, method=void dev.kske.eventbus.core.DispatchTest.onSimpleEventFirst(), useParameter=false]\n"
|
||||
+ "ReflectiveEventHandler[eventType=class dev.kske.eventbus.core.SimpleEvent, polymorphic=false, priority=150, method=static void dev.kske.eventbus.core.DispatchTest.onSimpleEventSecond(), useParameter=false]\n"
|
||||
+ "CallbackEventHandler[eventType=class dev.kske.eventbus.core.SimpleEvent, polymorphic=false, priority=100]\n"
|
||||
+ "==========================================================================================",
|
||||
executionOrder);
|
||||
}
|
||||
|
||||
@Event(SimpleEvent.class)
|
||||
@Priority(200)
|
||||
void onSimpleEventFirst() {
|
||||
++hits;
|
||||
assertTrue(hits == 1 || hits == 2);
|
||||
}
|
||||
|
||||
@Event(SimpleEvent.class)
|
||||
@Polymorphic(false)
|
||||
static void onSimpleEventSecond() {
|
||||
++hits;
|
||||
assertEquals(3, hits);
|
||||
}
|
||||
}
|
62
core/src/test/java/dev/kske/eventbus/core/ExceptionTest.java
Normal file
62
core/src/test/java/dev/kske/eventbus/core/ExceptionTest.java
Normal file
@ -0,0 +1,62 @@
|
||||
package dev.kske.eventbus.core;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Tests the dispatching of an exception event if an event handler threw an exception.
|
||||
*
|
||||
* @author Kai S. K. Engelbart
|
||||
* @since 1.1.0
|
||||
*/
|
||||
public class ExceptionTest {
|
||||
|
||||
EventBus bus = new EventBus();
|
||||
String event = "This event will cause an exception";
|
||||
RuntimeException exception = new RuntimeException("I failed");
|
||||
boolean exceptionEventHandled;
|
||||
|
||||
/**
|
||||
* Tests exception event delivery.
|
||||
*
|
||||
* @since 1.1.0
|
||||
*/
|
||||
@Test
|
||||
public void testExceptionEvent() {
|
||||
bus.registerListener(this);
|
||||
bus.registerListener(new ExceptionListener());
|
||||
bus.dispatch(event);
|
||||
assertTrue(exceptionEventHandled);
|
||||
bus.clearListeners();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests how the event bus reacts to an unhandled exception event. This should not lead to an
|
||||
* exception or an endless recursion and should be logged instead.
|
||||
*
|
||||
* @since 1.1.0
|
||||
*/
|
||||
@Test
|
||||
public void testUnhandledExceptionEvent() {
|
||||
bus.registerListener(this);
|
||||
bus.dispatch(event);
|
||||
bus.removeListener(this);
|
||||
}
|
||||
|
||||
@Event(String.class)
|
||||
void onString() {
|
||||
throw exception;
|
||||
}
|
||||
|
||||
class ExceptionListener {
|
||||
|
||||
@Event
|
||||
void onExceptionEvent(ExceptionEvent exceptionEvent) {
|
||||
assertEquals(bus, exceptionEvent.getEventBus());
|
||||
assertEquals(event, exceptionEvent.getEvent());
|
||||
assertEquals(exception, exceptionEvent.getCause());
|
||||
exceptionEventHandled = true;
|
||||
}
|
||||
}
|
||||
}
|
73
core/src/test/java/dev/kske/eventbus/core/NestedTest.java
Normal file
73
core/src/test/java/dev/kske/eventbus/core/NestedTest.java
Normal file
@ -0,0 +1,73 @@
|
||||
package dev.kske.eventbus.core;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.*;
|
||||
|
||||
/**
|
||||
* Tests nested event dispatches.
|
||||
*
|
||||
* @author Kai S. K. Engelbart
|
||||
* @since 1.2.0
|
||||
*/
|
||||
public class NestedTest {
|
||||
|
||||
EventBus bus;
|
||||
boolean nestedHit;
|
||||
|
||||
/**
|
||||
* Constructs an event bus and registers this test instance as an event listener.
|
||||
*
|
||||
* @since 1.2.0
|
||||
*/
|
||||
@BeforeEach
|
||||
public void registerListener() {
|
||||
bus = new EventBus();
|
||||
bus.registerListener(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Dispatches a simple event, which should in turn cause a string to be dispatched as a nested
|
||||
* event. If the corresponding handler sets {@link #nestedHit} to {@code true}, the test is
|
||||
* successful.
|
||||
*
|
||||
* @since 1.2.0
|
||||
*/
|
||||
@Test
|
||||
public void testNestedDispatch() {
|
||||
bus.dispatch(new SimpleEvent());
|
||||
assertTrue(nestedHit);
|
||||
}
|
||||
|
||||
/**
|
||||
* Dispatches a string as a nested event and cancels the current dispatch afterwards.
|
||||
*
|
||||
* @since 1.2.0
|
||||
*/
|
||||
@Event(SimpleEvent.class)
|
||||
void onSimpleEvent() {
|
||||
bus.dispatch("Nested event");
|
||||
bus.cancel();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets {@link #nestedHit} to {@code true} indicating that nested dispatches work.
|
||||
*
|
||||
* @since 1.2.0
|
||||
*/
|
||||
@Event(String.class)
|
||||
void onString() {
|
||||
nestedHit = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fails the test if an exception is caused during the dispatch.
|
||||
*
|
||||
* @param e the event containing the exception
|
||||
* @since 1.2.0
|
||||
*/
|
||||
@Event
|
||||
void onException(ExceptionEvent e) {
|
||||
fail("Exception during dispatch", e.getCause());
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package dev.kske.eventbus.core;
|
||||
|
||||
/**
|
||||
* A simple event for testing purposes.
|
||||
*
|
||||
* @author Kai S. K. Engelbart
|
||||
* @since 0.0.1
|
||||
*/
|
||||
public class SimpleEvent {}
|
@ -0,0 +1,9 @@
|
||||
package dev.kske.eventbus.core;
|
||||
|
||||
/**
|
||||
* Subclass of {@link SimpleEvent} for testing purposes.
|
||||
*
|
||||
* @author Kai S. K. Engelbart
|
||||
* @since 0.0.4
|
||||
*/
|
||||
public class SimpleEventSub extends SimpleEvent {}
|
Reference in New Issue
Block a user