Added tests for reading and writing

This commit is contained in:
Marcin Kostrzewski 2020-10-29 12:30:13 +01:00
parent 243f44ef23
commit 2e4bd8289e

View File

@ -1,6 +1,9 @@
package Main; package Main;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.io.*;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
public class ProtocolMessageTest { public class ProtocolMessageTest {
@ -33,4 +36,20 @@ public class ProtocolMessageTest {
int result = ProtocolMessage.charArrayToInt(out); int result = ProtocolMessage.charArrayToInt(out);
assertEquals(testedInt, result, "Integers not equal!"); 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());
}
} }