From 07643fdd510640d6c0b20963e4f4f36e06469fb5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 26 Aug 2022 12:15:00 +0200 Subject: [PATCH] =?utf8?q?=F0=9F=8E=A8=20Reduce=20dependency=20on=20Node?= =?utf8?q?=E2=80=99s=20fields?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Using fields is getting increasingly hard to mock with more recent Java versions. This change moves some of the necessary field access to the dependency injection configuration so it’s restricted to a single place in the source code and tests don’t have to mess with this. --- .../pterodactylus/sone/core/FreenetInterface.java | 25 ++++++++++--------- .../sone/freenet/HighLevelSimpleClientCreator.kt | 17 +++++++++++++ .../net/pterodactylus/sone/main/FreenetModule.kt | 20 ++++++++++++++++ .../sone/core/FreenetInterfaceTest.kt | 8 ++----- .../pterodactylus/sone/main/FreenetModuleTest.kt | 28 +++++++++++++++++++++- .../net/pterodactylus/sone/main/SonePluginTest.kt | 1 + .../net/pterodactylus/sone/test/FreenetObjects.kt | 8 +++++++ 7 files changed, 89 insertions(+), 18 deletions(-) create mode 100644 src/main/kotlin/net/pterodactylus/sone/freenet/HighLevelSimpleClientCreator.kt create mode 100644 src/test/kotlin/net/pterodactylus/sone/test/FreenetObjects.kt diff --git a/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java b/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java index e77badf..59e790c 100644 --- a/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java +++ b/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java @@ -66,6 +66,7 @@ import freenet.client.async.ClientPutCallback; import freenet.client.async.ClientPutter; import freenet.client.async.SnoopMetadata; import freenet.client.async.USKCallback; +import freenet.client.async.USKManager; import freenet.keys.FreenetURI; import freenet.keys.InsertableClientSSK; import freenet.keys.USK; @@ -93,6 +94,8 @@ public class FreenetInterface { /** The node to interact with. */ private final Node node; + private final USKManager uskManager; + private final ClientContext clientContext; private final SoneUriCreator soneUriCreator; @@ -110,11 +113,13 @@ public class FreenetInterface { private final RequestClient imageLoader = new RequestClientBuilder().realTime().build(); @Inject - public FreenetInterface(EventBus eventBus, Node node, SoneUriCreator soneUriCreator) { + public FreenetInterface(EventBus eventBus, Node node, USKManager uskManager, ClientContext clientContext, SoneUriCreator soneUriCreator, HighLevelSimpleClientCreator highLevelSimpleClientCreator) { this.eventBus = eventBus; this.node = node; + this.uskManager = uskManager; + this.clientContext = clientContext; this.soneUriCreator = soneUriCreator; - this.client = node.clientCore.makeClient(RequestStarter.INTERACTIVE_PRIORITY_CLASS, false, true); + this.client = highLevelSimpleClientCreator.makeClient(RequestStarter.INTERACTIVE_PRIORITY_CLASS, false, true); } // @@ -186,7 +191,7 @@ public class FreenetInterface { try { ClientGetter clientGetter = client.fetch(uri, 2097152, callback, fetchContext, RequestStarter.INTERACTIVE_PRIORITY_CLASS); clientGetter.setMetaSnoop(snoop); - clientGetter.restart(uri, fetchContext.filterData, node.clientCore.clientContext); + clientGetter.restart(uri, fetchContext.filterData, clientContext); } catch (FetchException fe) { /* stupid exception that can not actually be thrown! */ } @@ -254,7 +259,7 @@ public class FreenetInterface { USKCallback uskCallback) { try { soneUskCallbacks.put(FreenetURIsKt.getRoutingKeyString(requestUri), uskCallback); - node.clientCore.uskManager.subscribe(create(requestUri), + uskManager.subscribe(create(requestUri), uskCallback, true, requestClient); } catch (MalformedURLException mue1) { logger.log(WARNING, format("Could not subscribe USK “%s”!", @@ -266,9 +271,7 @@ public class FreenetInterface { USKCallback uskCallback) { try { soneUskCallbacks.put(FreenetURIsKt.getRoutingKeyString(requestUri), uskCallback); - node.clientCore - .uskManager - .subscribe(create(requestUri), uskCallback, false, requestClient); + uskManager.subscribe(create(requestUri), uskCallback, false, requestClient); } catch (MalformedURLException mue1) { logger.log(WARNING, format("Could not subscribe USK “%s”!", requestUri), @@ -291,7 +294,7 @@ public class FreenetInterface { logger.log(Level.FINEST, String.format("USKs left: %d", soneUskCallbacks.size())); uskCallbacks.forEach(uskCallback -> { try { - node.clientCore.uskManager.unsubscribe(USK.create(soneUriCreator.getRequestUri(sone)), uskCallback); + uskManager.unsubscribe(USK.create(soneUriCreator.getRequestUri(sone)), uskCallback); } catch (MalformedURLException mue1) { logger.log(Level.FINE, String.format("Could not unsubscribe USK “%s”!", soneUriCreator.getRequestUri(sone)), mue1); } @@ -327,7 +330,7 @@ public class FreenetInterface { }; try { - node.clientCore.uskManager.subscribe(USK.create(uri), uskCallback, true, requestClient); + uskManager.subscribe(USK.create(uri), uskCallback, true, requestClient); uriUskCallbacks.put(USK.create(uri).clearCopy().getURI(), uskCallback); } catch (MalformedURLException mue1) { logger.log(Level.WARNING, String.format("Could not subscribe to USK: %s", uri), mue1); @@ -347,7 +350,7 @@ public class FreenetInterface { logger.log(Level.INFO, String.format("Could not unregister unknown USK: %s", uri)); return; } - node.clientCore.uskManager.unsubscribe(USK.create(uri), uskCallback); + uskManager.unsubscribe(USK.create(uri), uskCallback); } catch (MalformedURLException mue1) { logger.log(Level.INFO, String.format("Could not unregister invalid USK: %s", uri), mue1); } @@ -435,7 +438,7 @@ public class FreenetInterface { */ @SuppressWarnings("synthetic-access") public void cancel() { - clientPutter.cancel(node.clientCore.clientContext); + clientPutter.cancel(clientContext); eventBus.post(new ImageInsertAbortedEvent(image)); bucket.free(); } diff --git a/src/main/kotlin/net/pterodactylus/sone/freenet/HighLevelSimpleClientCreator.kt b/src/main/kotlin/net/pterodactylus/sone/freenet/HighLevelSimpleClientCreator.kt new file mode 100644 index 0000000..0874838 --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/freenet/HighLevelSimpleClientCreator.kt @@ -0,0 +1,17 @@ +package net.pterodactylus.sone.freenet + +import freenet.client.HighLevelSimpleClient +import freenet.node.NodeClientCore + +fun interface HighLevelSimpleClientCreator { + + fun makeClient(prioClass: Short, forceDontIgnoreTooManyPathComponents: Boolean, realTimeFlag: Boolean): HighLevelSimpleClient + +} + +class DefaultHighLevelSimpleClientCreator(private val nodeClientCore: NodeClientCore) : HighLevelSimpleClientCreator { + + override fun makeClient(prioClass: Short, forceDontIgnoreTooManyPathComponents: Boolean, realTimeFlag: Boolean): HighLevelSimpleClient = + nodeClientCore.makeClient(prioClass, forceDontIgnoreTooManyPathComponents, realTimeFlag) + +} diff --git a/src/main/kotlin/net/pterodactylus/sone/main/FreenetModule.kt b/src/main/kotlin/net/pterodactylus/sone/main/FreenetModule.kt index 1438c2a..691ae4e 100644 --- a/src/main/kotlin/net/pterodactylus/sone/main/FreenetModule.kt +++ b/src/main/kotlin/net/pterodactylus/sone/main/FreenetModule.kt @@ -2,9 +2,13 @@ package net.pterodactylus.sone.main import com.google.inject.* import freenet.client.* +import freenet.client.async.ClientContext +import freenet.client.async.USKManager import freenet.clients.http.* import freenet.node.* import freenet.pluginmanager.* +import net.pterodactylus.sone.freenet.DefaultHighLevelSimpleClientCreator +import net.pterodactylus.sone.freenet.HighLevelSimpleClientCreator import net.pterodactylus.sone.freenet.plugin.* import javax.inject.Provider import javax.inject.Singleton @@ -27,4 +31,20 @@ class FreenetModule(private val pluginRespirator: PluginRespirator) : Module { @Singleton fun getSessionManager() = pluginRespirator.getSessionManager("Sone")!! + @Provides + fun getNodeClientCore(node: Node): NodeClientCore = + node.clientCore + + @Provides + fun getHighLevelSimpleClientCreator(nodeClientCore: NodeClientCore): HighLevelSimpleClientCreator = + DefaultHighLevelSimpleClientCreator(nodeClientCore) + + @Provides + fun getClientContext(nodeClientCore: NodeClientCore): ClientContext = + nodeClientCore.clientContext + + @Provides + fun getUskManager(nodeClientCore: NodeClientCore): USKManager = + nodeClientCore.uskManager + } diff --git a/src/test/kotlin/net/pterodactylus/sone/core/FreenetInterfaceTest.kt b/src/test/kotlin/net/pterodactylus/sone/core/FreenetInterfaceTest.kt index 9cbaff0..ffd5c60 100644 --- a/src/test/kotlin/net/pterodactylus/sone/core/FreenetInterfaceTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/core/FreenetInterfaceTest.kt @@ -51,7 +51,7 @@ class FreenetInterfaceTest { @Suppress("UnstableApiUsage") private val eventBus = mock() private val node = mock() - private val nodeClientCore = mock() + private val clientContext = mock() private val highLevelSimpleClient: HighLevelSimpleClient = mock(HighLevelSimpleClient::class.java, withSettings().extraInterfaces(RequestClient::class.java)) private val randomSource = DummyRandomSource() private val uskManager = mock() @@ -69,12 +69,8 @@ class FreenetInterfaceTest { private val freenetInterface: FreenetInterface init { - whenever(nodeClientCore.makeClient(anyShort(), anyBoolean(), anyBoolean())).thenReturn(highLevelSimpleClient) - setField(node, "clientCore", nodeClientCore) setField(node, "random", randomSource) - setField(nodeClientCore, "uskManager", uskManager) - setField(nodeClientCore, "clientContext", mock()) - freenetInterface = FreenetInterface(eventBus, node, soneUriCreator) + freenetInterface = FreenetInterface(eventBus, node, uskManager, clientContext, soneUriCreator) { _, _, _ -> highLevelSimpleClient } insertToken = freenetInterface.InsertToken(image) insertToken.setBucket(bucket) } diff --git a/src/test/kotlin/net/pterodactylus/sone/main/FreenetModuleTest.kt b/src/test/kotlin/net/pterodactylus/sone/main/FreenetModuleTest.kt index c6aed88..84cb567 100644 --- a/src/test/kotlin/net/pterodactylus/sone/main/FreenetModuleTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/main/FreenetModuleTest.kt @@ -2,9 +2,12 @@ package net.pterodactylus.sone.main import com.google.inject.* import freenet.client.* +import freenet.client.async.ClientContext +import freenet.client.async.USKManager import freenet.clients.http.* import freenet.node.* import freenet.pluginmanager.* +import net.pterodactylus.sone.freenet.HighLevelSimpleClientCreator import net.pterodactylus.sone.freenet.plugin.* import net.pterodactylus.sone.test.* import org.hamcrest.MatcherAssert.* @@ -24,10 +27,18 @@ class FreenetModuleTest { val expectedException = ExpectedException.none()!! private val sessionManager = mock() + private val uskManager = mock() + private val clientContext = mock() private val pluginRespirator = deepMock().apply { whenever(getSessionManager("Sone")).thenReturn(sessionManager) } - private val node = pluginRespirator.node!! + private val nodeClientCore = mock().also { + setField(it, "uskManager", uskManager) + setField(it, "clientContext", clientContext) + } + private val node = pluginRespirator.node!!.also { + setField(it, "clientCore", nodeClientCore) + } private val highLevelSimpleClient = pluginRespirator.hlSimpleClient!! private val toadletContainer: ToadletContainer = pluginRespirator.toadletContainer private val pageMaker: PageMaker = pluginRespirator.pageMaker @@ -113,4 +124,19 @@ class FreenetModuleTest { injector.verifySingletonInstance() } + @Test + fun `high level simple client creator is returned correctly`() { + assertThat(injector.getInstance(), notNullValue()); + } + + @Test + fun `client context is returned correctly`() { + assertThat(injector.getInstance(), sameInstance(clientContext)) + } + + @Test + fun `usk manager is returned correctly`() { + assertThat(injector.getInstance(), sameInstance(uskManager)) + } + } diff --git a/src/test/kotlin/net/pterodactylus/sone/main/SonePluginTest.kt b/src/test/kotlin/net/pterodactylus/sone/main/SonePluginTest.kt index a656702..3983aa2 100644 --- a/src/test/kotlin/net/pterodactylus/sone/main/SonePluginTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/main/SonePluginTest.kt @@ -38,6 +38,7 @@ class SonePluginTest { setField(node, "clientCore", clientCore) whenever(pluginRespirator.node).thenReturn(node) setField(clientCore, "uskManager", uskManager) + setField(clientCore, "clientContext", mock()) } @Test diff --git a/src/test/kotlin/net/pterodactylus/sone/test/FreenetObjects.kt b/src/test/kotlin/net/pterodactylus/sone/test/FreenetObjects.kt new file mode 100644 index 0000000..9c14658 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/test/FreenetObjects.kt @@ -0,0 +1,8 @@ +package net.pterodactylus.sone.test + +import freenet.client.HighLevelSimpleClient +import net.pterodactylus.sone.freenet.HighLevelSimpleClientCreator + +val dummyHighLevelSimpleClient = deepMock() + +val dummyHighLevelSimpleClientCreator: HighLevelSimpleClientCreator = HighLevelSimpleClientCreator { _, _, _ -> dummyHighLevelSimpleClient } -- 2.7.4