Use event bus to change FCP interface configuration.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 28 Sep 2014 20:23:12 +0000 (22:23 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 28 Sep 2014 20:23:39 +0000 (22:23 +0200)
src/main/java/net/pterodactylus/sone/core/Core.java
src/main/java/net/pterodactylus/sone/core/Preferences.java
src/main/java/net/pterodactylus/sone/fcp/FcpInterface.java
src/main/java/net/pterodactylus/sone/fcp/event/FullAccessRequiredChanged.java [new file with mode: 0644]
src/test/java/net/pterodactylus/sone/core/PreferencesTest.java
src/test/java/net/pterodactylus/sone/fcp/FcpInterfaceTest.java

index 2e2cf3e..12e700e 100644 (file)
@@ -1730,7 +1730,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                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));
-               options.addIntegerOption("FcpFullAccessRequired", new DefaultOption<Integer>(2, fcpInterface.new SetFullAccessRequired()));
+               options.addIntegerOption("FcpFullAccessRequired", new DefaultOption<Integer>(2));
 
                loadConfigurationValue("InsertionDelay");
                loadConfigurationValue("PostsPerPage");
index 1f28c69..7edff1a 100644 (file)
@@ -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;
        }
 
index 763a06e..c2a7cfa 100644 (file)
@@ -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<Integer> {
-
-               @Override
-               public void optionChanged(Option<Integer> 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 (file)
index 0000000..9638880
--- /dev/null
@@ -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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class FullAccessRequiredChanged {
+
+       private final FullAccessRequired fullAccessRequired;
+
+       public FullAccessRequiredChanged(FullAccessRequired fullAccessRequired) {
+               this.fullAccessRequired = fullAccessRequired;
+       }
+
+       public FullAccessRequired getFullAccessRequired() {
+               return fullAccessRequired;
+       }
+
+}
index c79e6ee..65d59e7 100644 (file)
@@ -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<FullAccessRequiredChanged> fullAccessRequiredChangedCaptor = forClass(FullAccessRequiredChanged.class);
+               verify(eventBus).post(fullAccessRequiredChangedCaptor.capture());
+               assertThat(fullAccessRequiredChangedCaptor.getValue().getFullAccessRequired(), is(ALWAYS));
+       }
+
 }
index 337b664..2312907 100644 (file)
@@ -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));
        }