import com.google.common.io.ByteStreams
import freenet.keys.FreenetURI
import net.pterodactylus.sone.core.FreenetInterface.BackgroundFetchCallback
-import net.pterodactylus.sone.test.capture
-import net.pterodactylus.sone.test.mock
+import net.pterodactylus.sone.test.*
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.equalTo
-import org.hamcrest.Matchers.equalTo
import org.junit.Test
import org.mockito.ArgumentMatchers.any
import org.mockito.ArgumentMatchers.eq
-import org.mockito.Mockito.`when`
import org.mockito.Mockito.times
import org.mockito.Mockito.verify
import java.io.ByteArrayOutputStream
elementLoader.loadElement(IMAGE_ID)
verify(freenetInterface).startFetch(eq(freenetURI), callback.capture())
callback.value.failed(freenetURI)
- `when`(ticker.read()).thenReturn(TimeUnit.MINUTES.toNanos(31))
+ whenever(ticker.read()).thenReturn(TimeUnit.MINUTES.toNanos(31))
val linkedElement = elementLoader.loadElement(IMAGE_ID)
assertThat(linkedElement.failed, equalTo(false))
assertThat(linkedElement.loading, equalTo(true))
@Before
fun setupCallbackCaptorAndUskManager() {
- doNothing().`when`(uskManager).subscribe(any(USK::class.java), callbackCaptor.capture(), anyBoolean(), any(RequestClient::class.java))
+ doNothing().whenever(uskManager).subscribe(any(USK::class.java), callbackCaptor.capture(), anyBoolean(), any(RequestClient::class.java))
}
@Test
@Test
fun `exception when inserting image is ignored`() {
- doThrow(SoneException::class.java).`when`(freenetInterface).insertImage(eq(temporaryImage), eq(image), any(InsertToken::class.java))
+ doThrow(SoneException::class.java).whenever(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))
}
doAnswer {
soneInserter.stop()
null
- }.`when`(core).touchConfiguration()
+ }.whenever(core).touchConfiguration()
soneInserter.serviceRun()
val soneEvents = ArgumentCaptor.forClass(SoneEvent::class.java)
verify(freenetInterface).insertDirectory(eq(insertUri), any<HashMap<String, Any>>(), eq("index.html"))
doAnswer {
soneInserter.stop()
null
- }.`when`(core).touchConfiguration()
+ }.whenever(core).touchConfiguration()
soneInserter.serviceRun()
val histogram = metricRegistry.histogram("sone.insert.duration")
assertThat(histogram.count, equalTo(1L))
doAnswer {
soneRescuer.stop()
fetchedSone
- }.`when`(soneDownloader).fetchSone(eq(sone), eq(keyWithMetaStrings), eq(true))
+ }.whenever(soneDownloader).fetchSone(eq(sone), eq(keyWithMetaStrings), eq(true))
}
private fun setupFreenetUri(): FreenetURI {
import net.pterodactylus.sone.data.SoneOptions.LoadExternalContent.TRUSTED
import net.pterodactylus.sone.freenet.wot.OwnIdentity
import net.pterodactylus.sone.freenet.wot.Trust
-import net.pterodactylus.sone.test.mock
+import net.pterodactylus.sone.test.*
import net.pterodactylus.sone.text.FreenetLinkPart
import net.pterodactylus.sone.text.LinkPart
import net.pterodactylus.sone.text.Part
import org.hamcrest.Matchers.emptyIterable
import org.junit.Before
import org.junit.Test
-import org.mockito.Mockito.`when`
/**
* Unit test for [LinkedElementsFilter].
@Before
fun setupSone() {
- `when`(sone.options).thenReturn(DefaultSoneOptions())
+ whenever(sone.options).thenReturn(DefaultSoneOptions())
}
@Before
fun setupImageLoader() {
- `when`(imageLoader.loadElement("KSK@link")).thenReturn(LinkedElement("KSK@link", failed = true))
- `when`(imageLoader.loadElement("KSK@loading.png")).thenReturn(LinkedElement("KSK@loading.png", loading = true))
- `when`(imageLoader.loadElement("KSK@link.png")).thenReturn(LinkedElement("KSK@link.png"))
+ whenever(imageLoader.loadElement("KSK@link")).thenReturn(LinkedElement("KSK@link", failed = true))
+ whenever(imageLoader.loadElement("KSK@loading.png")).thenReturn(LinkedElement("KSK@loading.png", loading = true))
+ whenever(imageLoader.loadElement("KSK@link.png")).thenReturn(LinkedElement("KSK@link.png"))
}
@Test
fun `filter finds images if the remote sone is local`() {
sone.options.loadLinkedImages = MANUALLY_TRUSTED
templateContext.set("currentSone", sone)
- `when`(remoteSone.isLocal).thenReturn(true)
+ whenever(remoteSone.isLocal).thenReturn(true)
parameters["sone"] = remoteSone
verifyThatImagesArePresent()
}
fun `filter does not find images if local sone requires manual trust and remote sone has only implicit trust`() {
sone.options.loadLinkedImages = MANUALLY_TRUSTED
templateContext.set("currentSone", sone)
- `when`(remoteSone.identity.getTrust(this.sone.identity as OwnIdentity)).thenReturn(Trust(null, 100, null))
+ whenever(remoteSone.identity.getTrust(this.sone.identity as OwnIdentity)).thenReturn(Trust(null, 100, null))
parameters["sone"] = remoteSone
verifyThatImagesAreNotPresent()
}
fun `filter does not find images if local sone requires manual trust and remote sone has explicit trust of zero`() {
sone.options.loadLinkedImages = MANUALLY_TRUSTED
templateContext.set("currentSone", sone)
- `when`(remoteSone.identity.getTrust(this.sone.identity as OwnIdentity)).thenReturn(Trust(0, null, null))
+ whenever(remoteSone.identity.getTrust(this.sone.identity as OwnIdentity)).thenReturn(Trust(0, null, null))
parameters["sone"] = remoteSone
verifyThatImagesAreNotPresent()
}
fun `filter finds images if local sone requires manual trust and remote sone has explicit trust of one`() {
sone.options.loadLinkedImages = MANUALLY_TRUSTED
templateContext.set("currentSone", sone)
- `when`(remoteSone.identity.getTrust(this.sone.identity as OwnIdentity)).thenReturn(Trust(1, null, null))
+ whenever(remoteSone.identity.getTrust(this.sone.identity as OwnIdentity)).thenReturn(Trust(1, null, null))
parameters["sone"] = remoteSone
verifyThatImagesArePresent()
}
@Test
fun `filter finds images if local sone requires following and remote sone is followed`() {
sone.options.loadLinkedImages = FOLLOWED
- `when`(sone.hasFriend("remote-id")).thenReturn(true)
+ whenever(sone.hasFriend("remote-id")).thenReturn(true)
templateContext["currentSone"] = sone
parameters["sone"] = remoteSone
verifyThatImagesArePresent()
fun `filter finds images if following is required and remote sone is a local sone`() {
sone.options.loadLinkedImages = FOLLOWED
templateContext["currentSone"] = sone
- `when`(remoteSone.isLocal).thenReturn(true)
+ whenever(remoteSone.isLocal).thenReturn(true)
parameters["sone"] = remoteSone
verifyThatImagesArePresent()
}
fun `filter does not find images if any trust is required and remote sone has implicit trust of zero`() {
sone.options.loadLinkedImages = TRUSTED
templateContext["currentSone"] = sone
- `when`(remoteSone.identity.getTrust(sone.identity as OwnIdentity)).thenReturn(Trust(null, 0, null))
+ whenever(remoteSone.identity.getTrust(sone.identity as OwnIdentity)).thenReturn(Trust(null, 0, null))
parameters["sone"] = remoteSone
verifyThatImagesAreNotPresent()
}
fun `filter finds images if any trust is required and remote sone has implicit trust of one`() {
sone.options.loadLinkedImages = TRUSTED
templateContext["currentSone"] = sone
- `when`(remoteSone.identity.getTrust(sone.identity as OwnIdentity)).thenReturn(Trust(null, 1, null))
+ whenever(remoteSone.identity.getTrust(sone.identity as OwnIdentity)).thenReturn(Trust(null, 1, null))
parameters["sone"] = remoteSone
verifyThatImagesArePresent()
}
fun `filter does not find images if any trust is required and remote sone has explicit trust of zero but implicit trust of one`() {
sone.options.loadLinkedImages = TRUSTED
templateContext["currentSone"] = sone
- `when`(remoteSone.identity.getTrust(sone.identity as OwnIdentity)).thenReturn(Trust(0, 1, null))
+ whenever(remoteSone.identity.getTrust(sone.identity as OwnIdentity)).thenReturn(Trust(0, 1, null))
parameters["sone"] = remoteSone
verifyThatImagesAreNotPresent()
}
fun `filter finds images if any trust is required and remote sone has explicit trust of one but no implicit trust`() {
sone.options.loadLinkedImages = TRUSTED
templateContext["currentSone"] = sone
- `when`(remoteSone.identity.getTrust(sone.identity as OwnIdentity)).thenReturn(Trust(1, null, null))
+ whenever(remoteSone.identity.getTrust(sone.identity as OwnIdentity)).thenReturn(Trust(1, null, null))
parameters["sone"] = remoteSone
verifyThatImagesArePresent()
}
fun `filter finds images if any trust is required and remote sone is a local sone`() {
sone.options.loadLinkedImages = TRUSTED
templateContext["currentSone"] = sone
- `when`(remoteSone.isLocal).thenReturn(true)
+ whenever(remoteSone.isLocal).thenReturn(true)
parameters["sone"] = remoteSone
verifyThatImagesArePresent()
}
private fun createSone(id: String = "sone-id"): Sone {
val sone = mock<Sone>()
- `when`(sone.id).thenReturn(id)
- `when`(sone.options).thenReturn(DefaultSoneOptions())
- `when`(sone.identity).thenReturn(mock<OwnIdentity>())
+ whenever(sone.id).thenReturn(id)
+ whenever(sone.options).thenReturn(DefaultSoneOptions())
+ whenever(sone.identity).thenReturn(mock<OwnIdentity>())
return sone
}
import com.google.inject.Guice
import net.pterodactylus.sone.core.Core
import net.pterodactylus.sone.data.Sone
-import net.pterodactylus.sone.test.getInstance
-import net.pterodactylus.sone.test.isProvidedByMock
-import net.pterodactylus.sone.test.mock
+import net.pterodactylus.sone.test.*
import net.pterodactylus.sone.text.SoneTextParser
import net.pterodactylus.sone.text.SoneTextParserContext
import net.pterodactylus.util.template.TemplateContext
import org.hamcrest.MatcherAssert.assertThat
-import org.hamcrest.Matchers.`is`
+import org.hamcrest.Matchers.equalTo
import org.hamcrest.Matchers.emptyIterable
import org.hamcrest.Matchers.notNullValue
import org.hamcrest.Matchers.sameInstance
import org.junit.Test
import org.mockito.ArgumentCaptor.forClass
-import org.mockito.Mockito.`when`
import org.mockito.Mockito.eq
import org.mockito.Mockito.verify
private fun setupSone(identity: String): Sone {
val sone = mock<Sone>()
- `when`(sone.id).thenReturn(identity)
- `when`(core.getSone(identity)).thenReturn(sone)
+ whenever(sone.id).thenReturn(identity)
+ whenever(core.getSone(identity)).thenReturn(sone)
return sone
}
filter.format(templateContext, "text", parameters)
val context = forClass(SoneTextParserContext::class.java)
verify(soneTextParser).parse(eq<String>("text") ?: "", context.capture())
- assertThat(context.value.postingSone, `is`(sone))
+ assertThat(context.value.postingSone, equalTo(sone))
}
@Test
import net.pterodactylus.sone.test.whenever
import net.pterodactylus.util.template.TemplateContext
import org.hamcrest.MatcherAssert.assertThat
-import org.hamcrest.Matchers.`is`
+import org.hamcrest.Matchers.equalTo
import org.hamcrest.Matchers.nullValue
import org.junit.Before
import org.junit.Test
@Test
fun `avatar ID is returned if profile belongs to local sone`() {
whenever(remoteSone.isLocal).thenReturn(true)
- assertThat(accessor.get(templateContext, profile, "avatar"), `is`<Any>("avatar-id"))
+ assertThat(accessor.get(templateContext, profile, "avatar"), equalTo<Any>("avatar-id"))
}
@Test
@Test
fun `avatar ID is returned if sone is configure to always show avatars`() {
currentSone.options.showCustomAvatars = ALWAYS
- assertThat(accessor.get(templateContext, profile, "avatar"), `is`<Any>("avatar-id"))
+ assertThat(accessor.get(templateContext, profile, "avatar"), equalTo<Any>("avatar-id"))
}
@Test
fun `avatar ID is returned if sone is configure to show avatars of followed sones and remote sone is followed`() {
currentSone.options.showCustomAvatars = FOLLOWED
whenever(currentSone.hasFriend("remote-sone")).thenReturn(true)
- assertThat(accessor.get(templateContext, profile, "avatar"), `is`<Any>("avatar-id"))
+ assertThat(accessor.get(templateContext, profile, "avatar"), equalTo<Any>("avatar-id"))
}
@Test
fun `avatar ID is returned if sone is configure to show avatars based on manual trust and explicit trust is one`() {
currentSone.options.showCustomAvatars = MANUALLY_TRUSTED
setTrust(Trust(1, null, null))
- assertThat(accessor.get(templateContext, profile, "avatar"), `is`<Any>("avatar-id"))
+ assertThat(accessor.get(templateContext, profile, "avatar"), equalTo<Any>("avatar-id"))
}
@Test
fun `avatar ID is returned if sone is configure to show avatars based on trust and explicit trust is one`() {
currentSone.options.showCustomAvatars = TRUSTED
setTrust(Trust(1, null, null))
- assertThat(accessor.get(templateContext, profile, "avatar"), `is`<Any>("avatar-id"))
+ assertThat(accessor.get(templateContext, profile, "avatar"), equalTo<Any>("avatar-id"))
}
@Test
fun `avatar ID is returned if sone is configure to show avatars based on trust and implicit trust is one`() {
currentSone.options.showCustomAvatars = TRUSTED
setTrust(Trust(0, 1, null))
- assertThat(accessor.get(templateContext, profile, "avatar"), `is`<Any>("avatar-id"))
+ assertThat(accessor.get(templateContext, profile, "avatar"), equalTo<Any>("avatar-id"))
}
@Test
fun `accessing other members uses reflection accessor`() {
- assertThat(accessor.get(templateContext, profile, "hashCode"), `is`<Any>(profile.hashCode()))
+ assertThat(accessor.get(templateContext, profile, "hashCode"), equalTo<Any>(profile.hashCode()))
}
}
import net.pterodactylus.sone.data.Profile
import net.pterodactylus.sone.data.Sone
-import net.pterodactylus.sone.test.mock
+import net.pterodactylus.sone.test.*
import net.pterodactylus.sone.text.FreenetLinkPart
import net.pterodactylus.sone.text.Part
import net.pterodactylus.sone.text.PlainTextPart
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.contains
import org.junit.Test
-import org.mockito.Mockito.`when`
/**
* Unit test for [ShortenFilter].
@Test
fun `sone parts are added but their length is ignored`() {
val sone = mock<Sone>()
- `when`(sone.profile).thenReturn(Profile(sone))
+ whenever(sone.profile).thenReturn(Profile(sone))
assertThat(shortenParts(15, 10, SonePart(sone), PlainTextPart("This is a long text.")), contains<Part>(
SonePart(sone),
PlainTextPart("This is a …")
@Test
fun `additional sone parts are ignored`() {
val sone = mock<Sone>()
- `when`(sone.profile).thenReturn(Profile(sone))
+ whenever(sone.profile).thenReturn(Profile(sone))
assertThat(shortenParts(15, 10, PlainTextPart("This is a long text."), SonePart(sone)), contains<Part>(
PlainTextPart("This is a …")
))
import com.google.inject.Module
import org.mockito.*
import org.mockito.invocation.InvocationOnMock
-import org.mockito.stubbing.OngoingStubbing
+import org.mockito.stubbing.*
inline fun <reified T : Any> mock(): T = Mockito.mock<T>(T::class.java)!!
inline fun <reified T : Any> mockBuilder(): T = Mockito.mock<T>(T::class.java, Mockito.RETURNS_SELF)!!
Module { it!!.bind(T::class.java).toInstance(mock()) }
inline fun <reified T: Any?> whenever(methodCall: T) = Mockito.`when`(methodCall)!!
+inline fun <reified T: Any?> Stubber.whenever(mock: T) = `when`(mock)!!
inline fun <reified T : Any> OngoingStubbing<T>.thenReturnMock(): OngoingStubbing<T> = this.thenReturn(mock())
package net.pterodactylus.sone.text
import org.hamcrest.MatcherAssert.assertThat
-import org.hamcrest.Matchers.`is`
+import org.hamcrest.Matchers.equalTo
import org.junit.Test
/**
@Test
fun linkIsUsedAsTitleIfNoTextIsGiven() {
- assertThat(FreenetLinkPart("link", "text", true).title, `is`("link"))
+ assertThat(FreenetLinkPart("link", "text", true).title, equalTo("link"))
}
}
package net.pterodactylus.sone.text
import org.hamcrest.MatcherAssert.assertThat
-import org.hamcrest.Matchers.`is`
+import org.hamcrest.Matchers.equalTo
import org.junit.Test
/**
@Test
fun linkIsUsedAsTitleIfNoTitleIsGiven() {
- assertThat(LinkPart("link", "text").title, `is`("link"))
+ assertThat(LinkPart("link", "text").title, equalTo("link"))
}
}
package net.pterodactylus.sone.text
-import net.pterodactylus.sone.data.Profile
import net.pterodactylus.sone.data.Sone
-import net.pterodactylus.sone.test.mock
+import net.pterodactylus.sone.test.*
import org.hamcrest.MatcherAssert.assertThat
-import org.hamcrest.Matchers.`is`
+import org.hamcrest.Matchers.equalTo
import org.junit.Test
-import org.mockito.Mockito.`when`
/**
* Unit test for [SonePart].
private val sone = mock<Sone>()
init {
- `when`(sone.profile).thenReturn(mock())
- `when`(sone.name).thenReturn("sone")
+ whenever(sone.profile).thenReturn(mock())
+ whenever(sone.name).thenReturn("sone")
}
private val part = SonePart(sone)
@Test
fun textIsConstructedFromSonesNiceName() {
- assertThat<String>(part.text, `is`<String>("sone"))
+ assertThat<String>(part.text, equalTo<String>("sone"))
}
}