🐛 Fix NPE when post isn’t loaded
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 12 Jan 2020 12:03:23 +0000 (13:03 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 12 Jan 2020 12:03:23 +0000 (13:03 +0100)
src/main/kotlin/net/pterodactylus/sone/text/SoneMentionDetector.kt
src/test/kotlin/net/pterodactylus/sone/test/Mocks.kt
src/test/kotlin/net/pterodactylus/sone/text/SoneMentionDetectorTest.kt

index e816c8e..b688e86 100644 (file)
@@ -50,7 +50,8 @@ class SoneMentionDetector @Inject constructor(private val eventBus: EventBus, pr
                                if (postReply.text.hasLinksToLocalSones()) {
                                        postReply.post
                                                        .also { mentionedPosts += it }
-                                                       .let(::MentionOfLocalSoneFoundEvent).also(eventBus::post)
+                                                       .let(::MentionOfLocalSoneFoundEvent)
+                                                       ?.also(eventBus::post)
                                }
                        }
                }
index 0bb8bc9..39756f6 100644 (file)
@@ -46,11 +46,11 @@ fun createPost(text: String = "", sone: Sone = remoteSone1, known: Boolean = fal
        }
 }
 
-fun emptyPostReply(text: String = "", post: Post = createPost(), sone: Sone = remoteSone1, known: Boolean = false) = object : PostReply {
+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> = Optional.of(post)
+       override fun getPostId() = post!!.id
+       override fun getPost(): Optional<Post> = Optional.fromNullable(post)
        override fun getTime() = 1L
        override fun getText() = text
        override fun isKnown() = known
index 6ae437b..7208230 100644 (file)
@@ -32,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)
@@ -143,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))