✅ Use real event bus in test
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 18 Apr 2020 21:32:12 +0000 (23:32 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 18 Apr 2020 21:32:12 +0000 (23:32 +0200)
src/test/kotlin/net/pterodactylus/sone/core/PreferencesTest.kt

index 245ff03..24bda21 100644 (file)
@@ -1,28 +1,32 @@
 package net.pterodactylus.sone.core
 
-import com.google.common.eventbus.*
-import net.pterodactylus.sone.core.event.*
-import net.pterodactylus.sone.fcp.FcpInterface.*
-import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired.*
-import net.pterodactylus.sone.fcp.event.*
-import net.pterodactylus.sone.test.*
-import net.pterodactylus.util.config.*
-import org.hamcrest.MatcherAssert.*
-import org.hamcrest.Matchers.*
-import org.junit.*
-import org.mockito.ArgumentMatchers.any
-import org.mockito.Mockito.atLeastOnce
-import org.mockito.Mockito.never
-import org.mockito.Mockito.verify
+import com.google.common.eventbus.EventBus
+import com.google.common.eventbus.Subscribe
+import net.pterodactylus.sone.core.event.InsertionDelayChangedEvent
+import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired
+import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired.ALWAYS
+import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired.NO
+import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired.WRITING
+import net.pterodactylus.sone.fcp.event.FcpInterfaceActivatedEvent
+import net.pterodactylus.sone.fcp.event.FcpInterfaceDeactivatedEvent
+import net.pterodactylus.sone.fcp.event.FullAccessRequiredChanged
+import net.pterodactylus.util.config.Configuration
+import net.pterodactylus.util.config.MapConfigurationBackend
+import org.hamcrest.MatcherAssert.assertThat
+import org.hamcrest.Matchers.emptyIterable
+import org.hamcrest.Matchers.equalTo
+import org.hamcrest.Matchers.hasItem
+import org.hamcrest.Matchers.instanceOf
+import org.hamcrest.Matchers.nullValue
+import org.junit.Test
 
 /**
  * Unit test for [Preferences].
  */
 class PreferencesTest {
 
-       private val eventBus = mock<EventBus>()
+       private val eventBus = EventBus()
        private val preferences = Preferences(eventBus)
-       private val eventsCaptor = capture<Any>()
 
        @Test
        fun `preferences retain insertion delay`() {
@@ -32,9 +36,14 @@ class PreferencesTest {
 
        @Test
        fun `preferences sends event on setting insertion delay`() {
+               val events = mutableListOf<InsertionDelayChangedEvent>()
+               eventBus.register(object {
+                       @Subscribe
+                       fun insertionDelayChangedEvent(event: InsertionDelayChangedEvent) =
+                                       events.add(event)
+               })
                preferences.newInsertionDelay = 15
-               verify(eventBus, atLeastOnce()).post(eventsCaptor.capture())
-               assertThat(eventsCaptor.allValues, hasItem(InsertionDelayChangedEvent(15)))
+               assertThat(events, hasItem(InsertionDelayChangedEvent(15)))
        }
 
        @Test(expected = IllegalArgumentException::class)
@@ -44,13 +53,19 @@ class PreferencesTest {
 
        @Test
        fun `no event is sent when invalid insertion delay is set`() {
+               val events = mutableListOf<InsertionDelayChangedEvent>()
+               eventBus.register(object {
+                       @Subscribe
+                       fun insertionDelayChanged(event: InsertionDelayChangedEvent) =
+                                       events.add(event)
+               })
                try {
                        preferences.newInsertionDelay = -15
                } catch (iae: IllegalArgumentException) {
                        /* ignore. */
                }
 
-               verify(eventBus, never()).post(any())
+               assertThat(events, emptyIterable())
        }
 
        @Test
@@ -189,23 +204,41 @@ class PreferencesTest {
 
        @Test
        fun `preferences retain fcp interface active of true`() {
+               val events = mutableListOf<FcpInterfaceActivatedEvent>()
+               eventBus.register(object {
+                       @Subscribe
+                       fun fcpInterfaceActivatedEvent(event: FcpInterfaceActivatedEvent) =
+                                       events.add(event)
+               })
                preferences.newFcpInterfaceActive = true
                assertThat(preferences.fcpInterfaceActive, equalTo(true))
-               verify(eventBus).post(any(FcpInterfaceActivatedEvent::class.java))
+               assertThat(events, hasItem<FcpInterfaceActivatedEvent>(instanceOf(FcpInterfaceActivatedEvent::class.java)))
        }
 
        @Test
        fun `preferences retain fcp interface active of false`() {
+               val events = mutableListOf<FcpInterfaceDeactivatedEvent>()
+               eventBus.register(object {
+                       @Subscribe
+                       fun fcpInterfaceDeactivatedEvent(event: FcpInterfaceDeactivatedEvent) =
+                                       events.add(event)
+               })
                preferences.newFcpInterfaceActive = false
                assertThat(preferences.fcpInterfaceActive, equalTo(false))
-               verify(eventBus).post(any(FcpInterfaceDeactivatedEvent::class.java))
+               assertThat(events, hasItem<FcpInterfaceDeactivatedEvent>(instanceOf(FcpInterfaceDeactivatedEvent::class.java)))
        }
 
        @Test
        fun `preferences return default value when fcp interface active is set to null`() {
+               val events = mutableListOf<FcpInterfaceDeactivatedEvent>()
+               eventBus.register(object {
+                       @Subscribe
+                       fun fcpInterfaceDeactivatedEvent(event: FcpInterfaceDeactivatedEvent) =
+                                       events.add(event)
+               })
                preferences.newFcpInterfaceActive = null
                assertThat(preferences.fcpInterfaceActive, equalTo(false))
-               verify(eventBus).post(any(FcpInterfaceDeactivatedEvent::class.java))
+               assertThat(events, hasItem<FcpInterfaceDeactivatedEvent>(instanceOf(FcpInterfaceDeactivatedEvent::class.java)))
        }
 
        @Test
@@ -215,38 +248,34 @@ class PreferencesTest {
 
        @Test
        fun `preferences retain fcp full access required of no`() {
-               preferences.newFcpFullAccessRequired = NO
-               assertThat(preferences.fcpFullAccessRequired, equalTo(NO))
                verifyFullAccessRequiredChangedEvent(NO)
        }
 
-       private fun verifyFullAccessRequiredChangedEvent(
-                       fullAccessRequired: FullAccessRequired) {
-               verify(eventBus).post(eventsCaptor.capture())
-               assertThat(eventsCaptor.value, instanceOf(FullAccessRequiredChanged::class.java))
-               assertThat((eventsCaptor.value as FullAccessRequiredChanged).fullAccessRequired,
-                               equalTo(fullAccessRequired))
+       private fun verifyFullAccessRequiredChangedEvent(set: FullAccessRequired?, expected: FullAccessRequired = set!!) {
+               val events = mutableListOf<FullAccessRequiredChanged>()
+               eventBus.register(object {
+                       @Subscribe
+                       fun fullAccessRequiredChanged(event: FullAccessRequiredChanged) =
+                                       events.add(event)
+               })
+               preferences.newFcpFullAccessRequired = set
+               assertThat(preferences.fcpFullAccessRequired, equalTo(expected))
+               assertThat(events.single().fullAccessRequired, equalTo(expected))
        }
 
        @Test
        fun `preferences retain fcp full access required of writing`() {
-               preferences.newFcpFullAccessRequired = WRITING
-               assertThat(preferences.fcpFullAccessRequired, equalTo(WRITING))
                verifyFullAccessRequiredChangedEvent(WRITING)
        }
 
        @Test
        fun `preferences retain fcp full access required of always`() {
-               preferences.newFcpFullAccessRequired = ALWAYS
-               assertThat(preferences.fcpFullAccessRequired, equalTo(ALWAYS))
                verifyFullAccessRequiredChangedEvent(ALWAYS)
        }
 
        @Test
        fun `preferences return default value when fcp full access required is set to null`() {
-               preferences.newFcpFullAccessRequired = null
-               assertThat(preferences.fcpFullAccessRequired, equalTo(ALWAYS))
-               verifyFullAccessRequiredChangedEvent(ALWAYS)
+               verifyFullAccessRequiredChangedEvent(null, ALWAYS)
        }
 
        @Test
@@ -265,9 +294,14 @@ class PreferencesTest {
        }
 
        private fun <T : Any> testPreferencesChangedEvent(name: String, setter: (T) -> Unit, value: T) {
+               val events = mutableListOf<PreferenceChangedEvent>()
+               eventBus.register(object {
+                       @Subscribe
+                       fun preferenceChanged(event: PreferenceChangedEvent) =
+                                       events.add(event)
+               })
                setter(value)
-               verify(eventBus, atLeastOnce()).post(eventsCaptor.capture())
-               assertThat(eventsCaptor.allValues, hasItem(PreferenceChangedEvent(name, value)))
+               assertThat(events, hasItem(PreferenceChangedEvent(name, value)))
        }
 
 }