Add class that activates the FCP interface from an option.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 29 Jul 2014 17:53:00 +0000 (19:53 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 29 Jul 2014 17:53:00 +0000 (19:53 +0200)
src/main/java/net/pterodactylus/sone/core/Core.java
src/main/java/net/pterodactylus/sone/fcp/FcpInterface.java
src/test/java/net/pterodactylus/sone/fcp/FcpInterfaceTest.java [new file with mode: 0644]

index 860effb..4ee0258 100644 (file)
@@ -76,6 +76,7 @@ import net.pterodactylus.sone.database.PostReplyProvider;
 import net.pterodactylus.sone.database.SoneProvider;
 import net.pterodactylus.sone.fcp.FcpInterface;
 import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired;
+import net.pterodactylus.sone.fcp.FcpInterface.SetActive;
 import net.pterodactylus.sone.freenet.wot.Identity;
 import net.pterodactylus.sone.freenet.wot.IdentityManager;
 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
@@ -1998,14 +1999,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                options.addIntegerOption("PositiveTrust", new DefaultOption<Integer>(75, new IntegerRangePredicate(0, 100)));
                options.addIntegerOption("NegativeTrust", new DefaultOption<Integer>(-25, new IntegerRangePredicate(-100, 100)));
                options.addStringOption("TrustComment", new DefaultOption<String>("Set from Sone Web Interface"));
-               options.addBooleanOption("ActivateFcpInterface", new DefaultOption<Boolean>(false, new OptionWatcher<Boolean>() {
-
-                       @Override
-                       @SuppressWarnings("synthetic-access")
-                       public void optionChanged(Option<Boolean> option, Boolean oldValue, Boolean newValue) {
-                               fcpInterface.setActive(newValue);
-                       }
-               }));
+               options.addBooleanOption("ActivateFcpInterface", new DefaultOption<Boolean>(false, fcpInterface.new SetActive()));
                options.addIntegerOption("FcpFullAccessRequired", new DefaultOption<Integer>(2, new OptionWatcher<Integer>() {
 
                        @Override
index 02d84cc..c71fd82 100644 (file)
@@ -26,19 +26,22 @@ import java.util.logging.Level;
 import java.util.logging.Logger;
 
 import net.pterodactylus.sone.core.Core;
+import net.pterodactylus.sone.core.Options.Option;
+import net.pterodactylus.sone.core.Options.OptionWatcher;
 import net.pterodactylus.sone.freenet.fcp.Command.AccessType;
 import net.pterodactylus.sone.freenet.fcp.Command.ErrorResponse;
 import net.pterodactylus.sone.freenet.fcp.Command.Response;
 import net.pterodactylus.util.logging.Logging;
 
-import com.google.inject.Inject;
-
 import freenet.pluginmanager.FredPluginFCP;
 import freenet.pluginmanager.PluginNotFoundException;
 import freenet.pluginmanager.PluginReplySender;
 import freenet.support.SimpleFieldSet;
 import freenet.support.api.Bucket;
 
+import com.google.common.annotations.VisibleForTesting;
+import com.google.inject.Inject;
+
 /**
  * Implementation of an FCP interface for other clients or plugins to
  * communicate with Sone.
@@ -106,6 +109,11 @@ public class FcpInterface {
        // ACCESSORS
        //
 
+       @VisibleForTesting
+       boolean isActive() {
+               return active;
+       }
+
        /**
         * Sets whether the FCP interface should handle requests. If {@code active}
         * is {@code false}, all requests are answered with an error.
@@ -216,4 +224,13 @@ public class FcpInterface {
                }
        }
 
+       public class SetActive implements OptionWatcher<Boolean> {
+
+               @Override
+               public void optionChanged(Option<Boolean> option, Boolean oldValue, Boolean newValue) {
+                       setActive(newValue);
+               }
+
+       }
+
 }
diff --git a/src/test/java/net/pterodactylus/sone/fcp/FcpInterfaceTest.java b/src/test/java/net/pterodactylus/sone/fcp/FcpInterfaceTest.java
new file mode 100644 (file)
index 0000000..6ecd687
--- /dev/null
@@ -0,0 +1,32 @@
+package net.pterodactylus.sone.fcp;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+
+import net.pterodactylus.sone.fcp.FcpInterface.SetActive;
+
+import org.junit.Test;
+
+/**
+ * Unit test for {@link FcpInterface} and its subclasses.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class FcpInterfaceTest {
+
+       private final FcpInterface fcpInterface = new FcpInterface(null);
+       private final SetActive setActive = fcpInterface.new SetActive();
+
+       @Test
+       public void setActiveCanActivateFcpInterface() {
+               setActive.optionChanged(null, null, true);
+               assertThat(fcpInterface.isActive(), is(true));
+       }
+
+       @Test
+       public void setActiveCanDeactivateFcpInterface() {
+               setActive.optionChanged(null, null, false);
+               assertThat(fcpInterface.isActive(), is(false));
+       }
+
+}