@Test
public void testMultipart() {

    Metadata metadata = new Metadata();
    InputStream stream = getStream("test-documents/testRFC822-multipart");
    ContentHandler handler = mock(XHTMLContentHandler.class);
    ParseContext context = new ParseContext();
    context.set(Parser.class, EXTRACT_ALL_ALTERNATIVES_PARSER);
    try {
        EXTRACT_ALL_ALTERNATIVES_PARSER.parse(stream, handler, metadata, context);
        verify(handler).startDocument();
        int bodyExpectedTimes = 4, multipackExpectedTimes = 5;
        // TIKA-1422. TesseractOCRParser interferes with the number of times the handler is invoked.
        // But, different versions of Tesseract lead to a different number of invocations. So, we
        // only verify the handler if Tesseract cannot run.
        if (!TesseractOCRParserTest.canRun()) {
            verify(handler, times(bodyExpectedTimes)).startElement(eq(XHTMLContentHandler.XHTML), eq("div"), eq("div"), any(Attributes.class));
            verify(handler, times(bodyExpectedTimes)).endElement(XHTMLContentHandler.XHTML, "div", "div");
        }
        verify(handler, times(multipackExpectedTimes)).startElement(eq(XHTMLContentHandler.XHTML), eq("p"), eq("p"), any(Attributes.class));
        verify(handler, times(multipackExpectedTimes)).endElement(XHTMLContentHandler.XHTML, "p", "p");
        verify(handler).endDocument();
    } catch (Exception e) {
        fail("Exception thrown: " + e.getMessage());
    }
    //repeat, this time looking at content
    metadata = new Metadata();
    stream = getStream("test-documents/testRFC822-multipart");
    handler = new BodyContentHandler();
    try {
        EXTRACT_ALL_ALTERNATIVES_PARSER.parse(stream, handler, metadata, context);
        //tests correct decoding of quoted printable text, including UTF-8 bytes into Unicode
        String bodyText = handler.toString();
        assertTrue(bodyText.contains("body 1"));
        assertTrue(bodyText.contains("body 2"));
        assertFalse(bodyText.contains("R0lGODlhNgE8AMQAA")); //part of encoded gif
    } catch (Exception e) {
        fail("Exception thrown: " + e.getMessage());
    }
}