|
| 1 | +/* |
| 2 | + * Copyright and related rights waived via CC0 |
| 3 | + * |
| 4 | + * You should have received a copy of the CC0 legalcode along with this |
| 5 | + * work. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>. |
| 6 | + */ |
| 7 | +package com.sun.mail; |
| 8 | + |
| 9 | +import java.io.File; |
| 10 | +import java.io.IOException; |
| 11 | +import java.nio.charset.StandardCharsets; |
| 12 | +import java.nio.file.Files; |
| 13 | +import java.nio.file.Path; |
| 14 | +import java.time.Duration; |
| 15 | +import java.util.Properties; |
| 16 | + |
| 17 | +import jakarta.mail.Message; |
| 18 | +import jakarta.mail.MessagingException; |
| 19 | +import jakarta.mail.Session; |
| 20 | +import jakarta.mail.Transport; |
| 21 | +import jakarta.mail.internet.InternetAddress; |
| 22 | +import jakarta.mail.internet.MimeMessage; |
| 23 | +import org.apache.commons.io.FileUtils; |
| 24 | +import org.assertj.core.api.Assertions; |
| 25 | +import org.awaitility.Awaitility; |
| 26 | +import org.junit.jupiter.api.AfterAll; |
| 27 | +import org.junit.jupiter.api.BeforeAll; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | + |
| 30 | +public class JakartaMailTest { |
| 31 | + |
| 32 | + @BeforeAll |
| 33 | + static void beforeAll() throws IOException { |
| 34 | + System.out.println("Starting email server ..."); |
| 35 | + new ProcessBuilder("docker", "run", "--cidfile", "greenmail.cid", "-p", "3025:3025", "--rm", "-e", |
| 36 | + "GREENMAIL_OPTS=-Dgreenmail.setup.test.all -Dgreenmail.hostname=0.0.0.0 -Dgreenmail.auth.disabled -Dgreenmail.verbose", "greenmail/standalone:1.6.10") |
| 37 | + .redirectOutput(new File("greenmail-stdout.txt")) |
| 38 | + .redirectError(new File("greenmail-stderr.txt")).start(); |
| 39 | + Awaitility.await().atMost(Duration.ofSeconds(10)).until(() -> { |
| 40 | + String logs = FileUtils.readFileToString(new File("greenmail-stdout.txt"), StandardCharsets.UTF_8); |
| 41 | + return logs.contains("smtp.SmtpServer| Started") && logs.contains("pop3.Pop3Server| Started") && logs.contains("imap.ImapServer| Started"); |
| 42 | + }); |
| 43 | + } |
| 44 | + |
| 45 | + @AfterAll |
| 46 | + static void tearDown() throws InterruptedException, IOException { |
| 47 | + File cidFile = new File("greenmail.cid"); |
| 48 | + String cid = FileUtils.readFileToString(cidFile, StandardCharsets.UTF_8); |
| 49 | + Files.delete(cidFile.toPath()); |
| 50 | + new ProcessBuilder("docker", "kill", cid).start().waitFor(); |
| 51 | + Files.delete(Path.of("greenmail-stdout.txt")); |
| 52 | + Files.delete(Path.of("greenmail-stderr.txt")); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void sendMailWithSmtp() throws MessagingException { |
| 57 | + Properties properties = new Properties(); |
| 58 | + properties.put("mail.smtp.host", "localhost"); |
| 59 | + properties.put("mail.smtp.port", "3025"); |
| 60 | + Session session = Session.getInstance(properties); |
| 61 | + Message message = new MimeMessage(session); |
| 62 | + message.setFrom(new InternetAddress("alice@localhost")); |
| 63 | + message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("bob@localhost")); |
| 64 | + message.setSubject("This is a test"); |
| 65 | + message.setText("Dear Bob, hello world! Alice."); |
| 66 | + Transport.send(message); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void resources() { |
| 71 | + ClassLoader classLoader = getClass().getClassLoader(); |
| 72 | + Assertions.assertThat(classLoader.getResource("META-INF/hk2-locator/default")).isNotNull(); |
| 73 | + Assertions.assertThat(classLoader.getResource("META-INF/gfprobe-provider.xml")).isNotNull(); |
| 74 | + Assertions.assertThat(classLoader.getResource("META-INF/javamail.charset.map")).isNotNull(); |
| 75 | + Assertions.assertThat(classLoader.getResource("META-INF/javamail.default.address.map")).isNotNull(); |
| 76 | + Assertions.assertThat(classLoader.getResource("META-INF/javamail.default.providers")).isNotNull(); |
| 77 | + Assertions.assertThat(classLoader.getResource("META-INF/mailcap")).isNotNull(); |
| 78 | + } |
| 79 | + |
| 80 | +} |
0 commit comments