From f1430dd9fb3ff91b2dc93956e79ca2f2ad6cb1fb Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 4 Nov 2014 19:22:00 +0100 Subject: [PATCH] Add configuration loader to interface with the configuration. --- .../sone/database/memory/ConfigurationLoader.java | 36 ++++++++++++++++++++ .../sone/database/memory/MemoryDatabase.java | 13 +++----- .../database/memory/ConfigurationLoaderTest.java | 38 ++++++++++++++++++++++ 3 files changed, 79 insertions(+), 8 deletions(-) create mode 100644 src/main/java/net/pterodactylus/sone/database/memory/ConfigurationLoader.java create mode 100644 src/test/java/net/pterodactylus/sone/database/memory/ConfigurationLoaderTest.java 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")); + } + +} -- 2.7.4