Replace test for ConfigurationLoader with Kotlin version
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 6 Feb 2018 05:47:07 +0000 (06:47 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 6 Feb 2018 05:54:38 +0000 (06:54 +0100)
src/test/java/net/pterodactylus/sone/database/memory/ConfigurationLoaderTest.java [deleted file]
src/test/java/net/pterodactylus/sone/test/TestValue.java
src/test/kotlin/net/pterodactylus/sone/database/memory/ConfigurationLoaderTest.kt [new file with mode: 0644]

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 (file)
index 8dfce3d..0000000
+++ /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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
-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.<String>from(null));
-               Set<String> 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.<String>from(null));
-               Set<String> 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.<String>from(null));
-               Set<String> knownPosts = configurationLoader.loadBookmarkedPosts();
-               assertThat(knownPosts, containsInAnyOrder("Post1", "Post2"));
-       }
-
-       @Test
-       public void loaderCanSaveBookmarkedPosts() throws ConfigurationException {
-               final Value<String> post1 = TestValue.<String>from(null);
-               final Value<String> post2 = TestValue.<String>from(null);
-               final Value<String> post3 = TestValue.<String>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<String> originalPosts = new HashSet<String>(asList("Post1", "Post2"));
-               configurationLoader.saveBookmarkedPosts(originalPosts);
-               HashSet<String> extractedPosts =
-                               new HashSet<String>(asList(post1.getValue(), post2.getValue()));
-               assertThat(extractedPosts, containsInAnyOrder("Post1", "Post2"));
-               assertThat(post3.getValue(), nullValue());
-       }
-
-}
index 94ff3c6..454163e 100644 (file)
@@ -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<T> implements Value<T> {
 
        private final AtomicReference<T> value = new AtomicReference<T>();
 
-       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<T> implements Value<T> {
        }
 
        @Override
+       @Nonnull
        public String toString() {
                return String.valueOf(value.get());
        }
 
-       public static <T> Value<T> from(T value) {
+       @Nonnull
+       public static <T> Value<T> from(@Nullable T value) {
                return new TestValue<T>(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 (file)
index 0000000..f0a1d10
--- /dev/null
@@ -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<Configuration>()
+       private val configurationLoader = ConfigurationLoader(configuration)
+
+       private fun setupStringValue(attribute: String, value: String? = null): Value<String?> =
+                       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())
+       }
+
+}