From f2a7ecad4a2f3fffe6eaa9ae435ff6be8b5ef754 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 8 Feb 2017 19:49:50 +0100 Subject: [PATCH] Add freenet module for Guice --- .../net/pterodactylus/sone/main/SonePlugin.java | 10 +-- .../net/pterodactylus/sone/main/FreenetModule.kt | 25 +++++++ .../pterodactylus/sone/main/FreenetModuleTest.kt | 78 ++++++++++++++++++++++ 3 files changed, 104 insertions(+), 9 deletions(-) create mode 100644 src/main/kotlin/net/pterodactylus/sone/main/FreenetModule.kt create mode 100644 src/test/kotlin/net/pterodactylus/sone/main/FreenetModuleTest.kt diff --git a/src/main/java/net/pterodactylus/sone/main/SonePlugin.java b/src/main/java/net/pterodactylus/sone/main/SonePlugin.java index e76c6ef..f394d44 100644 --- a/src/main/java/net/pterodactylus/sone/main/SonePlugin.java +++ b/src/main/java/net/pterodactylus/sone/main/SonePlugin.java @@ -54,7 +54,6 @@ import com.google.inject.spi.TypeListener; import freenet.client.async.PersistenceDisabledException; import freenet.l10n.BaseL10n.LANGUAGE; import freenet.l10n.PluginL10n; -import freenet.node.Node; import freenet.pluginmanager.FredPlugin; import freenet.pluginmanager.FredPluginBaseL10n; import freenet.pluginmanager.FredPluginFCP; @@ -238,15 +237,8 @@ public class SonePlugin implements FredPlugin, FredPluginFCP, FredPluginL10n, Fr final EventBus eventBus = new EventBus(); /* Freenet injector configuration. */ - AbstractModule freenetModule = new AbstractModule() { + FreenetModule freenetModule = new FreenetModule(pluginRespirator); - @Override - @SuppressWarnings("synthetic-access") - protected void configure() { - bind(PluginRespirator.class).toInstance(SonePlugin.this.pluginRespirator); - bind(Node.class).toInstance(SonePlugin.this.pluginRespirator.getNode()); - } - }; /* Sone injector configuration. */ AbstractModule soneModule = new AbstractModule() { diff --git a/src/main/kotlin/net/pterodactylus/sone/main/FreenetModule.kt b/src/main/kotlin/net/pterodactylus/sone/main/FreenetModule.kt new file mode 100644 index 0000000..a8da595 --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/main/FreenetModule.kt @@ -0,0 +1,25 @@ +package net.pterodactylus.sone.main + +import com.google.inject.Binder +import com.google.inject.Module +import com.google.inject.Provides +import freenet.client.HighLevelSimpleClient +import freenet.node.Node +import freenet.pluginmanager.PluginRespirator +import javax.inject.Singleton + +/** + * Guice [Module] that supplies some objects that are in fact supplied by the Freenet node. + */ +class FreenetModule(private val pluginRespirator: PluginRespirator): Module { + + override fun configure(binder: Binder): Unit = binder.run { + bind(PluginRespirator::class.java).toProvider { pluginRespirator } + pluginRespirator.node!!.let { node -> bind(Node::class.java).toProvider { node } } + bind(HighLevelSimpleClient::class.java).toProvider { pluginRespirator.hlSimpleClient!! } + } + + @Provides @Singleton + fun getSessionManager() = pluginRespirator.getSessionManager("Sone")!! + +} diff --git a/src/test/kotlin/net/pterodactylus/sone/main/FreenetModuleTest.kt b/src/test/kotlin/net/pterodactylus/sone/main/FreenetModuleTest.kt new file mode 100644 index 0000000..d0dc7c4 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/main/FreenetModuleTest.kt @@ -0,0 +1,78 @@ +package net.pterodactylus.sone.main + +import com.google.inject.Guice +import freenet.client.HighLevelSimpleClient +import freenet.clients.http.SessionManager +import freenet.node.Node +import freenet.pluginmanager.PluginRespirator +import net.pterodactylus.sone.test.deepMock +import net.pterodactylus.sone.test.getInstance +import net.pterodactylus.sone.test.mock +import net.pterodactylus.sone.test.whenever +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.sameInstance +import org.junit.Test +import org.mockito.Mockito.verify + +/** + * Unit test for [FreenetModule]. + */ +class FreenetModuleTest { + + private val sessionManager = mock() + private val pluginRespirator = deepMock().apply { + whenever(getSessionManager("Sone")).thenReturn(sessionManager) + } + private val node = pluginRespirator.node!! + private val highLevelSimpleClient = pluginRespirator.hlSimpleClient!! + private val module = FreenetModule(pluginRespirator) + private val injector = Guice.createInjector(module) + + private inline fun verifySingletonInstance() { + val firstInstance = injector.getInstance() + val secondInstance = injector.getInstance() + assertThat(firstInstance, sameInstance(secondInstance)) + } + + @Test + fun `plugin respirator is returned correctly`() { + assertThat(injector.getInstance(), sameInstance(pluginRespirator)) + } + + @Test + fun `plugin respirator is returned as singleton`() { + verifySingletonInstance() + } + + @Test + fun `node is returned correctly`() { + assertThat(injector.getInstance(), sameInstance(node)) + } + + @Test + fun `node is returned as singleton`() { + verifySingletonInstance() + } + + @Test + fun `high level simply client is returned correctly`() { + assertThat(injector.getInstance(), sameInstance(highLevelSimpleClient)) + } + + @Test + fun `high level simply client is returned as singleton`() { + verifySingletonInstance() + } + + @Test + fun `session manager is returned correctly`() { + assertThat(injector.getInstance(), sameInstance(sessionManager)) + } + + @Test + fun `session manager is returned as singleton`() { + verifySingletonInstance() + verify(pluginRespirator).getSessionManager("Sone") + } + +} -- 2.7.4