From a71b38bad4c9acd35902161e2579b6738408abc2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Mon, 2 Dec 2019 20:57:28 +0100 Subject: [PATCH] =?utf8?q?=F0=9F=9A=9A=20Move=20notification=20handler=20u?= =?utf8?q?sage=20into=20Sone=20plugin?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../net/pterodactylus/sone/main/SonePlugin.java | 9 ++++-- .../pterodactylus/sone/web/WebInterfaceModule.kt | 7 ----- .../net/pterodactylus/sone/main/SonePluginTest.kt | 33 ++++++++++++---------- 3 files changed, 24 insertions(+), 25 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/main/SonePlugin.java b/src/main/java/net/pterodactylus/sone/main/SonePlugin.java index a3cb385..b79df7c 100644 --- a/src/main/java/net/pterodactylus/sone/main/SonePlugin.java +++ b/src/main/java/net/pterodactylus/sone/main/SonePlugin.java @@ -27,6 +27,7 @@ import net.pterodactylus.sone.fcp.*; import net.pterodactylus.sone.freenet.wot.*; import net.pterodactylus.sone.web.*; import net.pterodactylus.sone.web.notification.NotificationHandler; +import net.pterodactylus.sone.web.notification.NotificationHandlerModule; import freenet.l10n.BaseL10n.*; import freenet.l10n.*; @@ -197,7 +198,9 @@ public class SonePlugin implements FredPlugin, FredPluginFCP, FredPluginL10n, Fr /* create the web interface. */ webInterface = injector.getInstance(WebInterface.class); - NotificationHandler notificationHandler = injector.getInstance(NotificationHandler.class); + + /* we need to request this to install all notification handlers. */ + injector.getInstance(NotificationHandler.class); /* start core! */ core.start(); @@ -206,7 +209,6 @@ public class SonePlugin implements FredPlugin, FredPluginFCP, FredPluginL10n, Fr webInterface.start(); webInterface.setFirstStart(injector.getInstance(Key.get(Boolean.class, Names.named("FirstStart")))); webInterface.setNewConfig(injector.getInstance(Key.get(Boolean.class, Names.named("NewConfig")))); - notificationHandler.start(); } @VisibleForTesting @@ -214,8 +216,9 @@ public class SonePlugin implements FredPlugin, FredPluginFCP, FredPluginL10n, Fr FreenetModule freenetModule = new FreenetModule(pluginRespirator); AbstractModule soneModule = new SoneModule(this, new EventBus()); Module webInterfaceModule = new WebInterfaceModule(); + Module notificationHandlerModule = new NotificationHandlerModule(); - return createInjector(freenetModule, soneModule, webInterfaceModule); + return createInjector(freenetModule, soneModule, webInterfaceModule, notificationHandlerModule); } @VisibleForTesting diff --git a/src/main/kotlin/net/pterodactylus/sone/web/WebInterfaceModule.kt b/src/main/kotlin/net/pterodactylus/sone/web/WebInterfaceModule.kt index 63f3fc6..3d87aa7 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/WebInterfaceModule.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/WebInterfaceModule.kt @@ -1,6 +1,5 @@ package net.pterodactylus.sone.web -import com.google.common.eventbus.* import com.google.inject.* import freenet.support.api.* import net.pterodactylus.sone.core.* @@ -11,7 +10,6 @@ import net.pterodactylus.sone.freenet.wot.* import net.pterodactylus.sone.main.* import net.pterodactylus.sone.template.* import net.pterodactylus.sone.text.* -import net.pterodactylus.sone.web.notification.* import net.pterodactylus.util.notify.* import net.pterodactylus.util.template.* import javax.inject.* @@ -134,9 +132,4 @@ class WebInterfaceModule : AbstractModule() { fun getNotificationManager() = NotificationManager() - @Provides - @Singleton - fun getNotificationHandler(eventBus: EventBus, loaders: Loaders, notificationManager: NotificationManager) = - NotificationHandler(eventBus, loaders, notificationManager) - } diff --git a/src/test/kotlin/net/pterodactylus/sone/main/SonePluginTest.kt b/src/test/kotlin/net/pterodactylus/sone/main/SonePluginTest.kt index eef312f..23c679b 100644 --- a/src/test/kotlin/net/pterodactylus/sone/main/SonePluginTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/main/SonePluginTest.kt @@ -22,7 +22,6 @@ import kotlin.test.* @Dirty class SonePluginTest { - private var injector = mockInjector() private val sonePlugin by lazy { SonePlugin { injector } } private val pluginRespirator = deepMock() private val node = deepMock() @@ -91,27 +90,31 @@ class SonePluginTest { } @Test - fun `notification handler is being started`() { + fun `notification handler is being requested`() { sonePlugin.runPlugin(pluginRespirator) - val notificationHandler = injector.getInstance() - verify(notificationHandler).start() + assertThat(getInjected(NotificationHandler::class.java), notNullValue()) } -} + private fun getInjected(clazz: Class, annotation: Annotation? = null): T? = + injected[TypeLiteral.get(clazz) to annotation] as? T + + private val injected = + mutableMapOf, Annotation?>, Any>() -private fun mockInjector() = mock().apply { - val injected = mutableMapOf, Annotation?>, Any>() - fun mockValue(clazz: Class<*>) = false.takeIf { clazz.name == java.lang.Boolean::class.java.name } ?: mock(clazz) - whenever(getInstance(any>())).then { - injected.getOrPut((it.getArgument(0) as Key<*>).let { it.typeLiteral to it.annotation }) { - it.getArgument>(0).typeLiteral.type.typeName.toClass().let(::mockValue) + private val injector = mock().apply { + fun mockValue(clazz: Class<*>) = false.takeIf { clazz.name == java.lang.Boolean::class.java.name } ?: mock(clazz) + whenever(getInstance(any>())).then { + injected.getOrPut((it.getArgument(0) as Key<*>).let { it.typeLiteral to it.annotation }) { + it.getArgument>(0).typeLiteral.type.typeName.toClass().let(::mockValue) + } } - } - whenever(getInstance(any>())).then { - injected.getOrPut(TypeLiteral.get(it.getArgument(0) as Class<*>) to null) { - it.getArgument>(0).let(::mockValue) + whenever(getInstance(any>())).then { + injected.getOrPut(TypeLiteral.get(it.getArgument(0) as Class<*>) to null) { + it.getArgument>(0).let(::mockValue) + } } } + } private fun String.toClass(): Class<*> = SonePlugin::class.java.classLoader.loadClass(this) -- 2.7.4