UserServiceImplTest.java 4.9 KB
Newer Older
Q
qinyingjie 已提交
1 2
package com.kwan.springbootkwan;

Q
qinyingjie 已提交
3
import com.kwan.springbootkwan.entity.MailInfo;
Q
qinyingjie 已提交
4 5
import com.kwan.springbootkwan.entity.User;
import com.kwan.springbootkwan.mapper.UserMapper;
Q
qinyingjie 已提交
6
import com.kwan.springbootkwan.service.ISendMsgHandle;
Q
qinyingjie 已提交
7
import com.kwan.springbootkwan.utils.RedisIdWorker;
Q
qinyingjie 已提交
8 9 10 11
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

Q
qinyingjie 已提交
12 13 14
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
Q
qinyingjie 已提交
15 16 17
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Q
qinyingjie 已提交
18

Q
qinyingjie 已提交
19 20 21 22 23 24

@SpringBootTest
public class UserServiceImplTest {

    @Autowired
    private UserMapper userService;
Q
qinyingjie 已提交
25
    @Autowired
Q
qinyingjie 已提交
26
    private ISendMsgHandle emailSendMsgHandle;
Q
qinyingjie 已提交
27 28 29 30 31 32

    @Test
    public void queryAll() {
        User user = userService.selectById(1);
        System.out.println(user);
    }
Q
qinyingjie 已提交
33 34 35 36 37 38 39 40 41 42

    /**
     * 发邮件
     */
    @Test
    void sendSimpleTextEmail() {
        MailInfo mailInfo = new MailInfo();
        mailInfo.setReceiver(new String[]{"qinyingjie@deepexi.com"});
        mailInfo.setSubject("测试主题");
        mailInfo.setContent("邮件内容");
Q
qinyingjie 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
        emailSendMsgHandle.sendSimpleTextEmail(mailInfo);
    }


    /**
     * 带html
     */
    @Test
    public void sendHTMLMail() {
        MailInfo mailBean = new MailInfo();
        //接收人
        mailBean.setReceiver(new String[]{"qinyingjie@deepexi.com"});
        mailBean.setSubject("SpringBootMailHTML之这是一封HTML格式的邮件");
        //抄送给谁
        mailBean.setCc(new String[]{"786775527@qq.com", "286968900@qq.com"});
        StringBuilder sb = new StringBuilder();
        sb.append("<h2>SpringBoot测试邮件HTML</h2>")
                .append("<p style='text-align:left'>这是一封HTML邮件...</p>")
                .append("<p> 时间为:" + new Date() + "</p>");
        mailBean.setContent(sb.toString());
        //true、false控制以普通文本发送还是以html格式发送
        emailSendMsgHandle.sendHtmlEmail(mailBean, true);
    }

    @Test
    void sendEmail() {
        MailInfo mailBean = new MailInfo();
        mailBean.setReceiver(new String[]{"qinyingjie@deepexi.com"});
        mailBean.setSubject("SpringBootMailHTML之这是一封HTML格式的邮件");
        //抄送给谁
        mailBean.setCc(new String[]{"786775527@qq.com", "286968900@qq.com"});
        StringBuilder sb = new StringBuilder();
        sb.append("<h2>SpringBoot测试邮件HTML</h2>")
                .append("<p style='text-align:left'>这是一封HTML邮件...</p>")
                .append("<p> 时间为:" + new Date() + "</p>");
        mailBean.setContent(sb.toString());
        mailBean.setAttachFileNames(new String[]{"/Users/Downloads/密码管理.xlsx", "/Users/Downloads/1635834323888.jpg"});
        emailSendMsgHandle.sendEnclosureEmail(mailBean);
    }

    @Test
    void sendEmail2() {
        MailInfo mailBean = new MailInfo();
        mailBean.setReceiver(new String[]{"qinyingjie@deepexi.com"});
        mailBean.setSubject("SpringBootMailHTML之这是一封HTML格式的邮件");
        mailBean.setCc(new String[]{"786775527@qq.com", "286968900@qq.com"});
        StringBuilder sb = new StringBuilder();
        sb.append("<h2>SpringBoot测试邮件HTML</h2>")
                .append("<p style='text-align:left'>这是一封HTML邮件...</p>")
                .append("<a href=http://www.baidu.com>点击进入百度</a><br/>")
                //内嵌图片
                .append("<img src=\"cid:a00000001\"><br/><br/>")
                .append("<img src=\"cid:a00000002\">")
                .append("<p> 时间为:" + new Date() + "</p>");
        mailBean.setContent(sb.toString());
        //附件
        mailBean.setAttachFileNames(new String[]{"/Users/Downloads/密码管理.xlsx", "/Users/Downloads/1635834323888.jpg"});
        //内嵌了多少张图片,如果没有,则new一个不带值的Map
        Map<String, String> image = new HashMap<>();
        image.put("a00000001", "/Users/Downloads/WechatIMG22.jpg");
        image.put("a00000002", "/Users/Downloads/face_1631083961355.png");

        mailBean.setImageMap(image);
        emailSendMsgHandle.sendEnclosureEmail(mailBean);
Q
qinyingjie 已提交
107
    }
Q
qinyingjie 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135

    @Autowired
    private RedisIdWorker redisIdWorker;

    private ExecutorService es = Executors.newFixedThreadPool(500);


    @Test
    public void testWorkerId() throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(300);
        Runnable task = () -> {
            for (int i = 0; i < 100; i++) {
                long id = redisIdWorker.nextId("order");
                System.out.println("id = " + id);
            }
            latch.countDown();
        };

        long begin = System.currentTimeMillis();
        for (int i = 0; i < 300; i++) {
            es.submit(task);
        }
        latch.await();
        long end = System.currentTimeMillis();
        System.out.println("times = " + (end- begin));

    }

Q
qinyingjie 已提交
136
}