From 2e4bd8289e182ffd72eaa3ab33dfe6375fa27212 Mon Sep 17 00:00:00 2001 From: Marcin Kostrzewski Date: Thu, 29 Oct 2020 12:30:13 +0100 Subject: [PATCH] Added tests for reading and writing --- src/test/java/Main/ProtocolMessageTest.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/test/java/Main/ProtocolMessageTest.java b/src/test/java/Main/ProtocolMessageTest.java index 0cd7abf..48e69fc 100644 --- a/src/test/java/Main/ProtocolMessageTest.java +++ b/src/test/java/Main/ProtocolMessageTest.java @@ -1,6 +1,9 @@ package Main; import org.junit.jupiter.api.Test; + +import java.io.*; + import static org.junit.jupiter.api.Assertions.*; public class ProtocolMessageTest { @@ -33,4 +36,20 @@ public class ProtocolMessageTest { int result = ProtocolMessage.charArrayToInt(out); assertEquals(testedInt, result, "Integers not equal!"); } + + @Test + public void inputReadingOutputWritingTest() throws IOException { + ProtocolMessage message = new ProtocolMessage("This is a text"); + + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + Writer writer = new PrintWriter(stream); + message.sendToStream(writer); + byte[] buffer = stream.toByteArray(); + + ByteArrayInputStream istream = new ByteArrayInputStream(buffer); + ProtocolMessage newMessage = new ProtocolMessage(); + Reader reader = new BufferedReader(new InputStreamReader(istream)); + newMessage.readFromStream(reader); + assertArrayEquals(message.getRaw(), newMessage.getRaw()); + } }