--- /dev/null
+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;
+ }
+
+}
/** The configuration. */
private final Configuration configuration;
+ private final ConfigurationLoader configurationLoader;
private final Map<String, Sone> allSones = new HashMap<String, Sone>();
public MemoryDatabase(SoneProvider soneProvider, Configuration configuration) {
this.soneProvider = soneProvider;
this.configuration = configuration;
+ this.configurationLoader = new ConfigurationLoader(configuration);
}
//
/** 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();
}
--- /dev/null
+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"));
+ }
+
+}