From: David ‘Bombe’ Roden Date: Tue, 6 Feb 2018 05:47:07 +0000 (+0100) Subject: Replace test for ConfigurationLoader with Kotlin version X-Git-Tag: v79^2~147 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=050cb5f8b7128ae6a23dba72503ca85f327b91e2;p=Sone.git Replace test for ConfigurationLoader with Kotlin version --- diff --git a/src/test/java/net/pterodactylus/sone/database/memory/ConfigurationLoaderTest.java b/src/test/java/net/pterodactylus/sone/database/memory/ConfigurationLoaderTest.java deleted file mode 100644 index 8dfce3d..0000000 --- a/src/test/java/net/pterodactylus/sone/database/memory/ConfigurationLoaderTest.java +++ /dev/null @@ -1,84 +0,0 @@ -package net.pterodactylus.sone.database.memory; - -import static java.util.Arrays.asList; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.containsInAnyOrder; -import static org.hamcrest.Matchers.nullValue; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import java.util.HashSet; -import java.util.Set; - -import net.pterodactylus.sone.test.TestValue; -import net.pterodactylus.util.config.Configuration; -import net.pterodactylus.util.config.ConfigurationException; -import net.pterodactylus.util.config.Value; - -import org.junit.Test; - -/** - * Unit test for {@link ConfigurationLoader}. - * - * @author David ‘Bombe’ Roden - */ -public class ConfigurationLoaderTest { - - private final Configuration configuration = mock(Configuration.class); - private final ConfigurationLoader configurationLoader = - new ConfigurationLoader(configuration); - - @Test - public void loaderCanLoadKnownPosts() { - when(configuration.getStringValue("KnownPosts/0/ID")) - .thenReturn(TestValue.from("Post2")); - when(configuration.getStringValue("KnownPosts/1/ID")) - .thenReturn(TestValue.from("Post1")); - when(configuration.getStringValue("KnownPosts/2/ID")) - .thenReturn(TestValue.from(null)); - Set knownPosts = configurationLoader.loadKnownPosts(); - assertThat(knownPosts, containsInAnyOrder("Post1", "Post2")); - } - - @Test - public void loaderCanLoadKnownPostReplies() { - when(configuration.getStringValue("KnownReplies/0/ID")) - .thenReturn(TestValue.from("PostReply2")); - when(configuration.getStringValue("KnownReplies/1/ID")) - .thenReturn(TestValue.from("PostReply1")); - when(configuration.getStringValue("KnownReplies/2/ID")) - .thenReturn(TestValue.from(null)); - Set knownPosts = configurationLoader.loadKnownPostReplies(); - assertThat(knownPosts, - containsInAnyOrder("PostReply1", "PostReply2")); - } - - @Test - public void loaderCanLoadBookmarkedPosts() { - when(configuration.getStringValue("Bookmarks/Post/0/ID")) - .thenReturn(TestValue.from("Post2")); - when(configuration.getStringValue("Bookmarks/Post/1/ID")) - .thenReturn(TestValue.from("Post1")); - when(configuration.getStringValue("Bookmarks/Post/2/ID")) - .thenReturn(TestValue.from(null)); - Set knownPosts = configurationLoader.loadBookmarkedPosts(); - assertThat(knownPosts, containsInAnyOrder("Post1", "Post2")); - } - - @Test - public void loaderCanSaveBookmarkedPosts() throws ConfigurationException { - final Value post1 = TestValue.from(null); - final Value post2 = TestValue.from(null); - final Value post3 = TestValue.from(null); - when(configuration.getStringValue("Bookmarks/Post/0/ID")).thenReturn(post1); - when(configuration.getStringValue("Bookmarks/Post/1/ID")).thenReturn(post2); - when(configuration.getStringValue("Bookmarks/Post/2/ID")).thenReturn(post3); - HashSet originalPosts = new HashSet(asList("Post1", "Post2")); - configurationLoader.saveBookmarkedPosts(originalPosts); - HashSet extractedPosts = - new HashSet(asList(post1.getValue(), post2.getValue())); - assertThat(extractedPosts, containsInAnyOrder("Post1", "Post2")); - assertThat(post3.getValue(), nullValue()); - } - -} diff --git a/src/test/java/net/pterodactylus/sone/test/TestValue.java b/src/test/java/net/pterodactylus/sone/test/TestValue.java index 94ff3c6..454163e 100644 --- a/src/test/java/net/pterodactylus/sone/test/TestValue.java +++ b/src/test/java/net/pterodactylus/sone/test/TestValue.java @@ -2,6 +2,9 @@ package net.pterodactylus.sone.test; import java.util.concurrent.atomic.AtomicReference; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + import net.pterodactylus.util.config.ConfigurationException; import net.pterodactylus.util.config.Value; @@ -16,23 +19,25 @@ public class TestValue implements Value { private final AtomicReference value = new AtomicReference(); - public TestValue(T originalValue) { + public TestValue(@Nullable T originalValue) { value.set(originalValue); } @Override + @Nullable public T getValue() throws ConfigurationException { return value.get(); } @Override - public T getValue(T defaultValue) { + @Nullable + public T getValue(@Nullable T defaultValue) { final T realValue = value.get(); return (realValue != null) ? realValue : defaultValue; } @Override - public void setValue(T newValue) throws ConfigurationException { + public void setValue(@Nullable T newValue) throws ConfigurationException { value.set(newValue); } @@ -48,11 +53,13 @@ public class TestValue implements Value { } @Override + @Nonnull public String toString() { return String.valueOf(value.get()); } - public static Value from(T value) { + @Nonnull + public static Value from(@Nullable T value) { return new TestValue(value); } diff --git a/src/test/kotlin/net/pterodactylus/sone/database/memory/ConfigurationLoaderTest.kt b/src/test/kotlin/net/pterodactylus/sone/database/memory/ConfigurationLoaderTest.kt new file mode 100644 index 0000000..f0a1d10 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/database/memory/ConfigurationLoaderTest.kt @@ -0,0 +1,67 @@ +package net.pterodactylus.sone.database.memory + +import net.pterodactylus.sone.test.TestValue.from +import net.pterodactylus.sone.test.mock +import net.pterodactylus.sone.test.whenever +import net.pterodactylus.util.config.Configuration +import net.pterodactylus.util.config.Value +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.containsInAnyOrder +import org.hamcrest.Matchers.nullValue +import org.junit.Test + +/** + * Unit test for [ConfigurationLoader]. + * + * @author [David ‘Bombe’ Roden](mailto:bombe@pterodactylus.net) + */ +class ConfigurationLoaderTest { + + private val configuration = mock() + private val configurationLoader = ConfigurationLoader(configuration) + + private fun setupStringValue(attribute: String, value: String? = null): Value = + from(value).apply { + whenever(configuration.getStringValue(attribute)).thenReturn(this) + } + + @Test + fun `loader can load known posts`() { + setupStringValue("KnownPosts/0/ID", "Post2") + setupStringValue("KnownPosts/1/ID", "Post1") + setupStringValue("KnownPosts/2/ID") + val knownPosts = configurationLoader.loadKnownPosts() + assertThat(knownPosts, containsInAnyOrder("Post1", "Post2")) + } + + @Test + fun `loader can load known post replies`() { + setupStringValue("KnownReplies/0/ID", "PostReply2") + setupStringValue("KnownReplies/1/ID", "PostReply1") + setupStringValue("KnownReplies/2/ID") + val knownPosts = configurationLoader.loadKnownPostReplies() + assertThat(knownPosts, containsInAnyOrder("PostReply1", "PostReply2")) + } + + @Test + fun `loader can load bookmarked posts`() { + setupStringValue("Bookmarks/Post/0/ID", "Post2") + setupStringValue("Bookmarks/Post/1/ID", "Post1") + setupStringValue("Bookmarks/Post/2/ID") + val knownPosts = configurationLoader.loadBookmarkedPosts() + assertThat(knownPosts, containsInAnyOrder("Post1", "Post2")) + } + + @Test + fun `loader can save bookmarked posts`() { + val post1 = setupStringValue("Bookmarks/Post/0/ID") + val post2 = setupStringValue("Bookmarks/Post/1/ID") + val post3 = setupStringValue("Bookmarks/Post/2/ID") + val originalPosts = setOf("Post1", "Post2") + configurationLoader.saveBookmarkedPosts(originalPosts) + val extractedPosts = setOf(post1.value, post2.value) + assertThat(extractedPosts, containsInAnyOrder("Post1", "Post2")) + assertThat(post3.value, nullValue()) + } + +}