byte[] raw = message.getBytes("US-ASCII");
ByteArrayInputStream instream = new ByteArrayInputStream(raw);
RootInputStream rootStream = new RootInputStream(instream);
BufferedLineReaderInputStream rawstream = new BufferedLineReaderInputStream(rootStream, 24);
MimeEntity entity = new MimeEntity(
rootStream,
rawstream,
null,
EntityStates.T_START_MESSAGE,
EntityStates.T_END_MESSAGE);
assertEquals(EntityStates.T_START_MESSAGE, entity.getState());
entity.advance();
assertEquals(EntityStates.T_START_HEADER, entity.getState());
entity.advance();
assertEquals(EntityStates.T_FIELD, entity.getState());
assertEquals("To", entity.getFieldName());
assertEquals(" Road Runner <runner@example.org>", entity.getFieldValue());
entity.advance();
assertEquals(EntityStates.T_FIELD, entity.getState());
assertEquals("From", entity.getFieldName());
assertEquals(" Wile E. Cayote <wile@example.org>", entity.getFieldValue());
entity.advance();
assertEquals(EntityStates.T_FIELD, entity.getState());
assertEquals("Date", entity.getFieldName());
assertEquals(" Tue, 12 Feb 2008 17:34:09 +0000 (GMT)", entity.getFieldValue());
entity.advance();
assertEquals(EntityStates.T_FIELD, entity.getState());
assertEquals("Subject", entity.getFieldName());
assertEquals(" Mail", entity.getFieldValue());
entity.advance();
assertEquals(EntityStates.T_FIELD, entity.getState());
assertEquals("Content-Type", entity.getFieldName());
assertEquals(" multipart/mixed;boundary=1729", entity.getFieldValue());
entity.advance();
assertEquals(EntityStates.T_END_HEADER, entity.getState());
entity.advance();
assertEquals(EntityStates.T_START_MULTIPART, entity.getState());
entity.advance();
assertEquals(EntityStates.T_PREAMBLE, entity.getState());
assertEquals("Hello!", IOUtils.toString(entity.getContentStream()));
EntityStateMachine p1 = entity.advance();
assertNotNull(p1);
assertEquals(EntityStates.T_START_BODYPART, p1.getState());
p1.advance();
assertEquals(EntityStates.T_START_HEADER, p1.getState());
p1.advance();
assertEquals(EntityStates.T_FIELD, p1.getState());
assertEquals("Content-Type", p1.getFieldName());
assertEquals(" text/plain; charset=US-ASCII", p1.getFieldValue());
p1.advance();
assertEquals(EntityStates.T_END_HEADER, p1.getState());
p1.advance();
assertEquals(EntityStates.T_BODY, p1.getState());
assertEquals("blah blah blah", IOUtils.toString(p1.getContentStream()));
p1.advance();
assertEquals(EntityStates.T_END_BODYPART, p1.getState());
p1.advance();
assertEquals(EntityStates.T_END_OF_STREAM, p1.getState());
EntityStateMachine p2 = entity.advance();
assertNotNull(p2);
assertEquals(EntityStates.T_START_BODYPART, p2.getState());
p2.advance();
assertEquals(EntityStates.T_START_HEADER, p2.getState());
p2.advance();
assertEquals(EntityStates.T_FIELD, p2.getState());
assertEquals("Content-Type", p2.getFieldName());
assertEquals(" text/plain; charset=US-ASCII", p2.getFieldValue());
p2.advance();
assertEquals(EntityStates.T_END_HEADER, p2.getState());
p2.advance();
assertEquals(EntityStates.T_BODY, p2.getState());
assertEquals("yada yada yada", IOUtils.toString(p2.getContentStream()));
p2.advance();
assertEquals(EntityStates.T_END_BODYPART, p2.getState());
p2.advance();
assertEquals(EntityStates.T_END_OF_STREAM, p2.getState());
entity.advance();
assertEquals(EntityStates.T_EPILOGUE, entity.getState());
assertEquals("Goodbye!", IOUtils.toString(entity.getContentStream()));
entity.advance();
assertEquals(EntityStates.T_END_MULTIPART, entity.getState());
entity.advance();
assertEquals(EntityStates.T_END_MESSAGE, entity.getState());
entity.advance();
assertEquals(EntityStates.T_END_OF_STREAM, entity.getState());
}