✨ Add exception suppressor that run code when an exception is caught
[rhynodge.git] / src / test / kotlin / net / pterodactylus / util / exception / ExceptionsTest.kt
1 package net.pterodactylus.util.exception
2
3 import org.hamcrest.MatcherAssert
4 import org.hamcrest.Matchers
5 import org.junit.Test
6 import java.util.concurrent.atomic.AtomicBoolean
7 import java.util.concurrent.atomic.AtomicReference
8
9 class ExceptionsTest {
10
11         @Test
12         fun `runnable is run by suppress method`() {
13                 val runnabledCalled = AtomicBoolean(false)
14                 suppressException({ runnabledCalled.set(true) })()
15                 MatcherAssert.assertThat(runnabledCalled.get(), Matchers.equalTo(true))
16         }
17
18         @Test
19         fun `exception thrown in runnable is suppressed`() {
20                 suppressException({ throw RuntimeException() })()
21         }
22
23         @Test
24         fun `error runnable is called with exception`() {
25                 val caughtException = AtomicReference<Exception>()
26                 suppressException({ throw IllegalStateException("Test") }) { caughtException.set(it) }()
27                 MatcherAssert.assertThat(caughtException.get(), Matchers.instanceOf(RuntimeException::class.java))
28                 MatcherAssert.assertThat(caughtException.get().message, Matchers.equalTo("Test"))
29         }
30
31 }