From: David ‘Bombe’ Roden Date: Tue, 4 Nov 2014 18:22:00 +0000 (+0100) Subject: Add configuration loader to interface with the configuration. X-Git-Tag: 0.9-rc1^2~3^2~54 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=f1430dd9fb3ff91b2dc93956e79ca2f2ad6cb1fb;p=Sone.git Add configuration loader to interface with the configuration. --- 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 index 0000000..39f2ca1 --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/database/memory/ConfigurationLoader.java @@ -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 David ‘Bombe’ Roden + */ +public class ConfigurationLoader { + + private final Configuration configuration; + + public ConfigurationLoader(Configuration configuration) { + this.configuration = configuration; + } + + public synchronized Set loadKnownPosts() { + Set knownPosts = new HashSet(); + int postCounter = 0; + while (true) { + String knownPostId = configuration + .getStringValue("KnownPosts/" + postCounter++ + "/ID") + .getValue(null); + if (knownPostId == null) { + break; + } + knownPosts.add(knownPostId); + } + return knownPosts; + } + +} diff --git a/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java b/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java index 3c1dc74..7640f61 100644 --- a/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java +++ b/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java @@ -82,6 +82,7 @@ public class MemoryDatabase extends AbstractService implements Database { /** The configuration. */ private final Configuration configuration; + private final ConfigurationLoader configurationLoader; private final Map allSones = new HashMap(); @@ -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 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 index 0000000..6449b43 --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/database/memory/ConfigurationLoaderTest.java @@ -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 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(new TestValue("Post2")); + when(configuration.getStringValue("KnownPosts/1/ID")) + .thenReturn(new TestValue("Post1")); + when(configuration.getStringValue("KnownPosts/2/ID")) + .thenReturn(new TestValue(null)); + Set knownPosts = configurationLoader.loadKnownPosts(); + assertThat(knownPosts, containsInAnyOrder("Post1", "Post2")); + } + +}