Add method to save bookmarked posts to configuration loader.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 4 Nov 2014 19:01:39 +0000 (20:01 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 4 Nov 2014 19:01:39 +0000 (20:01 +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

index b9ac859..fe23153 100644 (file)
@@ -1,9 +1,13 @@
 package net.pterodactylus.sone.database.memory;
 
+import static java.util.logging.Level.WARNING;
+
 import java.util.HashSet;
 import java.util.Set;
+import java.util.logging.Logger;
 
 import net.pterodactylus.util.config.Configuration;
+import net.pterodactylus.util.config.ConfigurationException;
 
 /**
  * Helper class for interacting with a {@link Configuration}.
@@ -12,6 +16,8 @@ import net.pterodactylus.util.config.Configuration;
  */
 public class ConfigurationLoader {
 
+       private static final Logger logger =
+                       Logger.getLogger(ConfigurationLoader.class.getName());
        private final Configuration configuration;
 
        public ConfigurationLoader(Configuration configuration) {
@@ -45,4 +51,25 @@ public class ConfigurationLoader {
                return ids;
        }
 
+       public synchronized void saveBookmarkedPosts(
+                       Set<String> bookmarkedPosts) {
+               saveIds("Bookmarks/Post", bookmarkedPosts);
+       }
+
+       private void saveIds(String prefix, Set<String> bookmarkedPosts) {
+               try {
+                       int idCounter = 0;
+                       for (String id : bookmarkedPosts) {
+                               configuration
+                                               .getStringValue(prefix + "/" + idCounter++ + "/ID")
+                                               .setValue(id);
+                       }
+                       configuration
+                                       .getStringValue(prefix + "/" + idCounter + "/ID")
+                                       .setValue(null);
+               } catch (ConfigurationException ce1) {
+                       logger.log(WARNING, "Could not save bookmarked posts!", ce1);
+               }
+       }
+
 }
index 4c99c53..34ff024 100644 (file)
@@ -45,6 +45,19 @@ public class MemoryBookmarkDatabase implements BookmarkDatabase {
                }
        }
 
+       public void stop() {
+               saveBookmarkedPosts();
+       }
+
+       private void saveBookmarkedPosts() {
+               lock.readLock().lock();
+               try {
+                       configurationLoader.saveBookmarkedPosts(this.bookmarkedPosts);
+               } finally {
+                       lock.readLock().unlock();
+               }
+       }
+
        @Override
        public void bookmarkPost(Post post) {
                lock.writeLock().lock();
index 18a5718..0f795eb 100644 (file)
@@ -168,6 +168,7 @@ public class MemoryDatabase extends AbstractService implements Database {
        @Override
        protected void doStop() {
                try {
+                       memoryBookmarkDatabase.stop();
                        save();
                        notifyStopped();
                } catch (DatabaseException de1) {
index de357e9..55b66ee 100644 (file)
@@ -1,14 +1,18 @@
 package net.pterodactylus.sone.database.memory;
 
+import static java.util.Arrays.asList;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.containsInAnyOrder;
+import static org.hamcrest.Matchers.nullValue;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
+import java.util.HashSet;
 import java.util.Set;
 
 import net.pterodactylus.sone.TestValue;
 import net.pterodactylus.util.config.Configuration;
+import net.pterodactylus.util.config.ConfigurationException;
 
 import org.junit.Test;
 
@@ -60,4 +64,24 @@ public class ConfigurationLoaderTest {
                assertThat(knownPosts, containsInAnyOrder("Post1", "Post2"));
        }
 
+       @Test
+       public void loaderCanSaveBookmarkedPosts() throws ConfigurationException {
+               final TestValue<String> post1 = new TestValue<String>(null);
+               final TestValue<String> post2 = new TestValue<String>(null);
+               final TestValue<String> post3 = new TestValue<String>(null);
+               when(configuration.getStringValue("Bookmarks/Post/0/ID"))
+                               .thenReturn(post1);
+               when(configuration.getStringValue("Bookmarks/Post/1/ID"))
+                               .thenReturn(post2);
+               when(configuration.getStringValue("Bookmarks/Post/2/ID"))
+                               .thenReturn(post3);
+               HashSet<String> originalPosts =
+                               new HashSet<String>(asList("Post1", "Post2"));
+               configurationLoader.saveBookmarkedPosts(originalPosts);
+               HashSet<String> extractedPosts = new HashSet<String>(
+                               asList(post1.getValue(), post2.getValue()));
+               assertThat(extractedPosts, containsInAnyOrder("Post1", "Post2"));
+               assertThat(post3.getValue(), nullValue());
+       }
+
 }