Add configuration loader to interface with the configuration.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 4 Nov 2014 18:22:00 +0000 (19:22 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 4 Nov 2014 18:22:00 +0000 (19:22 +0100)
src/main/java/net/pterodactylus/sone/database/memory/ConfigurationLoader.java [new file with mode: 0644]
src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java
src/test/java/net/pterodactylus/sone/database/memory/ConfigurationLoaderTest.java [new file with mode: 0644]

diff --git a/src/main/java/net/pterodactylus/sone/database/memory/ConfigurationLoader.java b/src/main/java/net/pterodactylus/sone/database/memory/ConfigurationLoader.java
new file mode 100644 (file)
index 0000000..39f2ca1
--- /dev/null
@@ -0,0 +1,36 @@
+package net.pterodactylus.sone.database.memory;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import net.pterodactylus.util.config.Configuration;
+
+/**
+ * Helper class for interacting with a {@link Configuration}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class ConfigurationLoader {
+
+       private final Configuration configuration;
+
+       public ConfigurationLoader(Configuration configuration) {
+               this.configuration = configuration;
+       }
+
+       public synchronized Set<String> loadKnownPosts() {
+               Set<String> knownPosts = new HashSet<String>();
+               int postCounter = 0;
+               while (true) {
+                       String knownPostId = configuration
+                                       .getStringValue("KnownPosts/" + postCounter++ + "/ID")
+                                       .getValue(null);
+                       if (knownPostId == null) {
+                               break;
+                       }
+                       knownPosts.add(knownPostId);
+               }
+               return knownPosts;
+       }
+
+}
index 3c1dc74..7640f61 100644 (file)
@@ -82,6 +82,7 @@ public class MemoryDatabase extends AbstractService implements Database {
 
        /** The configuration. */
        private final Configuration configuration;
+       private final ConfigurationLoader configurationLoader;
 
        private final Map<String, Sone> allSones = new HashMap<String, Sone>();
 
@@ -130,6 +131,7 @@ public class MemoryDatabase extends AbstractService implements Database {
        public MemoryDatabase(SoneProvider soneProvider, Configuration configuration) {
                this.soneProvider = soneProvider;
                this.configuration = configuration;
+               this.configurationLoader = new ConfigurationLoader(configuration);
        }
 
        //
@@ -731,16 +733,11 @@ public class MemoryDatabase extends AbstractService implements Database {
 
        /** Loads the known posts. */
        private void loadKnownPosts() {
+               Set<String> knownPosts = configurationLoader.loadKnownPosts();
                lock.writeLock().lock();
                try {
-                       int postCounter = 0;
-                       while (true) {
-                               String knownPostId = configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").getValue(null);
-                               if (knownPostId == null) {
-                                       break;
-                               }
-                               knownPosts.add(knownPostId);
-                       }
+                       this.knownPosts.clear();
+                       this.knownPosts.addAll(knownPosts);
                } finally {
                        lock.writeLock().unlock();
                }
diff --git a/src/test/java/net/pterodactylus/sone/database/memory/ConfigurationLoaderTest.java b/src/test/java/net/pterodactylus/sone/database/memory/ConfigurationLoaderTest.java
new file mode 100644 (file)
index 0000000..6449b43
--- /dev/null
@@ -0,0 +1,38 @@
+package net.pterodactylus.sone.database.memory;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.containsInAnyOrder;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.Set;
+
+import net.pterodactylus.sone.TestValue;
+import net.pterodactylus.util.config.Configuration;
+
+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(new TestValue<String>("Post2"));
+               when(configuration.getStringValue("KnownPosts/1/ID"))
+                               .thenReturn(new TestValue<String>("Post1"));
+               when(configuration.getStringValue("KnownPosts/2/ID"))
+                               .thenReturn(new TestValue<String>(null));
+               Set<String> knownPosts = configurationLoader.loadKnownPosts();
+               assertThat(knownPosts, containsInAnyOrder("Post1", "Post2"));
+       }
+
+}