Add Test for Cancellation
This commit is contained in:
		| @@ -12,7 +12,8 @@ import org.junit.jupiter.api.*; | |||||||
|  */ |  */ | ||||||
| class EventBusTest implements EventListener { | class EventBusTest implements EventListener { | ||||||
|  |  | ||||||
| 	int hits; | 	int			hits; | ||||||
|  | 	static int	canceledHits; | ||||||
|  |  | ||||||
| 	@BeforeEach | 	@BeforeEach | ||||||
| 	public void registerListener() { | 	public void registerListener() { | ||||||
| @@ -25,11 +26,15 @@ class EventBusTest implements EventListener { | |||||||
| 		EventBus.getInstance().dispatch(new SimpleEvent()); | 		EventBus.getInstance().dispatch(new SimpleEvent()); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	@Event( | 	@Test | ||||||
| 		eventType = SimpleEvent.class, | 	void testCancellation() { | ||||||
| 		includeSubtypes = true, | 		var test2 = new EventBusTest(); | ||||||
| 		priority = 200 | 		test2.registerListener(); | ||||||
| 	) | 		EventBus.getInstance().dispatch(new SimpleCancelEvent()); | ||||||
|  | 		assertTrue(canceledHits == 1); | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	@Event(eventType = SimpleEvent.class, includeSubtypes = true, priority = 200) | ||||||
| 	private void onSimpleEventFirst() { | 	private void onSimpleEventFirst() { | ||||||
| 		++hits; | 		++hits; | ||||||
| 		assertTrue(hits == 1 || hits == 2); | 		assertTrue(hits == 1 || hits == 2); | ||||||
| @@ -46,4 +51,16 @@ class EventBusTest implements EventListener { | |||||||
| 		++hits; | 		++hits; | ||||||
| 		assertEquals(4, hits); | 		assertEquals(4, hits); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	@Event(eventType = SimpleCancelEvent.class, priority = 500) | ||||||
|  | 	private void onSimpleCancelFirst() { | ||||||
|  | 		++canceledHits; | ||||||
|  | 		assertTrue(canceledHits == 1); | ||||||
|  | 		EventBus.getInstance().cancel(); | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	@Event(eventType = SimpleCancelEvent.class, priority = 200) | ||||||
|  | 	private void onSimpleCancelSecond() { | ||||||
|  | 		fail(); | ||||||
|  | 	} | ||||||
| } | } | ||||||
|   | |||||||
							
								
								
									
										9
									
								
								src/test/java/dev/kske/eventbus/SimpleCancelEvent.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								src/test/java/dev/kske/eventbus/SimpleCancelEvent.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | |||||||
|  | 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 {} | ||||||
		Reference in New Issue
	
	Block a user