+
+ @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 <T> getInjected(clazz: Class<T>, annotation: Annotation? = null): T? =
+ injected[TypeLiteral.get(clazz) to annotation] as? T
+
+ private val injected =
+ mutableMapOf<Pair<TypeLiteral<*>, Annotation?>, Any>()
+
+ private val injector = mock<Injector>().apply {
+ fun mockValue(clazz: Class<*>) = false.takeIf { clazz.name == java.lang.Boolean::class.java.name } ?: mock(clazz)
+ whenever(getInstance(any<Key<*>>())).then {
+ injected.getOrPut((it.getArgument(0) as Key<*>).let { it.typeLiteral to it.annotation }) {
+ it.getArgument<Key<*>>(0).typeLiteral.type.typeName.toClass().let(::mockValue)
+ }
+ }
+ whenever(getInstance(any<Class<*>>())).then {
+ injected.getOrPut(TypeLiteral.get(it.getArgument(0) as Class<*>) to null) {
+ it.getArgument<Class<*>>(0).let(::mockValue)
+ }
+ }
+ }
+