+++ /dev/null
-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());
- }
-
-}
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;
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);
}
}
@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);
}
--- /dev/null
+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())
+ }
+
+}