From: David ‘Bombe’ Roden Date: Fri, 11 Nov 2016 17:38:48 +0000 (+0100) Subject: Add test for guice creation of image loader X-Git-Tag: 0.9.7^2~444 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=c291432d933d11be8a3e13573aa3a09551ccf8f0;p=Sone.git Add test for guice creation of image loader --- diff --git a/src/main/kotlin/net/pterodactylus/sone/core/DefaultImageLoader.kt b/src/main/kotlin/net/pterodactylus/sone/core/DefaultImageLoader.kt index 469feb8..01e177a 100644 --- a/src/main/kotlin/net/pterodactylus/sone/core/DefaultImageLoader.kt +++ b/src/main/kotlin/net/pterodactylus/sone/core/DefaultImageLoader.kt @@ -4,11 +4,12 @@ import com.google.common.cache.CacheBuilder import freenet.keys.FreenetURI import java.io.ByteArrayInputStream import javax.imageio.ImageIO +import javax.inject.Inject /** * [ImageLoader] implementation that uses a simple Guava [com.google.common.cache.Cache]. */ -class DefaultImageLoader(private val freenetInterface: FreenetInterface) : ImageLoader { +class DefaultImageLoader @Inject constructor(private val freenetInterface: FreenetInterface) : ImageLoader { private val imageCache = CacheBuilder.newBuilder().build() private val callback = object : FreenetInterface.BackgroundFetchCallback { diff --git a/src/main/kotlin/net/pterodactylus/sone/core/ImageLoader.kt b/src/main/kotlin/net/pterodactylus/sone/core/ImageLoader.kt index 6d7fb36..5211015 100644 --- a/src/main/kotlin/net/pterodactylus/sone/core/ImageLoader.kt +++ b/src/main/kotlin/net/pterodactylus/sone/core/ImageLoader.kt @@ -1,8 +1,11 @@ package net.pterodactylus.sone.core +import com.google.inject.ImplementedBy + /** * Component that loads images and supplies information about them. */ +@ImplementedBy(DefaultImageLoader::class) interface ImageLoader { fun toLoadedImage(link: String): LoadedImage? diff --git a/src/test/kotlin/net/pterodactylus/sone/core/ImageLoaderTest.kt b/src/test/kotlin/net/pterodactylus/sone/core/ImageLoaderTest.kt new file mode 100644 index 0000000..d3a021c --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/core/ImageLoaderTest.kt @@ -0,0 +1,20 @@ +package net.pterodactylus.sone.core + +import com.google.inject.Guice.createInjector +import net.pterodactylus.sone.test.bindMock +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.notNullValue +import org.junit.Test + +/** + * Unit test for [ImageLoader]. + */ +class ImageLoaderTest { + + @Test + fun `default image loader can be loaded by guice`() { + val injector = createInjector(bindMock()) + assertThat(injector.getInstance(ImageLoader::class.java), notNullValue()); + } + +} diff --git a/src/test/kotlin/net/pterodactylus/sone/test/Mockotlin.kt b/src/test/kotlin/net/pterodactylus/sone/test/Mockotlin.kt index 5ce82df..8195021 100644 --- a/src/test/kotlin/net/pterodactylus/sone/test/Mockotlin.kt +++ b/src/test/kotlin/net/pterodactylus/sone/test/Mockotlin.kt @@ -1,7 +1,14 @@ package net.pterodactylus.sone.test +import com.google.inject.Module import org.mockito.ArgumentCaptor import org.mockito.Mockito inline fun mock(): T = Mockito.mock(T::class.java)!! inline fun capture(): ArgumentCaptor = ArgumentCaptor.forClass(T::class.java) + +inline fun bind(implementation: T): Module = + Module { it!!.bind(T::class.java).toInstance(implementation) } + +inline fun bindMock(): Module = + Module { it!!.bind(T::class.java).toInstance(mock()) }