This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-email.git


The following commit(s) were added to refs/heads/master by this push:
     new 974ea6b  Port to JUnit 5
974ea6b is described below

commit 974ea6b87fe1b9e3952d4117426debfc670d0fc0
Author: Gary Gregory <garydgreg...@gmail.com>
AuthorDate: Sat Dec 16 15:36:10 2023 -0500

    Port to JUnit 5
    
    - Remove junk assertions like assertTrue(true)
    - Fix possible NPE
---
 .../org/apache/commons/mail/MultiPartEmail.java    |   3 +-
 .../commons/mail/activation/PathDataSource.java    |   3 +-
 .../org/apache/commons/mail/AbstractEmailTest.java |   6 +
 .../java/org/apache/commons/mail/EmailTest.java    |  32 ++--
 .../org/apache/commons/mail/HtmlEmailTest.java     |  58 ++------
 .../apache/commons/mail/ImageHtmlEmailTest.java    |  22 +--
 .../apache/commons/mail/InvalidAddressTest.java    | 111 +++++++-------
 .../commons/mail/InvalidInternetAddressTest.java   | 107 ++++++--------
 .../apache/commons/mail/MultiPartEmailTest.java    | 163 ++++++---------------
 .../org/apache/commons/mail/SimpleEmailTest.java   |   9 +-
 10 files changed, 185 insertions(+), 329 deletions(-)

diff --git a/src/main/java/org/apache/commons/mail/MultiPartEmail.java 
b/src/main/java/org/apache/commons/mail/MultiPartEmail.java
index a9a69df..db02a61 100644
--- a/src/main/java/org/apache/commons/mail/MultiPartEmail.java
+++ b/src/main/java/org/apache/commons/mail/MultiPartEmail.java
@@ -24,6 +24,7 @@ import java.net.URL;
 import java.nio.file.Files;
 import java.nio.file.OpenOption;
 import java.nio.file.Path;
+import java.util.Objects;
 
 import javax.activation.DataHandler;
 import javax.activation.DataSource;
