View Javadoc
1   /*
2    * Licensed under the GPL License. You may not use this file except in compliance with the License.
3    * You may obtain a copy of the License at
4    *
5    *   https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
6    *
7    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
8    * WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
9    * PURPOSE.
10   */
11  package psiprobe.tools;
12  
13  import static org.junit.jupiter.api.Assertions.assertEquals;
14  import static org.junit.jupiter.api.Assertions.assertFalse;
15  import static org.junit.jupiter.api.Assertions.assertTrue;
16  
17  import com.codebox.bean.JavaBeanTester;
18  
19  import jakarta.activation.DataSource;
20  import jakarta.activation.FileDataSource;
21  
22  import java.io.File;
23  import java.io.IOException;
24  import java.nio.file.Files;
25  
26  import org.junit.jupiter.api.Test;
27  
28  /**
29   * The Class MailMessageTest.
30   */
31  class MailMessageTest {
32  
33    /**
34     * Javabean tester.
35     */
36    @Test
37    void javabeanTester() {
38      JavaBeanTester.builder(MailMessage.class).loadData().test();
39    }
40  
41    @Test
42    void testConstructorAndGetters() {
43      MailMessage msg = new MailMessage("to@example.com", "Subject", "Body");
44      assertEquals(1, msg.getToArray().length);
45      assertEquals("to@example.com", msg.getToArray()[0]);
46      assertEquals("Subject", msg.getSubject());
47      assertEquals("Body", msg.getBody());
48      assertFalse(msg.isBodyHtml());
49    }
50  
51    @Test
52    void testAddRecipientToAndClear() {
53      MailMessage msg = new MailMessage("a@b.com", "s", "b");
54      msg.addRecipientTo("c@d.com");
55      assertEquals(2, msg.getToArray().length);
56      msg.clearRecipientsTo();
57      assertEquals(0, msg.getToArray().length);
58    }
59  
60    @Test
61    void testAddRecipientCc() {
62      MailMessage msg = new MailMessage("a@b.com", "s", "b");
63      msg.addRecipientCc("cc@example.com");
64      assertEquals(1, msg.getCcArray().length);
65      msg.clearRecipientsCc();
66      assertEquals(0, msg.getCcArray().length);
67    }
68  
69    @Test
70    void testAddRecipientBcc() {
71      MailMessage msg = new MailMessage("a@b.com", "s", "b");
72      msg.addRecipientBcc("bcc@example.com");
73      assertEquals(1, msg.getBccArray().length);
74      msg.clearRecipientsBcc();
75      assertEquals(0, msg.getBccArray().length);
76    }
77  
78    @Test
79    void testAddNullRecipient() {
80      MailMessage msg = new MailMessage("a@b.com", "s", "b");
81      msg.addRecipientTo(null);
82      // null should not be added
83      assertEquals(1, msg.getToArray().length);
84    }
85  
86    @Test
87    void testAddAttachmentFile() throws IOException {
88      File file = Files.createTempFile("mail-attachment-", ".txt").toFile();
89      try {
90        MailMessage msg = new MailMessage("a@b.com", "s", "b");
91        msg.addAttachment(file);
92        assertEquals(1, msg.getAttachmentsArray().length);
93        msg.clearAttachments();
94        assertEquals(0, msg.getAttachmentsArray().length);
95      } finally {
96        Files.deleteIfExists(file.toPath());
97      }
98    }
99  
100   @Test
101   void testAddAttachmentDataSource() {
102     MailMessage msg = new MailMessage("a@b.com", "s", "b");
103     DataSource ds = new FileDataSource("/tmp/test.txt");
104     msg.addAttachment(ds);
105     assertEquals(1, msg.getAttachmentsArray().length);
106   }
107 
108   @Test
109   void testSetBodyHtml() {
110     MailMessage msg = new MailMessage("a@b.com", "s", "b");
111     msg.setBodyHtml(true);
112     assertTrue(msg.isBodyHtml());
113   }
114 
115   @Test
116   void testSetSubjectAndBody() {
117     MailMessage msg = new MailMessage("a@b.com", "original", "original body");
118     msg.setSubject("updated subject");
119     msg.setBody("updated body");
120     assertEquals("updated subject", msg.getSubject());
121     assertEquals("updated body", msg.getBody());
122   }
123 
124   @Test
125   void testChaining() {
126     MailMessage msg = new MailMessage("a@b.com", "s", "b");
127     // Verify methods return 'this' for chaining
128     MailMessage result = msg.addRecipientTo("x@y.com").addRecipientCc("cc@y.com")
129         .addRecipientBcc("bcc@y.com").setSubject("sub").setBody("body").setBodyHtml(true);
130     assertEquals(msg, result);
131   }
132 }