From 584ee8929b6f9526ec29e7d09a6817a5876deecf Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 10 Feb 2023 08:49:55 +0100 Subject: [PATCH] =?utf8?q?=E2=9C=85=20Add=20test=20for=20=E2=80=9CAllData?= =?utf8?q?=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Also, make a member variable final. --- src/main/java/net/pterodactylus/fcp/AllData.java | 2 +- .../java/net/pterodactylus/fcp/AllDataTest.java | 78 ++++++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 src/test/java/net/pterodactylus/fcp/AllDataTest.java diff --git a/src/main/java/net/pterodactylus/fcp/AllData.java b/src/main/java/net/pterodactylus/fcp/AllData.java index 9c85c9a..82931db 100644 --- a/src/main/java/net/pterodactylus/fcp/AllData.java +++ b/src/main/java/net/pterodactylus/fcp/AllData.java @@ -33,7 +33,7 @@ import java.io.InputStream; public class AllData extends BaseMessage implements Identifiable { /** The payload. */ - private InputStream payloadInputStream; + private final InputStream payloadInputStream; /** * Creates an “AllData” message that wraps the received message. diff --git a/src/test/java/net/pterodactylus/fcp/AllDataTest.java b/src/test/java/net/pterodactylus/fcp/AllDataTest.java new file mode 100644 index 0000000..6ec8812 --- /dev/null +++ b/src/test/java/net/pterodactylus/fcp/AllDataTest.java @@ -0,0 +1,78 @@ +package net.pterodactylus.fcp; + +import java.io.ByteArrayInputStream; +import java.io.IOException; + +import com.google.common.io.ByteStreams; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; + +/** + * Unit test for {@link AllData} + */ +public class AllDataTest { + + @Test + public void allDataReturnsIdentifierFromFcpMessage() { + receivedMessage.setField("Identifier", "identifier"); + assertThat(allData.getIdentifier(), equalTo("identifier")); + } + + @Test + public void allDataCanParseTheDataLengthFromTheFcpMessage() { + receivedMessage.setField("DataLength", "1234567"); + assertThat(allData.getDataLength(), equalTo(1234567L)); + } + + @Test + public void allDataReturnsMinus1ForUnparseableDataLength() { + receivedMessage.setField("DataLength", "broken"); + assertThat(allData.getDataLength(), equalTo(-1L)); + } + + @Test + public void allDataCanParseStartupTimeFromFcpMessage() { + receivedMessage.setField("StartupTime", "1234567"); + assertThat(allData.getStartupTime(), equalTo(1234567L)); + } + + @Test + public void allDataReturnsMinus1ForUnparseableStartupTime() { + receivedMessage.setField("StartupTime", "broken"); + assertThat(allData.getStartupTime(), equalTo(-1L)); + } + + @Test + public void allDataCanParseCompletionTimeFromFcpMessage() { + receivedMessage.setField("CompletionTime", "1234567"); + assertThat(allData.getCompletionTime(), equalTo(1234567L)); + } + + @Test + public void allDataReturnsMinus1ForUnparseableCompletionTime() { + receivedMessage.setField("CompletionTime", "broken"); + assertThat(allData.getCompletionTime(), equalTo(-1L)); + } + + @Test + public void allDataReturnsContentTypeFromFcpMessage() { + receivedMessage.setField("Metadata.ContentType", "application/test"); + assertThat(allData.getContentType(), equalTo("application/test")); + } + + @Test + public void allDataReturnsGivenPayloadInputStream() throws IOException { + byte[] payload = new byte[4]; + assertThat(allData.getPayloadInputStream().read(payload), equalTo(4)); + assertThat(payload, equalTo(new byte[] { 0, 1, 2, 3 })); + assertThat(allData.getPayloadInputStream().read(), equalTo(-1)); + } + + private final FcpMessage receivedMessage = new FcpMessage("AllData"); + private final AllData allData = new AllData(receivedMessage, new ByteArrayInputStream(new byte[] { 0, 1, 2, 3 })); + +} -- 2.7.4