Add freenet module for Guice
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 8 Feb 2017 18:49:50 +0000 (19:49 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 8 Feb 2017 19:00:53 +0000 (20:00 +0100)
src/main/java/net/pterodactylus/sone/main/SonePlugin.java
src/main/kotlin/net/pterodactylus/sone/main/FreenetModule.kt [new file with mode: 0644]
src/test/kotlin/net/pterodactylus/sone/main/FreenetModuleTest.kt [new file with mode: 0644]

index e76c6ef..f394d44 100644 (file)
@@ -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 (file)
index 0000000..a8da595
--- /dev/null
@@ -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 (file)
index 0000000..d0dc7c4
--- /dev/null
@@ -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<SessionManager>()
+       private val pluginRespirator = deepMock<PluginRespirator>().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 <reified T: Any> verifySingletonInstance() {
+               val firstInstance = injector.getInstance<T>()
+               val secondInstance = injector.getInstance<T>()
+               assertThat(firstInstance, sameInstance(secondInstance))
+       }
+
+       @Test
+       fun `plugin respirator is returned correctly`() {
+               assertThat(injector.getInstance<PluginRespirator>(), sameInstance(pluginRespirator))
+       }
+
+       @Test
+       fun `plugin respirator is returned as singleton`() {
+               verifySingletonInstance<PluginRespirator>()
+       }
+
+       @Test
+       fun `node is returned correctly`() {
+               assertThat(injector.getInstance<Node>(), sameInstance(node))
+       }
+
+       @Test
+       fun `node is returned as singleton`() {
+               verifySingletonInstance<Node>()
+       }
+
+       @Test
+       fun `high level simply client is returned correctly`() {
+               assertThat(injector.getInstance<HighLevelSimpleClient>(), sameInstance(highLevelSimpleClient))
+       }
+
+       @Test
+       fun `high level simply client is returned as singleton`() {
+               verifySingletonInstance<HighLevelSimpleClient>()
+       }
+
+       @Test
+       fun `session manager is returned correctly`() {
+               assertThat(injector.getInstance<SessionManager>(), sameInstance(sessionManager))
+       }
+
+       @Test
+       fun `session manager is returned as singleton`() {
+               verifySingletonInstance<SessionManager>()
+               verify(pluginRespirator).getSessionManager("Sone")
+       }
+
+}