From fdbdc26c744865a7200017cb117943d48df498ad Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 28 Sep 2014 22:23:12 +0200 Subject: [PATCH] Use event bus to change FCP interface configuration. --- .../java/net/pterodactylus/sone/core/Core.java | 2 +- .../net/pterodactylus/sone/core/Preferences.java | 2 ++ .../net/pterodactylus/sone/fcp/FcpInterface.java | 19 +++++------------ .../sone/fcp/event/FullAccessRequiredChanged.java | 24 ++++++++++++++++++++++ .../pterodactylus/sone/core/PreferencesTest.java | 12 +++++++++++ .../pterodactylus/sone/fcp/FcpInterfaceTest.java | 12 ++++++----- 6 files changed, 51 insertions(+), 20 deletions(-) create mode 100644 src/main/java/net/pterodactylus/sone/fcp/event/FullAccessRequiredChanged.java diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index 2e2cf3e..12e700e 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -1730,7 +1730,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, 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)); - options.addIntegerOption("FcpFullAccessRequired", new DefaultOption(2, fcpInterface.new SetFullAccessRequired())); + options.addIntegerOption("FcpFullAccessRequired", new DefaultOption(2)); loadConfigurationValue("InsertionDelay"); loadConfigurationValue("PostsPerPage"); diff --git a/src/main/java/net/pterodactylus/sone/core/Preferences.java b/src/main/java/net/pterodactylus/sone/core/Preferences.java index 1f28c69..7edff1a 100644 --- a/src/main/java/net/pterodactylus/sone/core/Preferences.java +++ b/src/main/java/net/pterodactylus/sone/core/Preferences.java @@ -22,6 +22,7 @@ import net.pterodactylus.sone.fcp.FcpInterface; import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired; import net.pterodactylus.sone.fcp.event.FcpInterfaceActivatedEvent; import net.pterodactylus.sone.fcp.event.FcpInterfaceDeactivatedEvent; +import net.pterodactylus.sone.fcp.event.FullAccessRequiredChanged; import com.google.common.eventbus.EventBus; @@ -375,6 +376,7 @@ public class Preferences { */ public Preferences setFcpFullAccessRequired(FullAccessRequired fcpFullAccessRequired) { options.getIntegerOption("FcpFullAccessRequired").set((fcpFullAccessRequired != null) ? fcpFullAccessRequired.ordinal() : null); + eventBus.post(new FullAccessRequiredChanged(fcpFullAccessRequired)); return this; } diff --git a/src/main/java/net/pterodactylus/sone/fcp/FcpInterface.java b/src/main/java/net/pterodactylus/sone/fcp/FcpInterface.java index 763a06e..c2a7cfa 100644 --- a/src/main/java/net/pterodactylus/sone/fcp/FcpInterface.java +++ b/src/main/java/net/pterodactylus/sone/fcp/FcpInterface.java @@ -32,6 +32,7 @@ import net.pterodactylus.sone.core.Options.Option; import net.pterodactylus.sone.core.Options.OptionWatcher; import net.pterodactylus.sone.fcp.event.FcpInterfaceActivatedEvent; import net.pterodactylus.sone.fcp.event.FcpInterfaceDeactivatedEvent; +import net.pterodactylus.sone.fcp.event.FullAccessRequiredChanged; import net.pterodactylus.sone.freenet.fcp.Command.AccessType; import net.pterodactylus.sone.freenet.fcp.Command.ErrorResponse; import net.pterodactylus.sone.freenet.fcp.Command.Response; @@ -130,13 +131,7 @@ public class FcpInterface { return fullAccessRequired.get(); } - /** - * Sets the action level for which full FCP access is required. - * - * @param fullAccessRequired - * The action level for which full FCP access is required - */ - public void setFullAccessRequired(FullAccessRequired fullAccessRequired) { + private void setFullAccessRequired(FullAccessRequired fullAccessRequired) { this.fullAccessRequired.set(checkNotNull(fullAccessRequired, "fullAccessRequired must not be null")); } @@ -238,13 +233,9 @@ public class FcpInterface { setActive(false); } - public class SetFullAccessRequired implements OptionWatcher { - - @Override - public void optionChanged(Option option, Integer oldValue, Integer newValue) { - setFullAccessRequired(FullAccessRequired.values()[newValue]); - } - + @Subscribe + public void fullAccessRequiredChanged(FullAccessRequiredChanged fullAccessRequiredChanged) { + setFullAccessRequired(fullAccessRequiredChanged.getFullAccessRequired()); } } diff --git a/src/main/java/net/pterodactylus/sone/fcp/event/FullAccessRequiredChanged.java b/src/main/java/net/pterodactylus/sone/fcp/event/FullAccessRequiredChanged.java new file mode 100644 index 0000000..9638880 --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/fcp/event/FullAccessRequiredChanged.java @@ -0,0 +1,24 @@ +package net.pterodactylus.sone.fcp.event; + +import net.pterodactylus.sone.fcp.FcpInterface; +import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired; + +/** + * Event that signals that the {@link FcpInterface}’s {@link + * FullAccessRequired} parameter was changed in the configuration. + * + * @author David ‘Bombe’ Roden + */ +public class FullAccessRequiredChanged { + + private final FullAccessRequired fullAccessRequired; + + public FullAccessRequiredChanged(FullAccessRequired fullAccessRequired) { + this.fullAccessRequired = fullAccessRequired; + } + + public FullAccessRequired getFullAccessRequired() { + return fullAccessRequired; + } + +} diff --git a/src/test/java/net/pterodactylus/sone/core/PreferencesTest.java b/src/test/java/net/pterodactylus/sone/core/PreferencesTest.java index c79e6ee..65d59e7 100644 --- a/src/test/java/net/pterodactylus/sone/core/PreferencesTest.java +++ b/src/test/java/net/pterodactylus/sone/core/PreferencesTest.java @@ -4,6 +4,7 @@ import static net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired.ALWAYS; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.is; +import static org.mockito.ArgumentCaptor.forClass; import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; @@ -15,10 +16,12 @@ import net.pterodactylus.sone.core.event.InsertionDelayChangedEvent; import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired; import net.pterodactylus.sone.fcp.event.FcpInterfaceActivatedEvent; import net.pterodactylus.sone.fcp.event.FcpInterfaceDeactivatedEvent; +import net.pterodactylus.sone.fcp.event.FullAccessRequiredChanged; import com.google.common.eventbus.EventBus; import org.junit.Before; import org.junit.Test; +import org.mockito.ArgumentCaptor; /** * Unit test for {@link Preferences}. @@ -252,4 +255,13 @@ public class PreferencesTest { verify(integerOption).set(null); } + @Test + public void settingFcpFullAccessRequiredIsForwardedToEventBus() { + preferences.setFcpFullAccessRequired(ALWAYS); + verify(integerOption).set(2); + ArgumentCaptor fullAccessRequiredChangedCaptor = forClass(FullAccessRequiredChanged.class); + verify(eventBus).post(fullAccessRequiredChangedCaptor.capture()); + assertThat(fullAccessRequiredChangedCaptor.getValue().getFullAccessRequired(), is(ALWAYS)); + } + } diff --git a/src/test/java/net/pterodactylus/sone/fcp/FcpInterfaceTest.java b/src/test/java/net/pterodactylus/sone/fcp/FcpInterfaceTest.java index 337b664..2312907 100644 --- a/src/test/java/net/pterodactylus/sone/fcp/FcpInterfaceTest.java +++ b/src/test/java/net/pterodactylus/sone/fcp/FcpInterfaceTest.java @@ -6,9 +6,9 @@ import static net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired.WRITING import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; -import net.pterodactylus.sone.fcp.FcpInterface.SetFullAccessRequired; import net.pterodactylus.sone.fcp.event.FcpInterfaceActivatedEvent; import net.pterodactylus.sone.fcp.event.FcpInterfaceDeactivatedEvent; +import net.pterodactylus.sone.fcp.event.FullAccessRequiredChanged; import org.junit.Test; @@ -20,7 +20,6 @@ import org.junit.Test; public class FcpInterfaceTest { private final FcpInterface fcpInterface = new FcpInterface(null); - private final SetFullAccessRequired setFullAccessRequired = fcpInterface.new SetFullAccessRequired(); @Test public void fcpInterfaceCanBeActivated() { @@ -36,19 +35,22 @@ public class FcpInterfaceTest { @Test public void setFullAccessRequiredCanSetAccessToNo() { - setFullAccessRequired.optionChanged(null, null, 0); + fcpInterface.fullAccessRequiredChanged( + new FullAccessRequiredChanged(NO)); assertThat(fcpInterface.getFullAccessRequired(), is(NO)); } @Test public void setFullAccessRequiredCanSetAccessToWriting() { - setFullAccessRequired.optionChanged(null, null, 1); + fcpInterface.fullAccessRequiredChanged( + new FullAccessRequiredChanged(WRITING)); assertThat(fcpInterface.getFullAccessRequired(), is(WRITING)); } @Test public void setFullAccessRequiredCanSetAccessToAlways() { - setFullAccessRequired.optionChanged(null, null, 2); + fcpInterface.fullAccessRequiredChanged( + new FullAccessRequiredChanged(ALWAYS)); assertThat(fcpInterface.getFullAccessRequired(), is(ALWAYS)); } -- 2.7.4