♻️ Refactor FCP message test base class
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 8 Feb 2023 11:44:30 +0000 (12:44 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 8 Feb 2023 11:44:30 +0000 (12:44 +0100)
All the methods have been moved to helper classes in the “test” package, and
the abstract class itself has been removed.

src/test/java/net/pterodactylus/fcp/AbstractFcpMessageTest.java [deleted file]
src/test/java/net/pterodactylus/fcp/AddPeerTest.java
src/test/java/net/pterodactylus/fcp/FcpMessageTest.java
src/test/java/net/pterodactylus/fcp/WatchFeedsTest.java
src/test/java/net/pterodactylus/fcp/test/Matchers.java [new file with mode: 0644]
src/test/java/net/pterodactylus/fcp/test/MessageUtils.java [new file with mode: 0644]

diff --git a/src/test/java/net/pterodactylus/fcp/AbstractFcpMessageTest.java b/src/test/java/net/pterodactylus/fcp/AbstractFcpMessageTest.java
deleted file mode 100644 (file)
index d5c9d24..0000000
+++ /dev/null
@@ -1,21 +0,0 @@
-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 154d8d0..15b27e5 100644 (file)
@@ -1,13 +1,7 @@
 package net.pterodactylus.fcp;
 
 import java.net.URL;
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
 
-import org.hamcrest.Description;
-import org.hamcrest.Matcher;
-import org.hamcrest.TypeSafeDiagnosingMatcher;
 import org.junit.Test;
 
 import static net.pterodactylus.fcp.AddPeer.Trust.HIGH;
@@ -16,18 +10,18 @@ import static net.pterodactylus.fcp.AddPeer.Trust.NORMAL;
 import static net.pterodactylus.fcp.AddPeer.Visibility.NAME_ONLY;
 import static net.pterodactylus.fcp.AddPeer.Visibility.NO;
 import static net.pterodactylus.fcp.AddPeer.Visibility.YES;
+import static net.pterodactylus.fcp.test.Matchers.isMessage;
 import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.containsInAnyOrder;
 
 /**
  * Unit test for {@link AddPeer}.
  */
-public class AddPeerTest extends AbstractFcpMessageTest {
+public class AddPeerTest {
 
        @Test
-       public void canCreateAddPeerWithFile() throws Exception {
+       public void canCreateAddPeerWithFile() {
                AddPeer addPeer = new AddPeer(LOW, NO, "file");
-               assertThat(encodeMessage(addPeer), isMessage("AddPeer",
+               assertThat(addPeer, isMessage("AddPeer",
                                "File=file",
                                "Trust=LOW",
                                "Visibility=NO"
@@ -35,9 +29,9 @@ public class AddPeerTest extends AbstractFcpMessageTest {
        }
 
        @Test
-       public void canCreateAddPeerWithIdentifierAndFile() throws Exception {
+       public void canCreateAddPeerWithIdentifierAndFile() {
                AddPeer addPeer = new AddPeer(LOW, NO, "identifier", "file");
-               assertThat(encodeMessage(addPeer), isMessage("AddPeer",
+               assertThat(addPeer, isMessage("AddPeer",
                                "File=file",
                                "Identifier=identifier",
                                "Trust=LOW",
@@ -48,7 +42,7 @@ public class AddPeerTest extends AbstractFcpMessageTest {
        @Test
        public void canCreateAddPeerWithUrl() throws Exception {
                AddPeer addPeer = new AddPeer(NORMAL, NAME_ONLY, new URL("http://url"));
-               assertThat(encodeMessage(addPeer), isMessage("AddPeer",
+               assertThat(addPeer, isMessage("AddPeer",
                                "URL=http://url",
                                "Trust=NORMAL",
                                "Visibility=NAME_ONLY"
@@ -58,7 +52,7 @@ public class AddPeerTest extends AbstractFcpMessageTest {
        @Test
        public void canCreateAddPeerWithIdentifierAndUrl() throws Exception {
                AddPeer addPeer = new AddPeer(NORMAL, NAME_ONLY, "identifier", new URL("http://url"));
-               assertThat(encodeMessage(addPeer), isMessage("AddPeer",
+               assertThat(addPeer, isMessage("AddPeer",
                                "URL=http://url",
                                "Identifier=identifier",
                                "Trust=NORMAL",
@@ -67,10 +61,10 @@ public class AddPeerTest extends AbstractFcpMessageTest {
        }
 
        @Test
-       public void canCreateAddPeerWithNodeRef() throws Exception {
+       public void canCreateAddPeerWithNodeRef() {
                NodeRef nodeRef = createNodeRef();
                AddPeer addPeer = new AddPeer(HIGH, YES, nodeRef);
-               assertThat(encodeMessage(addPeer), isMessage("AddPeer",
+               assertThat(addPeer, isMessage("AddPeer",
                                "lastGoodVersion=test,1.2.3,4.5,678",
                                "opennet=true",
                                "identity=identity",
@@ -93,10 +87,10 @@ public class AddPeerTest extends AbstractFcpMessageTest {
        }
 
        @Test
-       public void canCreateAddPeerWithIdentifierAndNodeRef() throws Exception {
+       public void canCreateAddPeerWithIdentifierAndNodeRef() {
                NodeRef nodeRef = createNodeRef();
                AddPeer addPeer = new AddPeer(HIGH, YES, "identifier", nodeRef);
-               assertThat(encodeMessage(addPeer), isMessage("AddPeer",
+               assertThat(addPeer, isMessage("AddPeer",
                                "Identifier=identifier",
                                "lastGoodVersion=test,1.2.3,4.5,678",
                                "opennet=true",
@@ -137,38 +131,4 @@ public class AddPeerTest extends AbstractFcpMessageTest {
                return nodeRef;
        }
 
-       private static Matcher<Iterable<String>> isMessage(String name, String... lines) {
-               return new TypeSafeDiagnosingMatcher<Iterable<String>>() {
-                       @Override
-                       protected boolean matchesSafely(Iterable<String> encodedMessage, Description mismatchDescription) {
-                               Iterator<String> messageLines = encodedMessage.iterator();
-                               String messageName = messageLines.next();
-                               if (!messageName.equals(name)) {
-                                       mismatchDescription.appendText("name is ").appendValue(messageName);
-                                       return false;
-                               }
-                               List<String> parameterLines = new ArrayList<>();
-                               while (true) {
-                                       String actualLine = messageLines.next();
-                                       if (actualLine.contains("=")) {
-                                               parameterLines.add(actualLine);
-                                       } else {
-                                               if (!containsInAnyOrder(lines).matches(parameterLines)) {
-                                                       containsInAnyOrder(lines).describeMismatch(parameterLines, mismatchDescription);
-                                                       return false;
-                                               }
-                                               break;
-                                       }
-                               }
-                               return true;
-                       }
-
-                       @Override
-                       public void describeTo(Description description) {
-                               description.appendText("is message named ").appendValue(name)
-                                               .appendText(" with parameters ").appendValue(lines);
-                       }
-               };
-       }
-
 }
index e2b9fcf..75541cf 100644 (file)
@@ -6,6 +6,8 @@ import static org.hamcrest.Matchers.contains;
 import java.io.ByteArrayInputStream;
 import java.util.List;
 
+import net.pterodactylus.fcp.test.MessageUtils;
+
 import org.junit.Test;
 
 /**
@@ -13,14 +15,14 @@ import org.junit.Test;
  *
  * @author <a href="mailto:david.roden@bietr.de">David Roden</a>
  */
-public class FcpMessageTest extends AbstractFcpMessageTest {
+public class FcpMessageTest {
 
        private final FcpMessage fcpMessage = new FcpMessage("TestMessage");
 
        @Test
        public void fcpMessageWithPayloadIsTerminatedByData() throws Exception {
                fcpMessage.setPayloadInputStream(new ByteArrayInputStream("Test".getBytes()));
-               List<String> lines = encodeMessage(fcpMessage);
+               List<String> lines = MessageUtils.encodeMessage(fcpMessage);
                assertThat(lines, contains(
                        "TestMessage",
                        "Data",
index e4bedfe..09819ef 100644 (file)
@@ -1,35 +1,27 @@
 package net.pterodactylus.fcp;
 
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.contains;
-
 import org.junit.Test;
 
+import static net.pterodactylus.fcp.test.Matchers.isMessage;
+import static org.hamcrest.MatcherAssert.assertThat;
+
 /**
  * Unit test for {@link WatchFeeds}.
  *
  * @author <a href="mailto:david.roden@bietr.de">David Roden</a>
  */
-public class WatchFeedsTest extends AbstractFcpMessageTest {
+public class WatchFeedsTest {
 
        @Test
        public void enablingWatchFeedsSendsCorrectOutput() throws Exception {
                WatchFeeds watchFeeds = new WatchFeeds(true);
-               assertThat(encodeMessage(watchFeeds), contains(
-                       "WatchFeeds",
-                       "Enabled=true",
-                       "EndMessage"
-               ));
+               assertThat(watchFeeds, isMessage("WatchFeeds", "Enabled=true"));
        }
 
        @Test
        public void disablingWatchFeedsSendsCorrectOutput() throws Exception {
                WatchFeeds watchFeeds = new WatchFeeds(false);
-               assertThat(encodeMessage(watchFeeds), contains(
-                       "WatchFeeds",
-                       "Enabled=false",
-                       "EndMessage"
-               ));
+               assertThat(watchFeeds, isMessage("WatchFeeds", "Enabled=false"));
        }
 
 }
diff --git a/src/test/java/net/pterodactylus/fcp/test/Matchers.java b/src/test/java/net/pterodactylus/fcp/test/Matchers.java
new file mode 100644 (file)
index 0000000..962c6fd
--- /dev/null
@@ -0,0 +1,54 @@
+package net.pterodactylus.fcp.test;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import net.pterodactylus.fcp.FcpMessage;
+
+import org.hamcrest.Description;
+import org.hamcrest.Matcher;
+import org.hamcrest.TypeSafeDiagnosingMatcher;
+
+import static org.hamcrest.Matchers.containsInAnyOrder;
+
+public class Matchers {
+
+       public static Matcher<FcpMessage> isMessage(String name, String... lines) {
+               return isMessage(name, containsInAnyOrder(lines));
+       }
+
+       public static Matcher<FcpMessage> isMessage(String name, Matcher<? super Iterable<? super String>> linesMatcher) {
+               return new TypeSafeDiagnosingMatcher<FcpMessage>() {
+                       @Override
+                       protected boolean matchesSafely(FcpMessage fcpMessage, Description mismatchDescription) {
+                               Iterator<String> messageLines = MessageUtils.encodeMessage(fcpMessage).iterator();
+                               String messageName = messageLines.next();
+                               if (!messageName.equals(name)) {
+                                       mismatchDescription.appendText("name is ").appendValue(messageName);
+                                       return false;
+                               }
+                               List<String> parameterLines = new ArrayList<>();
+                               while (true) {
+                                       String actualLine = messageLines.next();
+                                       if (actualLine.contains("=")) {
+                                               parameterLines.add(actualLine);
+                                       } else {
+                                               if (!linesMatcher.matches(parameterLines)) {
+                                                       linesMatcher.describeMismatch(parameterLines, mismatchDescription);
+                                                       return false;
+                                               }
+                                               return true;
+                                       }
+                               }
+                       }
+
+                       @Override
+                       public void describeTo(Description description) {
+                               description.appendText("is message named ").appendValue(name)
+                                               .appendText(" with parameters ").appendDescriptionOf(linesMatcher);
+                       }
+               };
+       }
+
+}
diff --git a/src/test/java/net/pterodactylus/fcp/test/MessageUtils.java b/src/test/java/net/pterodactylus/fcp/test/MessageUtils.java
new file mode 100644 (file)
index 0000000..f65972f
--- /dev/null
@@ -0,0 +1,23 @@
+package net.pterodactylus.fcp.test;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.List;
+
+import net.pterodactylus.fcp.FcpMessage;
+
+import static java.util.Arrays.asList;
+
+public class MessageUtils {
+
+       public static List<String> encodeMessage(FcpMessage fcpMessage) {
+               try {
+                       ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+                       fcpMessage.write(outputStream);
+                       return asList(outputStream.toString().split("\r?\n"));
+               } catch (IOException e) {
+                       throw new RuntimeException(e);
+               }
+       }
+
+}