Load bookmarked posts in configuration loader, too.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 4 Nov 2014 18:39:50 +0000 (19:39 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 4 Nov 2014 18:39:50 +0000 (19:39 +0100)
src/main/java/net/pterodactylus/sone/database/memory/ConfigurationLoader.java
src/main/java/net/pterodactylus/sone/database/memory/MemoryBookmarkDatabase.java
src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java
src/test/java/net/pterodactylus/sone/database/memory/ConfigurationLoaderTest.java
src/test/java/net/pterodactylus/sone/database/memory/MemoryBookmarkDatabaseTest.java

index 078b5f4..24505ac 100644 (file)
@@ -48,4 +48,19 @@ public class ConfigurationLoader {
                return knownPostReplies;
        }
 
+       public synchronized Set<String> loadBookmarkedPosts() {
+               Set<String> bookmarkedPosts = new HashSet<String>();
+               int postCounter = 0;
+               while (true) {
+                       String bookmarkedPostId = configuration
+                                       .getStringValue("Bookmarks/Post/" + postCounter++ + "/ID")
+                                       .getValue(null);
+                       if (bookmarkedPostId == null) {
+                               break;
+                       }
+                       bookmarkedPosts.add(bookmarkedPostId);
+               }
+               return bookmarkedPosts;
+       }
+
 }
index 1aec573..78c8a4f 100644 (file)
@@ -21,10 +21,28 @@ public class MemoryBookmarkDatabase implements BookmarkDatabase {
 
        private final ReadWriteLock lock = new ReentrantReadWriteLock();
        private final MemoryDatabase memoryDatabase;
+       private final ConfigurationLoader configurationLoader;
        private final Set<String> bookmarkedPosts = new HashSet<String>();
 
-       public MemoryBookmarkDatabase(MemoryDatabase memoryDatabase) {
+       public MemoryBookmarkDatabase(MemoryDatabase memoryDatabase,
+                       ConfigurationLoader configurationLoader) {
                this.memoryDatabase = memoryDatabase;
+               this.configurationLoader = configurationLoader;
+       }
+
+       public void start() {
+               loadBookmarkedPosts();
+       }
+
+       private void loadBookmarkedPosts() {
+               Set<String> bookmarkedPosts = configurationLoader.loadBookmarkedPosts();
+               lock.writeLock().lock();
+               try {
+                       this.bookmarkedPosts.clear();
+                       this.bookmarkedPosts.addAll(bookmarkedPosts);
+               } finally {
+                       lock.writeLock().unlock();
+               }
        }
 
        @Override
index 3c5ef55..6481a1b 100644 (file)
@@ -116,8 +116,7 @@ public class MemoryDatabase extends AbstractService implements Database {
        private final Map<String, Image> allImages = new HashMap<String, Image>();
        private final Multimap<String, Image> soneImages = HashMultimap.create();
 
-       private final MemoryBookmarkDatabase memoryBookmarkDatabase =
-                       new MemoryBookmarkDatabase(this);
+       private final MemoryBookmarkDatabase memoryBookmarkDatabase;
 
        /**
         * Creates a new memory database.
@@ -132,6 +131,8 @@ public class MemoryDatabase extends AbstractService implements Database {
                this.soneProvider = soneProvider;
                this.configuration = configuration;
                this.configurationLoader = new ConfigurationLoader(configuration);
+               memoryBookmarkDatabase =
+                               new MemoryBookmarkDatabase(this, configurationLoader);
        }
 
        //
@@ -157,6 +158,7 @@ public class MemoryDatabase extends AbstractService implements Database {
        /** {@inheritDocs} */
        @Override
        protected void doStart() {
+               memoryBookmarkDatabase.start();
                loadKnownPosts();
                loadKnownPostReplies();
                notifyStarted();
index 82d465b..de357e9 100644 (file)
@@ -48,4 +48,16 @@ public class ConfigurationLoaderTest {
                                containsInAnyOrder("PostReply1", "PostReply2"));
        }
 
+       @Test
+       public void loaderCanLoadBookmarkedPosts() {
+               when(configuration.getStringValue("Bookmarks/Post/0/ID"))
+                               .thenReturn(new TestValue<String>("Post2"));
+               when(configuration.getStringValue("Bookmarks/Post/1/ID"))
+                               .thenReturn(new TestValue<String>("Post1"));
+               when(configuration.getStringValue("Bookmarks/Post/2/ID"))
+                               .thenReturn(new TestValue<String>(null));
+               Set<String> knownPosts = configurationLoader.loadBookmarkedPosts();
+               assertThat(knownPosts, containsInAnyOrder("Post1", "Post2"));
+       }
+
 }
index 93efe11..ce61ade 100644 (file)
@@ -29,8 +29,9 @@ import org.mockito.stubbing.Answer;
 public class MemoryBookmarkDatabaseTest {
 
        private final MemoryDatabase memoryDatabase = mock(MemoryDatabase.class);
+       private final ConfigurationLoader configurationLoader = mock(ConfigurationLoader.class);
        private final MemoryBookmarkDatabase bookmarkDatabase =
-                       new MemoryBookmarkDatabase(memoryDatabase);
+                       new MemoryBookmarkDatabase(memoryDatabase, configurationLoader);
        private final Map<String, Post> posts = new HashMap<String, Post>();
 
        @Before