Added reasonable testing for StreamReciever, further testing would probably need StreamReciever to be rewritten #story[817]

This commit is contained in:
Peter Galloway
2017-04-28 18:29:35 +12:00
parent fe480d5cb6
commit 07bbd7e06d
2 changed files with 114 additions and 3 deletions
@@ -19,6 +19,7 @@ public class StreamReceiver {
public StreamReceiver(String hostAddress, int hostPort, PriorityBlockingQueue packetBuffer) {
try {
host = new Socket(hostAddress, hostPort);
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
@@ -26,6 +27,12 @@ public class StreamReceiver {
this.packetBuffer = packetBuffer;
}
public StreamReceiver(Socket host, PriorityBlockingQueue packetBuffer){
this.host=host;
this.packetBuffer = packetBuffer;
}
public void connect(){
try {
stream = host.getInputStream();
@@ -66,7 +73,6 @@ public class StreamReceiver {
} catch (Exception e) {
moreBytes = false;
}
}
}
@@ -100,8 +106,8 @@ public class StreamReceiver {
}
/**
* takes an array of up to 7 bytes and returns a positive
* long constructed from the input bytes
* takes an array of up to 7 bytes in little endian format and
* returns a positive long constructed from the input bytes
*
* @return a positive long if there is less than 7 bytes -1 otherwise
*/
@@ -0,0 +1,105 @@
package seng302.models.parsers;
import org.junit.Before;
import org.junit.Test;
import java.io.*;
import java.lang.reflect.Method;
import java.net.Socket;
import java.util.Comparator;
import java.util.concurrent.PriorityBlockingQueue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* Created by ptg19 on 26/04/17.
*/
public class StreamReceiverTest {
private PriorityBlockingQueue pq;
private byte[] brokenPacket = {0x47, (byte) 0x83, 37, // sync1 sync2 and message type
0b00000000, 0b01000000, 0b00100010, 0b00100100, 0b00011000, 0b00000000, //timestamp
0b00000000, 0b00010000, 0b01000000, 0b00000000, //source id
0b00100010, 0b00101000, // message length
0b00010010, 0b00010010, 0b00010010}; //random start of payload
private byte[] workingPacket = {0x47, (byte) 0x83, 37, // sync1 sync2 and message type
0b00000000, 0b01000000, 0b00100010, 0b00100100, 0b00011000, 0b00000000, //timestamp
0b00000000, 0b00010000, 0b01000000, 0b00000000, //source id
0b00000010, 0b00000000, // message length
0b00010010, 0b00010010, // payload
0b00100110, (byte)0b10000111, 0b00110101, 0b01111000}; //crc
private byte[] crcMismatchPacket = {0x47, (byte) 0x83, 37, // sync1 sync2 and message type
0b00000000, 0b01000000, 0b00100010, 0b00100100, 0b00011000, 0b00000000, //timestamp
0b00000000, 0b00000000, 0b01000000, 0b00000000, //source id
0b00000010, 0b00000000, // message length
0b00010010, 0b00010010, // payload
0b00100110, (byte)0b10000111, 0b00110101, 0b01111000}; //crc
@Before
public void setup(){
pq = new PriorityBlockingQueue<>(256, new Comparator<StreamPacket>() {
@Override
public int compare(StreamPacket s1, StreamPacket s2) {
return (int) (s1.getTimeStamp() - s2.getTimeStamp());
}
});
}
@Test
public void connectExitsOnUnexpectedStreamEnd() throws Exception {
Socket host=mock(Socket.class);
InputStream stream = new ByteArrayInputStream(brokenPacket);
when(host.getInputStream()).thenReturn(stream);
StreamReceiver streamReceiver = new StreamReceiver(host, pq);
streamReceiver.connect();
assert pq.size() == 0;
}
@Test
public void connectReadsAPacket() throws Exception {
Socket host=mock(Socket.class);
InputStream stream = new ByteArrayInputStream(workingPacket);
when(host.getInputStream()).thenReturn(stream);
StreamReceiver streamReceiver = new StreamReceiver(host, pq);
streamReceiver.connect();
assert pq.size() == 1;
}
@Test
public void connectDropsAMismatchedCrc() throws Exception {
Socket host=mock(Socket.class);
InputStream stream = new ByteArrayInputStream(crcMismatchPacket);
when(host.getInputStream()).thenReturn(stream);
StreamReceiver streamReceiver = new StreamReceiver(host, pq);
streamReceiver.connect();
assert pq.size() == 0;
}
@Test
public void bytestoLongTest() {
Socket host=mock(Socket.class);
StreamReceiver streamReceiver = new StreamReceiver(host, pq);
try {
Class[] args = new Class[1];
args[0] = byte[].class;
Method bytesToLong = streamReceiver.getClass().getDeclaredMethod("bytesToLong", args);
bytesToLong.setAccessible(true);
byte[] sevenBtyeNumber = {0b01100100, 0b00110100, 0b00010100, 0b00000000, 0b00000000, 0b00000000, (byte)0b10000000};
assert bytesToLong.invoke(streamReceiver, sevenBtyeNumber).equals(36028797020288100L);
byte[] eightByteNumber = {0b01100100, 0b00110100, 0b00010100, 0b00000000, 0b00000000, 0b00000000, (byte)0b10000000, 0b00100101};
assert bytesToLong.invoke(streamReceiver, eightByteNumber).equals(-1L);
byte[] emptyArray = {};
assert bytesToLong.invoke(streamReceiver, emptyArray).equals(0L);
} catch (Exception e){
System.out.println("");
}
}
}