From 8bbda5586eb86696dc9c93fa379534b6d75c5272 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 22 Feb 2019 19:28:45 +0100 Subject: [PATCH] Replace PreferencesTest with Kotlin version --- .../net/pterodactylus/sone/core/Preferences.java | 1 + .../pterodactylus/sone/core/PreferencesTest.java | 330 --------------------- .../net/pterodactylus/sone/core/PreferencesTest.kt | 326 ++++++++++++++++++++ 3 files changed, 327 insertions(+), 330 deletions(-) delete mode 100644 src/test/java/net/pterodactylus/sone/core/PreferencesTest.java create mode 100644 src/test/kotlin/net/pterodactylus/sone/core/PreferencesTest.kt diff --git a/src/main/java/net/pterodactylus/sone/core/Preferences.java b/src/main/java/net/pterodactylus/sone/core/Preferences.java index ce56e8b..73d964c 100644 --- a/src/main/java/net/pterodactylus/sone/core/Preferences.java +++ b/src/main/java/net/pterodactylus/sone/core/Preferences.java @@ -137,6 +137,7 @@ public class Preferences { */ public Preferences setPostsPerPage(Integer postsPerPage) { this.postsPerPage.set(postsPerPage); + eventBus.post(new PreferenceChangedEvent("PostsPerPage", getPostsPerPage())); return this; } diff --git a/src/test/java/net/pterodactylus/sone/core/PreferencesTest.java b/src/test/java/net/pterodactylus/sone/core/PreferencesTest.java deleted file mode 100644 index 7327abc..0000000 --- a/src/test/java/net/pterodactylus/sone/core/PreferencesTest.java +++ /dev/null @@ -1,330 +0,0 @@ -package net.pterodactylus.sone.core; - -import static net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired.ALWAYS; -import static net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired.NO; -import static net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired.WRITING; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.hasItem; -import static org.hamcrest.Matchers.is; -import static org.mockito.ArgumentCaptor.forClass; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.atLeastOnce; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.verify; - -import net.pterodactylus.sone.core.event.InsertionDelayChangedEvent; -import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired; -import net.pterodactylus.sone.fcp.event.FcpInterfaceActivatedEvent; -import net.pterodactylus.sone.fcp.event.FcpInterfaceDeactivatedEvent; -import net.pterodactylus.sone.fcp.event.FullAccessRequiredChanged; - -import com.google.common.eventbus.EventBus; -import org.junit.Test; -import org.mockito.ArgumentCaptor; - -/** - * Unit test for {@link Preferences}. - */ -public class PreferencesTest { - - private final EventBus eventBus = mock(EventBus.class); - private final Preferences preferences = new Preferences(eventBus); - - @Test - public void preferencesRetainInsertionDelay() { - preferences.setInsertionDelay(15); - assertThat(preferences.getInsertionDelay(), is(15)); - } - - @Test - public void preferencesSendsEventOnSettingInsertionDelay() { - preferences.setInsertionDelay(15); - ArgumentCaptor eventsCaptor = forClass(Object.class); - verify(eventBus, atLeastOnce()).post(eventsCaptor.capture()); - assertThat(eventsCaptor.getAllValues(), hasItem(new InsertionDelayChangedEvent(15))); - } - - @Test(expected = IllegalArgumentException.class) - public void invalidInsertionDelayIsRejected() { - preferences.setInsertionDelay(-15); - } - - @Test - public void noEventIsSentWhenInvalidInsertionDelayIsSet() { - try { - preferences.setInsertionDelay(-15); - } catch (IllegalArgumentException iae) { - /* ignore. */ - } - verify(eventBus, never()).post(any()); - } - - @Test - public void preferencesReturnDefaultValueWhenInsertionDelayIsSetToNull() { - preferences.setInsertionDelay(null); - assertThat(preferences.getInsertionDelay(), is(60)); - } - - @Test - public void preferencesStartWithInsertionDelayDefaultValue() { - assertThat(preferences.getInsertionDelay(), is(60)); - } - - @Test - public void preferencesRetainPostsPerPage() { - preferences.setPostsPerPage(15); - assertThat(preferences.getPostsPerPage(), is(15)); - } - - @Test(expected = IllegalArgumentException.class) - public void invalidPostsPerPageIsRejected() { - preferences.setPostsPerPage(-15); - } - - @Test - public void preferencesReturnDefaultValueWhenPostsPerPageIsSetToNull() { - preferences.setPostsPerPage(null); - assertThat(preferences.getPostsPerPage(), is(10)); - } - - @Test - public void preferencesStartWithPostsPerPageDefaultValue() { - assertThat(preferences.getPostsPerPage(), is(10)); - } - - @Test - public void preferencesRetainImagesPerPage() { - preferences.setImagesPerPage(15); - assertThat(preferences.getImagesPerPage(), is(15)); - } - - @Test(expected = IllegalArgumentException.class) - public void invalidImagesPerPageIsRejected() { - preferences.setImagesPerPage(-15); - } - - @Test - public void preferencesReturnDefaultValueWhenImagesPerPageIsSetToNull() { - preferences.setImagesPerPage(null); - assertThat(preferences.getImagesPerPage(), is(9)); - } - - @Test - public void preferencesStartWithImagesPerPageDefaultValue() { - assertThat(preferences.getImagesPerPage(), is(9)); - } - - @Test - public void preferencesRetainCharactersPerPost() { - preferences.setCharactersPerPost(150); - assertThat(preferences.getCharactersPerPost(), is(150)); - } - - @Test(expected = IllegalArgumentException.class) - public void invalidCharactersPerPostIsRejected() { - preferences.setCharactersPerPost(-15); - } - - @Test - public void preferencesReturnDefaultValueWhenCharactersPerPostIsSetToNull() { - preferences.setCharactersPerPost(null); - assertThat(preferences.getCharactersPerPost(), is(400)); - } - - @Test - public void preferencesStartWithCharactersPerPostDefaultValue() { - assertThat(preferences.getCharactersPerPost(), is(400)); - } - - @Test - public void preferencesRetainPostCutOffLength() { - preferences.setPostCutOffLength(150); - assertThat(preferences.getPostCutOffLength(), is(150)); - } - - @Test(expected = IllegalArgumentException.class) - public void invalidPostCutOffLengthIsRejected() { - preferences.setPostCutOffLength(-15); - } - - @Test(expected = IllegalArgumentException.class) - public void cutOffLengthOfMinusOneIsNotAllowed() { - preferences.setPostCutOffLength(-1); - } - - @Test - public void preferencesReturnDefaultValueWhenPostCutOffLengthIsSetToNull() { - preferences.setPostCutOffLength(null); - assertThat(preferences.getPostCutOffLength(), is(200)); - } - - @Test - public void preferencesStartWithPostCutOffLengthDefaultValue() { - assertThat(preferences.getPostCutOffLength(), is(200)); - } - - @Test - public void preferencesRetainRequireFullAccessOfTrue() { - preferences.setRequireFullAccess(true); - assertThat(preferences.isRequireFullAccess(), is(true)); - } - - @Test - public void preferencesRetainRequireFullAccessOfFalse() { - preferences.setRequireFullAccess(false); - assertThat(preferences.isRequireFullAccess(), is(false)); - } - - @Test - public void preferencesReturnDefaultValueWhenRequireFullAccessIsSetToNull() { - preferences.setRequireFullAccess(null); - assertThat(preferences.isRequireFullAccess(), is(false)); - } - - @Test - public void preferencesStartWithRequireFullAccessDefaultValue() { - assertThat(preferences.isRequireFullAccess(), is(false)); - } - - @Test - public void preferencesRetainPositiveTrust() { - preferences.setPositiveTrust(15); - assertThat(preferences.getPositiveTrust(), is(15)); - } - - @Test(expected = IllegalArgumentException.class) - public void invalidPositiveTrustIsRejected() { - preferences.setPositiveTrust(-15); - } - - @Test - public void preferencesReturnDefaultValueWhenPositiveTrustIsSetToNull() { - preferences.setPositiveTrust(null); - assertThat(preferences.getPositiveTrust(), is(75)); - } - - @Test - public void preferencesStartWithPositiveTrustDefaultValue() { - assertThat(preferences.getPositiveTrust(), is(75)); - } - - @Test - public void preferencesRetainNegativeTrust() { - preferences.setNegativeTrust(-15); - assertThat(preferences.getNegativeTrust(), is(-15)); - } - - @Test(expected = IllegalArgumentException.class) - public void invalidNegativeTrustIsRejected() { - preferences.setNegativeTrust(150); - } - - @Test - public void preferencesReturnDefaultValueWhenNegativeTrustIsSetToNull() { - preferences.setNegativeTrust(null); - assertThat(preferences.getNegativeTrust(), is(-25)); - } - - @Test - public void preferencesStartWithNegativeTrustDefaultValue() { - assertThat(preferences.getNegativeTrust(), is(-25)); - } - - @Test - public void preferencesRetainTrustComment() { - preferences.setTrustComment("Trust"); - assertThat(preferences.getTrustComment(), is("Trust")); - } - - @Test - public void preferencesReturnDefaultValueWhenTrustCommentIsSetToNull() { - preferences.setTrustComment(null); - assertThat(preferences.getTrustComment(), - is("Set from Sone Web Interface")); - } - - @Test - public void preferencesStartWithTrustCommentDefaultValue() { - assertThat(preferences.getTrustComment(), - is("Set from Sone Web Interface")); - } - - @Test - public void preferencesRetainFcpInterfaceActiveOfTrue() { - preferences.setFcpInterfaceActive(true); - assertThat(preferences.isFcpInterfaceActive(), is(true)); - verify(eventBus).post(any(FcpInterfaceActivatedEvent.class)); - } - - @Test - public void preferencesRetainFcpInterfaceActiveOfFalse() { - preferences.setFcpInterfaceActive(false); - assertThat(preferences.isFcpInterfaceActive(), is(false)); - verify(eventBus).post(any(FcpInterfaceDeactivatedEvent.class)); - } - - @Test - public void preferencesReturnDefaultValueWhenFcpInterfaceActiveIsSetToNull() { - preferences.setFcpInterfaceActive(null); - assertThat(preferences.isFcpInterfaceActive(), is(false)); - verify(eventBus).post(any(FcpInterfaceDeactivatedEvent.class)); - } - - @Test - public void preferencesStartWithFcpInterfaceActiveDefaultValue() { - assertThat(preferences.isFcpInterfaceActive(), is(false)); - } - - @Test - public void preferencesRetainFcpFullAccessRequiredOfNo() { - preferences.setFcpFullAccessRequired(NO); - assertThat(preferences.getFcpFullAccessRequired(), is(NO)); - verifyFullAccessRequiredChangedEvent(NO); - } - - private void verifyFullAccessRequiredChangedEvent( - FullAccessRequired fullAccessRequired) { - ArgumentCaptor fullAccessRequiredCaptor = - forClass(FullAccessRequiredChanged.class); - verify(eventBus).post(fullAccessRequiredCaptor.capture()); - assertThat( - fullAccessRequiredCaptor.getValue().getFullAccessRequired(), - is(fullAccessRequired)); - } - - @Test - public void preferencesRetainFcpFullAccessRequiredOfWriting() { - preferences.setFcpFullAccessRequired(WRITING); - assertThat(preferences.getFcpFullAccessRequired(), is(WRITING)); - verifyFullAccessRequiredChangedEvent(WRITING); - } - - @Test - public void preferencesRetainFcpFullAccessRequiredOfAlways() { - preferences.setFcpFullAccessRequired(ALWAYS); - assertThat(preferences.getFcpFullAccessRequired(), is(ALWAYS)); - verifyFullAccessRequiredChangedEvent(ALWAYS); - } - - @Test - public void preferencesReturnDefaultValueWhenFcpFullAccessRequiredIsSetToNull() { - preferences.setFcpFullAccessRequired(null); - assertThat(preferences.getFcpFullAccessRequired(), is(ALWAYS)); - verifyFullAccessRequiredChangedEvent(ALWAYS); - } - - @Test - public void preferencesStartWithFcpFullAccessRequiredDefaultValue() { - assertThat(preferences.getFcpFullAccessRequired(), is(ALWAYS)); - } - - @Test - public void settingInsertionDelayToValidValueSendsChangeEvent() { - preferences.setInsertionDelay(30); - ArgumentCaptor eventsCaptor = forClass(Object.class); - verify(eventBus, atLeastOnce()).post(eventsCaptor.capture()); - assertThat(eventsCaptor.getAllValues(), hasItem(new PreferenceChangedEvent("InsertionDelay", 30))); - } - -} diff --git a/src/test/kotlin/net/pterodactylus/sone/core/PreferencesTest.kt b/src/test/kotlin/net/pterodactylus/sone/core/PreferencesTest.kt new file mode 100644 index 0000000..f4f4818 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/core/PreferencesTest.kt @@ -0,0 +1,326 @@ +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 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 + +/** + * Unit test for [Preferences]. + */ +class PreferencesTest { + + private val eventBus = mock() + private val preferences = Preferences(eventBus) + private val eventsCaptor = capture() + + @Test + fun `preferences retain insertion delay`() { + preferences.insertionDelay = 15 + assertThat(preferences.insertionDelay, equalTo(15)) + } + + @Test + fun `preferences sends event on setting insertion delay`() { + preferences.insertionDelay = 15 + verify(eventBus, atLeastOnce()).post(eventsCaptor.capture()) + assertThat(eventsCaptor.allValues, hasItem(InsertionDelayChangedEvent(15))) + } + + @Test(expected = IllegalArgumentException::class) + fun `invalid insertion delay is rejected`() { + preferences.insertionDelay = -15 + } + + @Test + fun `no event is sent when invalid insertion delay is set`() { + try { + preferences.insertionDelay = -15 + } catch (iae: IllegalArgumentException) { + /* ignore. */ + } + + verify(eventBus, never()).post(any()) + } + + @Test + fun `preferences return default value when insertion delay is set to null`() { + preferences.setInsertionDelay(null) + assertThat(preferences.insertionDelay, equalTo(60)) + } + + @Test + fun `preferences start with insertion delay default value`() { + assertThat(preferences.insertionDelay, equalTo(60)) + } + + @Test + fun `preferences retain posts per page`() { + preferences.postsPerPage = 15 + assertThat(preferences.postsPerPage, equalTo(15)) + } + + @Test(expected = IllegalArgumentException::class) + fun `invalid posts per page is rejected`() { + preferences.postsPerPage = -15 + } + + @Test + fun `preferences return default value when posts per page is set to null`() { + preferences.setPostsPerPage(null) + assertThat(preferences.postsPerPage, equalTo(10)) + } + + @Test + fun `preferences start with posts per page default value`() { + assertThat(preferences.postsPerPage, equalTo(10)) + } + + @Test + fun `preferences retain images per page`() { + preferences.imagesPerPage = 15 + assertThat(preferences.imagesPerPage, equalTo(15)) + } + + @Test(expected = IllegalArgumentException::class) + fun `invalid images per page is rejected`() { + preferences.imagesPerPage = -15 + } + + @Test + fun `preferences return default value when images per page is set to null`() { + preferences.setImagesPerPage(null) + assertThat(preferences.imagesPerPage, equalTo(9)) + } + + @Test + fun `preferences start with images per page default value`() { + assertThat(preferences.imagesPerPage, equalTo(9)) + } + + @Test + fun `preferences retain characters per post`() { + preferences.charactersPerPost = 150 + assertThat(preferences.charactersPerPost, equalTo(150)) + } + + @Test(expected = IllegalArgumentException::class) + fun `invalid characters per post is rejected`() { + preferences.charactersPerPost = -15 + } + + @Test + fun `preferences return default value when characters per post is set to null`() { + preferences.setCharactersPerPost(null) + assertThat(preferences.charactersPerPost, equalTo(400)) + } + + @Test + fun `preferences start with characters per post default value`() { + assertThat(preferences.charactersPerPost, equalTo(400)) + } + + @Test + fun `preferences retain post cut off length`() { + preferences.postCutOffLength = 150 + assertThat(preferences.postCutOffLength, equalTo(150)) + } + + @Test(expected = IllegalArgumentException::class) + fun `invalid post cut off length is rejected`() { + preferences.postCutOffLength = -15 + } + + @Test(expected = IllegalArgumentException::class) + fun `cut off length of minus one is not allowed`() { + preferences.postCutOffLength = -1 + } + + @Test + fun `preferences return default value when post cut off length is set to null`() { + preferences.setPostCutOffLength(null) + assertThat(preferences.postCutOffLength, equalTo(200)) + } + + @Test + fun `preferences start with post cut off length default value`() { + assertThat(preferences.postCutOffLength, equalTo(200)) + } + + @Test + fun `preferences retain require full access of true`() { + preferences.isRequireFullAccess = true + assertThat(preferences.isRequireFullAccess, equalTo(true)) + } + + @Test + fun `preferences retain require full access of false`() { + preferences.isRequireFullAccess = false + assertThat(preferences.isRequireFullAccess, equalTo(false)) + } + + @Test + fun `preferences return default value when require full access is set to null`() { + preferences.setRequireFullAccess(null) + assertThat(preferences.isRequireFullAccess, equalTo(false)) + } + + @Test + fun `preferences start with require full access default value`() { + assertThat(preferences.isRequireFullAccess, equalTo(false)) + } + + @Test + fun `preferences retain positive trust`() { + preferences.positiveTrust = 15 + assertThat(preferences.positiveTrust, equalTo(15)) + } + + @Test(expected = IllegalArgumentException::class) + fun `invalid positive trust is rejected`() { + preferences.positiveTrust = -15 + } + + @Test + fun `preferences return default value when positive trust is set to null`() { + preferences.setPositiveTrust(null) + assertThat(preferences.positiveTrust, equalTo(75)) + } + + @Test + fun `preferences start with positive trust default value`() { + assertThat(preferences.positiveTrust, equalTo(75)) + } + + @Test + fun `preferences retain negative trust`() { + preferences.negativeTrust = -15 + assertThat(preferences.negativeTrust, equalTo(-15)) + } + + @Test(expected = IllegalArgumentException::class) + fun `invalid negative trust is rejected`() { + preferences.negativeTrust = 150 + } + + @Test + fun `preferences return default value when negative trust is set to null`() { + preferences.setNegativeTrust(null) + assertThat(preferences.negativeTrust, equalTo(-25)) + } + + @Test + fun `preferences start with negative trust default value`() { + assertThat(preferences.negativeTrust, equalTo(-25)) + } + + @Test + fun `preferences retain trust comment`() { + preferences.trustComment = "Trust" + assertThat(preferences.trustComment, equalTo("Trust")) + } + + @Test + fun `preferences return default value when trust comment is set to null`() { + preferences.trustComment = null + assertThat(preferences.trustComment, + equalTo("Set from Sone Web Interface")) + } + + @Test + fun `preferences start with trust comment default value`() { + assertThat(preferences.trustComment, + equalTo("Set from Sone Web Interface")) + } + + @Test + fun `preferences retain fcp interface active of true`() { + preferences.isFcpInterfaceActive = true + assertThat(preferences.isFcpInterfaceActive, equalTo(true)) + verify(eventBus).post(any(FcpInterfaceActivatedEvent::class.java)) + } + + @Test + fun `preferences retain fcp interface active of false`() { + preferences.isFcpInterfaceActive = false + assertThat(preferences.isFcpInterfaceActive, equalTo(false)) + verify(eventBus).post(any(FcpInterfaceDeactivatedEvent::class.java)) + } + + @Test + fun `preferences return default value when fcp interface active is set to null`() { + preferences.setFcpInterfaceActive(null) + assertThat(preferences.isFcpInterfaceActive, equalTo(false)) + verify(eventBus).post(any(FcpInterfaceDeactivatedEvent::class.java)) + } + + @Test + fun `preferences start with fcp interface active default value`() { + assertThat(preferences.isFcpInterfaceActive, equalTo(false)) + } + + @Test + fun `preferences retain fcp full access required of no`() { + preferences.fcpFullAccessRequired = 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)) + } + + @Test + fun `preferences retain fcp full access required of writing`() { + preferences.fcpFullAccessRequired = WRITING + assertThat(preferences.fcpFullAccessRequired, equalTo(WRITING)) + verifyFullAccessRequiredChangedEvent(WRITING) + } + + @Test + fun `preferences retain fcp full access required of always`() { + preferences.fcpFullAccessRequired = ALWAYS + assertThat(preferences.fcpFullAccessRequired, equalTo(ALWAYS)) + verifyFullAccessRequiredChangedEvent(ALWAYS) + } + + @Test + fun `preferences return default value when fcp full access required is set to null`() { + preferences.fcpFullAccessRequired = null + assertThat(preferences.fcpFullAccessRequired, equalTo(ALWAYS)) + verifyFullAccessRequiredChangedEvent(ALWAYS) + } + + @Test + fun `preferences start with fcp full access required default value`() { + assertThat(preferences.fcpFullAccessRequired, equalTo(ALWAYS)) + } + + @Test + fun `setting insertion delay to valid value sends change event`() { + preferences.insertionDelay = 30 + verify(eventBus, atLeastOnce()).post(eventsCaptor.capture()) + assertThat(eventsCaptor.allValues, hasItem(PreferenceChangedEvent("InsertionDelay", 30))) + } + + @Test + fun `setting posts per page to valid value sends change event`() { + preferences.postsPerPage = 30 + verify(eventBus, atLeastOnce()).post(eventsCaptor.capture()) + assertThat(eventsCaptor.allValues, hasItem(PreferenceChangedEvent("PostsPerPage", 30))) + } + +} -- 2.7.4