From: David ‘Bombe’ Roden Date: Wed, 8 Feb 2023 11:44:30 +0000 (+0100) Subject: ♻️ Refactor FCP message test base class X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=8387dfb05ca9272d6086c3833fc2f52a36edd9d6;p=jFCPlib.git ♻️ Refactor FCP message test base class All the methods have been moved to helper classes in the “test” package, and the abstract class itself has been removed. --- diff --git a/src/test/java/net/pterodactylus/fcp/AbstractFcpMessageTest.java b/src/test/java/net/pterodactylus/fcp/AbstractFcpMessageTest.java deleted file mode 100644 index d5c9d24..0000000 --- a/src/test/java/net/pterodactylus/fcp/AbstractFcpMessageTest.java +++ /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 David ‘Bombe’ Roden - */ -public class AbstractFcpMessageTest { - - protected List encodeMessage(FcpMessage fcpMessage) throws Exception { - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - fcpMessage.write(outputStream); - return Arrays.asList(outputStream.toString().split("\r?\n")); - } - -} diff --git a/src/test/java/net/pterodactylus/fcp/AddPeerTest.java b/src/test/java/net/pterodactylus/fcp/AddPeerTest.java index 154d8d0..15b27e5 100644 --- a/src/test/java/net/pterodactylus/fcp/AddPeerTest.java +++ b/src/test/java/net/pterodactylus/fcp/AddPeerTest.java @@ -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> isMessage(String name, String... lines) { - return new TypeSafeDiagnosingMatcher>() { - @Override - protected boolean matchesSafely(Iterable encodedMessage, Description mismatchDescription) { - Iterator messageLines = encodedMessage.iterator(); - String messageName = messageLines.next(); - if (!messageName.equals(name)) { - mismatchDescription.appendText("name is ").appendValue(messageName); - return false; - } - List 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); - } - }; - } - } diff --git a/src/test/java/net/pterodactylus/fcp/FcpMessageTest.java b/src/test/java/net/pterodactylus/fcp/FcpMessageTest.java index e2b9fcf..75541cf 100644 --- a/src/test/java/net/pterodactylus/fcp/FcpMessageTest.java +++ b/src/test/java/net/pterodactylus/fcp/FcpMessageTest.java @@ -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 David Roden */ -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 lines = encodeMessage(fcpMessage); + List lines = MessageUtils.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 index e4bedfe..09819ef 100644 --- a/src/test/java/net/pterodactylus/fcp/WatchFeedsTest.java +++ b/src/test/java/net/pterodactylus/fcp/WatchFeedsTest.java @@ -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 David Roden */ -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 index 0000000..962c6fd --- /dev/null +++ b/src/test/java/net/pterodactylus/fcp/test/Matchers.java @@ -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 isMessage(String name, String... lines) { + return isMessage(name, containsInAnyOrder(lines)); + } + + public static Matcher isMessage(String name, Matcher> linesMatcher) { + return new TypeSafeDiagnosingMatcher() { + @Override + protected boolean matchesSafely(FcpMessage fcpMessage, Description mismatchDescription) { + Iterator messageLines = MessageUtils.encodeMessage(fcpMessage).iterator(); + String messageName = messageLines.next(); + if (!messageName.equals(name)) { + mismatchDescription.appendText("name is ").appendValue(messageName); + return false; + } + List 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 index 0000000..f65972f --- /dev/null +++ b/src/test/java/net/pterodactylus/fcp/test/MessageUtils.java @@ -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 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); + } + } + +}