diff options
author | therealbush <therealbush@users.noreply.github.com> | 2022-04-14 00:31:16 -0700 |
---|---|---|
committer | therealbush <therealbush@users.noreply.github.com> | 2022-04-14 00:31:16 -0700 |
commit | b49065b50edd48d0f335e17bcf6868dc2f916ad5 (patch) | |
tree | a9ad233135919970f3a6f36a1369ef701a9ff677 /src/test | |
parent | b196c7c2fe06d84e862648a8bf25058f72c6c0b1 (diff) | |
download | eventbus-kotlin-b49065b50edd48d0f335e17bcf6868dc2f916ad5.tar.gz eventbus-kotlin-b49065b50edd48d0f335e17bcf6868dc2f916ad5.tar.bz2 eventbus-kotlin-b49065b50edd48d0f335e17bcf6868dc2f916ad5.zip |
soon (lol)
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/java/JavaTest.java | 62 | ||||
-rw-r--r-- | src/test/java/TestJava.java | 80 | ||||
-rw-r--r-- | src/test/kotlin/KotlinTest.kt | 252 | ||||
-rw-r--r-- | src/test/kotlin/TestKotlin.kt | 215 |
4 files changed, 314 insertions, 295 deletions
diff --git a/src/test/java/JavaTest.java b/src/test/java/JavaTest.java new file mode 100644 index 0000000..0b942b6 --- /dev/null +++ b/src/test/java/JavaTest.java @@ -0,0 +1,62 @@ +import me.bush.illnamethislater.Listener; +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.core.config.Configurator; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.TestInstance; +import org.junit.jupiter.api.TestInstance.Lifecycle; +import me.bush.illnamethislater.EventBus; +import org.apache.logging.log4j.LogManager; +import org.junit.jupiter.api.Test; + +import static me.bush.illnamethislater.ListenerKt.listener; + +/** + * I was getting NCDFE when trying to load this class + * from the other test and I don't care enough to fix it. + * + * @author bush + * @since 1.0.0 + */ +@TestInstance(Lifecycle.PER_CLASS) +public class JavaTest { + private EventBus eventBus; + private final Logger logger = LogManager.getLogger(); + + @BeforeAll + public void setup() { + Configurator.setRootLevel(Level.ALL); + logger.info("Running Java tests"); + eventBus = new EventBus(); + eventBus.subscribe(this); + } + + @Test + public void javaSubscriberTest() { + eventBus.subscribe(this); + SimpleEvent event = new SimpleEvent(); + eventBus.post(event); + Assertions.assertEquals(event.getCount(), 4); + } + + public Listener someInstanceListenerField = listener(SimpleEvent.class, event -> { + event.setCount(event.getCount() + 1); + }); + + public Listener someInstanceListenerMethod() { + return listener(SimpleEvent.class, event -> { + event.setCount(event.getCount() + 1); + }); + } + + public static Listener someStaticListenerMethod() { + return listener(SimpleEvent.class, event -> { + event.setCount(event.getCount() + 1); + }); + } + + public static Listener someStaticListenerField = listener(SimpleEvent.class, event -> { + event.setCount(event.getCount() + 1); + }); +} diff --git a/src/test/java/TestJava.java b/src/test/java/TestJava.java deleted file mode 100644 index c6c77a8..0000000 --- a/src/test/java/TestJava.java +++ /dev/null @@ -1,80 +0,0 @@ -import me.bush.illnamethislater.Listener; -import org.apache.logging.log4j.Level; -import org.apache.logging.log4j.Logger; -import org.apache.logging.log4j.core.config.Configurator; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.TestInstance; -import org.junit.jupiter.api.TestInstance.Lifecycle; -import me.bush.illnamethislater.EventBus; -import org.apache.logging.log4j.LogManager; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.Test; - -import static me.bush.illnamethislater.ListenerKt.listener; - -/** - * I was getting noclassdeffound when trying to load this Java - * class in the other test and I don't care enough to fix it. - * - * @author bush - * @since 1.0.0 - */ -@TestInstance(Lifecycle.PER_CLASS) -public class TestJava { - private static boolean thisShouldChange; - private boolean thisShouldChangeToo; - private EventBus eventBus; - private final Logger logger = LogManager.getLogger(); - - @BeforeAll - public void setup() { - Configurator.setRootLevel(Level.ALL); - - // Test that init works - logger.info("Initializing"); - eventBus = new EventBus(); - - // Ensure no npe - logger.info("Logging empty debug info"); - eventBus.debugInfo(); - - // Test that basic subscribing reflection works - logger.info("Subscribing"); - eventBus.subscribe(this); - - logger.info("Testing Events"); - } - - @AfterAll - public void unsubscribe() { - logger.info("Unsubscribing"); - eventBus.unsubscribe(this); - eventBus.debugInfo(); - } - - //////////////////////////////////////////////////////////////////////////////////////////////////////////////// - //////////////////////////////////////////////////////////////////////////////////////////////////////////////// - - @Test - public void javaSubscriberTest() { - eventBus.subscribe(this); - eventBus.post(new MyEvent()); - Assertions.assertTrue(TestJava.thisShouldChange); - Assertions.assertTrue(this.thisShouldChangeToo); - // TODO: 4/2/2022 fix calling from java - } - - public Listener listener = listener(MyEvent.class, 200, event -> { - Assertions.assertEquals(event.getSomeString(), "donda"); - event.setSomeString("donda 2"); - this.thisShouldChangeToo = true; - }); - - public static Listener someStaticListener() { - return listener(MyEvent.class, 100, event -> { - Assertions.assertEquals(event.getSomeString(), "donda 2"); - thisShouldChange = true; - }); - } -} diff --git a/src/test/kotlin/KotlinTest.kt b/src/test/kotlin/KotlinTest.kt new file mode 100644 index 0000000..6ad3273 --- /dev/null +++ b/src/test/kotlin/KotlinTest.kt @@ -0,0 +1,252 @@ +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.runBlocking +import me.bush.illnamethislater.* +import org.apache.logging.log4j.Level +import org.apache.logging.log4j.LogManager +import org.apache.logging.log4j.core.config.Configurator +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.BeforeAll +import org.junit.jupiter.api.TestInstance +import org.junit.jupiter.api.Test +import org.opentest4j.AssertionFailedError +import sun.misc.Unsafe +import kotlin.jvm.internal.PropertyReference0Impl +import kotlin.random.Random +import kotlin.reflect.KCallable +import kotlin.reflect.KProperty +import kotlin.reflect.full.* +import kotlin.reflect.javaType +import kotlin.reflect.jvm.isAccessible +import kotlin.reflect.jvm.javaField +import kotlin.reflect.jvm.javaGetter + +/** + * I don't know how to do these.... + * + * @author bush + * @since 1.0.0 + */ +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +class KotlinTest { + private lateinit var eventBus: EventBus + private val logger = LogManager.getLogger("Kotlin Test") + + @BeforeAll + fun `setup logger and initialize eventbus` () { + // Log level defaults to only error + Configurator.setRootLevel(Level.ALL) + eventBus = EventBus( + // Defaults + Config( + logger = LogManager.getLogger("Eventbus"), + parallelScope = CoroutineScope(Dispatchers.Default), + thirdPartyCompatibility = true, + annotationRequired = false + ) + ) + } + + //////////////////////////////////////////////////////////////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + @Test + fun `test listener priority and ability to cancel events or receive cancelled events` () { + eventBus.subscribe(this) + val event = SimpleEvent() + eventBus.post(event) + Assertions.assertEquals(3, event.count) + eventBus.unsubscribe(this) + } + + // Last to be called; does not have receiveCancelled, but the last listener un-cancelled the event. + fun listener4() = listener<SimpleEvent>(priority = Int.MIN_VALUE) { + Assertions.assertEquals(2, it.count) + it.count++ + } + + // Will not be called; second-highest priority, no receiveCancelled. + val listener2 + get() = listener<SimpleEvent>(priority = 0) { + Assertions.fail("This should not be called") + } + + // First to be called; highest priority. + private val listener1 = listener<SimpleEvent>(priority = 10) { + Assertions.assertEquals(0, it.count) + it.count++ + // Cancel, so next listener shouldn't receive it. + it.cancel() + } + + // Second to be called; has receiveCancelled and can un-cancel the event. + fun listener3() = listener<SimpleEvent>(priority = Int.MIN_VALUE + 100, receiveCancelled = true) { + Assertions.assertEquals(1, it.count) + it.count++ + it.cancelled = false + } + + //////////////////////////////////////////////////////////////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + @Test + fun `test subscribing on a kotlin singleton object` () { + eventBus.subscribe(ObjectTest) + val event = SimpleEvent() + eventBus.post(event) + Assertions.assertEquals(3, event.count) + eventBus.unsubscribe(ObjectTest) + } + + //////////////////////////////////////////////////////////////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + @Test + fun `test primitive types and listeners which don't belong to a class` () { + val random = Random.nextInt() + var changed = 0 + val listener = listener<Int> { + changed = it + } + eventBus.register(listener) + eventBus.post(random) + Assertions.assertEquals(random, changed) + eventBus.unregister(listener) + } + + //////////////////////////////////////////////////////////////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + @Test + fun `test that we can detect if an external event is cancelled` () { + eventBus.subscribe(this) + val event = ExternalEvent() + eventBus.post(event) + eventBus.unsubscribe(this) + } + + @EventListener + fun externalListener1() = listener<ExternalEvent>(priority = 1) { + it.canceled = true + } + + // Should not be called + fun externalListener2() = listener<ExternalEvent>(priority = -1) { + Assertions.fail("This should not be called") + } + + //////////////////////////////////////////////////////////////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + @Test + fun `test parallel event posting` () { + runBlocking { + sus() + } + } + + suspend fun sus() { + println() + } + + fun sussy() { + println() + } + + //////////////////////////////////////////////////////////////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + @Test + fun `call every method on multiple threads concurrently to ensure no CME is thrown` () { + + } + + //////////////////////////////////////////////////////////////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + @Test + fun `test that inheritance doesn't affect events or listeners` () { + val superTest = listener<SimpleEvent> { + Assertions.fail("This should not be called") + } + eventBus.register(superTest) + // No listeners should be called when a superclass is posted. + eventBus.post(Any()) + eventBus.unregister(superTest) + val subTest = listener<Any> { + Assertions.fail("This should not be called") + } + eventBus.register(subTest) + // No listeners should be called when a subclass is posted. + eventBus.post(SimpleEvent()) + eventBus.unregister(subTest) + } + + //////////////////////////////////////////////////////////////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + @Test + fun `test that require annotation mode works` () { + val eventBus = EventBus(Config(annotationRequired = true)) + eventBus.subscribe(this) + eventBus.post(Unit) + Assertions.assertTrue(called) + } + + var called = false + + @EventListener + val annotation = listener<Unit> { + called = true + } + + val noAnnotation = listener<Unit> { + Assertions.fail("This should not be called") + } + + //////////////////////////////////////////////////////////////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + @Test + fun `test subscribing on a companion object` () { + eventBus.subscribe(KotlinTest) + val string = "i love bush's eventbus <3" + eventBus.post(string) + Assertions.assertEquals(string, value) + eventBus.unsubscribe(KotlinTest) + } + + companion object { + var value = "" + + @EventListener + private val listener = listener<String> { + value = it + } + } +} + +object ObjectTest { + private val listener1 = listener<SimpleEvent> { + it.count++ + } + + @EventListener + private val listener2 get() = listener<SimpleEvent> { + it.count++ + } + + private fun listener3() = listener<SimpleEvent> { + it.count++ + } +} + +class SimpleEvent : Event() { + override val cancellable = true + + var count = 0 +} + +class ExternalEvent { + var canceled = false +} diff --git a/src/test/kotlin/TestKotlin.kt b/src/test/kotlin/TestKotlin.kt deleted file mode 100644 index 2ae6313..0000000 --- a/src/test/kotlin/TestKotlin.kt +++ /dev/null @@ -1,215 +0,0 @@ -import me.bush.illnamethislater.Event -import me.bush.illnamethislater.EventBus -import me.bush.illnamethislater.listener -import org.apache.logging.log4j.Level -import org.apache.logging.log4j.LogManager -import org.apache.logging.log4j.core.config.Configurator -import org.junit.jupiter.api.AfterAll -import org.junit.jupiter.api.Assertions -import org.junit.jupiter.api.BeforeAll -import org.junit.jupiter.api.Test -import org.junit.jupiter.api.TestInstance -import org.junit.jupiter.api.TestInstance.Lifecycle -import kotlin.random.Random - -/** - * I don't know how to do these.... - * - * @author bush - * @since 1.0.0 - */ -@TestInstance(Lifecycle.PER_CLASS) -class Test { - private lateinit var eventBus: EventBus - private val logger = LogManager.getLogger() - - //////////////////////////////////////////////////////////////////////////////////////////////////////////////// - //////////////////////////////////////////////////////////////////////////////////////////////////////////////// - - @BeforeAll - fun setup() { - Configurator.setRootLevel(Level.ALL) - - // Test that init works - logger.info("Initializing") - eventBus = EventBus() - - // Ensure no npe - logger.info("Logging empty debug info") - eventBus.debugInfo() - - // Test that basic subscribing reflection works - logger.info("Subscribing") - eventBus.subscribe(this) - - logger.info("Testing Events") - } - - @AfterAll - fun unsubscribe() { - logger.info("Unsubscribing") - eventBus.unsubscribe(this) - eventBus.debugInfo() - } - - //////////////////////////////////////////////////////////////////////////////////////////////////////////////// - //////////////////////////////////////////////////////////////////////////////////////////////////////////////// - - // Tests debug info format - @Test - fun debugInfoTest() { - eventBus.debugInfo() - } - - //////////////////////////////////////////////////////////////////////////////////////////////////////////////// - //////////////////////////////////////////////////////////////////////////////////////////////////////////////// - - // Tests autoboxing and internal data structure against primitives. - @Test - fun primitiveListenerTest() { - val random = Random.nextInt() - eventBus.post(random) - Assertions.assertEquals(random, primitiveTestValue) - } - - private var primitiveTestValue = 0 - - val primitiveListener = listener<Int> { - primitiveTestValue = it - } - - //////////////////////////////////////////////////////////////////////////////////////////////////////////////// - //////////////////////////////////////////////////////////////////////////////////////////////////////////////// - - // Tests unsubscribing of listeners which don't belong to a subscriber. - @Test - fun freeListenerTest() { - // Register listener and keep the value - val listener = eventBus.register(listener<String> { - freeListenerTestValue = it - }) - val valueOne = "i love bush's eventbus <3" - val valueTwo = "sdklasdjsakdsadlksadlksdl" - // Will change the value - eventBus.post(valueOne) - Assertions.assertEquals(valueOne, freeListenerTestValue) - // Remove the listener - eventBus.unregister(listener) - // No effect - eventBus.post(valueTwo) - // Value will not change - Assertions.assertEquals(valueOne, freeListenerTestValue) - } - - private var freeListenerTestValue: String? = null - - //////////////////////////////////////////////////////////////////////////////////////////////////////////////// - //////////////////////////////////////////////////////////////////////////////////////////////////////////////// - - // Tests priority and receiveCancelled functionality. - @Test - fun myEventListenerTest() { - val event = MyEvent() - eventBus.post(event) - Assertions.assertEquals(event.lastListener, "myEventListener3") - } - - // First to be called; highest priority. - val myEventListener0 = listener<MyEvent>(priority = 10) { - Assertions.assertEquals(it.lastListener, "") - it.lastListener = "myEventListener0" - it.cancel() - } - - // Will not be called; second-highest priority, no receiveCancelled. - val myEventListener1 - get() = listener<MyEvent>(priority = 0) { - Assertions.assertTrue(false) - } - - // Second to be called; has receiveCancelled and can un-cancel the event. - fun myEventListener2() = listener<MyEvent>(priority = Int.MIN_VALUE + 100, receiveCancelled = true) { - Assertions.assertEquals(it.lastListener, "myEventListener0") - it.lastListener = "myEventListener2" - it.cancelled = false - } - - // Last to be called; does not have receiveCancelled, but the last listener un-cancelled the event. - fun myEventListener3() = listener<MyEvent>(priority = Int.MIN_VALUE) { - Assertions.assertEquals(it.lastListener, "myEventListener2") - it.lastListener = "myEventListener3" - } - - //////////////////////////////////////////////////////////////////////////////////////////////////////////////// - //////////////////////////////////////////////////////////////////////////////////////////////////////////////// - - // Tests external event cancel state functionality. - @Test - fun externalEventListenerTest() { - var unchanged = "this will not change" - // Cancels the event - eventBus.register(listener<ExternalEvent>(200) { - it.canceled = true - }) - // This shouldn't be called - eventBus.register(listener<ExternalEvent>(100) { - unchanged = "changed" - }) - eventBus.post(ExternalEvent()) - Assertions.assertEquals(unchanged, "this will not change") - // Tests that duplicates are detected, and that both - // "canceled" and "cancelled" are detected as valid fields - eventBus.register(listener<ExternalDuplicates> {}) - } - - //////////////////////////////////////////////////////////////////////////////////////////////////////////////// - //////////////////////////////////////////////////////////////////////////////////////////////////////////////// - - // Tests parallel invocation functionality. - @Test - fun parallelListenerTest() { - - } - - //////////////////////////////////////////////////////////////////////////////////////////////////////////////// - //////////////////////////////////////////////////////////////////////////////////////////////////////////////// - - // Tests reflection against singleton object classes. - @Test - fun objectSubscriberTest() { - eventBus.subscribe(ObjectSubscriber) - eventBus.post(Unit) - Assertions.assertTrue(ObjectSubscriber.willChange) - } - - //////////////////////////////////////////////////////////////////////////////////////////////////////////////// - //////////////////////////////////////////////////////////////////////////////////////////////////////////////// - - // todo test thread safety - // todo ensure boolean functions return proper value (subscribe, unsubscribe, etc) -} - -object ObjectSubscriber { - var willChange = false - - fun listener() = listener<Unit> { - willChange = true - } -} - -class MyEvent : Event() { - override val cancellable = true - - var lastListener = "" - var someString = "donda" -} - -class ExternalEvent { - var canceled = false -} - -// Should give us a warning about duplicates -class ExternalDuplicates { - var canceled = false - var cancelled = false -} |