X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Ftest%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Fmain%2FSonePluginTest.kt;h=d1e2cecb5309ec0a23d2ecd93736a4bdc4dc2676;hp=9e5158bb3eb08da0bd6037b0766cd15fc2e3bfb7;hb=4af887c75667793c197b564f16b7e95b1f63cbc7;hpb=4e4f20c6e94411757bef48141ea08d0132cb0e09 diff --git a/src/test/kotlin/net/pterodactylus/sone/main/SonePluginTest.kt b/src/test/kotlin/net/pterodactylus/sone/main/SonePluginTest.kt index 9e5158b..d1e2cec 100644 --- a/src/test/kotlin/net/pterodactylus/sone/main/SonePluginTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/main/SonePluginTest.kt @@ -15,6 +15,7 @@ import net.pterodactylus.sone.web.* import net.pterodactylus.sone.web.notification.* import org.hamcrest.MatcherAssert.* import org.hamcrest.Matchers.* +import org.junit.experimental.categories.* import org.mockito.Mockito.* import java.io.* import java.util.concurrent.atomic.* @@ -24,9 +25,10 @@ import kotlin.test.* * Unit test for [SonePlugin]. */ @Dirty +@Category(NotParallel::class) class SonePluginTest { - private val sonePlugin by lazy { SonePlugin { injector } } + private var sonePlugin = SonePlugin { injector } private val pluginRespirator = deepMock() private val node = deepMock() private val clientCore = deepMock() @@ -76,7 +78,7 @@ class SonePluginTest { private fun runSonePluginWithRealInjector(injectorConsumer: (Injector) -> Unit = {}): Injector { lateinit var injector: Injector - val sonePlugin = SonePlugin { + sonePlugin = SonePlugin { Guice.createInjector(*it).also { injector = it injectorConsumer(it) @@ -100,9 +102,15 @@ class SonePluginTest { assertThat(getInjected(NotificationHandler::class.java), notNullValue()) } + @Test + fun `ticker shutdown is being requested`() { + sonePlugin.runPlugin(pluginRespirator) + assertThat(getInjected(TickerShutdown::class.java), notNullValue()) + } + private class FirstStartListener(private val firstStartReceived: AtomicBoolean) { @Subscribe - fun firstStart(firstStart: FirstStart) { + fun firstStart(@Suppress("UNUSED_PARAMETER") firstStart: FirstStart) { firstStartReceived.set(true) } } @@ -115,7 +123,6 @@ class SonePluginTest { val eventBus = it.getInstance(EventBus::class.java) eventBus.register(FirstStartListener(firstStartReceived)) } - sonePlugin.runPlugin(pluginRespirator) assertThat(firstStartReceived.get(), equalTo(true)) } @@ -128,11 +135,89 @@ class SonePluginTest { val eventBus = it.getInstance(EventBus::class.java) eventBus.register(FirstStartListener(firstStartReceived)) } - sonePlugin.runPlugin(pluginRespirator) assertThat(firstStartReceived.get(), equalTo(false)) } } + private class ConfigNotReadListener(private val configNotReadReceiver: AtomicBoolean) { + @Subscribe + fun configNotRead(@Suppress("UNUSED_PARAMETER") configNotRead: ConfigNotRead) { + configNotReadReceiver.set(true) + } + } + + @Test + fun `config-not-read event is sent to event bus when new config is true`() { + File("sone.properties").deleteAfter { + writeText("Invalid") + val configNotReadReceived = AtomicBoolean() + runSonePluginWithRealInjector { + val eventBus = it.getInstance(EventBus::class.java) + eventBus.register(ConfigNotReadListener(configNotReadReceived)) + } + assertThat(configNotReadReceived.get(), equalTo(true)) + } + } + + @Test + fun `config-not-read event is not sent to event bus when first start is true`() { + File("sone.properties").delete() + val configNotReadReceived = AtomicBoolean() + runSonePluginWithRealInjector { + val eventBus = it.getInstance(EventBus::class.java) + eventBus.register(ConfigNotReadListener(configNotReadReceived)) + } + assertThat(configNotReadReceived.get(), equalTo(false)) + } + + @Test + fun `config-not-read event is not sent to event bus when new config is false`() { + File("sone.properties").deleteAfter { + writeText("# comment") + val configNotReadReceived = AtomicBoolean() + runSonePluginWithRealInjector { + val eventBus = it.getInstance(EventBus::class.java) + eventBus.register(ConfigNotReadListener(configNotReadReceived)) + } + assertThat(configNotReadReceived.get(), equalTo(false)) + } + } + + private class StartupListener(private val startupReceived: () -> Unit) { + @Subscribe + fun startup(@Suppress("UNUSED_PARAMETER") startup: Startup) { + startupReceived() + } + } + + @Test + fun `startup event is sent to event bus`() { + val startupReceived = AtomicBoolean() + runSonePluginWithRealInjector { + val eventBus = it.getInstance(EventBus::class.java) + eventBus.register(StartupListener { startupReceived.set(true) }) + } + assertThat(startupReceived.get(), equalTo(true)) + } + + private class ShutdownListener(private val shutdownReceived: () -> Unit) { + @Subscribe + fun shutdown(@Suppress("UNUSED_PARAMETER") shutdown: Shutdown) { + shutdownReceived() + } + } + + @Test + fun `shutdown event is sent to event bus on terminate`() { + val shutdownReceived = AtomicBoolean() + runSonePluginWithRealInjector { + val eventBus = it.getInstance(EventBus::class.java) + eventBus.register(ShutdownListener { shutdownReceived.set(true) }) + } + sonePlugin.terminate() + assertThat(shutdownReceived.get(), equalTo(true)) + } + private fun getInjected(clazz: Class, annotation: Annotation? = null): T? = injected[TypeLiteral.get(clazz) to annotation] as? T