From b343a185e8b89ea5973bf57ec74217652834f5ea Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 29 Jul 2014 19:53:00 +0200 Subject: [PATCH] Add class that activates the FCP interface from an option. --- .../java/net/pterodactylus/sone/core/Core.java | 10 ++----- .../net/pterodactylus/sone/fcp/FcpInterface.java | 21 ++++++++++++-- .../pterodactylus/sone/fcp/FcpInterfaceTest.java | 32 ++++++++++++++++++++++ 3 files changed, 53 insertions(+), 10 deletions(-) create mode 100644 src/test/java/net/pterodactylus/sone/fcp/FcpInterfaceTest.java diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index 860effb..4ee0258 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -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(75, new IntegerRangePredicate(0, 100))); options.addIntegerOption("NegativeTrust", new DefaultOption(-25, new IntegerRangePredicate(-100, 100))); options.addStringOption("TrustComment", new DefaultOption("Set from Sone Web Interface")); - options.addBooleanOption("ActivateFcpInterface", new DefaultOption(false, new OptionWatcher() { - - @Override - @SuppressWarnings("synthetic-access") - public void optionChanged(Option option, Boolean oldValue, Boolean newValue) { - fcpInterface.setActive(newValue); - } - })); + options.addBooleanOption("ActivateFcpInterface", new DefaultOption(false, fcpInterface.new SetActive())); options.addIntegerOption("FcpFullAccessRequired", new DefaultOption(2, new OptionWatcher() { @Override diff --git a/src/main/java/net/pterodactylus/sone/fcp/FcpInterface.java b/src/main/java/net/pterodactylus/sone/fcp/FcpInterface.java index 02d84cc..c71fd82 100644 --- a/src/main/java/net/pterodactylus/sone/fcp/FcpInterface.java +++ b/src/main/java/net/pterodactylus/sone/fcp/FcpInterface.java @@ -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 { + + @Override + public void optionChanged(Option 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 index 0000000..6ecd687 --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/fcp/FcpInterfaceTest.java @@ -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 David ‘Bombe’ Roden + */ +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)); + } + +} -- 2.7.4