From: David ‘Bombe’ Roden Date: Thu, 1 Feb 2018 19:33:16 +0000 (+0100) Subject: Add test for DI constructability of ImageInserter X-Git-Tag: v79^2~149 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=87630ec07d4de0e516133d29a3135f81716e5383;p=Sone.git Add test for DI constructability of ImageInserter --- diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index 04f7181..6f819c3 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -208,22 +208,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, * The database */ @Inject - public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager, SoneDownloader soneDownloader, UpdateChecker updateChecker, WebOfTrustUpdater webOfTrustUpdater, EventBus eventBus, Database database) { - super("Sone Core"); - this.configuration = configuration; - this.freenetInterface = freenetInterface; - this.identityManager = identityManager; - this.soneDownloader = soneDownloader; - this.imageInserter = new ImageInserter(freenetInterface, freenetInterface.new InsertTokenSupplier()); - this.updateChecker = updateChecker; - this.webOfTrustUpdater = webOfTrustUpdater; - this.eventBus = eventBus; - this.database = database; - preferences = new Preferences(eventBus); - } - - @VisibleForTesting - protected Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager, SoneDownloader soneDownloader, ImageInserter imageInserter, UpdateChecker updateChecker, WebOfTrustUpdater webOfTrustUpdater, EventBus eventBus, Database database) { + public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager, SoneDownloader soneDownloader, ImageInserter imageInserter, UpdateChecker updateChecker, WebOfTrustUpdater webOfTrustUpdater, EventBus eventBus, Database database) { super("Sone Core"); this.configuration = configuration; this.freenetInterface = freenetInterface; diff --git a/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java b/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java index 946371b..7d1b007 100644 --- a/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java +++ b/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java @@ -32,6 +32,7 @@ import java.util.logging.Level; import java.util.logging.Logger; import javax.annotation.Nonnull; +import javax.inject.Inject; import net.pterodactylus.sone.core.event.ImageInsertAbortedEvent; import net.pterodactylus.sone.core.event.ImageInsertFailedEvent; @@ -43,7 +44,6 @@ import net.pterodactylus.sone.data.TemporaryImage; import com.google.common.base.Function; import com.google.common.eventbus.EventBus; -import com.google.inject.Inject; import com.google.inject.Singleton; import freenet.client.ClientMetadata; @@ -558,11 +558,18 @@ public class FreenetInterface { } - public class InsertTokenSupplier implements Function { + public static class InsertTokenSupplier implements Function { + + private final FreenetInterface freenetInterface; + + @Inject + public InsertTokenSupplier(FreenetInterface freenetInterface) { + this.freenetInterface = freenetInterface; + } @Override public InsertToken apply(Image image) { - return new InsertToken(image); + return freenetInterface.new InsertToken(image); } } diff --git a/src/main/java/net/pterodactylus/sone/core/ImageInserter.java b/src/main/java/net/pterodactylus/sone/core/ImageInserter.java index f47b713..cdab2ba 100644 --- a/src/main/java/net/pterodactylus/sone/core/ImageInserter.java +++ b/src/main/java/net/pterodactylus/sone/core/ImageInserter.java @@ -27,7 +27,10 @@ import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; +import javax.inject.Inject; + import net.pterodactylus.sone.core.FreenetInterface.InsertToken; +import net.pterodactylus.sone.core.FreenetInterface.InsertTokenSupplier; import net.pterodactylus.sone.data.Image; import net.pterodactylus.sone.data.TemporaryImage; @@ -61,7 +64,8 @@ public class ImageInserter { * @param insertTokenSupplier * The supplier for insert tokens */ - public ImageInserter(FreenetInterface freenetInterface, Function insertTokenSupplier) { + @Inject + public ImageInserter(FreenetInterface freenetInterface, InsertTokenSupplier insertTokenSupplier) { this.freenetInterface = freenetInterface; this.insertTokenSupplier = insertTokenSupplier; } diff --git a/src/test/java/net/pterodactylus/sone/core/FreenetInterfaceTest.java b/src/test/java/net/pterodactylus/sone/core/FreenetInterfaceTest.java index 2ebc428..283d2e2 100644 --- a/src/test/java/net/pterodactylus/sone/core/FreenetInterfaceTest.java +++ b/src/test/java/net/pterodactylus/sone/core/FreenetInterfaceTest.java @@ -420,7 +420,7 @@ public class FreenetInterfaceTest { @Test public void insertTokenSupplierSuppliesInsertTokens() { - InsertTokenSupplier insertTokenSupplier = freenetInterface.new InsertTokenSupplier(); + InsertTokenSupplier insertTokenSupplier = new InsertTokenSupplier(freenetInterface); assertThat(insertTokenSupplier.apply(image), notNullValue()); } diff --git a/src/test/kotlin/net/pterodactylus/sone/core/ImageInserterTest.kt b/src/test/kotlin/net/pterodactylus/sone/core/ImageInserterTest.kt index 0a34d26..39a11dc 100644 --- a/src/test/kotlin/net/pterodactylus/sone/core/ImageInserterTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/core/ImageInserterTest.kt @@ -4,8 +4,12 @@ import net.pterodactylus.sone.core.FreenetInterface.InsertToken import net.pterodactylus.sone.core.FreenetInterface.InsertTokenSupplier import net.pterodactylus.sone.data.Image import net.pterodactylus.sone.data.TemporaryImage +import net.pterodactylus.sone.test.getInstance import net.pterodactylus.sone.test.mock import net.pterodactylus.sone.test.whenever +import net.pterodactylus.sone.web.baseInjector +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.notNullValue import org.junit.Test import org.mockito.ArgumentMatchers.any import org.mockito.ArgumentMatchers.eq @@ -51,4 +55,9 @@ class ImageInserterTest { verify(insertToken).cancel() } + @Test + fun `image inserter can be created by dependency injection`() { + assertThat(baseInjector.getInstance(), notNullValue()) + } + }