### Question:
AbstractSerialPort implements IConnectionInterface { public void purge() { if (getInputStream() != null) { try { byte[] availableBytes = new byte[getInputStream().available()]; if (getInputStream().available() > 0) getInputStream().read(availableBytes, 0, getInputStream().available()); } catch (IOException e) { logger.error(e.getMessage(), e); } } } protected  AbstractSerialPort(String port, SerialPortParameters parameters); protected  AbstractSerialPort(String port, int baudRate); protected  AbstractSerialPort(String port, int baudRate, int receiveTimeout); protected  AbstractSerialPort(String port, SerialPortParameters parameters, int receiveTimeout); @Override boolean isOpen(); String getPort(); abstract void setDTR(boolean state); abstract void setRTS(boolean state); abstract boolean isCTS(); abstract boolean isDSR(); abstract boolean isCD(); boolean isHardwareFlowControl(); void setPortParameters(int baudRate, int dataBits, int stopBits, int parity, int flowControl); void setPortParameters(SerialPortParameters parameters); abstract void setBreak(boolean enabled); abstract void sendBreak(int duration); abstract void setReadTimeout(int timeout); abstract int getReadTimeout(); void purge(); void flush(); @Override void writeData(byte[] data); @Override void writeData(byte[] data, int offset, int length); @Override int readData(byte[] data); @Override int readData(byte[] data, int offset, int length); @Override ConnectionType getConnectionType(); SerialPortParameters getPortParameters(); int getReceiveTimeout(); @Override String toString(); static final int DEFAULT_PORT_TIMEOUT; static final int DEFAULT_DATA_BITS; static final int DEFAULT_STOP_BITS; static final int DEFAULT_PARITY; static final int DEFAULT_FLOW_CONTROL; }### Answer:
@Test public final void testPurgeNullInputStream() throws IOException { TestAbstractSerialPort port = Mockito.spy(new TestAbstractSerialPort("COM1", 9600)); mockInputStream = null; port.purge(); Mockito.verify(port, Mockito.times(1)).getInputStream(); }
@Test public final void testPurgeExceptionWhenAvailable() throws IOException { TestAbstractSerialPort port = Mockito.spy(new TestAbstractSerialPort("COM1", 9600)); Mockito.doThrow(new IOException("IO exception")).when(mockInputStream).available(); port.purge(); Mockito.verify(port, Mockito.times(2)).getInputStream(); Mockito.verify(mockInputStream, Mockito.times(1)).available(); Mockito.verify(mockInputStream, Mockito.times(0)).read(Mockito.any(byte[].class), Mockito.anyInt(), Mockito.anyInt()); }
@Test public final void testPurgeExceptionAvailableBytesToRead() throws IOException { TestAbstractSerialPort port = Mockito.spy(new TestAbstractSerialPort("COM1", 9600)); Mockito.when(mockInputStream.available()).thenReturn(10); Mockito.when(mockInputStream.read(Mockito.any(byte[].class), Mockito.eq(0), Mockito.eq(10))).thenReturn(10); port.purge(); Mockito.verify(port, Mockito.times(5)).getInputStream(); Mockito.verify(mockInputStream, Mockito.times(3)).available(); Mockito.verify(mockInputStream, Mockito.times(1)).read(Mockito.any(byte[].class), Mockito.anyInt(), Mockito.anyInt()); }
@Test public final void testPurge() throws IOException { TestAbstractSerialPort port = Mockito.spy(new TestAbstractSerialPort("COM1", 9600)); port.purge(); Mockito.verify(port, Mockito.times(5)).getInputStream(); Mockito.verify(mockInputStream, Mockito.times(3)).available(); Mockito.verify(mockInputStream, Mockito.times(1)).read(Mockito.any(byte[].class), Mockito.anyInt(), Mockito.anyInt()); Mockito.verify(mockInputStream, Mockito.times(1)).read(Mockito.any(byte[].class), Mockito.eq(0), Mockito.eq(RECEIVED_BUFFER.length)); }