From 60d34bd23ab1c8f86a8246ead6136bbbfd44bcb0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 1 Feb 2018 19:45:37 +0100 Subject: [PATCH] Replace image inserter test with Kotlin version --- .../pterodactylus/sone/core/ImageInserterTest.java | 58 ---------------------- .../pterodactylus/sone/core/ImageInserterTest.kt | 54 ++++++++++++++++++++ 2 files changed, 54 insertions(+), 58 deletions(-) delete mode 100644 src/test/java/net/pterodactylus/sone/core/ImageInserterTest.java create mode 100644 src/test/kotlin/net/pterodactylus/sone/core/ImageInserterTest.kt diff --git a/src/test/java/net/pterodactylus/sone/core/ImageInserterTest.java b/src/test/java/net/pterodactylus/sone/core/ImageInserterTest.java deleted file mode 100644 index 75788d1..0000000 --- a/src/test/java/net/pterodactylus/sone/core/ImageInserterTest.java +++ /dev/null @@ -1,58 +0,0 @@ -package net.pterodactylus.sone.core; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.doThrow; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import net.pterodactylus.sone.core.FreenetInterface.InsertToken; -import net.pterodactylus.sone.data.Image; -import net.pterodactylus.sone.data.TemporaryImage; - -import com.google.common.base.Function; -import org.junit.Test; - -/** - * Unit test for {@link ImageInserter}. - * - * @author David ‘Bombe’ Roden - */ -public class ImageInserterTest { - - private final TemporaryImage temporaryImage = when(mock(TemporaryImage.class).getId()).thenReturn("image-id").getMock(); - private final Image image = when(mock(Image.class).getId()).thenReturn("image-id").getMock(); - private final FreenetInterface freenetInterface = mock(FreenetInterface.class); - private final InsertToken insertToken = mock(InsertToken.class); - private final Function insertTokenSupplier = when(mock(Function.class).apply(any(Image.class))).thenReturn(insertToken).getMock(); - private final ImageInserter imageInserter = new ImageInserter(freenetInterface, insertTokenSupplier); - - @Test - public void inserterInsertsImage() throws SoneException { - imageInserter.insertImage(temporaryImage, image); - verify(freenetInterface).insertImage(eq(temporaryImage), eq(image), any(InsertToken.class)); - } - - @Test - public void exceptionWhenInsertingImageIsIgnored() throws SoneException { - doThrow(SoneException.class).when(freenetInterface).insertImage(eq(temporaryImage), eq(image), any(InsertToken.class)); - imageInserter.insertImage(temporaryImage, image); - verify(freenetInterface).insertImage(eq(temporaryImage), eq(image), any(InsertToken.class)); - } - - @Test - public void cancellingImageInsertThatIsNotRunningDoesNothing() { - imageInserter.cancelImageInsert(image); - verify(insertToken, never()).cancel(); - } - - @Test - public void cancellingImage() { - imageInserter.insertImage(temporaryImage, image); - imageInserter.cancelImageInsert(image); - verify(insertToken).cancel(); - } - -} diff --git a/src/test/kotlin/net/pterodactylus/sone/core/ImageInserterTest.kt b/src/test/kotlin/net/pterodactylus/sone/core/ImageInserterTest.kt new file mode 100644 index 0000000..0a34d26 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/core/ImageInserterTest.kt @@ -0,0 +1,54 @@ +package net.pterodactylus.sone.core + +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.mock +import net.pterodactylus.sone.test.whenever +import org.junit.Test +import org.mockito.ArgumentMatchers.any +import org.mockito.ArgumentMatchers.eq +import org.mockito.Mockito.doThrow +import org.mockito.Mockito.never +import org.mockito.Mockito.verify + +/** + * Unit test for [ImageInserter]. + */ +class ImageInserterTest { + + private val temporaryImage = mock().apply { whenever(id).thenReturn("image-id") } + private val image = mock().apply { whenever(id).thenReturn("image-id") } + private val freenetInterface = mock() + private val insertToken = mock() + private val insertTokenSupplier: InsertTokenSupplier = mock().apply { whenever(apply(any())).thenReturn(insertToken) } + private val imageInserter = ImageInserter(freenetInterface, insertTokenSupplier) + + @Test + fun `inserter inserts image`() { + imageInserter.insertImage(temporaryImage, image) + verify(freenetInterface).insertImage(eq(temporaryImage), eq(image), any(InsertToken::class.java)) + } + + @Test + fun `exception when inserting image is ignored`() { + doThrow(SoneException::class.java).`when`(freenetInterface).insertImage(eq(temporaryImage), eq(image), any(InsertToken::class.java)) + imageInserter.insertImage(temporaryImage, image) + verify(freenetInterface).insertImage(eq(temporaryImage), eq(image), any(InsertToken::class.java)) + } + + @Test + fun `cancelling image insert that is not running does nothing`() { + imageInserter.cancelImageInsert(image) + verify(insertToken, never()).cancel() + } + + @Test + fun `cancelling image cancels the insert token`() { + imageInserter.insertImage(temporaryImage, image) + imageInserter.cancelImageInsert(image) + verify(insertToken).cancel() + } + +} -- 2.7.4