From b6de5505ae5d2f835a235de6e9f23cb83affe4b7 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:24:46 +0100 Subject: [PATCH] Load known post replies in configuration loader, too. --- .../sone/database/memory/ConfigurationLoader.java | 15 +++++++++++++++ .../sone/database/memory/MemoryDatabase.java | 11 +++-------- .../sone/database/memory/ConfigurationLoaderTest.java | 13 +++++++++++++ 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/database/memory/ConfigurationLoader.java b/src/main/java/net/pterodactylus/sone/database/memory/ConfigurationLoader.java index 39f2ca1..078b5f4 100644 --- a/src/main/java/net/pterodactylus/sone/database/memory/ConfigurationLoader.java +++ b/src/main/java/net/pterodactylus/sone/database/memory/ConfigurationLoader.java @@ -33,4 +33,19 @@ public class ConfigurationLoader { return knownPosts; } + public synchronized Set loadKnownPostReplies() { + Set knownPostReplies = new HashSet(); + int replyCounter = 0; + while (true) { + String knownReplyId = configuration + .getStringValue("KnownReplies/" + replyCounter++ + "/ID") + .getValue(null); + if (knownReplyId == null) { + break; + } + knownPostReplies.add(knownReplyId); + } + return knownPostReplies; + } + } 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 7640f61..3c5ef55 100644 --- a/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java +++ b/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java @@ -782,16 +782,11 @@ public class MemoryDatabase extends AbstractService implements Database { /** Loads the known post replies. */ private void loadKnownPostReplies() { + Set knownPostReplies = configurationLoader.loadKnownPostReplies(); lock.writeLock().lock(); try { - int replyCounter = 0; - while (true) { - String knownReplyId = configuration.getStringValue("KnownReplies/" + replyCounter++ + "/ID").getValue(null); - if (knownReplyId == null) { - break; - } - knownPostReplies.add(knownReplyId); - } + this.knownPostReplies.clear(); + this.knownPostReplies.addAll(knownPostReplies); } 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 index 6449b43..82d465b 100644 --- a/src/test/java/net/pterodactylus/sone/database/memory/ConfigurationLoaderTest.java +++ b/src/test/java/net/pterodactylus/sone/database/memory/ConfigurationLoaderTest.java @@ -35,4 +35,17 @@ public class ConfigurationLoaderTest { assertThat(knownPosts, containsInAnyOrder("Post1", "Post2")); } + @Test + public void loaderCanLoadKnownPostReplies() { + when(configuration.getStringValue("KnownReplies/0/ID")) + .thenReturn(new TestValue("PostReply2")); + when(configuration.getStringValue("KnownReplies/1/ID")) + .thenReturn(new TestValue("PostReply1")); + when(configuration.getStringValue("KnownReplies/2/ID")) + .thenReturn(new TestValue(null)); + Set knownPosts = configurationLoader.loadKnownPostReplies(); + assertThat(knownPosts, + containsInAnyOrder("PostReply1", "PostReply2")); + } + } -- 2.7.4