Split EventBusTest into DispatchTest and CancelTest, add Javadoc

This commit is contained in:
Kai S. K. Engelbart 2020-11-26 08:14:11 +01:00
parent 659bd7888f
commit ec73be9046
Signed by: kske
GPG Key ID: 8BEB13EC5DF7EF13
4 changed files with 110 additions and 72 deletions

View 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;
}
}

View 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);
}
}

View File

@ -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();
}
}

View File

@ -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 {}