use of com.fsck.k9.message.MessageBuilder.Callback in project k-9 by k9mail.
the class LockableDatabase method execute.
/**
* Execute a DB callback in a shared context (doesn't prevent concurrent
* shared executions), taking care of locking the DB storage.
*
* <p>
* Can be instructed to start a transaction if none is currently active in
* the current thread. Callback will participate in any active transaction (no
* inner transaction created).
* </p>
*
* @param transactional
* <code>true</code> the callback must be executed in a
* transactional context.
* @param callback
* Never <code>null</code>.
* @return Whatever {@link DbCallback#doDbWork(SQLiteDatabase)} returns.
*/
public <T> T execute(final boolean transactional, final DbCallback<T> callback) throws MessagingException {
lockRead();
final boolean doTransaction = transactional && inTransaction.get() == null;
try {
final boolean debug = K9.isDebugLoggingEnabled();
if (doTransaction) {
inTransaction.set(Boolean.TRUE);
mDb.beginTransaction();
}
try {
final T result = callback.doDbWork(mDb);
if (doTransaction) {
mDb.setTransactionSuccessful();
}
return result;
} finally {
if (doTransaction) {
final long begin;
if (debug) {
begin = System.currentTimeMillis();
} else {
begin = 0L;
}
// not doing endTransaction in the same 'finally' block of unlockRead() because endTransaction() may throw an exception
mDb.endTransaction();
if (debug) {
Timber.v("LockableDatabase: Transaction ended, took %d ms / %s", currentTimeMillis() - begin, new Exception().getStackTrace()[1]);
}
}
}
} finally {
if (doTransaction) {
inTransaction.set(null);
}
unlockRead();
}
}
use of com.fsck.k9.message.MessageBuilder.Callback in project k-9 by k9mail.
the class MessageBuilderTest method build_usingHtmlFormat_shouldUseMultipartAlternativeInCorrectOrder.
@Test
public void build_usingHtmlFormat_shouldUseMultipartAlternativeInCorrectOrder() {
MessageBuilder messageBuilder = createHtmlMessageBuilder();
messageBuilder.buildAsync(callback);
MimeMessage message = getMessageFromCallback();
assertEquals(MimeMultipart.class, message.getBody().getClass());
assertEquals("multipart/alternative", ((MimeMultipart) message.getBody()).getMimeType());
List<BodyPart> parts = ((MimeMultipart) message.getBody()).getBodyParts();
// RFC 2046 - 5.1.4. - Best type is last displayable
assertEquals("text/plain", parts.get(0).getMimeType());
assertEquals("text/html", parts.get(1).getMimeType());
}
use of com.fsck.k9.message.MessageBuilder.Callback in project k-9 by k9mail.
the class MessageBuilderTest method build_withAttachment_shouldSucceed.
@Test
public void build_withAttachment_shouldSucceed() throws Exception {
MessageBuilder messageBuilder = createSimpleMessageBuilder();
Attachment attachment = createAttachmentWithContent("text/plain", "attach.txt", TEST_ATTACHMENT_TEXT);
messageBuilder.setAttachments(Collections.singletonList(attachment));
messageBuilder.buildAsync(callback);
MimeMessage message = getMessageFromCallback();
assertEquals(MESSAGE_HEADERS + MESSAGE_CONTENT_WITH_ATTACH, getMessageContents(message));
}
use of com.fsck.k9.message.MessageBuilder.Callback in project k-9 by k9mail.
the class MessageBuilderTest method build_withAttachment_nonAscii_shouldSucceed.
@Test
public void build_withAttachment_nonAscii_shouldSucceed() throws Exception {
MessageBuilder messageBuilder = createSimpleMessageBuilder();
Attachment attachment = createAttachmentWithContent("text/plain", ATTACHMENT_FILENAME_NON_ASCII, TEST_ATTACHMENT_TEXT);
messageBuilder.setAttachments(Collections.singletonList(attachment));
messageBuilder.buildAsync(callback);
MimeMessage message = getMessageFromCallback();
assertEquals(MESSAGE_HEADERS + MESSAGE_CONTENT_WITH_ATTACH_NON_ASCII_FILENAME, getMessageContents(message));
}
use of com.fsck.k9.message.MessageBuilder.Callback in project k-9 by k9mail.
the class MessageBuilderTest method buildWithException_detachAndReattach_shouldThrow.
@Test
public void buildWithException_detachAndReattach_shouldThrow() throws MessagingException {
Callback anotherCallback = mock(Callback.class);
MessageBuilder messageBuilder = new SimpleMessageBuilder(messageIdGenerator, boundaryGenerator, resourceProvider) {
@Override
protected void buildMessageInternal() {
queueMessageBuildException(new MessagingException("expected error"));
}
};
Robolectric.getBackgroundThreadScheduler().pause();
messageBuilder.buildAsync(callback);
messageBuilder.detachCallback();
Robolectric.getBackgroundThreadScheduler().unPause();
messageBuilder.reattachCallback(anotherCallback);
verifyNoMoreInteractions(callback);
verify(anotherCallback).onMessageBuildException(any(MessagingException.class));
verifyNoMoreInteractions(anotherCallback);
}
Aggregations