MimeMessageTestpublic class MimeMessageTest extends TestCase This is a series of unit tests for the MimeMessage class. These tests must be locally
complete - no server(s) required. |
Fields Summary |
---|
private final String | SHORT_UNICODEup arrow, down arrow, left arrow, right arrow | private final String | SHORT_UNICODE_ENCODED | private final String | SHORT_PLAINa string without any unicode | private final String | LONG_UNICODE_16longer unicode strings | private final String | LONG_UNICODE_64 | private final String | LONG_PLAIN_16longer plain strings (with fold points) | private final String | LONG_PLAIN_64 | private final String | LONG_PLAIN_256 |
Methods Summary |
---|
private void | doTestSetSentDate()
// "Thu, 01 Jan 2009 09:00:00 +0000" => 1230800400000L
long expectedTime = 1230800400000L;
Date date = new Date(expectedTime);
MimeMessage message = new MimeMessage();
message.setSentDate(date);
String[] headers = message.getHeader("Date");
assertEquals(1, headers.length);
// Explicitly specify the locale so that the object does not depend on the default
// locale.
SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.US);
Date result = format.parse(headers[0]);
assertEquals(expectedTime, result.getTime());
| public void | testGetContentId()Confirm getContentID() correctly works.
MimeMessage message = new MimeMessage();
// no content-id
assertNull(message.getContentId());
// normal case
final String cid1 = "cid.1@android.com";
message.setHeader(MimeHeader.HEADER_CONTENT_ID, cid1);
assertEquals(cid1, message.getContentId());
// surrounded by optional bracket
message.setHeader(MimeHeader.HEADER_CONTENT_ID, "<" + cid1 + ">");
assertEquals(cid1, message.getContentId());
| public void | testMessageId()Simple tests of the new "Message-ID" header
// Test 1. Every message gets a default and unique message-id
MimeMessage message1 = new MimeMessage();
MimeMessage message2 = new MimeMessage();
String id1 = message1.getMessageId();
String id2 = message2.getMessageId();
assertNotNull(id1);
assertNotNull(id2);
assertFalse("Message-ID should be unique", id1.equals(id2));
// Test 2. Set and get using API
final String testId1 = "test-message-id-one";
message1.setMessageId(testId1);
assertEquals("set and get Message-ID", testId1, message1.getMessageId());
// Test 3. Should only be one Message-ID per message
final String testId2 = "test-message-id-two";
message2.setMessageId(testId1);
message2.setMessageId(testId2);
assertEquals("set and get Message-ID", testId2, message2.getMessageId());
| public void | testSetLongSubject()Confirm folding operations on unicode subjects
MimeMessage message = new MimeMessage();
// test 1: long unicode - readback in unicode
message.setSubject(LONG_UNICODE_16);
assertEquals("unicode readback 16", LONG_UNICODE_16, message.getSubject());
// test 2: longer unicode (will fold)
message.setSubject(LONG_UNICODE_64);
assertEquals("unicode readback 64", LONG_UNICODE_64, message.getSubject());
// test 3: check folding & encoding
String rawHeader = message.getFirstHeader("Subject");
String[] split = rawHeader.split("\r\n");
assertTrue("long subject should fold", split.length > 1);
for (String s : split) {
assertTrue("split lines max length 78", s.length() <= 76); // 76+\r\n = 78
String trimmed = s.trim();
assertTrue("split lines are encoded",
trimmed.startsWith("=?") && trimmed.endsWith("?="));
}
| public void | testSetSentDate()Confirms that setSentDate() correctly set the "Date" header of a Mime message.
We tries a same test twice using two locales, Locale.US and the other, since
MimeMessage depends on the date formatter, which may emit wrong date format
in the locale other than Locale.US.
// TODO: more tests.
Locale savedLocale = Locale.getDefault();
Locale.setDefault(Locale.US);
doTestSetSentDate();
Locale.setDefault(Locale.JAPAN);
doTestSetSentDate();
Locale.setDefault(savedLocale);
| public void | testSetSubject()Confirm that setSubject() works with unicode strings
MimeMessage message = new MimeMessage();
message.setSubject(SHORT_UNICODE);
// test 1: readback in unicode
assertEquals("unicode readback", SHORT_UNICODE, message.getSubject());
// test 2: raw readback is escaped
String rawHeader = message.getFirstHeader("Subject");
assertEquals("raw readback", SHORT_UNICODE_ENCODED, rawHeader);
| public void | testSetSubjectPlain()Confirm that setSubject() works with plain strings
MimeMessage message = new MimeMessage();
message.setSubject(SHORT_PLAIN);
// test 1: readback
assertEquals("plain subjects", SHORT_PLAIN, message.getSubject());
// test 2: raw readback is not escaped
String rawHeader = message.getFirstHeader("Subject");
assertEquals("plain subject not encoded", -1, rawHeader.indexOf("=?"));
// test 3: long subject (shouldn't fold)
message.setSubject(LONG_PLAIN_64);
rawHeader = message.getFirstHeader("Subject");
String[] split = rawHeader.split("\r\n");
assertEquals("64 shouldn't fold", 1, split.length);
// test 4: very long subject (should fold)
message.setSubject(LONG_PLAIN_256);
rawHeader = message.getFirstHeader("Subject");
split = rawHeader.split("\r\n");
assertTrue("long subject should fold", split.length > 1);
for (String s : split) {
assertTrue("split lines max length 78", s.length() <= 76); // 76+\r\n = 78
String trimmed = s.trim();
assertFalse("split lines are not encoded", trimmed.startsWith("=?"));
}
|
|