๐Ÿ› Fix NPE when post isnโ€™t loaded
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / text / SoneMentionDetectorTest.kt
index 24d2335..7208230 100644 (file)
@@ -17,8 +17,6 @@
 
 package net.pterodactylus.sone.text
 
-import com.google.common.base.*
-import com.google.common.base.Optional.*
 import com.google.common.eventbus.*
 import net.pterodactylus.sone.core.event.*
 import net.pterodactylus.sone.data.*
@@ -34,7 +32,8 @@ import kotlin.test.*
 @Suppress("UnstableApiUsage")
 class SoneMentionDetectorTest {
 
-       private val eventBus = EventBus()
+       private val caughtExceptions = mutableListOf<Throwable>()
+       private val eventBus = EventBus { exception, _ -> caughtExceptions += exception }
        private val soneProvider = TestSoneProvider()
        private val postProvider = TestPostProvider()
        private val soneTextParser = SoneTextParser(soneProvider, postProvider)
@@ -145,6 +144,14 @@ class SoneMentionDetectorTest {
        }
 
        @Test
+       fun `detector does not emit event for reply without post`() {
+               val reply = emptyPostReply("text mentions sone://${localSone1.id} and sone://${localSone2.id}.", post = null)
+               eventBus.post(NewPostReplyFoundEvent(reply))
+               assertThat(caughtExceptions, emptyIterable())
+               assertThat(capturedFoundEvents, emptyIterable())
+       }
+
+       @Test
        fun `detector does not emit removed event when a post without mention is removed`() {
                val post = createPost()
                eventBus.post(PostRemovedEvent(post))
@@ -226,20 +233,6 @@ class SoneMentionDetectorTest {
 
 }
 
-private val remoteSone1 = createRemoteSone()
-private val remoteSone2 = createRemoteSone()
-
-private val localSone1 = createLocalSone()
-private val localSone2 = createLocalSone()
-
-private fun createPost(text: String = "", sone: Sone = remoteSone1, known: Boolean = false): Post.EmptyPost {
-       return object : Post.EmptyPost("post-id") {
-               override fun getSone() = sone
-               override fun getText() = text
-               override fun isKnown() = known
-       }
-}
-
 private class TestSoneProvider : SoneProvider {
 
        override val sones: Collection<Sone> get() = remoteSones + localSones
@@ -268,14 +261,3 @@ private class TestPostReplyProvider : PostReplyProvider {
        override fun getReplies(postId: String) = postReplies[postId] ?: emptyList()
 
 }
-
-private fun emptyPostReply(text: String = "", post: Post = createPost(), sone: Sone = remoteSone1, known: Boolean = false) = object : PostReply {
-       override val id = "reply-id"
-       override fun getSone() = sone
-       override fun getPostId() = post.id
-       override fun getPost(): Optional<Post> = of(post)
-       override fun getTime() = 1L
-       override fun getText() = text
-       override fun isKnown() = known
-       override fun setKnown(known: Boolean): PostReply = this
-}