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;
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() {
--- /dev/null
+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")!!
+
+}
--- /dev/null
+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")
+ }
+
+}