From 89f0f8720319a522f04faf2b6ffdbdd273c59be5 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Thu, 2 Jun 2016 20:43:07 +0100 Subject: [PATCH 1/8] Upgrade kemitix-parent to 1.2.0 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index a0f0bde..9dd2a54 100644 --- a/pom.xml +++ b/pom.xml @@ -27,7 +27,7 @@ net.kemitix kemitix-parent - 1.1.0 + 1.2.0 From 1bc8bac22979e774096f051ff8d387cfb913dd19 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Thu, 2 Jun 2016 20:44:51 +0100 Subject: [PATCH 2/8] Update checkstyle ruleset from kemitix-parent-1.2.0 template --- checkstyle.xml | 355 ++++++++++++++++++++++++++----------------------- 1 file changed, 185 insertions(+), 170 deletions(-) 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"> - + + + - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + - - - - - - - - - - + - - - - - - - - - - + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - From 79032c0dd83aeb16f76d96e8af8305742ff3354f Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Thu, 2 Jun 2016 20:46:39 +0100 Subject: [PATCH 3/8] WiserAssertions: apply checkstyle rule DeclarationOrder --- .../wiser/assertions/WiserAssertions.java | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/main/java/net/kemitix/wiser/assertions/WiserAssertions.java b/src/main/java/net/kemitix/wiser/assertions/WiserAssertions.java index 920adc5..6bce8a6 100644 --- a/src/main/java/net/kemitix/wiser/assertions/WiserAssertions.java +++ b/src/main/java/net/kemitix/wiser/assertions/WiserAssertions.java @@ -3,16 +3,15 @@ package net.kemitix.wiser.assertions; import org.subethamail.wiser.Wiser; import org.subethamail.wiser.WiserMessage; +import javax.mail.MessagingException; +import javax.mail.internet.MimeMessage; +import javax.mail.internet.MimeMultipart; import java.io.IOException; import java.text.MessageFormat; import java.util.List; import java.util.function.Predicate; import java.util.function.Supplier; -import javax.mail.MessagingException; -import javax.mail.internet.MimeMessage; -import javax.mail.internet.MimeMultipart; - /** * Provides a set of assertions for checking the status of any messages received * by subethamail's Wiser. @@ -46,6 +45,15 @@ public final class WiserAssertions { */ 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 +66,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}. From 97d67cb5574eca946a452135d8957e9196cb2fcd Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Thu, 2 Jun 2016 20:51:48 +0100 Subject: [PATCH 4/8] WiserAssertions: apply checkstyle rule MultipleStringLiterals --- .../kemitix/wiser/assertions/WiserAssertions.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/main/java/net/kemitix/wiser/assertions/WiserAssertions.java b/src/main/java/net/kemitix/wiser/assertions/WiserAssertions.java index 6bce8a6..e583c72 100644 --- a/src/main/java/net/kemitix/wiser/assertions/WiserAssertions.java +++ b/src/main/java/net/kemitix/wiser/assertions/WiserAssertions.java @@ -40,6 +40,9 @@ import java.util.function.Supplier; */ public final class WiserAssertions { + private static final String ERROR_MESSAGE_SUBJECT + = "No message with subject [{0}] found!"; + /** * The messages received by Wiser. */ @@ -105,9 +108,8 @@ public final class WiserAssertions { public WiserAssertions withSubject(final String subject) { Predicate predicate = m -> subject.equals(unchecked(getMimeMessage(m)::getSubject)); - findFirstOrElseThrow(predicate, - assertionError("No message with subject [{0}] found!", - subject)); + findFirstOrElseThrow(predicate, assertionError(ERROR_MESSAGE_SUBJECT, + subject)); return this; } @@ -123,9 +125,8 @@ public final class WiserAssertions { Predicate predicate = m -> unchecked(getMimeMessage(m)::getSubject) .contains(subject); - findFirstOrElseThrow(predicate, - assertionError("No message with subject [{0}] found!", - subject)); + findFirstOrElseThrow(predicate, assertionError(ERROR_MESSAGE_SUBJECT, + subject)); return this; } From cef89074ef1a110aba0f4f11bab2376139a584a0 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Thu, 2 Jun 2016 20:52:41 +0100 Subject: [PATCH 5/8] WiserAssertions: apply rule ReturnCount --- .../kemitix/wiser/assertions/WiserAssertions.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/kemitix/wiser/assertions/WiserAssertions.java b/src/main/java/net/kemitix/wiser/assertions/WiserAssertions.java index e583c72..9ec032e 100644 --- a/src/main/java/net/kemitix/wiser/assertions/WiserAssertions.java +++ b/src/main/java/net/kemitix/wiser/assertions/WiserAssertions.java @@ -181,16 +181,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"); + if (result == null) { + throw new RuntimeException("Unexpected MimeMessage content"); + } + return result; } /** From af4b48e2121960c68e10e0176bdbc1ba9af59cc3 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Thu, 2 Jun 2016 20:55:35 +0100 Subject: [PATCH 6/8] WiserAssertions: suppress checkstyle rule IllegalCatch --- src/main/java/net/kemitix/wiser/assertions/WiserAssertions.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/net/kemitix/wiser/assertions/WiserAssertions.java b/src/main/java/net/kemitix/wiser/assertions/WiserAssertions.java index 9ec032e..006a7c3 100644 --- a/src/main/java/net/kemitix/wiser/assertions/WiserAssertions.java +++ b/src/main/java/net/kemitix/wiser/assertions/WiserAssertions.java @@ -253,6 +253,7 @@ public final class WiserAssertions { * * @return the product of the supplier */ + @SuppressWarnings("illegalCatch") public static T unchecked(final ThrowingSupplier supplier) { try { return supplier.get(); From d20322cc65ecf8655bc5db4e351ecc4318db071c Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Thu, 2 Jun 2016 20:56:22 +0100 Subject: [PATCH 7/8] WiserAssertions: suppress checkstyle rule IllegalThrows --- src/main/java/net/kemitix/wiser/assertions/WiserAssertions.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/net/kemitix/wiser/assertions/WiserAssertions.java b/src/main/java/net/kemitix/wiser/assertions/WiserAssertions.java index 006a7c3..34c2f2e 100644 --- a/src/main/java/net/kemitix/wiser/assertions/WiserAssertions.java +++ b/src/main/java/net/kemitix/wiser/assertions/WiserAssertions.java @@ -302,6 +302,7 @@ public final class WiserAssertions { * * @throws Throwable on error */ + @SuppressWarnings("illegalthrows") T get() throws Throwable; } } From 235ea4795746f317a92ac68d4428e6dd98ca1965 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Thu, 2 Jun 2016 21:13:39 +0100 Subject: [PATCH 8/8] Update code style and reorganise methods --- .../wiser/assertions/WiserAssertions.java | 203 +++++++++--------- 1 file changed, 105 insertions(+), 98 deletions(-) diff --git a/src/main/java/net/kemitix/wiser/assertions/WiserAssertions.java b/src/main/java/net/kemitix/wiser/assertions/WiserAssertions.java index 34c2f2e..76da070 100644 --- a/src/main/java/net/kemitix/wiser/assertions/WiserAssertions.java +++ b/src/main/java/net/kemitix/wiser/assertions/WiserAssertions.java @@ -3,39 +3,48 @@ package net.kemitix.wiser.assertions; import org.subethamail.wiser.Wiser; import org.subethamail.wiser.WiserMessage; -import javax.mail.MessagingException; -import javax.mail.internet.MimeMessage; -import javax.mail.internet.MimeMultipart; import java.io.IOException; import java.text.MessageFormat; import java.util.List; import java.util.function.Predicate; import java.util.function.Supplier; +import javax.mail.MessagingException; +import javax.mail.internet.MimeMessage; +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 { @@ -83,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}. @@ -106,13 +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)); - findFirstOrElseThrow(predicate, assertionError(ERROR_MESSAGE_SUBJECT, - subject)); + Predicate predicate = m -> subject.equals( + unchecked(getMimeMessage(m)::getSubject)); + findFirstOrElseThrow(predicate, + 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. @@ -122,11 +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); - findFirstOrElseThrow(predicate, assertionError(ERROR_MESSAGE_SUBJECT, - subject)); + Predicate predicate = m -> unchecked( + getMimeMessage(m)::getSubject).contains(subject); + findFirstOrElseThrow(predicate, + 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( @@ -197,71 +269,6 @@ public final class WiserAssertions { return result; } - /** - * 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 - */ - @SuppressWarnings("illegalCatch") - public static T unchecked(final ThrowingSupplier supplier) { - try { - return supplier.get(); - } catch (Throwable e) { - throw new RuntimeException(e); - } - } - /** * Converts a {@link MimeMultipart} into a {@link String} stripping out the * mime part boundary and headers..