Skip to content

Commit

Permalink
changed test to be more flexible to aquire dummy port
Browse files Browse the repository at this point in the history
  • Loading branch information
ai-republic committed Dec 27, 2023
1 parent d1d60ff commit 6d5ae61
Showing 1 changed file with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ public static void main(final String[] args) throws Exception {
}


SerialPort getPort() {
final SerialPort[] ports = SerialPort.getCommPorts();
if (ports != null && ports.length > 0) {
return ports[0];
}

throw new IllegalArgumentException("No port available on this machine!");
}


@Test
public void testSerialEventValidFullFrame() {
// GIVEN: a port configured with startflag and framelength
Expand All @@ -51,7 +61,7 @@ public void testSerialEventValidFullFrame() {

// WHEN:
// a valid full frame is received
port.serialEvent(new SerialPortEvent(SerialPort.getCommPort("com1"), SerialPort.LISTENING_EVENT_DATA_RECEIVED, new byte[] { Integer.valueOf(0xA5).byteValue(), 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C }));
port.serialEvent(new SerialPortEvent(getPort(), SerialPort.LISTENING_EVENT_DATA_RECEIVED, new byte[] { Integer.valueOf(0xA5).byteValue(), 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C }));

// THEN:
// a frame should be added to the queue
Expand All @@ -68,7 +78,7 @@ public void testSerialEventInvalidFullFrame() {

// WHEN:
// an invalid full frame is received
port.serialEvent(new SerialPortEvent(SerialPort.getCommPort("com1"), SerialPort.LISTENING_EVENT_DATA_RECEIVED, new byte[] { Integer.valueOf(0xA4).byteValue(), 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C }));
port.serialEvent(new SerialPortEvent(getPort(), SerialPort.LISTENING_EVENT_DATA_RECEIVED, new byte[] { Integer.valueOf(0xA4).byteValue(), 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C }));

// THEN:
// no frame should be added to the queue
Expand All @@ -85,7 +95,7 @@ public void testSerialEventShiftedFullFrame() {

// WHEN:
// a full frame with startflag in wrong place is received
port.serialEvent(new SerialPortEvent(SerialPort.getCommPort("com1"), SerialPort.LISTENING_EVENT_DATA_RECEIVED, new byte[] { 0x00, 0x01, Integer.valueOf(0xA5).byteValue(), 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C }));
port.serialEvent(new SerialPortEvent(getPort(), SerialPort.LISTENING_EVENT_DATA_RECEIVED, new byte[] { 0x00, 0x01, Integer.valueOf(0xA5).byteValue(), 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C }));

// THEN:
// no frame should be added to the queue
Expand All @@ -102,7 +112,7 @@ public void testSerialEventShiftedTooLongFrame() {

// WHEN:
// a full frame with startflag in wrong place is received
port.serialEvent(new SerialPortEvent(SerialPort.getCommPort("com1"), SerialPort.LISTENING_EVENT_DATA_RECEIVED, new byte[] { 0x00, 0x01, Integer.valueOf(0xA5).byteValue(), 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F }));
port.serialEvent(new SerialPortEvent(getPort(), SerialPort.LISTENING_EVENT_DATA_RECEIVED, new byte[] { 0x00, 0x01, Integer.valueOf(0xA5).byteValue(), 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F }));

// THEN:
// 1 frame should be added to the queue
Expand Down

0 comments on commit 6d5ae61

Please sign in to comment.