Add “WatchFeeds” command
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Sun, 11 Sep 2016 15:00:59 +0000 (17:00 +0200)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Sun, 11 Sep 2016 15:00:59 +0000 (17:00 +0200)
src/main/java/net/pterodactylus/fcp/WatchFeeds.java [new file with mode: 0644]
src/test/java/net/pterodactylus/fcp/AbstractFcpMessageTest.java [new file with mode: 0644]
src/test/java/net/pterodactylus/fcp/FcpMessageTest.java
src/test/java/net/pterodactylus/fcp/WatchFeedsTest.java [new file with mode: 0644]

diff --git a/src/main/java/net/pterodactylus/fcp/WatchFeeds.java b/src/main/java/net/pterodactylus/fcp/WatchFeeds.java
new file mode 100644 (file)
index 0000000..e71df53
--- /dev/null
@@ -0,0 +1,15 @@
+package net.pterodactylus.fcp;
+
+/**
+ * Implementation of the “WatchFeeds” command.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class WatchFeeds extends FcpMessage {
+
+       public WatchFeeds(boolean enabled) {
+               super("WatchFeeds");
+               setField("Enabled", String.valueOf(enabled));
+       }
+
+}
diff --git a/src/test/java/net/pterodactylus/fcp/AbstractFcpMessageTest.java b/src/test/java/net/pterodactylus/fcp/AbstractFcpMessageTest.java
new file mode 100644 (file)
index 0000000..d5c9d24
--- /dev/null
@@ -0,0 +1,21 @@
+package net.pterodactylus.fcp;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * Base test for all tests that verify a message’s appearance.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class AbstractFcpMessageTest {
+
+       protected List<String> encodeMessage(FcpMessage fcpMessage) throws Exception {
+               ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+               fcpMessage.write(outputStream);
+               return Arrays.asList(outputStream.toString().split("\r?\n"));
+       }
+
+}
index bc0dbd1..e2b9fcf 100644 (file)
@@ -4,8 +4,6 @@ import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.contains;
 
 import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.util.Arrays;
 import java.util.List;
 
 import org.junit.Test;
@@ -15,16 +13,14 @@ import org.junit.Test;
  *
  * @author <a href="mailto:david.roden@bietr.de">David Roden</a>
  */
-public class FcpMessageTest {
+public class FcpMessageTest extends AbstractFcpMessageTest {
 
        private final FcpMessage fcpMessage = new FcpMessage("TestMessage");
 
        @Test
        public void fcpMessageWithPayloadIsTerminatedByData() throws Exception {
                fcpMessage.setPayloadInputStream(new ByteArrayInputStream("Test".getBytes()));
-               ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
-               fcpMessage.write(outputStream);
-               List<String> lines = Arrays.asList(outputStream.toString().split("\r?\n"));
+               List<String> lines = encodeMessage(fcpMessage);
                assertThat(lines, contains(
                        "TestMessage",
                        "Data",
diff --git a/src/test/java/net/pterodactylus/fcp/WatchFeedsTest.java b/src/test/java/net/pterodactylus/fcp/WatchFeedsTest.java
new file mode 100644 (file)
index 0000000..e4bedfe
--- /dev/null
@@ -0,0 +1,35 @@
+package net.pterodactylus.fcp;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.contains;
+
+import org.junit.Test;
+
+/**
+ * Unit test for {@link WatchFeeds}.
+ *
+ * @author <a href="mailto:david.roden@bietr.de">David Roden</a>
+ */
+public class WatchFeedsTest extends AbstractFcpMessageTest {
+
+       @Test
+       public void enablingWatchFeedsSendsCorrectOutput() throws Exception {
+               WatchFeeds watchFeeds = new WatchFeeds(true);
+               assertThat(encodeMessage(watchFeeds), contains(
+                       "WatchFeeds",
+                       "Enabled=true",
+                       "EndMessage"
+               ));
+       }
+
+       @Test
+       public void disablingWatchFeedsSendsCorrectOutput() throws Exception {
+               WatchFeeds watchFeeds = new WatchFeeds(false);
+               assertThat(encodeMessage(watchFeeds), contains(
+                       "WatchFeeds",
+                       "Enabled=false",
+                       "EndMessage"
+               ));
+       }
+
+}