/** * Tests a successful search and parse. * * @throws IOException Won't. * @throws MessagingException Won't. */ @Test public void testSuccess() throws MessagingException, IOException { sut = new MessageContentExtractor(Collections.singletonList(BASIC_MARKER), null); Message msg = (Message) makeMultipart( true, makeContentPart("text/plain", true, "Not this"), makeContentPart("text/plain", false, " \t\r\n \t \r\nFrom: blah"), makeMultipart( false, makeContentPart("text/plain", true, ""), makeContentPart("image/png", false, new byte[12]), makeContentPart("text/plain", true, "From: xyz"), makeMultipart( false, makeContentPart("text/html", false, "<head></head>"), makeContentPart("text/plain", false, TEXT_TO_FIND)), makeContentPart("text/plain", false, "Not this")), makeContentPart("text/plain", false, "Not this either"), makeContentPart("text/html", false, "<p>Not this</p>")); String result = sut.extract(msg); context.assertIsSatisfied(); assertEquals(TEXT_TO_FIND, result); }
/** * Common parts of several parsing tests. * * @param input String to parse. * @param expected Expected extraction. * @throws IOException Won't. * @throws MessagingException Won't. */ private void commonParseTest(final String input, final String expected) throws MessagingException, IOException { sut = new MessageContentExtractor(Arrays.asList("abc", "def"), Arrays.asList("\\d+", "mo+")); Message msg = (Message) makeMultipart(true, makeContentPart("text/plain", false, input)); String result = sut.extract(msg); context.assertIsSatisfied(); assertEquals(expected, result); }
/** * Tests null lists. * * @throws IOException Won't. * @throws MessagingException Won't. */ @Test public void testCtor() throws MessagingException, IOException { sut = new MessageContentExtractor(null, null); Message msg = (Message) makeMultipart( true, makeContentPart("text/plain", false, "stuff\r\nmore\r\nDate: 1/1/11")); String result = sut.extract(msg); context.assertIsSatisfied(); assertEquals("stuff\r\nmore\r\nDate: 1/1/11", result); }
/** * Tests parsing: 'From' must be at start of line to be a header. * * @throws IOException Won't. * @throws MessagingException Won't. */ @Test public void testParse1() throws MessagingException, IOException { sut = new MessageContentExtractor(Collections.singletonList(BASIC_MARKER), null); Message msg = (Message) makeMultipart( true, makeContentPart( "text/plain", false, " From: A\r\n\tFrom: B\r\n \t\t From: C\r\n\r\n From: D\r\nFrom: E")); String result = sut.extract(msg); context.assertIsSatisfied(); assertEquals("From: A\r\n\tFrom: B\r\n \t\t From: C\r\n\r\n From: D", result); }