From 72e43af1e442398e304167c1e18e229d254fba7b Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 29 Nov 2019 10:47:07 +0100 Subject: [PATCH] =?utf8?q?=F0=9F=8E=A8=20Replace=20PreferencesLoaderTest?= =?utf8?q?=20with=20Kotlin=20version?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../sone/core/PreferencesLoaderTest.java | 73 ---------------------- .../sone/core/PreferencesLoaderTest.kt | 61 ++++++++++++++++++ 2 files changed, 61 insertions(+), 73 deletions(-) delete mode 100644 src/test/java/net/pterodactylus/sone/core/PreferencesLoaderTest.java create mode 100644 src/test/kotlin/net/pterodactylus/sone/core/PreferencesLoaderTest.kt diff --git a/src/test/java/net/pterodactylus/sone/core/PreferencesLoaderTest.java b/src/test/java/net/pterodactylus/sone/core/PreferencesLoaderTest.java deleted file mode 100644 index 72831fc..0000000 --- a/src/test/java/net/pterodactylus/sone/core/PreferencesLoaderTest.java +++ /dev/null @@ -1,73 +0,0 @@ -package net.pterodactylus.sone.core; - -import static net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired.WRITING; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.not; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import net.pterodactylus.sone.test.TestValue; -import net.pterodactylus.util.config.Configuration; - -import com.google.common.eventbus.EventBus; -import org.junit.Before; -import org.junit.Test; - -/** - * Unit test for {@link PreferencesLoader}. - */ -public class PreferencesLoaderTest { - - private final EventBus eventBus = mock(EventBus.class); - private final Preferences preferences = new Preferences(eventBus); - private final Configuration configuration = mock(Configuration.class); - private final PreferencesLoader preferencesLoader = - new PreferencesLoader(preferences); - - @Before - public void setupConfiguration() { - setupIntValue("InsertionDelay", 15); - setupIntValue("PostsPerPage", 25); - setupIntValue("ImagesPerPage", 12); - setupIntValue("CharactersPerPost", 150); - setupIntValue("PostCutOffLength", 300); - setupBooleanValue("RequireFullAccess", true); - setupBooleanValue("ActivateFcpInterface", true); - setupIntValue("FcpFullAccessRequired", 1); - } - - private void setupIntValue(String optionName, int value) { - when(configuration.getIntValue("Option/" + optionName)).thenReturn( - TestValue.from(value)); - } - - private void setupBooleanValue(String optionName, boolean value) { - when(configuration.getBooleanValue( - "Option/" + optionName)).thenReturn( - TestValue.from(value)); - } - - @Test - public void configurationIsLoadedCorrectly() { - setupConfiguration(); - preferencesLoader.loadFrom(configuration); - assertThat(preferences.getInsertionDelay(), is(15)); - assertThat(preferences.getPostsPerPage(), is(25)); - assertThat(preferences.getImagesPerPage(), is(12)); - assertThat(preferences.getCharactersPerPost(), is(150)); - assertThat(preferences.getPostCutOffLength(), is(300)); - assertThat(preferences.getRequireFullAccess(), is(true)); - assertThat(preferences.getFcpInterfaceActive(), is(true)); - assertThat(preferences.getFcpFullAccessRequired(), is(WRITING)); - } - - @Test - public void configurationIsLoadedCorrectlyWithCutOffLengthMinusOne() { - setupConfiguration(); - setupIntValue("PostCutOffLength", -1); - preferencesLoader.loadFrom(configuration); - assertThat(preferences.getPostCutOffLength(), not(is(-1))); - } - -} diff --git a/src/test/kotlin/net/pterodactylus/sone/core/PreferencesLoaderTest.kt b/src/test/kotlin/net/pterodactylus/sone/core/PreferencesLoaderTest.kt new file mode 100644 index 0000000..0cd7e6d --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/core/PreferencesLoaderTest.kt @@ -0,0 +1,61 @@ +package net.pterodactylus.sone.core + +import com.google.common.eventbus.* +import net.pterodactylus.sone.fcp.FcpInterface.* +import net.pterodactylus.util.config.* +import org.hamcrest.MatcherAssert.* +import org.hamcrest.Matchers.* +import org.junit.* + +/** + * Unit test for [PreferencesLoader]. + */ +class PreferencesLoaderTest { + + @Suppress("UnstableApiUsage") + private val eventBus = EventBus() + private val preferences = Preferences(eventBus) + private val configuration = Configuration(MapConfigurationBackend()) + private val preferencesLoader = PreferencesLoader(preferences) + + @Before + fun setupConfiguration() { + setupIntValue("InsertionDelay", 15) + setupIntValue("PostsPerPage", 25) + setupIntValue("ImagesPerPage", 12) + setupIntValue("CharactersPerPost", 150) + setupIntValue("PostCutOffLength", 300) + setupBooleanValue("RequireFullAccess", true) + setupBooleanValue("ActivateFcpInterface", true) + setupIntValue("FcpFullAccessRequired", 1) + } + + private fun setupIntValue(optionName: String, value: Int) { + configuration.getIntValue("Option/$optionName").value = value + } + + private fun setupBooleanValue(optionName: String, value: Boolean) { + configuration.getBooleanValue("Option/$optionName").value = value + } + + @Test + fun `configuration is loaded correctly`() { + preferencesLoader.loadFrom(configuration) + assertThat(preferences.insertionDelay, equalTo(15)) + assertThat(preferences.postsPerPage, equalTo(25)) + assertThat(preferences.imagesPerPage, equalTo(12)) + assertThat(preferences.charactersPerPost, equalTo(150)) + assertThat(preferences.postCutOffLength, equalTo(300)) + assertThat(preferences.requireFullAccess, equalTo(true)) + assertThat(preferences.fcpInterfaceActive, equalTo(true)) + assertThat(preferences.fcpFullAccessRequired, equalTo(FullAccessRequired.WRITING)) + } + + @Test + fun `configuration is loaded correctly with cut off length minus one`() { + setupIntValue("PostCutOffLength", -1) + preferencesLoader.loadFrom(configuration) + assertThat(preferences.postCutOffLength, not(equalTo(-1))) + } + +} -- 2.7.4