diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7a03907..85e055c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -4,7 +4,7 @@ # - push / pull_request on the feature/3.0.x branch # - manual workflow_dispatch # -# Runs `mvn -B clean verify` which includes the JaCoCo coverage gate +# Runs `sh ./mvnw -B clean verify` which includes the JaCoCo coverage gate # (90% line coverage, haltOnFailure=false) configured in the POM. name: CI @@ -35,7 +35,7 @@ jobs: cache: maven - name: Build and verify with JaCoCo coverage gate - run: mvn -B --no-transfer-progress clean verify + run: sh ./mvnw -B --no-transfer-progress clean verify - name: Upload JaCoCo coverage report if: always() diff --git a/hitool-core/src/main/java/hitool/core/beanutils/BeanPropertyUtils.java b/hitool-core/src/main/java/hitool/core/beanutils/BeanPropertyUtils.java index 28b39ee..e407c0f 100644 --- a/hitool-core/src/main/java/hitool/core/beanutils/BeanPropertyUtils.java +++ b/hitool-core/src/main/java/hitool/core/beanutils/BeanPropertyUtils.java @@ -244,36 +244,25 @@ public static void setProperty(Object bean, String name, Object value, String name3 = matcher.group(1); int index = Integer.parseInt(matcher.group(2)); System.out.println(matcher.group(0)); - Object value3 = BeanUtils.getProperty(propertyVlaue, name3); - if (value3 != null) { - if (value3 instanceof Collection) { - ArrayList values = new ArrayList(); - Iterator items = ((Collection) value).iterator(); - while (items.hasNext()) { - Object item = items.next(); - if (item == null) { - values.add(null); - } else { - values.add(item); - } - } - BeanUtils.setProperty(propertyVlaue, name3,values); - } else if (value.getClass().isArray()) { - int length = Array.getLength(value); - Object[] results = new Object[length]; - for (int i = 0; i < length; i++) { - Object item = Array.get(value, i); - if (item == null) { - results[i] = null; - } else { - results[i] = item; - } - } - BeanUtils.setProperty(propertyVlaue, name3,results); - } else { - BeanUtils.setProperty(propertyVlaue, name3,value); + Object current = getPropertyUtils().getProperty(propertyVlaue, name3); + Class fieldType = getPropertyUtils().getPropertyType(propertyVlaue, name3); + if (fieldType != null && fieldType.isArray()) { + int length = current == null ? 0 : Array.getLength(current); + Object array = Array.newInstance(fieldType.getComponentType(), Math.max(length, index + 1)); + if (length > 0) { + System.arraycopy(current, 0, array, 0, length); } - } + Array.set(array, index, value); + getPropertyUtils().setProperty(propertyVlaue, name3, array); + } else if (current instanceof List) { + List list = (List) current; + while (list.size() <= index) { + list.add(null); + } + list.set(index, value); + } else { + BeanUtils.setProperty(propertyVlaue, name2, value); + } } else { BeanUtils.setProperty(propertyVlaue,name2,value); } diff --git a/hitool-core/src/main/java/hitool/core/compress/BZip2Utils.java b/hitool-core/src/main/java/hitool/core/compress/BZip2Utils.java index 0500a83..5534a64 100644 --- a/hitool-core/src/main/java/hitool/core/compress/BZip2Utils.java +++ b/hitool-core/src/main/java/hitool/core/compress/BZip2Utils.java @@ -82,7 +82,7 @@ public static void compress(File file) throws IOException { * @throws Exception */ public static void compress(File srcFile, boolean delete) throws IOException { - File destFile = new File(srcFile.getParentFile(), FilenameUtils.getBaseName(srcFile.getName()) + EXT); + File destFile = new File(srcFile.getParentFile(), srcFile.getName() + EXT); // 压缩 BZip2Utils.compress(srcFile, destFile); if (delete) { @@ -175,7 +175,7 @@ public static void decompress(File srcFile, boolean delete) throws IOException { } public static void decompress(File srcFile, File destDir) throws IOException { - File destFile = new File(destDir, FilenameUtils.getBaseName(srcFile.getName()) + EXT); + File destFile = new File(destDir, FilenameUtils.getBaseName(srcFile.getName())); try(InputStream input = new FileInputStream(srcFile); OutputStream output = new FileOutputStream(destFile);) { BZip2Utils.decompress(input, output); diff --git a/hitool-core/src/main/java/hitool/core/compress/GZipUtils.java b/hitool-core/src/main/java/hitool/core/compress/GZipUtils.java index 70670ec..391b0bb 100644 --- a/hitool-core/src/main/java/hitool/core/compress/GZipUtils.java +++ b/hitool-core/src/main/java/hitool/core/compress/GZipUtils.java @@ -80,7 +80,7 @@ public static void compress(File file) throws IOException { * @throws Exception */ public static void compress(File srcFile, boolean delete) throws IOException { - File destFile = new File(srcFile.getParentFile(), FilenameUtils.getBaseName(srcFile.getName()) + EXT); + File destFile = new File(srcFile.getParentFile(), srcFile.getName() + EXT); // 压缩 GZipUtils.compress(srcFile, destFile); if (delete) { @@ -193,7 +193,7 @@ public static void decompress(File srcFile, boolean delete) throws IOException { } public static void decompress(File srcFile, File destDir) throws IOException { - File destFile = new File(destDir, FilenameUtils.getBaseName(srcFile.getName()) + EXT); + File destFile = new File(destDir, FilenameUtils.getBaseName(srcFile.getName())); try (InputStream input = new FileInputStream(srcFile); OutputStream output = new FileOutputStream(destFile);){ GZipUtils.decompress(input, output); diff --git a/hitool-core/src/main/java/hitool/core/lang3/network/MacAddressUtils.java b/hitool-core/src/main/java/hitool/core/lang3/network/MacAddressUtils.java index 427601e..ce1511a 100644 --- a/hitool-core/src/main/java/hitool/core/lang3/network/MacAddressUtils.java +++ b/hitool-core/src/main/java/hitool/core/lang3/network/MacAddressUtils.java @@ -273,7 +273,11 @@ public static String getHostMacAddress(String host) { // 获得网络接口对象(即网卡),并得到mac地址,mac地址存在于一个byte数组中。 byte[] mac = null; try { - mac = NetworkInterface.getByInetAddress(InetAddress.getByName(host)).getHardwareAddress(); + NetworkInterface networkInterface = NetworkInterface.getByInetAddress(InetAddress.getByName(host)); + if (networkInterface == null) { + return null; + } + mac = networkInterface.getHardwareAddress(); } catch (SocketException e) { e.printStackTrace(); } catch (UnknownHostException e) { diff --git a/hitool-core/src/main/java/hitool/core/io/fileTypes.properties b/hitool-core/src/main/resources/hitool/core/io/fileTypes.properties similarity index 100% rename from hitool-core/src/main/java/hitool/core/io/fileTypes.properties rename to hitool-core/src/main/resources/hitool/core/io/fileTypes.properties diff --git a/hitool-core/src/test/java/hitool/core/beanutils/BeanPropertyUtilsTest.java b/hitool-core/src/test/java/hitool/core/beanutils/BeanPropertyUtilsTest.java index 9890d29..94adcd0 100644 --- a/hitool-core/src/test/java/hitool/core/beanutils/BeanPropertyUtilsTest.java +++ b/hitool-core/src/test/java/hitool/core/beanutils/BeanPropertyUtilsTest.java @@ -36,8 +36,8 @@ public void testUtils() { BeanPropertyUtils.populate(entity, source, true); // System.out.println(entity.getMethodName()); System.out.println("===========================RESULT================================"); - System.out.println(entity.getProxyBean().getArray()[0]); - System.out.println("proxyBean.proxyBean.methodName:"+entity.getProxyBean().getProxyBean().getMethodName()); + org.junit.Assert.assertEquals("测试名称1", entity.getProxyBean().getArray()[1]); + org.junit.Assert.assertEquals("测试名称1", entity.getProxyBean().getProxyBean().getMethodName()); } diff --git a/hitool-core/src/test/java/hitool/core/compress/BZip2UtilsTest.java b/hitool-core/src/test/java/hitool/core/compress/BZip2UtilsTest.java index d91cbbd..ca86015 100644 --- a/hitool-core/src/test/java/hitool/core/compress/BZip2UtilsTest.java +++ b/hitool-core/src/test/java/hitool/core/compress/BZip2UtilsTest.java @@ -36,17 +36,20 @@ public final void testDataCompress() throws Exception { @Test public final void testFileCompress() throws Exception { - FileOutputStream fos = new FileOutputStream("d:/f.txt"); + File inputFile = File.createTempFile("hitool-compress-", ".txt"); + inputFile.deleteOnExit(); + new File(inputFile.getAbsolutePath() + ".bz2").deleteOnExit(); + FileOutputStream fos = new FileOutputStream(inputFile); fos.write(inputStr.getBytes()); fos.flush(); fos.close(); - BZip2Utils.compress("d:/f.txt"); + BZip2Utils.compress(inputFile.getAbsolutePath()); - BZip2Utils.decompress("d:/f.txt.bz2"); + BZip2Utils.decompress(inputFile.getAbsolutePath() + ".bz2"); - File file = new File("d:/f.txt"); + File file = new File(inputFile.getAbsolutePath()); FileInputStream fis = new FileInputStream(file); diff --git a/hitool-core/src/test/java/hitool/core/compress/GZipUtilsTest.java b/hitool-core/src/test/java/hitool/core/compress/GZipUtilsTest.java index 81591eb..24c0b2b 100644 --- a/hitool-core/src/test/java/hitool/core/compress/GZipUtilsTest.java +++ b/hitool-core/src/test/java/hitool/core/compress/GZipUtilsTest.java @@ -38,17 +38,20 @@ public final void testDataCompress() throws Exception { @Test public final void testFileCompress() throws Exception { - FileOutputStream fos = new FileOutputStream("d:/f.txt"); + File inputFile = File.createTempFile("hitool-compress-", ".txt"); + inputFile.deleteOnExit(); + new File(inputFile.getAbsolutePath() + ".gz").deleteOnExit(); + FileOutputStream fos = new FileOutputStream(inputFile); fos.write(inputStr.getBytes()); fos.flush(); fos.close(); - GZipUtils.compress("d:/f.txt", false); + GZipUtils.compress(inputFile.getAbsolutePath(), false); - GZipUtils.decompress("d:/f.txt.gz", false); + GZipUtils.decompress(inputFile.getAbsolutePath() + ".gz", false); - File file = new File("d:/f.txt"); + File file = new File(inputFile.getAbsolutePath()); FileInputStream fis = new FileInputStream(file); diff --git a/hitool-core/src/test/java/hitool/core/io/utils/FiletypeUtils_Test.java b/hitool-core/src/test/java/hitool/core/io/utils/FiletypeUtils_Test.java index 03eecd7..ac33ee9 100644 --- a/hitool-core/src/test/java/hitool/core/io/utils/FiletypeUtils_Test.java +++ b/hitool-core/src/test/java/hitool/core/io/utils/FiletypeUtils_Test.java @@ -11,7 +11,8 @@ public class FiletypeUtils_Test extends TestCase { public void testname() throws Exception { - assertSame(FiletypeUtils.getFileType(new File("D://cos.jar")), "jar"); + byte[] jarHeader = new byte[] {0x50, 0x4B, 0x03, 0x04, 0x0A, 0, 0, 0, 0, 0, 0}; + assertEquals("jar", FiletypeUtils.getFileType(jarHeader)); } diff --git a/hitool-core/src/test/java/hitool/core/lang3/MacAddressUtils_Test.java b/hitool-core/src/test/java/hitool/core/lang3/MacAddressUtils_Test.java index dad259f..f3bdcbc 100644 --- a/hitool-core/src/test/java/hitool/core/lang3/MacAddressUtils_Test.java +++ b/hitool-core/src/test/java/hitool/core/lang3/MacAddressUtils_Test.java @@ -21,7 +21,7 @@ public void testRemoteMacAddr_4() { } public void testRemoteMacAddr_3() { - System.out.println("HostMacAddress is : " + MacAddressUtils.getHostMacAddress("192.168.31.1")); + assertNull(MacAddressUtils.getHostMacAddress("127.0.0.1")); } } diff --git a/hitool-mail/pom.xml b/hitool-mail/pom.xml index 24114c6..41149dd 100644 --- a/hitool-mail/pom.xml +++ b/hitool-mail/pom.xml @@ -48,6 +48,12 @@ jakarta.activation jakarta.activation-api + + org.eclipse.angus + angus-activation + 2.0.2 + runtime + com.sun.mail @@ -81,16 +87,6 @@ spring-context-support true provided - - - spring-beans - org.springframework - - - spring-context - org.springframework - - diff --git a/hitool-mail/src/main/java/hitool/mail/def/CommonsMailClientImpl.java b/hitool-mail/src/main/java/hitool/mail/def/CommonsMailClientImpl.java index db47cdb..696d444 100644 --- a/hitool-mail/src/main/java/hitool/mail/def/CommonsMailClientImpl.java +++ b/hitool-mail/src/main/java/hitool/mail/def/CommonsMailClientImpl.java @@ -127,7 +127,7 @@ protected void sendMail(EmailBody email,boolean isHtml) throws Exception{ } // 设置收件人的名片和地址 - if(email.getMailto() != null){ + if(email.getMailto() != null && !email.getMailto().isEmpty()){ List mailtoList = new ArrayList(); for (String mailto : email.getMailto().keySet()) { // 添加一个收件人 @@ -138,7 +138,7 @@ protected void sendMail(EmailBody email,boolean isHtml) throws Exception{ } // 设置抄送的名片和地址 - if(email.getMailcc() != null){ + if(email.getMailcc() != null && !email.getMailcc().isEmpty()){ List mailccList = new ArrayList(); for (String mailcc : email.getMailcc().keySet()) { // 添加一个抄送 @@ -149,7 +149,7 @@ protected void sendMail(EmailBody email,boolean isHtml) throws Exception{ } // 设置密送的名片和地址 - if(email.getMailBcc() != null){ + if(email.getMailBcc() != null && !email.getMailBcc().isEmpty()){ List mailBccList = new ArrayList(); for (String mailcc : email.getMailBcc().keySet()) { // 添加一个密送 diff --git a/hitool-mail/src/test/java/hitool/mail/CommonsMailClientTest.java b/hitool-mail/src/test/java/hitool/mail/CommonsMailClientTest.java index fd7cc36..fef50f5 100644 --- a/hitool-mail/src/test/java/hitool/mail/CommonsMailClientTest.java +++ b/hitool-mail/src/test/java/hitool/mail/CommonsMailClientTest.java @@ -2,7 +2,6 @@ import hitool.mail.conf.EmailBody; import hitool.mail.def.CommonsMailClientImpl; -import hitool.mail.provider.EmailPropertiesProvider; import hitool.mail.provider.def.DefaultEmailPropertiesProvider; import org.junit.After; import org.junit.Before; @@ -10,103 +9,40 @@ import java.util.Properties; +import static org.junit.Assert.assertTrue; + public class CommonsMailClientTest { + private LocalSmtpServer smtp; + private CommonsMailClientImpl mailClient; - protected Properties props = System.getProperties(); - protected EmailPropertiesProvider propsProvider; - protected JavaMailClientAdapter mailClient; - - @Before - public void setup(){ - - - /* - * 我们是没办法使用javamail通过一般的代理服务器发送邮件的,比如下面的代码是没有效果的: - */ - /*Properties props = System.getProperties(); - props.setProperty("proxySet","true"); - props.setProperty("ProxyHost","192.168.155.1"); - props.setProperty("ProxyPort","1080");*/ - //或者这样,也是没用 - /*Properties props = System.getProperties(); - props.setProperty("proxySet","true"); - props.setProperty("http.proxyHost","192.168.155.1"); - props.setProperty("http.proxyPort","808");*/ + @Before + public void setup() throws Exception { + smtp = new LocalSmtpServer(); + Properties props = new Properties(); + props.setProperty(JavaMailKey.MAIL_HOST, "127.0.0.1"); + props.setProperty(JavaMailKey.MAIL_PORT, Integer.toString(smtp.port())); + props.setProperty(JavaMailKey.MAIL_USER, "sender@example.test"); + props.setProperty(JavaMailKey.MAIL_SMTP_AUTH, "false"); + props.setProperty(JavaMailKey.MAIL_SMTP_TIMEOUT, "5"); + DefaultEmailPropertiesProvider provider = new DefaultEmailPropertiesProvider(); + provider.setProps(props); + mailClient = new CommonsMailClientImpl(); + mailClient.setPropsProvider(provider); + } - /* - * 不过可以通过socks网关来访问外网的email服务器,当然,前提是你安装了一个socks服务器 - * 此处使用ccproxy代理测试 - */ - - // 设置代理服务器 - props.setProperty("proxySet", "true"); - props.setProperty("socksProxyHost", "10.71.19.215"); - props.setProperty("socksProxyPort", "1080"); - - //props.setProperty("mail.smtp.host", "smtp.163.com"); - //props.setProperty("mail.smtp.port", "465"); - - - props.put(JavaMailKey.MAIL_DEBUG, "true"); - props.setProperty(JavaMailKey.MAIL_HOST, "smtp.163.com"); - props.setProperty(JavaMailKey.MAIL_PORT, "25"); - //props.setProperty(JavaMailKey.MAIL_USER, "xxx@163.com"); - //props.setProperty(JavaMailKey.MAIL_PASSWORD, "xxx"); - props.setProperty(JavaMailKey.MAIL_USER, "xxx@163.com"); - props.setProperty(JavaMailKey.MAIL_PASSWORD, "xxx"); - //commons-mail需要设置mail.smtp.user和mail.smtp.password,才能正常发送邮件 - props.put(JavaMailKey.MAIL_SMTP_USER, "xxx@163.com"); - props.put(JavaMailKey.MAIL_SMTP_PASSWORD, "xxx"); - props.put(JavaMailKey.MAIL_SMTP_AUTH, "true"); - - - props.put(JavaMailKey.MAIL_SMTP_AUTH, "true"); - // props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY); - // props.setProperty("mail.smtp.socketFactory.fallback", "false"); - // props.setProperty("mail.smtp.socketFactory.port", "465"); - - // props.put("mail.store.protocol", "pop3"); - // props.put("mail.transport.protocol", "smtp"); - - mailClient = new CommonsMailClientImpl(); - - - propsProvider = new DefaultEmailPropertiesProvider(); - - propsProvider.setProps(props); - - mailClient.setPropsProvider(propsProvider); - - - } - - @Test - public void testDelete() throws Exception{ - - //构建邮件对象 - EmailBody email = new EmailBody(); - - // 设置发件人信息 - email.setFrom("xxx-消息中心", props.getProperty(JavaMailKey.MAIL_USER), false); + @Test + public void sendsMimeMessageToLocalServer() throws Exception { + EmailBody email = new EmailBody(); + email.setFrom("Sender", "sender@example.test", false); + email.setMailto("Recipient", "recipient@example.test"); + email.setSubject("Mail client test"); + email.setContent("Hello from Hitool"); + assertTrue(mailClient.sendMime(email)); + assertTrue(smtp.awaitMessage().contains("recipient@example.test")); + } - // 设置收件人的邮箱 - String sendTo = "xxx@qq.com"; - email.setMailto(sendTo, sendTo); - - // 设置邮件标题 - email.setSubject("邮件客户端测试"); - // 设置邮件的内容体 - String content = "如果您收到此邮件说明测试成功。"; - email.setContent(content); - - mailClient.sendMime(email); - - - } - - @After - public void shutdown(){ - - } - + @After + public void shutdown() throws Exception { + smtp.close(); + } } diff --git a/hitool-mail/src/test/java/hitool/mail/JavaMailClientTest.java b/hitool-mail/src/test/java/hitool/mail/JavaMailClientTest.java index 874512e..fb9784e 100644 --- a/hitool-mail/src/test/java/hitool/mail/JavaMailClientTest.java +++ b/hitool-mail/src/test/java/hitool/mail/JavaMailClientTest.java @@ -2,7 +2,6 @@ import hitool.mail.conf.EmailBody; import hitool.mail.def.JavaMailClientImpl; -import hitool.mail.provider.EmailPropertiesProvider; import hitool.mail.provider.def.DefaultEmailPropertiesProvider; import org.junit.After; import org.junit.Before; @@ -10,97 +9,40 @@ import java.util.Properties; +import static org.junit.Assert.assertTrue; + public class JavaMailClientTest { + private LocalSmtpServer smtp; + private JavaMailClientImpl mailClient; - protected Properties props = System.getProperties(); - protected EmailPropertiesProvider propsProvider; - protected JavaMailClientAdapter mailClient; - - @Before - public void setup(){ - - - /* - * 我们是没办法使用javamail通过一般的代理服务器发送邮件的,比如下面的代码是没有效果的: - */ - /*Properties props = System.getProperties(); - props.setProperty("proxySet","true"); - props.setProperty("ProxyHost","192.168.155.1"); - props.setProperty("ProxyPort","1080");*/ - //或者这样,也是没用 - /*Properties props = System.getProperties(); - props.setProperty("proxySet","true"); - props.setProperty("http.proxyHost","192.168.155.1"); - props.setProperty("http.proxyPort","808");*/ + @Before + public void setup() throws Exception { + smtp = new LocalSmtpServer(); + Properties props = new Properties(); + props.setProperty(JavaMailKey.MAIL_HOST, "127.0.0.1"); + props.setProperty(JavaMailKey.MAIL_PORT, Integer.toString(smtp.port())); + props.setProperty(JavaMailKey.MAIL_SMTP_PORT, Integer.toString(smtp.port())); + props.setProperty(JavaMailKey.MAIL_USER, "sender@example.test"); + props.setProperty(JavaMailKey.MAIL_SMTP_AUTH, "false"); + DefaultEmailPropertiesProvider provider = new DefaultEmailPropertiesProvider(); + provider.setProps(props); + mailClient = new JavaMailClientImpl(); + mailClient.setPropsProvider(provider); + } - /* - * 不过可以通过socks网关来访问外网的email服务器,当然,前提是你安装了一个socks服务器 - * 此处使用ccproxy代理测试 - */ - - // 设置代理服务器 - props.setProperty("proxySet", "true"); - props.setProperty("socksProxyHost", "10.71.19.215"); - props.setProperty("socksProxyPort", "1080"); - - //props.setProperty("mail.smtp.host", "smtp.163.com"); - //props.setProperty("mail.smtp.port", "465"); - - - props.put(JavaMailKey.MAIL_DEBUG, "true"); - props.setProperty(JavaMailKey.MAIL_HOST, "smtp.163.com"); - props.setProperty(JavaMailKey.MAIL_PORT, "25"); - props.setProperty(JavaMailKey.MAIL_USER, "xxx@163.com"); - props.setProperty(JavaMailKey.MAIL_PASSWORD, "xxx"); - - - props.put(JavaMailKey.MAIL_SMTP_AUTH, "true"); - // props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY); - // props.setProperty("mail.smtp.socketFactory.fallback", "false"); - // props.setProperty("mail.smtp.socketFactory.port", "465"); - - // props.put("mail.store.protocol", "pop3"); - // props.put("mail.transport.protocol", "smtp"); - - mailClient = new JavaMailClientImpl(); - - - propsProvider = new DefaultEmailPropertiesProvider(); - - propsProvider.setProps(props); - - mailClient.setPropsProvider(propsProvider); - - - } - - @Test - public void testDelete() throws Exception{ - - //构建邮件对象 - EmailBody email = new EmailBody(); - - // 设置发件人信息 - email.setFrom("xxx-消息中心", props.getProperty(JavaMailKey.MAIL_USER), false); + @Test + public void sendsMimeMessageToLocalServer() throws Exception { + EmailBody email = new EmailBody(); + email.setFrom("Sender", "sender@example.test", false); + email.setMailto("Recipient", "recipient@example.test"); + email.setSubject("Mail client test"); + email.setContent("Hello from Hitool"); + assertTrue(mailClient.sendMime(email)); + assertTrue(smtp.awaitMessage().contains("recipient@example.test")); + } - // 设置收件人的邮箱 - String sendTo = "xxx@qq.com"; - email.setMailto(sendTo, sendTo); - - // 设置邮件标题 - email.setSubject("邮件客户端测试"); - // 设置邮件的内容体 - String content = "如果您收到此邮件说明测试成功。"; - email.setContent(content); - - mailClient.sendMime(email); - - - } - - @After - public void shutdown(){ - - } - + @After + public void shutdown() throws Exception { + smtp.close(); + } } diff --git a/hitool-mail/src/test/java/hitool/mail/LocalSmtpServer.java b/hitool-mail/src/test/java/hitool/mail/LocalSmtpServer.java new file mode 100644 index 0000000..969d5c9 --- /dev/null +++ b/hitool-mail/src/test/java/hitool/mail/LocalSmtpServer.java @@ -0,0 +1,83 @@ +package hitool.mail; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.net.InetAddress; +import java.net.ServerSocket; +import java.net.Socket; +import java.nio.charset.StandardCharsets; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +final class LocalSmtpServer implements AutoCloseable { + private final ServerSocket server; + private final CountDownLatch received = new CountDownLatch(1); + private final Thread worker; + private volatile String message; + + LocalSmtpServer() throws IOException { + server = new ServerSocket(0, 1, InetAddress.getLoopbackAddress()); + server.setSoTimeout(10000); + worker = new Thread(this::serve, "hitool-test-smtp"); + worker.setDaemon(true); + worker.start(); + } + + int port() { + return server.getLocalPort(); + } + + String awaitMessage() throws InterruptedException { + if (!received.await(5, TimeUnit.SECONDS)) { + throw new AssertionError("No message reached the local SMTP server"); + } + return message; + } + + private void serve() { + try (Socket socket = server.accept(); + BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8)); + PrintWriter out = new PrintWriter(socket.getOutputStream(), true)) { + socket.setSoTimeout(10000); + out.print("220 localhost ESMTP\r\n"); + out.flush(); + String line; + while ((line = in.readLine()) != null) { + if (line.startsWith("EHLO") || line.startsWith("HELO")) { + out.print("250 localhost\r\n"); + } else if (line.equals("DATA")) { + out.print("354 End data with .\r\n"); + out.flush(); + StringBuilder data = new StringBuilder(); + while ((line = in.readLine()) != null && !line.equals(".")) { + data.append(line).append('\n'); + } + message = data.toString(); + received.countDown(); + out.print("250 Message accepted\r\n"); + } else if (line.equals("QUIT")) { + out.print("221 Bye\r\n"); + out.flush(); + break; + } else { + out.print("250 OK\r\n"); + } + out.flush(); + } + } catch (IOException e) { + // The assertion in awaitMessage reports a failed exchange. + } + } + + @Override + public void close() throws IOException { + server.close(); + try { + worker.join(1000); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } +} diff --git a/hitool-mail/src/test/java/hitool/mail/SpringMailClientTest.java b/hitool-mail/src/test/java/hitool/mail/SpringMailClientTest.java index aee0bc9..b6335b9 100644 --- a/hitool-mail/src/test/java/hitool/mail/SpringMailClientTest.java +++ b/hitool-mail/src/test/java/hitool/mail/SpringMailClientTest.java @@ -2,7 +2,6 @@ import hitool.mail.conf.EmailBody; import hitool.mail.def.SpringMailClientImpl; -import hitool.mail.provider.EmailPropertiesProvider; import hitool.mail.provider.def.DefaultEmailPropertiesProvider; import org.junit.After; import org.junit.Before; @@ -11,98 +10,40 @@ import java.util.Properties; +import static org.junit.Assert.assertTrue; + public class SpringMailClientTest { + private LocalSmtpServer smtp; + private SpringMailClientImpl mailClient; - protected Properties props = System.getProperties(); - protected EmailPropertiesProvider propsProvider; - protected SpringMailClientImpl mailClient; - - @Before - public void setup(){ - - - /* - * 我们是没办法使用javamail通过一般的代理服务器发送邮件的,比如下面的代码是没有效果的: - */ - /*Properties props = System.getProperties(); - props.setProperty("proxySet","true"); - props.setProperty("ProxyHost","192.168.155.1"); - props.setProperty("ProxyPort","1080");*/ - //或者这样,也是没用 - /*Properties props = System.getProperties(); - props.setProperty("proxySet","true"); - props.setProperty("http.proxyHost","192.168.155.1"); - props.setProperty("http.proxyPort","808");*/ + @Before + public void setup() throws Exception { + smtp = new LocalSmtpServer(); + Properties props = new Properties(); + props.setProperty(JavaMailKey.MAIL_HOST, "127.0.0.1"); + props.setProperty(JavaMailKey.MAIL_PORT, Integer.toString(smtp.port())); + props.setProperty(JavaMailKey.MAIL_USER, "sender@example.test"); + props.setProperty(JavaMailKey.MAIL_SMTP_AUTH, "false"); + DefaultEmailPropertiesProvider provider = new DefaultEmailPropertiesProvider(); + provider.setProps(props); + mailClient = new SpringMailClientImpl(); + mailClient.setMailSender(new JavaMailSenderImpl()); + mailClient.setPropsProvider(provider); + } - /* - * 不过可以通过socks网关来访问外网的email服务器,当然,前提是你安装了一个socks服务器 - * 此处使用ccproxy代理测试 - */ - - // 设置代理服务器 - //props.setProperty("proxySet", "true"); - //props.setProperty("socksProxyHost", "10.71.19.215"); - //props.setProperty("socksProxyPort", "1080"); - - //props.setProperty("mail.smtp.host", "smtp.163.com"); - //props.setProperty("mail.smtp.port", "465"); - - - props.put(JavaMailKey.MAIL_DEBUG, "true"); - props.setProperty(JavaMailKey.MAIL_HOST, "smtp.163.com"); - props.setProperty(JavaMailKey.MAIL_PORT, "25"); - props.setProperty(JavaMailKey.MAIL_USER, "xxx@163.com"); - props.setProperty(JavaMailKey.MAIL_PASSWORD, "xxx"); - - - props.put(JavaMailKey.MAIL_SMTP_AUTH, "true"); - // props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY); - // props.setProperty("mail.smtp.socketFactory.fallback", "false"); - // props.setProperty("mail.smtp.socketFactory.port", "465"); - - // props.put("mail.store.protocol", "pop3"); - // props.put("mail.transport.protocol", "smtp"); - - mailClient = new SpringMailClientImpl(); - - mailClient.setMailSender(new JavaMailSenderImpl()); - - propsProvider = new DefaultEmailPropertiesProvider(); - - propsProvider.setProps(props); - - mailClient.setPropsProvider(propsProvider); - - - } - - @Test - public void testDelete() throws Exception{ - - //构建邮件对象 - EmailBody email = new EmailBody(); - - // 设置发件人信息 - email.setFrom("xxx-消息中心", props.getProperty(JavaMailKey.MAIL_USER), false); + @Test + public void sendsMimeMessageToLocalServer() throws Exception { + EmailBody email = new EmailBody(); + email.setFrom("Sender", "sender@example.test", false); + email.setMailto("Recipient", "recipient@example.test"); + email.setSubject("Mail client test"); + email.setContent("Hello from Hitool"); + assertTrue(mailClient.sendMime(email)); + assertTrue(smtp.awaitMessage().contains("recipient@example.test")); + } - // 设置收件人的邮箱 - String sendTo = "xxx@qq.com"; - email.setMailto(sendTo, sendTo); - - // 设置邮件标题 - email.setSubject("邮件客户端测试"); - // 设置邮件的内容体 - String content = "如果您收到此邮件说明测试成功。"; - email.setContent(content); - - mailClient.sendMime(email); - - - } - - @After - public void shutdown(){ - - } - + @After + public void shutdown() throws Exception { + smtp.close(); + } }