-
-
Notifications
You must be signed in to change notification settings - Fork 271
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#338: Return Nullable AsyncResponse from mailer.sendMail(singleArgume…
…nt), as async can now be configured through the mailerBuilder as well
- Loading branch information
Showing
4 changed files
with
53 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
package demo; | ||
|
||
import org.simplejavamail.api.mailer.AsyncResponse; | ||
import org.simplejavamail.api.mailer.Mailer; | ||
import org.simplejavamail.email.EmailBuilder; | ||
|
||
import javax.mail.util.ByteArrayDataSource; | ||
import java.io.IOException; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Demonstration program for the Simple Java Mail framework. Just fill your gmail, password and press GO. | ||
*/ | ||
|
@@ -27,7 +30,7 @@ public static void main(final String[] args) throws IOException { | |
} | ||
|
||
private static void testSimplePlainText(Mailer mailerTLS) { | ||
mailerTLS.sendMail(EmailBuilder.startingBlank() | ||
final AsyncResponse result = mailerTLS.sendMail(EmailBuilder.startingBlank() | ||
.to(YOUR_GMAIL_ADDRESS) | ||
.from("Simple Java Mail demo", "[email protected]") | ||
.withSubject("Demo email - simple (using plain text)") | ||
|
@@ -39,6 +42,8 @@ private static void testSimplePlainText(Mailer mailerTLS) { | |
"- plain text (root)") | ||
.clearHTMLText() | ||
.buildEmail()); | ||
|
||
assertThat(result).isNull(); | ||
} | ||
|
||
private static void testSimpleHTMLText(Mailer mailerTLS) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
|
||
import javax.mail.Session; | ||
import javax.mail.internet.MimeMessage; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
@@ -26,8 +27,11 @@ public void setup() { | |
|
||
@Test | ||
public void emailSentSuccesfullyShouldInvokeOnSuccessHandler() throws Exception { | ||
AsyncResponse asyncResponse = sendAsyncMail(true); | ||
|
||
emailSentSuccesfullyShouldInvokeOnSuccessHandler(sendAsyncMailUsingMailerAPI(true)); | ||
emailSentSuccesfullyShouldInvokeOnSuccessHandler(sendAsyncMailUsingMailerBuilderAPI(true)); | ||
} | ||
|
||
private void emailSentSuccesfullyShouldInvokeOnSuccessHandler(AsyncResponse asyncResponse) throws InterruptedException, ExecutionException { | ||
// set handlers, then wait for result | ||
final AtomicReference<Boolean> successHandlerInvoked = new AtomicReference<>(false); | ||
final AtomicReference<Boolean> exceptionHandlerInvoked = new AtomicReference<>(false); | ||
|
@@ -44,15 +48,18 @@ public void accept(final Exception e) { | |
} | ||
}); | ||
asyncResponse.getFuture().get(); | ||
|
||
assertThat(successHandlerInvoked).hasValue(true); | ||
assertThat(exceptionHandlerInvoked).hasValue(false); | ||
} | ||
|
||
@Test | ||
public void emailSentSuccesfullyShouldInvokeOnSuccessHandlerAfterDelay() throws Exception { | ||
AsyncResponse asyncResponse = sendAsyncMail(true); | ||
|
||
emailSentSuccesfullyShouldInvokeOnSuccessHandlerAfterDelay(sendAsyncMailUsingMailerAPI(true)); | ||
emailSentSuccesfullyShouldInvokeOnSuccessHandlerAfterDelay(sendAsyncMailUsingMailerBuilderAPI(true)); | ||
} | ||
|
||
private void emailSentSuccesfullyShouldInvokeOnSuccessHandlerAfterDelay(AsyncResponse asyncResponse) throws InterruptedException, ExecutionException { | ||
// wait for result, then set handlers | ||
asyncResponse.getFuture().get(); | ||
final AtomicReference<Boolean> successHandlerInvoked = new AtomicReference<>(false); | ||
|
@@ -69,15 +76,18 @@ public void accept(final Exception e) { | |
exceptionHandlerInvoked.set(true); | ||
} | ||
}); | ||
|
||
assertThat(successHandlerInvoked).hasValue(true); | ||
assertThat(exceptionHandlerInvoked).hasValue(false); | ||
} | ||
|
||
@Test | ||
public void emailSentSuccesfullyShouldInvokeOnExceptionHandler() throws Exception { | ||
AsyncResponse asyncResponse = sendAsyncMail(false); | ||
|
||
public void emailSentSuccesfullyShouldInvokeOnExceptionHandler() { | ||
emailSentSuccesfullyShouldInvokeOnExceptionHandler(sendAsyncMailUsingMailerAPI(false)); | ||
emailSentSuccesfullyShouldInvokeOnExceptionHandler(sendAsyncMailUsingMailerBuilderAPI(false)); | ||
} | ||
|
||
private void emailSentSuccesfullyShouldInvokeOnExceptionHandler(AsyncResponse asyncResponse) { | ||
// set handlers, then wait for result | ||
final AtomicReference<Boolean> successHandlerInvoked = new AtomicReference<>(false); | ||
final AtomicReference<Boolean> exceptionHandlerInvoked = new AtomicReference<>(false); | ||
|
@@ -93,20 +103,20 @@ public void accept(final Exception e) { | |
exceptionHandlerInvoked.set(true); | ||
} | ||
}); | ||
|
||
try { | ||
asyncResponse.getFuture().get(); | ||
} catch (Exception e) { | ||
// good | ||
} | ||
|
||
assertThat(successHandlerInvoked).hasValue(false); | ||
assertThat(exceptionHandlerInvoked).hasValue(true); | ||
} | ||
|
||
@Test | ||
public void emailSentSuccesfullyShouldInvokeOnExceptionHandlerAfterDelay() throws Exception { | ||
AsyncResponse asyncResponse = sendAsyncMail(false); | ||
AsyncResponse asyncResponse = sendAsyncMailUsingMailerAPI(false); | ||
|
||
// wait for result, then set handlers | ||
try { | ||
|
@@ -132,9 +142,9 @@ public void accept(final Exception e) { | |
assertThat(successHandlerInvoked).hasValue(false); | ||
assertThat(exceptionHandlerInvoked).hasValue(true); | ||
} | ||
|
||
@Nullable | ||
private AsyncResponse sendAsyncMail(boolean sendSuccesfully) { | ||
private AsyncResponse sendAsyncMailUsingMailerAPI(boolean sendSuccesfully) { | ||
final boolean async = true; | ||
return MailerBuilder | ||
.withSMTPServer("localhost", 0) | ||
|
@@ -145,6 +155,19 @@ private AsyncResponse sendAsyncMail(boolean sendSuccesfully) { | |
.withPlainText("") | ||
.buildEmail(), async); | ||
} | ||
|
||
@Nullable | ||
private AsyncResponse sendAsyncMailUsingMailerBuilderAPI(boolean sendSuccesfully) { | ||
return MailerBuilder | ||
.withSMTPServer("localhost", 0) | ||
.withCustomMailer(new MySimulatingMailer(sendSuccesfully)) | ||
.async() | ||
.buildMailer().sendMail(EmailBuilder.startingBlank() | ||
.to("[email protected]") | ||
.from("Simple Java Mail demo", "[email protected]") | ||
.withPlainText("") | ||
.buildEmail()); | ||
} | ||
|
||
private static class MySimulatingMailer implements CustomMailer { | ||
final boolean shouldSendSuccesfully; | ||
|