Increase test coverage.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / SoneInserter.java
index 76903f0..734847a 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.
@@ -201,7 +209,6 @@ public class SoneInserter extends AbstractService {
                                        try {
                                                sone.setStatus(SoneStatus.inserting);
                                                long insertTime = currentTimeMillis();
-                                               insertInformation.setTime(insertTime);
                                                eventBus.post(new SoneInsertingEvent(sone));
                                                FreenetURI finalUri = freenetInterface.insertDirectory(insertInformation.getInsertUri(), insertInformation.generateManifestEntries(), "index.html");
                                                eventBus.post(new SoneInsertedEvent(sone, currentTimeMillis() - insertTime));
@@ -242,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
@@ -249,7 +265,8 @@ public class SoneInserter extends AbstractService {
         *
         * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
         */
-       private class InsertInformation {
+       @VisibleForTesting
+       class InsertInformation {
 
                private final String fingerprint;
 
@@ -266,7 +283,7 @@ public class SoneInserter extends AbstractService {
                        this.fingerprint = sone.getFingerprint();
                        soneProperties.put("id", sone.getId());
                        soneProperties.put("name", sone.getName());
-                       soneProperties.put("time", sone.getTime());
+                       soneProperties.put("time", currentTimeMillis());
                        soneProperties.put("requestUri", sone.getRequestUri());
                        soneProperties.put("insertUri", sone.getInsertUri());
                        soneProperties.put("profile", sone.getProfile());
@@ -281,7 +298,8 @@ public class SoneInserter extends AbstractService {
                // ACCESSORS
                //
 
-               private String getFingerprint() {
+               @VisibleForTesting
+               String getFingerprint() {
                        return fingerprint;
                }
 
@@ -294,16 +312,6 @@ public class SoneInserter extends AbstractService {
                        return (FreenetURI) soneProperties.get("insertUri");
                }
 
-               /**
-                * Sets the time of the Sone at the time of the insert.
-                *
-                * @param time
-                *            The time of the Sone
-                */
-               public void setTime(long time) {
-                       soneProperties.put("time", time);
-               }
-
                //
                // ACTIONS
                //