Add class for setting insertion delay from an option.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 29 Jul 2014 17:47:03 +0000 (19:47 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 29 Jul 2014 17:47:03 +0000 (19:47 +0200)
src/main/java/net/pterodactylus/sone/core/Core.java
src/main/java/net/pterodactylus/sone/core/SoneInserter.java
src/test/java/net/pterodactylus/sone/core/SoneInserterTest.java [new file with mode: 0644]

index 643c835..860effb 100644 (file)
@@ -41,6 +41,7 @@ import java.util.logging.Logger;
 import net.pterodactylus.sone.core.Options.DefaultOption;
 import net.pterodactylus.sone.core.Options.Option;
 import net.pterodactylus.sone.core.Options.OptionWatcher;
+import net.pterodactylus.sone.core.SoneInserter.SetInsertionDelay;
 import net.pterodactylus.sone.core.event.ImageInsertFinishedEvent;
 import net.pterodactylus.sone.core.event.MarkPostKnownEvent;
 import net.pterodactylus.sone.core.event.MarkPostReplyKnownEvent;
@@ -1988,14 +1989,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
         */
        private void loadConfiguration() {
                /* create options. */
-               options.addIntegerOption("InsertionDelay", new DefaultOption<Integer>(60, new IntegerRangePredicate(0, Integer.MAX_VALUE), new OptionWatcher<Integer>() {
-
-                       @Override
-                       public void optionChanged(Option<Integer> option, Integer oldValue, Integer newValue) {
-                               SoneInserter.setInsertionDelay(newValue);
-                       }
-
-               }));
+               options.addIntegerOption("InsertionDelay", new DefaultOption<Integer>(60, new IntegerRangePredicate(0, Integer.MAX_VALUE), new SetInsertionDelay()));
                options.addIntegerOption("PostsPerPage", new DefaultOption<Integer>(10, new IntegerRangePredicate(1, Integer.MAX_VALUE)));
                options.addIntegerOption("ImagesPerPage", new DefaultOption<Integer>(9, new IntegerRangePredicate(1, Integer.MAX_VALUE)));
                options.addIntegerOption("CharactersPerPost", new DefaultOption<Integer>(400, Predicates.<Integer> or(new IntegerRangePredicate(50, Integer.MAX_VALUE), Predicates.equalTo(-1))));
index 60eebec..46a3fe7 100644 (file)
@@ -32,6 +32,8 @@ import java.util.concurrent.atomic.AtomicInteger;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
+import net.pterodactylus.sone.core.Options.Option;
+import net.pterodactylus.sone.core.Options.OptionWatcher;
 import net.pterodactylus.sone.core.event.SoneInsertAbortedEvent;
 import net.pterodactylus.sone.core.event.SoneInsertedEvent;
 import net.pterodactylus.sone.core.event.SoneInsertingEvent;
@@ -54,6 +56,7 @@ import net.pterodactylus.util.template.TemplateException;
 import net.pterodactylus.util.template.TemplateParser;
 import net.pterodactylus.util.template.XmlFilter;
 
+import com.google.common.annotations.VisibleForTesting;
 import com.google.common.collect.FluentIterable;
 import com.google.common.collect.Ordering;
 import com.google.common.eventbus.EventBus;
@@ -138,6 +141,11 @@ public class SoneInserter extends AbstractService {
                return this;
        }
 
+       @VisibleForTesting
+       static AtomicInteger getInsertionDelay() {
+               return insertionDelay;
+       }
+
        /**
         * Changes the insertion delay, i.e. the time the Sone inserter waits after it
         * has noticed a Sone modification before it starts the insert.
@@ -241,6 +249,15 @@ public class SoneInserter extends AbstractService {
                }
        }
 
+       static class SetInsertionDelay implements OptionWatcher<Integer> {
+
+               @Override
+               public void optionChanged(Option<Integer> option, Integer oldValue, Integer newValue) {
+                       setInsertionDelay(newValue);
+               }
+
+       }
+
        /**
         * Container for information that are required to insert a Sone. This
         * container merely exists to copy all relevant data without holding a lock
diff --git a/src/test/java/net/pterodactylus/sone/core/SoneInserterTest.java b/src/test/java/net/pterodactylus/sone/core/SoneInserterTest.java
new file mode 100644 (file)
index 0000000..ed436fe
--- /dev/null
@@ -0,0 +1,24 @@
+package net.pterodactylus.sone.core;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+
+import net.pterodactylus.sone.core.SoneInserter.SetInsertionDelay;
+
+import org.junit.Test;
+
+/**
+ * Unit test for {@link SoneInserter} and its subclasses.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class SoneInserterTest {
+
+       @Test
+       public void insertionDelayIsForwardedToSoneInserter() {
+               SetInsertionDelay setInsertionDelay = new SetInsertionDelay();
+               setInsertionDelay.optionChanged(null, null, 15);
+               assertThat(SoneInserter.getInsertionDelay().get(), is(15));
+       }
+
+}