Add test for DI constructability of ImageInserter
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 1 Feb 2018 19:33:16 +0000 (20:33 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 1 Feb 2018 19:33:44 +0000 (20:33 +0100)
src/main/java/net/pterodactylus/sone/core/Core.java
src/main/java/net/pterodactylus/sone/core/FreenetInterface.java
src/main/java/net/pterodactylus/sone/core/ImageInserter.java
src/test/java/net/pterodactylus/sone/core/FreenetInterfaceTest.java
src/test/kotlin/net/pterodactylus/sone/core/ImageInserterTest.kt

index 04f7181..6f819c3 100644 (file)
@@ -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;
index 946371b..7d1b007 100644 (file)
@@ -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<Image, InsertToken> {
+       public static class InsertTokenSupplier implements Function<Image, InsertToken> {
+
+               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);
                }
 
        }
index f47b713..cdab2ba 100644 (file)
@@ -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<Image, InsertToken> insertTokenSupplier) {
+       @Inject
+       public ImageInserter(FreenetInterface freenetInterface, InsertTokenSupplier insertTokenSupplier) {
                this.freenetInterface = freenetInterface;
                this.insertTokenSupplier = insertTokenSupplier;
        }
index 2ebc428..283d2e2 100644 (file)
@@ -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());
        }
 
index 0a34d26..39a11dc 100644 (file)
@@ -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<ImageInserter>(), notNullValue())
+       }
+
 }