@Test
  public void inputStreamSplitReadLineTest() throws IOException {
    int streamDelayTooLong = 100;
    int timeout = 50;
    int cpuDelay = 10;

    StringBuilder builder = new StringBuilder();
    InputStream stream = org.mockito.Mockito.mock(InputStream.class);
    byte[] bytes = "o".getBytes();
    byte[] bytes2 = "k\nwo".getBytes();
    byte[] bytes3 = "rl".getBytes();
    byte[] bytes4 = "d\n".getBytes();
    Mockito.when(stream.available())
        .thenReturn(bytes.length)
        .thenReturn(bytes.length)
        .thenReturn(bytes2.length)
        .thenReturn(bytes2.length)
        .thenReturn(bytes3.length)
        .thenReturn(bytes3.length)
        .thenReturn(bytes4.length)
        .thenReturn(bytes4.length)
        .thenReturn(0);
    Mockito.when(
            stream.read(
                Mockito.any(byte[].class), Mockito.any(Integer.class), Mockito.any(Integer.class)))
        .thenAnswer(new InputStreamReadDelayedAnswer(streamDelayTooLong, bytes))
        .thenAnswer(new InputStreamReadDelayedAnswer(streamDelayTooLong, bytes2))
        .thenAnswer(new InputStreamReadDelayedAnswer(streamDelayTooLong, bytes3))
        .thenAnswer(new InputStreamReadDelayedAnswer(streamDelayTooLong, bytes4))
        .thenThrow(
            new IllegalArgumentException(
                "The read method should never have been called this time."));

    // This tests that an inputstream read wasn't able to complete a full line read in the given
    // timeout period.
    ParseState state = IOUtilities.readLine(stream, builder, "[\n]", 0, timeout, cpuDelay);
    Assert.assertEquals(1, state.parseLocation);
    Assert.assertEquals(null, state.currentLine);

    // The next read should be able to read the the ok
    state = IOUtilities.readLine(stream, builder, "[\n]", state.parseLocation, timeout, cpuDelay);
    Assert.assertEquals(0, state.parseLocation);
    Assert.assertEquals("ok\n", state.currentLine);

    // Again, this read won't be able to read the full amount of data in the timeout period.
    state = IOUtilities.readLine(stream, builder, "[\n]", state.parseLocation, timeout, cpuDelay);
    Assert.assertEquals(4, state.parseLocation);
    Assert.assertEquals(null, state.currentLine);

    // There is nothing left on the stream to read so it continues to parse the rest of the
    // StringBuilder
    state = IOUtilities.readLine(stream, builder, "[\n]", state.parseLocation, timeout, 10);
    Assert.assertEquals(0, state.parseLocation);
    Assert.assertEquals("world\n", state.currentLine);
  }
  @Test
  public void serialPortSplitReadLineTest() throws IOException {
    int streamDelayTooLong = 100;
    int timeout = 50;
    int cpuDelay = 10;

    StringBuilder builder = new StringBuilder();
    SerialCommunicationsPort serial = org.mockito.Mockito.mock(SerialCommunicationsPort.class);
    Printer printer = org.mockito.Mockito.mock(Printer.class);
    byte[] bytes = "o".getBytes();
    byte[] bytes2 = "k\nwo".getBytes();
    byte[] bytes3 = "rl".getBytes();
    byte[] bytes4 = "d\n".getBytes();
    Mockito.when(serial.read())
        .thenAnswer(new SerialPortReadDelayedAnswer(streamDelayTooLong, bytes))
        .thenAnswer(new SerialPortReadDelayedAnswer(streamDelayTooLong, bytes2))
        .thenAnswer(new SerialPortReadDelayedAnswer(streamDelayTooLong, bytes3))
        .thenAnswer(new SerialPortReadDelayedAnswer(streamDelayTooLong, bytes4))
        .thenThrow(
            new IllegalArgumentException(
                "The read method should never have been called this time."));

    // This tests that an inputstream read wasn't able to complete a full line read in the given
    // timeout period.
    ParseState state = IOUtilities.readLine(printer, serial, builder, 0, timeout, cpuDelay);
    Assert.assertEquals(1, state.parseLocation);
    Assert.assertEquals(null, state.currentLine);

    // The next read should be able to read the the ok
    state = IOUtilities.readLine(printer, serial, builder, state.parseLocation, timeout, cpuDelay);
    Assert.assertEquals(0, state.parseLocation);
    Assert.assertEquals("ok\n", state.currentLine);

    // Again, this read won't be able to read the full amount of data in the timeout period.
    state = IOUtilities.readLine(printer, serial, builder, state.parseLocation, timeout, cpuDelay);
    Assert.assertEquals(4, state.parseLocation);
    Assert.assertEquals(null, state.currentLine);

    // There is nothing left on the stream to read so it continues to parse the rest of the
    // StringBuilder
    state = IOUtilities.readLine(printer, serial, builder, state.parseLocation, timeout, cpuDelay);
    Assert.assertEquals(0, state.parseLocation);
    Assert.assertEquals("world\n", state.currentLine);
  }
  private void testSingleLine(String line, StringBuilder builder, InputStream stream)
      throws IOException {
    int cpuDelay = 10;
    byte[] firstBytes = line.getBytes();
    Mockito.when(stream.available()).thenReturn(firstBytes.length).thenReturn(firstBytes.length);
    Mockito.when(
            stream.read(Mockito.any(byte[].class), Mockito.eq(0), Mockito.eq(firstBytes.length)))
        .thenAnswer(new InputStreamReadDelayedAnswer(0, firstBytes));

    ParseState state = IOUtilities.readLine(stream, builder, "[\n]", 0, 0, cpuDelay);

    Assert.assertEquals(0, state.parseLocation);
    Assert.assertEquals(line, state.currentLine);
  }