From 7fafddbfba3a0fc110c641d9196d14ac1230c56a Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 18 Apr 2020 23:32:12 +0200 Subject: [PATCH] =?utf8?q?=E2=9C=85=20Use=20real=20event=20bus=20in=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../net/pterodactylus/sone/core/PreferencesTest.kt | 112 ++++++++++++++------- 1 file changed, 73 insertions(+), 39 deletions(-) diff --git a/src/test/kotlin/net/pterodactylus/sone/core/PreferencesTest.kt b/src/test/kotlin/net/pterodactylus/sone/core/PreferencesTest.kt index 245ff03..24bda21 100644 --- a/src/test/kotlin/net/pterodactylus/sone/core/PreferencesTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/core/PreferencesTest.kt @@ -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() + private val eventBus = EventBus() private val preferences = Preferences(eventBus) - private val eventsCaptor = capture() @Test fun `preferences retain insertion delay`() { @@ -32,9 +36,14 @@ class PreferencesTest { @Test fun `preferences sends event on setting insertion delay`() { + val events = mutableListOf() + 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() + 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() + 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(instanceOf(FcpInterfaceActivatedEvent::class.java))) } @Test fun `preferences retain fcp interface active of false`() { + val events = mutableListOf() + 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(instanceOf(FcpInterfaceDeactivatedEvent::class.java))) } @Test fun `preferences return default value when fcp interface active is set to null`() { + val events = mutableListOf() + 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(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() + 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 testPreferencesChangedEvent(name: String, setter: (T) -> Unit, value: T) { + val events = mutableListOf() + 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))) } } -- 2.7.4