From 8bbd85568dc97aa6741835e45a40cc7a0e44691f Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 12 Jan 2020 13:03:23 +0100 Subject: [PATCH] =?utf8?q?=F0=9F=90=9B=20Fix=20NPE=20when=20post=20isn?= =?utf8?q?=E2=80=99t=20loaded?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../kotlin/net/pterodactylus/sone/text/SoneMentionDetector.kt | 3 ++- src/test/kotlin/net/pterodactylus/sone/test/Mocks.kt | 6 +++--- .../net/pterodactylus/sone/text/SoneMentionDetectorTest.kt | 11 ++++++++++- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/net/pterodactylus/sone/text/SoneMentionDetector.kt b/src/main/kotlin/net/pterodactylus/sone/text/SoneMentionDetector.kt index e816c8e..b688e86 100644 --- a/src/main/kotlin/net/pterodactylus/sone/text/SoneMentionDetector.kt +++ b/src/main/kotlin/net/pterodactylus/sone/text/SoneMentionDetector.kt @@ -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) } } } diff --git a/src/test/kotlin/net/pterodactylus/sone/test/Mocks.kt b/src/test/kotlin/net/pterodactylus/sone/test/Mocks.kt index 0bb8bc9..39756f6 100644 --- a/src/test/kotlin/net/pterodactylus/sone/test/Mocks.kt +++ b/src/test/kotlin/net/pterodactylus/sone/test/Mocks.kt @@ -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 = Optional.of(post) + override fun getPostId() = post!!.id + override fun getPost(): Optional = Optional.fromNullable(post) override fun getTime() = 1L override fun getText() = text override fun isKnown() = known diff --git a/src/test/kotlin/net/pterodactylus/sone/text/SoneMentionDetectorTest.kt b/src/test/kotlin/net/pterodactylus/sone/text/SoneMentionDetectorTest.kt index 6ae437b..7208230 100644 --- a/src/test/kotlin/net/pterodactylus/sone/text/SoneMentionDetectorTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/text/SoneMentionDetectorTest.kt @@ -32,7 +32,8 @@ import kotlin.test.* @Suppress("UnstableApiUsage") class SoneMentionDetectorTest { - private val eventBus = EventBus() + private val caughtExceptions = mutableListOf() + 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)) -- 2.7.4