@@ -241,7 +242,7 @@ public class MultiPartEmail extends Email {
             if (!Files.exists(file)) {
                 throw new IOException("\"" + fileName + "\" does not exist");
             }
-            return attach(new PathDataSource(file, 
FileTypeMap.getDefaultFileTypeMap(), options), file.getFileName().toString(), 
null,
+            return attach(new PathDataSource(file, 
FileTypeMap.getDefaultFileTypeMap(), options), 
Objects.toString(file.getFileName(), null), null,
                     EmailAttachment.ATTACHMENT);
         } catch (final IOException e) {
             throw new EmailException("Cannot attach file \"" + fileName + 
"\"", e);
diff --git 
a/src/main/java/org/apache/commons/mail/activation/PathDataSource.java 
b/src/main/java/org/apache/commons/mail/activation/PathDataSource.java
index 09796dc..61167f3 100644
--- a/src/main/java/org/apache/commons/mail/activation/PathDataSource.java
+++ b/src/main/java/org/apache/commons/mail/activation/PathDataSource.java
@@ -108,8 +108,7 @@ public class PathDataSource implements DataSource {
      */
     @Override
     public String getName() {
-        final Path fileName = path.getFileName();
-        return fileName != null ? fileName.toString() : null;
+        return Objects.toString(path.getFileName(), null);
     }
 
     /**
diff --git a/src/test/java/org/apache/commons/mail/AbstractEmailTest.java 
b/src/test/java/org/apache/commons/mail/AbstractEmailTest.java
index 81d8d33..ca0567f 100644
--- a/src/test/java/org/apache/commons/mail/AbstractEmailTest.java
+++ b/src/test/java/org/apache/commons/mail/AbstractEmailTest.java
@@ -291,6 +291,12 @@ public abstract class AbstractEmailTest {
         }
     }
 
+    protected void stopServer() {
+        if (fakeMailServer != null) {
+            fakeMailServer.stop();
+        }
+    }
+
     @AfterEach
     public void tearDownEmailTest() {
         // stop the fake email server (if started)
diff --git a/src/test/java/org/apache/commons/mail/EmailTest.java 
b/src/test/java/org/apache/commons/mail/EmailTest.java
index a7f8f2a..458d184 100644
--- a/src/test/java/org/apache/commons/mail/EmailTest.java
+++ b/src/test/java/org/apache/commons/mail/EmailTest.java
@@ -22,7 +22,6 @@ import static 
org.junit.jupiter.api.Assertions.assertNotEquals;
 import static org.junit.jupiter.api.Assertions.assertNull;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.junit.jupiter.api.Assertions.fail;
 
 import java.io.File;
 import java.nio.charset.Charset;
@@ -689,14 +688,13 @@ public class EmailTest extends AbstractEmailTest {
         email.addTo(strTestMailTo);
         email.setAuthentication(null, null);
 
-        assertThrows(EmailException.class, () -> email.send());
+        assertThrows(EmailException.class, email::send);
     }
 
     @Test
     public void testSendBadHostName() {
-        try {
+        final EmailException e = assertThrows(EmailException.class, () -> {
             getMailServer();
-
             email = new MockEmailConcrete();
             email.setSubject("Test Email #1 Subject");
             email.setHostName("bad.host.com");
@@ -705,20 +703,16 @@ public class EmailTest extends AbstractEmailTest {
             email.addCc("m...@home.com");
             email.addBcc("m...@home.com");
             email.addReplyTo("m...@home.com");
-
             email.setContent("test string object", " ; charset=" + 
EmailConstants.US_ASCII);
-
             email.send();
-            fail("Should have thrown an exception");
-        } catch (final EmailException e) {
-            assertTrue(e.getCause() instanceof ParseException);
-            fakeMailServer.stop();
-        }
+        });
+        assertTrue(e.getCause() instanceof ParseException);
+        stopServer();
     }
 
     @Test
     public void testSendCorrectSmtpPortContainedInException() {
-        try {
+        final EmailException e = assertThrows(EmailException.class, () -> {
             getMailServer();
 
             email = new MockEmailConcrete();
@@ -728,11 +722,9 @@ public class EmailTest extends AbstractEmailTest {
             email.addTo(strTestMailTo);
             email.setAuthentication(null, null);
             email.send();
-            fail("Should have thrown an exception");
-        } catch (final EmailException e) {
-            assertTrue(e.getMessage().contains("bad.host.com:465"));
-            fakeMailServer.stop();
-        }
+        });
+        assertTrue(e.getMessage().contains("bad.host.com:465"));
+        stopServer();
     }
 
     @Test
@@ -744,7 +736,7 @@ public class EmailTest extends AbstractEmailTest {
         email.setSmtpPort(getMailServerPort());
         email.setFrom("m...@home.com");
 
-        assertThrows(EmailException.class, () -> email.send());
+        assertThrows(EmailException.class, email::send);
     }
 
     @Test
@@ -756,7 +748,7 @@ public class EmailTest extends AbstractEmailTest {
         email.setSmtpPort(getMailServerPort());
         email.addTo("m...@home.com");
 
-        assertThrows(EmailException.class, () -> email.send());
+        assertThrows(EmailException.class, email::send);
     }
 
     @Test
@@ -778,7 +770,7 @@ public class EmailTest extends AbstractEmailTest {
 
         email = new MockEmailConcrete();
 
-        assertThrows(EmailException.class, () -> email.send());
+        assertThrows(EmailException.class, email::send);
     }
 
     @Test
diff --git a/src/test/java/org/apache/commons/mail/HtmlEmailTest.java 
b/src/test/java/org/apache/commons/mail/HtmlEmailTest.java
index aaac1a0..3d011dc 100644
--- a/src/test/java/org/apache/commons/mail/HtmlEmailTest.java
+++ b/src/test/java/org/apache/commons/mail/HtmlEmailTest.java
@@ -20,8 +20,8 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertNotEquals;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.junit.jupiter.api.Assertions.fail;
 
 import java.io.File;
 import java.io.IOException;
@@ -147,13 +147,7 @@ public class HtmlEmailTest extends AbstractEmailTest {
         // this should NOT be called when sending a message
         email.buildMimeMessage();
 
-        try {
-            email.send();
-        } catch (final IllegalStateException e) {
-            return;
-        }
-
-        fail("Expecting an exception when calling buildMimeMessage() before 
send() ...");
+        assertThrows(IllegalStateException.class, email::send);
     }
 
     @Test
@@ -163,12 +157,7 @@ public class HtmlEmailTest extends AbstractEmailTest {
         final FileDataSource dataSource = new FileDataSource(tmpFile);
 
         // does embedding a datasource without a name fail?
-        try {
-            email.embed(dataSource, "");
-            fail("embedding with an empty string for a name should fail");
-        } catch (final EmailException e) {
-            // expected
-        }
+        assertThrows(EmailException.class, () -> email.embed(dataSource, ""));
 
         // properly embed the datasource
         final String cid = email.embed(dataSource, "testname");
@@ -183,11 +172,7 @@ public class HtmlEmailTest extends AbstractEmailTest {
         final File anotherFile = File.createTempFile("testEmbedDataSource2", 
"txt");
         anotherFile.deleteOnExit();
         final FileDataSource anotherDS = new FileDataSource(anotherFile);
-        try {
-            email.embed(anotherDS, "testname");
-        } catch (final EmailException e) {
-            // expected
-        }
+        assertThrows(EmailException.class, () -> email.embed(anotherDS, 
"testname"));
     }
 
     @Test
@@ -262,21 +247,11 @@ public class HtmlEmailTest extends AbstractEmailTest {
         // Test Exceptions
 
         // Does an invalid URL throw an exception?
-        try {
-            email.embed(createInvalidURL(), "Bad URL");
-            fail("Should have thrown an exception");
-        } catch (final EmailException e) {
-            // expected
-        }
+        assertThrows(EmailException.class, () -> 
email.embed(createInvalidURL(), "Bad URL"));
 
         // if we try to embed a different URL under a previously used name,
         // does it complain?
-        try {
-            email.embed(new URL("http://www.google.com";), "Test name");
-            fail("shouldn't be able to use an existing name with a different 
URL!");
-        } catch (final EmailException e) {
-            // expected
-        }
+        assertThrows(EmailException.class, () -> email.embed(new 
URL("http://www.google.com";), "Test name"));
     }
 
     @Test
@@ -300,12 +275,7 @@ public class HtmlEmailTest extends AbstractEmailTest {
         }
         // Test Exception
         for (final String invalidChar : testCharsNotValid) {
-            try {
-                email.setHtmlMsg(invalidChar);
-                fail("Should have thrown an exception");
-            } catch (final EmailException e) {
-                assertTrue(true);
-            }
+            assertThrows(EmailException.class, () -> 
email.setHtmlMsg(invalidChar));
         }
 
     }
@@ -321,12 +291,7 @@ public class HtmlEmailTest extends AbstractEmailTest {
         }
         // Test Exception
         for (final String invalidChar : testCharsNotValid) {
-            try {
-                email.setMsg(invalidChar);
-                fail("Should have thrown an exception");
-            } catch (final EmailException e) {
-                assertTrue(true);
-            }
+            assertThrows(EmailException.class, () -> 
email.setMsg(invalidChar));
         }
 
     }
@@ -340,12 +305,7 @@ public class HtmlEmailTest extends AbstractEmailTest {
         }
         // Test Exception
         for (final String invalidChar : testCharsNotValid) {
-            try {
-                email.setTextMsg(invalidChar);
-                fail("Should have thrown an exception");
-            } catch (final EmailException e) {
-                assertTrue(true);
-            }
+            assertThrows(EmailException.class, () -> 
email.setTextMsg(invalidChar));
         }
 
     }
diff --git a/src/test/java/org/apache/commons/mail/ImageHtmlEmailTest.java 
b/src/test/java/org/apache/commons/mail/ImageHtmlEmailTest.java
index e22f2c1..626aef8 100644
--- a/src/test/java/org/apache/commons/mail/ImageHtmlEmailTest.java
+++ b/src/test/java/org/apache/commons/mail/ImageHtmlEmailTest.java
@@ -17,8 +17,8 @@
 package org.apache.commons.mail;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.junit.jupiter.api.Assertions.fail;
 
 import java.io.File;
 import java.io.IOException;
@@ -243,13 +243,9 @@ public class ImageHtmlEmailTest extends HtmlEmailTest {
         // Create the email message
         final ImageHtmlEmail email = new ImageHtmlEmail();
 
-        // set the html message
-        try {
-            email.setHtmlMsg(null);
-            fail("Should fail here!");
-        } catch (final EmailException e) {
-            assertTrue(e.getMessage().contains("Invalid message."), 
e.getMessage());
-        }
+        // set the HTML message
+        final EmailException e = assertThrows(EmailException.class, () -> 
email.setHtmlMsg(null));
+        assertTrue(e.getMessage().contains("Invalid message."), 
e.getMessage());
     }
 
     @Test
@@ -259,13 +255,9 @@ public class ImageHtmlEmailTest extends HtmlEmailTest {
         // Create the email message
         final ImageHtmlEmail email = new ImageHtmlEmail();
 
-        // set the html message
-        try {
-            email.setHtmlMsg("");
-            fail("Should fail here!");
-        } catch (final EmailException e) {
-            assertTrue(e.getMessage().contains("Invalid message."), 
e.getMessage());
-        }
+        // set the HTML message
+        final EmailException e = assertThrows(EmailException.class, () -> 
email.setHtmlMsg(""));
+        assertTrue(e.getMessage().contains("Invalid message."), 
e.getMessage());
 
     }
 
diff --git a/src/test/java/org/apache/commons/mail/InvalidAddressTest.java 
b/src/test/java/org/apache/commons/mail/InvalidAddressTest.java
index 849c347..e82bd2e 100644
--- a/src/test/java/org/apache/commons/mail/InvalidAddressTest.java
+++ b/src/test/java/org/apache/commons/mail/InvalidAddressTest.java
@@ -16,7 +16,9 @@
  */
 package org.apache.commons.mail;
 
-import static org.junit.jupiter.api.Assertions.fail;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import java.util.stream.IntStream;
 
 import org.apache.commons.mail.mocks.MockEmailConcrete;
 import org.junit.jupiter.api.BeforeEach;
@@ -28,18 +30,41 @@ import org.junit.jupiter.api.Test;
  * @since 1.0
  */
 public class InvalidAddressTest extends AbstractEmailTest {
-    /** */
-    private static final String[] ARR_INVALID_EMAILS = { "local 
n...@domain.com", "local(n...@domain.com", "local)n...@domain.com", 
"local<n...@domain.com",
-            "local>n...@domain.com", "local,n...@domain.com", 
"local;n...@domain.com", "local:n...@domain.com", "local[n...@domain.com",
+
+    // @formatter:off
+    private static final String[] ARR_INVALID_EMAILS = {
+            "local n...@domain.com",
+            "local(n...@domain.com",
+            "local)n...@domain.com",
+            "local<n...@domain.com",
+            "local>n...@domain.com",
+            "local,n...@domain.com",
+            "local;n...@domain.com",
+            "local:n...@domain.com",
+            "local[n...@domain.com",
             "local]n...@domain.com",
             // "local\\n...@domain.com", is considered valid for mail-1.4.1
-            "local\"n...@domain.com", "local\tn...@domain.com", 
"local\nn...@domain.com", "local\rn...@domain.com", "local.name@domain com",
-            "local.name@domain(com", "local.name@domain)com", 
"local.name@domain<com", "local.name@domain>com", "local.name@domain,com",
-            "local.name@domain;com", "local.name@domain:com",
-
+            "local\"n...@domain.com",
+            "local\tn...@domain.com",
+            "local\nn...@domain.com",
+            "local\rn...@domain.com",
+            "local.name@domain com",
+            "local.name@domain(com",
+            "local.name@domain)com",
+            "local.name@domain<com",
+            "local.name@domain>com",
+            "local.name@domain,com",
+            "local.name@domain;com",
+            "local.name@domain:com",
             // "local.name@domain[com",
-            "local.name@domain]com", "local.name@domain\\com", 
"local.name@domain\tcom", "local.name@domain\ncom", "local.name@domain\rcom", 
"local.name@",
+            "local.name@domain]com",
+            "local.name@domain\\com",
+            "local.name@domain\tcom",
+            "local.name@domain\ncom",
+            "local.name@domain\rcom",
+            "local.name@",
             "@domain.com" };
+    // @formatter:on
 
     private MockEmailConcrete email;
 
@@ -51,65 +76,41 @@ public class InvalidAddressTest extends AbstractEmailTest {
 
     @Test
     public void testAddInvalidBcc() throws Exception {
-        // Test adding invalid 'Bcc' addresses
-        for (int i = 0; i < ARR_INVALID_EMAILS.length; i++) {
-            try {
-                // add bcc
-                email.addBcc(ARR_INVALID_EMAILS[i], "Joe");
-
-                // Expected an exception to be thrown
-                fail("addBcc " + i + " passed: " + ARR_INVALID_EMAILS[i]);
-            } catch (final EmailException ignore) {
-                // Expected Result
-            }
-        }
+        // Test adding invalid 'BCC' addresses
+        // @formatter:off
+        IntStream.range(0, ARR_INVALID_EMAILS.length).forEach(i -> 
assertThrows(EmailException.class,
+                () -> email.addBcc(ARR_INVALID_EMAILS[i], "Joe"),
+                () -> "addBcc " + i + " passed: " + ARR_INVALID_EMAILS[i]));
+        // @formatter:on
     }
 
     @Test
     public void testAddInvalidCc() throws Exception {
-        // Test adding invalid 'cc' addresses
-        for (int i = 0; i < ARR_INVALID_EMAILS.length; i++) {
-            try {
-                // add cc
-                email.addCc(ARR_INVALID_EMAILS[i], "Joe");
-
-                // Expected an exception to be thrown
-                fail("addCc " + i + " passed: " + ARR_INVALID_EMAILS[i]);
-            } catch (final EmailException ignore) {
-                // Expected Result
-            }
-        }
+        // Test adding invalid 'CC' addresses
+        // @formatter:off
+        IntStream.range(0, ARR_INVALID_EMAILS.length).forEach(i -> 
assertThrows(EmailException.class,
+                () -> email.addCc(ARR_INVALID_EMAILS[i], "Joe"),
+                () -> "addCc " + i + " passed: " + ARR_INVALID_EMAILS[i]));
+        // @formatter:on
     }
 
     @Test
     public void testAddInvalidTo() throws Exception {
         // Test adding invalid 'to' addresses
-        for (int i = 0; i < ARR_INVALID_EMAILS.length; i++) {
-            try {
-                // Add To
-                email.addTo(ARR_INVALID_EMAILS[i], "Joe");
-
-                // Expected an exception to be thrown
-                fail("addTo " + i + " passed: " + ARR_INVALID_EMAILS[i]);
-            } catch (final EmailException ignore) {
-                // Expected Result
-            }
-        }
+        // @formatter:off
+        IntStream.range(0, ARR_INVALID_EMAILS.length).forEach(i -> 
assertThrows(EmailException.class,
+                () -> email.addTo(ARR_INVALID_EMAILS[i], "Joe"),
+                () -> "addTo " + i + " passed: " + ARR_INVALID_EMAILS[i]));
+        // @formatter:on
     }
 
     @Test
     public void testSetInvalidFrom() throws Exception {
         // Test setting invalid 'from' addresses
-        for (int i = 0; i < ARR_INVALID_EMAILS.length; i++) {
-            try {
-                // set from
-                email.setFrom(ARR_INVALID_EMAILS[i]);
-
-                // Expected an exception to be thrown
-                fail("setFrom " + i + " passed: " + ARR_INVALID_EMAILS[i]);
-            } catch (final EmailException ignore) {
-                // Expected Result
-            }
-        }
+        // @formatter:off
+        IntStream.range(0, ARR_INVALID_EMAILS.length).forEach(i -> 
assertThrows(EmailException.class,
+                () -> email.setFrom(ARR_INVALID_EMAILS[i], "Joe"),
+                () -> "setFrom " + i + " passed: " + ARR_INVALID_EMAILS[i]));
+        // @formatter:on
     }
 }
diff --git 
a/src/test/java/org/apache/commons/mail/InvalidInternetAddressTest.java 
b/src/test/java/org/apache/commons/mail/InvalidInternetAddressTest.java
index 3a221f2..a36412f 100644
--- a/src/test/java/org/apache/commons/mail/InvalidInternetAddressTest.java
+++ b/src/test/java/org/apache/commons/mail/InvalidInternetAddressTest.java
@@ -16,15 +16,15 @@
  */
 package org.apache.commons.mail;
 
-import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.fail;
 
-import java.lang.reflect.Method;
 import java.nio.charset.StandardCharsets;
+import java.util.stream.IntStream;
 
 import javax.mail.internet.InternetAddress;
 
-import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 
 /**
@@ -36,69 +36,59 @@ public class InvalidInternetAddressTest extends 
AbstractEmailTest {
     /** */
     private static final String VALID_QUOTED_EMAIL = "\"John 
O'Groats\"@domain.com";
 
-    /** JavaMail 1.2. does not know about this */
-    private static Method validateMethod;
-
-    /** */
-    private static final String[] ARR_INVALID_EMAILS = { "local 
n...@domain.com", "local(n...@domain.com", "local)n...@domain.com", 
"local<n...@domain.com",
-            "local>n...@domain.com", "local,n...@domain.com", 
"local;n...@domain.com", "local:n...@domain.com", "local[n...@domain.com",
+    // @formatter:off
+    private static final String[] ARR_INVALID_EMAILS = {
+            "local n...@domain.com",
+            "local(n...@domain.com",
+            "local)n...@domain.com",
+            "local<n...@domain.com",
+            "local>n...@domain.com",
+            "local,n...@domain.com",
+            "local;n...@domain.com",
+            "local:n...@domain.com",
+            "local[n...@domain.com",
             "local]n...@domain.com",
             // "local\\n...@domain.com", -- works for javamail-1.4.4
             // "local\"n...@domain.com", -- works for javamail-1.4.4
-            "local\tn...@domain.com", "local\nn...@domain.com", 
"local\rn...@domain.com", "local.name@domain com", "local.name@domain(com",
-            "local.name@domain)com", "local.name@domain<com", 
"local.name@domain>com", "local.name@domain,com", "local.name@domain;com",
+            "local\tn...@domain.com",
+            "local\nn...@domain.com",
+            "local\rn...@domain.com",
+            "local.name@domain com",
+            "local.name@domain(com",
+            "local.name@domain)com",
+            "local.name@domain<com",
+            "local.name@domain>com",
+            "local.name@domain,com",
+            "local.name@domain;com",
             "local.name@domain:com",
             // "local.name@domain[com", -- works for javamail-1.5.5
-            "local.name@domain]com", "local.name@domain\\com", 
"local.name@domain\tcom", "local.name@domain\ncom", "local.name@domain\rcom", 
"local.name@",
+            "local.name@domain]com",
+            "local.name@domain\\com",
+            "local.name@domain\tcom",
+            "local.name@domain\ncom",
+            "local.name@domain\rcom",
+            "local.name@",
             "@domain.com" };
-
-    /**
-     * Setup for a test
-     */
-    @BeforeEach
-    public void setUpInvalidInternetAddressTest() {
-        try {
-            validateMethod = InternetAddress.class.getMethod("validate");
-        } catch (final Exception e) {
-            assertEquals(NoSuchMethodException.class, e.getClass(), "Got wrong 
Exception when looking for validate()");
-        }
-    }
+    // @formatter:on
 
     @Test
     public void testStrictConstructor() throws Exception {
         // Prove InternetAddress constructor is throwing exception.
 
         // test Invalid Email addresses
-        for (int i = 0; i < ARR_INVALID_EMAILS.length; i++) {
-
-            try {
-                // Create Internet Address using "strict" constructor
-                new InternetAddress(ARR_INVALID_EMAILS[i]);
-
-                // Expected an exception to be thrown
-                fail("Strict " + i + " passed: " + ARR_INVALID_EMAILS[i]);
-            } catch (final Exception ex) {
-                // Expected Result
-            }
-
-        }
+        // @formatter:off
+        IntStream.range(0, ARR_INVALID_EMAILS.length).forEach(i -> 
assertThrows(Exception.class,
+                () -> new InternetAddress(ARR_INVALID_EMAILS[i]),
+                () -> "Strict " + i + " passed: " + ARR_INVALID_EMAILS[i]));
+        // @formatter:on
 
         // test valid 'quoted' Email addresses
-        try {
-
-            // Create Internet Address using "strict" constructor
-            new InternetAddress(VALID_QUOTED_EMAIL);
-
-        } catch (final Exception ex) {
-            fail("Valid Quoted Email failed: " + VALID_QUOTED_EMAIL + " - " + 
ex.getMessage());
-        }
+        // Create Internet Address using "strict" constructor
+        assertDoesNotThrow(() -> new InternetAddress(VALID_QUOTED_EMAIL), () 
-> "Valid Quoted Email failed: " + VALID_QUOTED_EMAIL);
     }
 
     @Test
     public void testValidateMethod() throws Exception {
-        if (validateMethod == null) {
-            return;
-        }
         // Prove InternetAddress constructor isn't throwing exception and
         // the validate() method is
 
@@ -111,7 +101,7 @@ public class InvalidInternetAddressTest extends 
AbstractEmailTest {
             final int atIndex = ARR_INVALID_EMAILS[i].indexOf("@");
             final boolean domainBracket = atIndex >= 0 && 
ARR_INVALID_EMAILS[i].indexOf("[", atIndex) >= 0;
             try {
-                validateMethod.invoke(address, (Object[]) null);
+                address.validate();
 
                 if (!(quoted || domainBracket)) {
                     fail("Validate " + i + " passed: " + 
ARR_INVALID_EMAILS[i]);
@@ -124,18 +114,11 @@ public class InvalidInternetAddressTest extends 
AbstractEmailTest {
         }
 
         // test valid 'quoted' Email addresses
-        try {
-            validateMethod.invoke(new InternetAddress(VALID_QUOTED_EMAIL, 
"Joe"), (Object[]) null);
-        } catch (final Exception ex) {
-            fail("Valid Quoted Email failed: " + VALID_QUOTED_EMAIL + " - " + 
ex.getMessage());
-        }
+        assertDoesNotThrow(() -> new InternetAddress(VALID_QUOTED_EMAIL, 
"Joe").validate(), () -> "Valid Quoted Email failed: " + VALID_QUOTED_EMAIL);
     }
 
     @Test
     public void testValidateMethodCharset() throws Exception {
-        if (validateMethod == null) {
-            return;
-        }
         // Prove InternetAddress constructor isn't throwing exception and
         // the validate() method is
 
@@ -149,7 +132,7 @@ public class InvalidInternetAddressTest extends 
AbstractEmailTest {
             final boolean domainBracket = atIndex >= 0 && 
ARR_INVALID_EMAILS[i].indexOf("[", atIndex) >= 0;
 
             try {
-                validateMethod.invoke(address, (Object[]) null);
+                address.validate();
                 if (!(quoted || domainBracket)) {
                     fail("Validate " + i + " passed: " + 
ARR_INVALID_EMAILS[i]);
                 }
@@ -163,11 +146,9 @@ public class InvalidInternetAddressTest extends 
AbstractEmailTest {
         }
 
         // test valid 'quoted' Email addresses
-        try {
-            validateMethod.invoke(new InternetAddress(VALID_QUOTED_EMAIL, 
"Joe", StandardCharsets.UTF_8.name()), (Object[]) null);
-        } catch (final Exception ex) {
-            fail("Valid Quoted Email failed: " + VALID_QUOTED_EMAIL + " - " + 
ex.getMessage());
-        }
+        assertDoesNotThrow(() -> new InternetAddress(VALID_QUOTED_EMAIL, 
"Joe", StandardCharsets.UTF_8.name()).validate(),
+                () -> "Valid Quoted Email failed: " + VALID_QUOTED_EMAIL);
+
     }
 
 }
diff --git a/src/test/java/org/apache/commons/mail/MultiPartEmailTest.java 
b/src/test/java/org/apache/commons/mail/MultiPartEmailTest.java
index 9d87815..a00cf4b 100644
--- a/src/test/java/org/apache/commons/mail/MultiPartEmailTest.java
+++ b/src/test/java/org/apache/commons/mail/MultiPartEmailTest.java
@@ -17,8 +17,8 @@
 package org.apache.commons.mail;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.junit.jupiter.api.Assertions.fail;
 
 import java.io.File;
 import java.io.IOException;
@@ -57,34 +57,26 @@ public class MultiPartEmailTest extends AbstractEmailTest {
 
     @Test
     public void testAddPart() throws Exception {
-
         // setup
         email = new MockMultiPartEmailConcrete();
         final String strMessage = "hello";
         final String strContentType = "text/plain";
-
         // add part
         email.addPart(strMessage, strContentType);
-
         // validate
         assertEquals(strContentType, 
email.getContainer().getBodyPart(0).getContentType());
         assertEquals(strMessage, 
email.getContainer().getBodyPart(0).getDataHandler().getContent());
-
     }
 
     @Test
     public void testAddPart2() throws Exception {
-
         // setup
         email = new MockMultiPartEmailConcrete();
         final String strSubtype = "subtype/abc123";
-
         // add part
         email.addPart(new MimeMultipart(strSubtype));
-
         // validate
         
assertTrue(email.getContainer().getBodyPart(0).getDataHandler().getContentType().contains(strSubtype));
-
     }
 
     /**
@@ -95,7 +87,6 @@ public class MultiPartEmailTest extends AbstractEmailTest {
     public void testAttach2() throws MalformedURLException, EmailException {
         // Test Success - URL
         email.attach(new URL(strTestURL), "Test Attachment", "Test Attachment 
Desc");
-
         // bad name
         email.attach(new URL(strTestURL), null, "Test Attachment Desc");
     }
@@ -106,132 +97,85 @@ public class MultiPartEmailTest extends AbstractEmailTest 
{
         email.attach(new URLDataSource(new URL(strTestURL)), "Test 
Attachment", "Test Attachment Desc");
         // Test Exceptions
         // null datasource
-        try {
-            final URLDataSource urlDs = null;
-            email.attach(urlDs, "Test Attachment", "Test Attachment Desc");
-            fail("Should have thrown an exception");
-        } catch (final EmailException e) {
-            assertTrue(true);
-        }
-
+        assertThrows(EmailException.class, () -> email.attach((URLDataSource) 
null, "Test Attachment", "Test Attachment Desc"));
         // invalid datasource
-        try {
-            final URLDataSource urlDs = new URLDataSource(createInvalidURL());
-            email.attach(urlDs, "Test Attachment", "Test Attachment Desc");
-            fail("Should have thrown an exception");
-        } catch (final EmailException e) {
-            assertTrue(true);
-        }
+        assertThrows(EmailException.class, () -> email.attach(new 
URLDataSource(createInvalidURL()), "Test Attachment", "Test Attachment Desc"));
     }
 
     @Test
     public void testAttachFile() throws Exception {
-        EmailAttachment attachment;
+        final EmailAttachment attachment1;
         // Test Success - EmailAttachment
-        attachment = new EmailAttachment();
-        attachment.setName("Test Attachment");
-        attachment.setDescription("Test Attachment Desc");
-        attachment.setPath(testFile.getAbsolutePath());
-        email.attach(attachment);
+        attachment1 = new EmailAttachment();
+        attachment1.setName("Test Attachment");
+        attachment1.setDescription("Test Attachment Desc");
+        attachment1.setPath(testFile.getAbsolutePath());
+        email.attach(attachment1);
         assertTrue(email.isBoolHasAttachments());
         // Test Success - URL
-        attachment = new EmailAttachment();
-        attachment.setName("Test Attachment");
-        attachment.setDescription("Test Attachment Desc");
-        attachment.setURL(new URL(strTestURL));
-        email.attach(attachment);
+        final EmailAttachment attachment2 = new EmailAttachment();
+        attachment2.setName("Test Attachment");
+        attachment2.setDescription("Test Attachment Desc");
+        attachment2.setURL(new URL(strTestURL));
+        email.attach(attachment2);
         // Test Success - File
         email.attach(testFile);
         assertTrue(email.isBoolHasAttachments());
         // Test Exceptions
         // null attachment
-        try {
-            email.attach((EmailAttachment) null);
-            fail("Should have thrown an exception");
-        } catch (final EmailException e) {
-            assertTrue(true);
-        }
-
+        assertThrows(EmailException.class, () -> 
email.attach((EmailAttachment) null));
         // bad url
-        attachment = new EmailAttachment();
-        try {
-            attachment.setURL(createInvalidURL());
-            email.attach(attachment);
-            fail("Should have thrown an exception");
-        } catch (final EmailException e) {
-            assertTrue(true);
-        }
+        final EmailAttachment attachment3 = new EmailAttachment();
+        attachment3.setURL(createInvalidURL());
+        assertThrows(EmailException.class, () -> email.attach(attachment3));
 
         // bad file
-        attachment = new EmailAttachment();
-        try {
-            attachment.setPath("");
-            email.attach(attachment);
-            fail("Should have thrown an exception");
-        } catch (final EmailException e) {
-            assertTrue(true);
-        }
+        final EmailAttachment attachment4 = new EmailAttachment();
+        attachment4.setPath("");
+        assertThrows(EmailException.class, () -> email.attach(attachment4));
     }
 
     @Test
     public void testAttachFileLocking() throws Exception {
         // EMAIL-120: attaching a FileDataSource may result in a locked file
         // resource on windows systems
-
         final File tmpFile = File.createTempFile("attachment", ".eml");
-
         email.attach(new FileDataSource(tmpFile), "Test Attachment", "Test 
Attachment Desc");
-
         assertTrue(tmpFile.delete());
     }
 
     @Test
     public void testAttachPath() throws Exception {
-        EmailAttachment attachment;
+        final EmailAttachment attachment1;
         // Test Success - EmailAttachment
-        attachment = new EmailAttachment();
-        attachment.setName("Test Attachment");
-        attachment.setDescription("Test Attachment Desc");
-        attachment.setPath(testPath.toAbsolutePath().toString());
-        email.attach(attachment);
+        attachment1 = new EmailAttachment();
+        attachment1.setName("Test Attachment");
+        attachment1.setDescription("Test Attachment Desc");
+        attachment1.setPath(testPath.toAbsolutePath().toString());
+        email.attach(attachment1);
         assertTrue(email.isBoolHasAttachments());
         // Test Success - URL
-        attachment = new EmailAttachment();
-        attachment.setName("Test Attachment");
-        attachment.setDescription("Test Attachment Desc");
-        attachment.setURL(new URL(strTestURL));
-        email.attach(attachment);
+        final EmailAttachment attachment2 = new EmailAttachment();
+        attachment2.setName("Test Attachment");
+        attachment2.setDescription("Test Attachment Desc");
+        attachment2.setURL(new URL(strTestURL));
+        email.attach(attachment2);
         // Test Success - File
         email.attach(testPath);
         assertTrue(email.isBoolHasAttachments());
         // Test Exceptions
         // null attachment
-        try {
-            email.attach((EmailAttachment) null);
-            fail("Should have thrown an exception");
-        } catch (final EmailException e) {
-            assertTrue(true);
-        }
+        assertThrows(EmailException.class, () -> 
email.attach((EmailAttachment) null));
 
         // bad url
-        attachment = new EmailAttachment();
-        try {
-            attachment.setURL(createInvalidURL());
-            email.attach(attachment);
-            fail("Should have thrown an exception");
-        } catch (final EmailException e) {
-            assertTrue(true);
-        }
+        final EmailAttachment attachment3 = new EmailAttachment();
+        attachment3.setURL(createInvalidURL());
+        assertThrows(EmailException.class, () -> email.attach(attachment3));
 
         // bad file
-        attachment = new EmailAttachment();
-        try {
-            attachment.setPath("");
-            email.attach(attachment);
-            fail("Should have thrown an exception");
-        } catch (final EmailException e) {
-            assertTrue(true);
-        }
+        final EmailAttachment attachment4 = new EmailAttachment();
+        attachment4.setPath("");
+        assertThrows(EmailException.class, () -> email.attach(attachment4));
     }
 
     /** TODO implement test for GetContainer */
@@ -252,14 +196,9 @@ public class MultiPartEmailTest extends AbstractEmailTest {
     /** Init called twice should fail */
     @Test
     public void testInit() {
+        email.init();
         // call the init function twice to trigger the IllegalStateException
-        try {
-            email.init();
-            email.init();
-            fail("Should have thrown an exception");
-        } catch (final IllegalStateException e) {
-            assertTrue(true);
-        }
+        assertThrows(IllegalStateException.class, email::init);
     }
 
     /**
@@ -304,7 +243,7 @@ public class MultiPartEmailTest extends AbstractEmailTest {
 
         testEmail.send();
 
-        fakeMailServer.stop();
+        stopServer();
         // validate message
         validateSend(fakeMailServer, strSubject, testEmail.getMsg(), 
testEmail.getFromAddress(), testEmail.getToAddresses(), 
testEmail.getCcAddresses(),
                 testEmail.getBccAddresses(), true);
@@ -313,14 +252,8 @@ public class MultiPartEmailTest extends AbstractEmailTest {
         validateSend(fakeMailServer, strSubject, attachment.getName(), 
testEmail.getFromAddress(), testEmail.getToAddresses(), 
testEmail.getCcAddresses(),
                 testEmail.getBccAddresses(), false);
         // Test Exceptions
-        try {
-            getMailServer();
-
-            email.send();
-            fail("Should have thrown an exception");
-        } catch (final EmailException e) {
-            fakeMailServer.stop();
-        }
+        getMailServer();
+        assertThrows(EmailException.class, email::send);
     }
 
     @Test
@@ -341,12 +274,8 @@ public class MultiPartEmailTest extends AbstractEmailTest {
         }
         // Test Exceptions
         for (final String invalidChar : testCharsNotValid) {
-            try {
-                email.setMsg(invalidChar);
-                fail("Should have thrown an exception");
-            } catch (final EmailException e) {
-                assertTrue(true);
-            }
+            assertThrows(EmailException.class, () -> 
email.setMsg(invalidChar));
+
         }
     }
 }
diff --git a/src/test/java/org/apache/commons/mail/SimpleEmailTest.java 
b/src/test/java/org/apache/commons/mail/SimpleEmailTest.java
index 2720a7c..a92fae9 100644
--- a/src/test/java/org/apache/commons/mail/SimpleEmailTest.java
+++ b/src/test/java/org/apache/commons/mail/SimpleEmailTest.java
@@ -17,8 +17,8 @@
 package org.apache.commons.mail;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.junit.jupiter.api.Assertions.fail;
 
 import java.io.IOException;
 import java.nio.charset.StandardCharsets;
@@ -92,12 +92,7 @@ public class SimpleEmailTest extends AbstractEmailTest {
         }
         // Test Exception
         for (final String invalidChar : testCharsNotValid) {
-            try {
-                email.setMsg(invalidChar);
-                fail("Should have thrown an exception");
-            } catch (final EmailException e) {
-                assertTrue(true);
-            }
+            assertThrows(EmailException.class, () -> 
email.setMsg(invalidChar));
         }
 
     }


Reply via email to