🐛 Register manually created core with event bus
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 26 Jul 2019 14:17:00 +0000 (16:17 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 26 Jul 2019 14:17:00 +0000 (16:17 +0200)
src/main/java/net/pterodactylus/sone/main/SonePlugin.java
src/main/kotlin/net/pterodactylus/sone/main/SoneModule.kt
src/test/kotlin/net/pterodactylus/sone/main/SoneModuleTest.kt

index e82e771..540f3ac 100644 (file)
@@ -22,6 +22,7 @@ import static java.util.logging.Logger.*;
 import java.util.logging.Logger;
 import java.util.logging.*;
 
+import com.google.common.eventbus.*;
 import net.pterodactylus.sone.core.*;
 import net.pterodactylus.sone.fcp.*;
 import net.pterodactylus.sone.freenet.wot.*;
@@ -205,7 +206,7 @@ public class SonePlugin implements FredPlugin, FredPluginFCP, FredPluginL10n, Fr
        @VisibleForTesting
        protected Injector createInjector() {
                FreenetModule freenetModule = new FreenetModule(pluginRespirator);
-               AbstractModule soneModule = new SoneModule(this);
+               AbstractModule soneModule = new SoneModule(this, new EventBus());
                Module webInterfaceModule = new WebInterfaceModule();
 
                return createInjector(freenetModule, soneModule, webInterfaceModule);
index 66cd29a..31cc54a 100644 (file)
@@ -16,7 +16,7 @@ import net.pterodactylus.util.config.ConfigurationException
 import net.pterodactylus.util.version.Version
 import java.io.*
 
-class SoneModule(private val sonePlugin: SonePlugin) : AbstractModule() {
+open class SoneModule(private val sonePlugin: SonePlugin, private val eventBus: EventBus) : AbstractModule() {
 
        override fun configure() {
                val sonePropertiesFile = File("sone.properties")
@@ -37,7 +37,6 @@ class SoneModule(private val sonePlugin: SonePlugin) : AbstractModule() {
                                                        .getValue(null)
                                                        ?.let { DebugLoaders(it) }
                                }
-               val eventBus = EventBus()
 
                bind(Configuration::class.java).toInstance(configuration)
                bind(EventBus::class.java).toInstance(eventBus)
@@ -66,7 +65,7 @@ class SoneModule(private val sonePlugin: SonePlugin) : AbstractModule() {
        fun getCore(configuration: Configuration, freenetInterface: FreenetInterface, identityManager: IdentityManager, soneDownloader: SoneDownloader, imageInserter: ImageInserter, updateChecker: UpdateChecker, webOfTrustUpdater: WebOfTrustUpdater, eventBus: EventBus, database: Database) =
                        Core(configuration, freenetInterface, identityManager, soneDownloader, imageInserter, updateChecker, webOfTrustUpdater, eventBus, database).apply {
                                debugInformation.showVersionInformation = configuration.getBooleanValue("Debug/ShowVersionInformation").getValue(false)
-                       }
+                       }.also(eventBus::register)
 
 }
 
index d928214..6cac0a8 100644 (file)
@@ -16,6 +16,7 @@ import net.pterodactylus.util.config.*
 import net.pterodactylus.util.version.Version
 import org.hamcrest.MatcherAssert.*
 import org.hamcrest.Matchers.*
+import org.mockito.Mockito.*
 import java.io.*
 import java.util.concurrent.atomic.*
 import kotlin.test.*
@@ -36,11 +37,13 @@ class SoneModuleTest {
                whenever(l10n()).thenReturn(l10n)
        }
 
-       private val injector by lazy { createInjector(
-                       SoneModule(sonePlugin),
-                       FreenetInterface::class.isProvidedByDeepMock(),
-                       PluginRespirator::class.isProvidedByDeepMock()
-       ) }
+       private val injector by lazy {
+               createInjector(
+                               SoneModule(sonePlugin, EventBus()),
+                               FreenetInterface::class.isProvidedByDeepMock(),
+                               PluginRespirator::class.isProvidedByDeepMock()
+               )
+       }
 
        @AfterTest
        fun removePropertiesFromCurrentDirectory() {
@@ -195,4 +198,16 @@ class SoneModuleTest {
                assertThat(secondCore, sameInstance(firstCore))
        }
 
+       @Test
+       fun `core is registered with event bus`() {
+               val eventBus = mock<EventBus>()
+               val injector = createInjector(
+                               SoneModule(sonePlugin, eventBus),
+                               FreenetInterface::class.isProvidedByDeepMock(),
+                               PluginRespirator::class.isProvidedByDeepMock()
+               )
+               val core = injector.getInstance<Core>()
+               verify(eventBus).register(core)
+       }
+
 }