From a99776fc81f885bb55546ac1c2c7017efbdad688 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 23 Nov 2024 22:10:11 +0100 Subject: [PATCH] =?utf8?q?=E2=9C=85=20Add=20tests=20for=20FcpMessage?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../java/net/pterodactylus/fcp/FcpMessageTest.java | 89 +++++++++++++++++----- 1 file changed, 70 insertions(+), 19 deletions(-) diff --git a/src/test/java/net/pterodactylus/fcp/FcpMessageTest.java b/src/test/java/net/pterodactylus/fcp/FcpMessageTest.java index 75541cf..3c88e84 100644 --- a/src/test/java/net/pterodactylus/fcp/FcpMessageTest.java +++ b/src/test/java/net/pterodactylus/fcp/FcpMessageTest.java @@ -1,33 +1,84 @@ package net.pterodactylus.fcp; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.contains; +import org.junit.Test; import java.io.ByteArrayInputStream; -import java.util.List; +import java.io.ByteArrayOutputStream; +import java.io.IOException; -import net.pterodactylus.fcp.test.MessageUtils; - -import org.junit.Test; +import static java.nio.charset.StandardCharsets.UTF_8; +import static java.util.stream.Collectors.toList; +import static java.util.stream.StreamSupport.stream; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.aMapWithSize; +import static org.hamcrest.Matchers.allOf; +import static org.hamcrest.Matchers.anEmptyMap; +import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasEntry; +import static org.junit.Assert.assertThrows; -/** - * Unit test for {@link FcpMessage}. - * - * @author David Roden - */ public class FcpMessageTest { - private final FcpMessage fcpMessage = new FcpMessage("TestMessage"); + @Test + public void nameOfFcpMessageIsRetained() { + FcpMessage message = new FcpMessage("Test"); + assertThat(message.getName(), equalTo("Test")); + } @Test - public void fcpMessageWithPayloadIsTerminatedByData() throws Exception { + public void newMessageDoesNotHaveFields() { + assertThat(fcpMessage.getFields(), anEmptyMap()); + } + + @Test + public void setFieldIsRetained() { + fcpMessage.put("Test field", "test value"); + assertThat(fcpMessage.getFields(), allOf(aMapWithSize(1), hasEntry("Test field", "test value"))); + } + + @Test + public void setFieldIsReturnedAsSet() { + fcpMessage.put("Test field", "test value"); + assertThat(fcpMessage.hasField("Test field"), equalTo(true)); + } + + @Test + public void settingFieldWithValueNullThrowsNullPointerException() { + assertThrows(NullPointerException.class, () -> fcpMessage.put("Test field", null)); + } + + @Test + public void settingFieldWithNameNullThrowsNullPointerException() { + assertThrows(NullPointerException.class, () -> fcpMessage.put(null, "test value")); + } + + @Test + public void setFieldsCanBeIteratorOver() { + fcpMessage.put("Test1", "value1"); + fcpMessage.put("Test2", "value2"); + fcpMessage.put("Test3", "value3"); + assertThat(stream(fcpMessage.spliterator(), false).collect(toList()), containsInAnyOrder("Test1", "Test2", "Test3")); + } + + @Test + public void messageAndFieldsAreWrittenToOutputStreamUsingCrLfAndUtf8() throws IOException { + fcpMessage.put("Test field", "test välue"); + try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { + fcpMessage.write(outputStream); + assertThat(outputStream.toByteArray(), equalTo("TestMessage\r\nTest field=test välue\r\nEndMessage\r\n".getBytes(UTF_8))); + } + } + + @Test + public void messageWithPayloadIsTerminatedWithDataAndWithoutTerminatingLineBreak() throws Exception { fcpMessage.setPayloadInputStream(new ByteArrayInputStream("Test".getBytes())); - List lines = MessageUtils.encodeMessage(fcpMessage); - assertThat(lines, contains( - "TestMessage", - "Data", - "Test" - )); + try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { + fcpMessage.write(outputStream); + assertThat(outputStream.toByteArray(), equalTo("TestMessage\r\nData\r\nTest".getBytes(UTF_8))); + } } + private final FcpMessage fcpMessage = new FcpMessage("TestMessage"); + } -- 2.7.4