From: David ‘Bombe’ Roden Date: Fri, 10 Feb 2023 07:49:55 +0000 (+0100) Subject: ✅ Add test for “AllData” X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=584ee8929b6f9526ec29e7d09a6817a5876deecf;p=jFCPlib.git ✅ Add test for “AllData” Also, make a member variable final. --- 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 })); + +}