/** * Reads the next response available on the stream and returns an {@code ImapResponse} object that represents it. */ public ImapResponse readResponse(ImapResponseCallback callback) throws IOException { try { int peek = inputStream.peek(); if (peek == '+') { readContinuationRequest(callback); } else if (peek == '*') { readUntaggedResponse(callback); } else { readTaggedResponse(callback); } if (exception != null) { throw new ImapResponseParserException("readResponse(): Exception in callback method", exception); } return response; } finally { response = null; exception = null; } }
@Test public void readResponse_withCallbackThatThrowsRepeatedly_shouldConsumeAllInputAndThrowFirstException() throws Exception { ImapResponseParser parser = createParser("* {3}\r\none {3}\r\ntwo\r\n"); TestImapResponseCallback callback = TestImapResponseCallback.readBytesAndThrow(3); try { parser.readResponse(callback); fail(); } catch (ImapResponseParserException e) { assertEquals("readResponse(): Exception in callback method", e.getMessage()); assertEquals(ImapResponseParserTestException.class, e.getCause().getClass()); assertEquals(0, ((ImapResponseParserTestException) e.getCause()).instanceNumber); } assertAllInputConsumed(); }
@Test public void testParseLiteralWithThrowingCallback() throws Exception { ImapResponseParser parser = createParser("* {4}\r\ntest\r\n"); ImapResponseCallback callback = TestImapResponseCallback.readBytesAndThrow(0); try { parser.readResponse(callback); fail(); } catch (ImapResponseParserException e) { assertEquals("readResponse(): Exception in callback method", e.getMessage()); } assertAllInputConsumed(); }
@Test public void readResponse_withPartlyConsumingCallbackThatThrows_shouldReadAllDataAndThrow() throws Exception { ImapResponseParser parser = createParser("* {4}\r\ntest\r\n"); TestImapResponseCallback callback = TestImapResponseCallback.readBytesAndThrow(2); try { parser.readResponse(callback); fail(); } catch (ImapResponseParserException e) { assertEquals("readResponse(): Exception in callback method", e.getMessage()); assertEquals(ImapResponseParserTestException.class, e.getCause().getClass()); } assertAllInputConsumed(); }