diff --git a/checkstyle.xml b/checkstyle.xml index ba1e682..3eea98b 100644 --- a/checkstyle.xml +++ b/checkstyle.xml @@ -1,192 +1,207 @@ + "-//Puppy Crawl//DTD Check Configuration 1.3//EN" + "http://www.puppycrawl.com/dtds/configuration_1_3.dtd"> - + + + - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + - - - - - - - - - - + - - - - - - - - - - + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - diff --git a/pom.xml b/pom.xml index 062697b..db824f2 100644 --- a/pom.xml +++ b/pom.xml @@ -27,7 +27,7 @@ net.kemitix kemitix-parent - 1.1.0 + 1.2.0 diff --git a/src/main/java/net/kemitix/wiser/assertions/WiserAssertions.java b/src/main/java/net/kemitix/wiser/assertions/WiserAssertions.java index 920adc5..76da070 100644 --- a/src/main/java/net/kemitix/wiser/assertions/WiserAssertions.java +++ b/src/main/java/net/kemitix/wiser/assertions/WiserAssertions.java @@ -16,36 +16,56 @@ import javax.mail.internet.MimeMultipart; /** * Provides a set of assertions for checking the status of any messages received * by subethamail's Wiser. - * *
- * {@code
+ * 
  * {@literal @}Before
- * public void setUp() throws IOException {
- *     wiser = new Wiser(PORT);
- *     wiser.start();
- * }
+ *  public void setUp() throws IOException {
+ *      wiser = new Wiser(PORT);
+ *      wiser.start();
+ *  }
  *
- * {@literal @}After public void tearDown() { wiser.stop(); }
+ * {@literal @}After
+ *  public void tearDown() {
+ *      wiser.stop();
+ *  }
  *
- * {@literal @}Test public void testMail() { //given ...
- *
- * //when ...
- *
- * //then WiserAssertions.assertReceivedMessage(wiser) .from(sender)
- * .to(recipient_alpha) .to(recipient_beta) .withSubjectContains(subject_prefix)
- * .withSubjectContains(subject_suffix) .withContentContains(message_element_1)
- * .withContentContains(message_element_2)
- * .withContentContains(message_element_3); }
- * }
+ * {@literal @}Test
+ *  public void testMail() {
+ *      //given ...
+ *      //when ...
+ *      //then
+ *      WiserAssertions.assertReceivedMessage(wiser)
+ *                     .from(sender)
+ *                     .to(recipient_alpha)
+ *                     .to(recipient_beta)
+ *                     .withSubjectContains(subject_prefix)
+ *                     .withSubjectContains(subject_suffix)
+ *                     .withContentContains(message_element_1)
+ *                     .withContentContains(message_element_2)
+ *                     .withContentContains(message_element_3);
+ *  }
+ * 
  * 
*/ public final class WiserAssertions { + private static final String ERROR_MESSAGE_SUBJECT + = "No message with subject [{0}] found!"; + /** * The messages received by Wiser. */ private final List messages; + /** + * Private constructor. + * + * @param wiserMessages the messages to be tested by the assertions + */ + private WiserAssertions(final List wiserMessages) { + this.messages = wiserMessages; + } + /** * Creates an instance of {@code} WiserAssertions} ready to make assertions * on any messages received by the {@link Wiser} server. @@ -58,15 +78,6 @@ public final class WiserAssertions { return new WiserAssertions(wiser.getMessages()); } - /** - * Private constructor. - * - * @param wiserMessages the messages to be tested by the assertions - */ - private WiserAssertions(final List wiserMessages) { - this.messages = wiserMessages; - } - /** * Checks that there was at least one email received that was sent from the * {@code sender}. @@ -81,6 +92,39 @@ public final class WiserAssertions { return this; } + /** + * Checks that at least on message matches the predicate or the supplied + * exception will be thrown. + * + * @param predicate the condition a message must match + * @param exceptionSupplier the supplier of the exception + */ + private void findFirstOrElseThrow( + final Predicate predicate, + final Supplier exceptionSupplier) { + messages.stream() + .filter(predicate) + .findFirst() + .orElseThrow(exceptionSupplier); + } + + /** + * Returns a {@link Supplier} for an {@link AssertionError}. + * + * @param errorMessage the message for the exception + * @param args the parameters to insert into the message using + * {@link MessageFormat} + * + * @return a supplier of an {@link AssertionError} + */ + @SuppressWarnings( + {"ThrowableInstanceNotThrown", "ThrowableInstanceNeverThrown"}) + private static Supplier assertionError( + final String errorMessage, final Object... args) { + return () -> new AssertionError( + MessageFormat.format(errorMessage, args)); + } + /** * Checks that there was at least one email received that was sent to the * {@code recipient}. @@ -104,14 +148,44 @@ public final class WiserAssertions { * @return the {@code WiserAssertions} instance */ public WiserAssertions withSubject(final String subject) { - Predicate predicate - = m -> subject.equals(unchecked(getMimeMessage(m)::getSubject)); + Predicate predicate = m -> subject.equals( + unchecked(getMimeMessage(m)::getSubject)); findFirstOrElseThrow(predicate, - assertionError("No message with subject [{0}] found!", - subject)); + assertionError(ERROR_MESSAGE_SUBJECT, subject)); return this; } + /** + * Convert any checked Exceptions into unchecked Exceptions. + * + * @param the item type to be returned after suppressing any + * checked exceptions + * @param supplier the source of the return value that could cause a checked + * exception + * + * @return the product of the supplier + */ + @SuppressWarnings("illegalCatch") + public static T unchecked(final ThrowingSupplier supplier) { + try { + return supplier.get(); + } catch (Throwable e) { + throw new RuntimeException(e); + } + } + + /** + * Returns the mime message within the {@link WiserMessage} converting any + * {@link MessagingException}s into {@link RuntimeException}s. + * + * @param wiserMessage the message + * + * @return the mime message + */ + private MimeMessage getMimeMessage(final WiserMessage wiserMessage) { + return unchecked(wiserMessage::getMimeMessage); + } + /** * Checks that there was at least one email received that has a subject that * contains the search text. @@ -121,12 +195,10 @@ public final class WiserAssertions { * @return the {@code WiserAssertions} instance */ public WiserAssertions withSubjectContains(final String subject) { - Predicate predicate - = m -> unchecked(getMimeMessage(m)::getSubject) - .contains(subject); + Predicate predicate = m -> unchecked( + getMimeMessage(m)::getSubject).contains(subject); findFirstOrElseThrow(predicate, - assertionError("No message with subject [{0}] found!", - subject)); + assertionError(ERROR_MESSAGE_SUBJECT, subject)); return this; } @@ -140,8 +212,8 @@ public final class WiserAssertions { */ public WiserAssertions withContent(final String content) { findFirstOrElseThrow(m -> { - ThrowingSupplier contentAsString - = () -> getMimeMessageBody(m).trim(); + ThrowingSupplier contentAsString = () -> getMimeMessageBody( + m).trim(); return content.equals(unchecked(contentAsString)); }, assertionError("No message with content [{0}] found!", content)); return this; @@ -158,8 +230,8 @@ public final class WiserAssertions { public WiserAssertions withContentContains(final String content) { StringBuilder messageContent = new StringBuilder(); findFirstOrElseThrow((WiserMessage m) -> { - ThrowingSupplier contentAsString - = () -> getMimeMessageBody(m).trim(); + ThrowingSupplier contentAsString = () -> getMimeMessageBody( + m).trim(); messageContent.append(unchecked(contentAsString)); return unchecked(contentAsString).contains(content); }, assertionError( @@ -181,80 +253,20 @@ public final class WiserAssertions { private String getMimeMessageBody(final WiserMessage message) throws IOException, MessagingException { Object content = getMimeMessage(message).getContent(); + String result = null; if (content instanceof String) { - return (String) content; + result = (String) content; } if (content instanceof MimeMessage) { - return content.toString(); + result = content.toString(); } if (content instanceof MimeMultipart) { - return getMimeMultipartAsString((MimeMultipart) content); + result = getMimeMultipartAsString((MimeMultipart) content); } - throw new RuntimeException("Unexpected MimeMessage content"); - } - - /** - * Checks that at least on message matches the predicate or the supplied - * exception will be thrown. - * - * @param predicate the condition a message must match - * @param exceptionSupplier the supplier of the exception - */ - private void findFirstOrElseThrow( - final Predicate predicate, - final Supplier exceptionSupplier - ) { - messages.stream().filter(predicate) - .findFirst().orElseThrow(exceptionSupplier); - } - - /** - * Returns the mime message within the {@link WiserMessage} converting any - * {@link MessagingException}s into {@link RuntimeException}s. - * - * @param wiserMessage the message - * - * @return the mime message - */ - private MimeMessage getMimeMessage(final WiserMessage wiserMessage) { - return unchecked(wiserMessage::getMimeMessage); - } - - /** - * Returns a {@link Supplier} for an {@link AssertionError}. - * - * @param errorMessage the message for the exception - * @param args the parameters to insert into the message using - * {@link MessageFormat} - * - * @return a supplier of an {@link AssertionError} - */ - @SuppressWarnings( - {"ThrowableInstanceNotThrown", "ThrowableInstanceNeverThrown"}) - private static Supplier assertionError( - final String errorMessage, - final Object... args - ) { - return () - -> new AssertionError(MessageFormat.format(errorMessage, args)); - } - - /** - * Convert any checked Exceptions into unchecked Exceptions. - * - * @param the item type to be returned after suppressing any - * checked exceptions - * @param supplier the source of the return value that could cause a checked - * exception - * - * @return the product of the supplier - */ - public static T unchecked(final ThrowingSupplier supplier) { - try { - return supplier.get(); - } catch (Throwable e) { - throw new RuntimeException(e); + if (result == null) { + throw new RuntimeException("Unexpected MimeMessage content"); } + return result; } /** @@ -297,6 +309,7 @@ public final class WiserAssertions { * * @throws Throwable on error */ + @SuppressWarnings("illegalthrows") T get() throws Throwable; } }