SipInitialLine extends SipParser { public static final SipInitialLine parse(final Buffer buffer) throws SipParseException { Buffer part1 = null; Buffer part2 = null; Buffer part3 = null; try { part1 = buffer.readUntil(SipParser.SP); part2 = buffer.readUntil(SipParser.SP); part3 = buffer.readLine(); if (SipParser.SIP2_0.equals(part1)) { final int statusCode = part2.parseToInt(); return new SipResponseLine(statusCode, part3); } expectSIP2_0(part3); return new SipRequestLine(part1, part2); } catch (final NumberFormatException e) { final int index = buffer.getReaderIndex() - part3.capacity() - part2.capacity() - 1; throw new SipParseException(index, "unable to parse the SIP response code as an integer"); } catch (final ByteNotFoundException e) { throw new SipParseException(buffer.getReaderIndex(), "expected space"); } catch (final SipParseException e) { e.printStackTrace();; final int index = buffer.getReaderIndex() - part3.capacity() + e.getErrorOffset() - 1; throw new SipParseException(index, "Wrong SIP version"); } catch (final IOException e) { throw new SipParseException(buffer.getReaderIndex(), "could not read from stream", e); } } protected  SipInitialLine(); abstract boolean equals(final Object other); abstract Buffer getBuffer(); static boolean isResponseLine(final Buffer buffer); static final SipInitialLine parse(final Buffer buffer); static final SipInitialLine parse(final Buffer part1, final Buffer part2, final Buffer part3); SipRequestLine toRequestLine(); SipResponseLine toResponseLine(); boolean isResponseLine(); boolean isRequestLine(); abstract void getBytes(Buffer dst);  }
@Test public void testBadInitialLineNotEnoughSections() throws Exception { final Buffer buffer = Buffers.wrap("INVITE sip:alice@example"); try { SipInitialLine.parse(buffer); fail("Expected SipParseException"); } catch (final SipParseException e) { assertThat(e.getErrorOffset(), is(7)); } }
@Test public void testBadInitialLineWrongSipVersion() throws Exception { final Buffer buffer = Buffers.wrap("INVITE sip:alice@example.com SIP/3.0"); try { SipInitialLine.parse(buffer); fail("Expected SipParseException"); } catch (final SipParseException e) { assertThat(e.getErrorOffset(), is(33)); } }
@Test public void testBadInitialLineBadStatusCode() throws Exception { final Buffer buffer = Buffers.wrap("SIP/2.0 twohundred OK"); try { SipInitialLine.parse(buffer); fail("Expected SipParseException"); } catch (final SipParseException e) { assertThat(e.getErrorOffset(), is(8)); } }
@Test public void testBufferThrowsIOException() throws Exception { final Buffer buffer = mock(Buffer.class); when(buffer.readUntil(SipParser.SP)).thenThrow(new IOException("From unit tests")); when(buffer.getReaderIndex()).thenReturn(5); try { SipInitialLine.parse(buffer); fail("Expected SipParseException"); } catch (final SipParseException e) { assertThat(e.getErrorOffset(), is(5)); } }