Split EventBusTest into DispatchTest and CancelTest, add Javadoc
This commit is contained in:
parent
659bd7888f
commit
ec73be9046
52
src/test/java/dev/kske/eventbus/CancelTest.java
Normal file
52
src/test/java/dev/kske/eventbus/CancelTest.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
58
src/test/java/dev/kske/eventbus/DispatchTest.java
Normal file
58
src/test/java/dev/kske/eventbus/DispatchTest.java
Normal file
@ -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);
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
@ -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 {}
|
Loading…
Reference in New Issue
Block a user