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}.
*/
public class ConfigurationLoader {
+ private static final Logger logger =
+ Logger.getLogger(ConfigurationLoader.class.getName());
private final Configuration configuration;
public ConfigurationLoader(Configuration configuration) {
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);
+ }
+ }
+
}
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;
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());
+ }
+
}