⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 baseemailtestcase.java

📁 也是apache的
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements.  See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License.  You may obtain a copy of the License at * *     http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.commons.mail;import java.io.BufferedOutputStream;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileWriter;import java.io.IOException;import java.util.Date;import java.util.Enumeration;import java.util.List;import javax.activation.DataHandler;import javax.mail.Header;import javax.mail.MessagingException;import javax.mail.Multipart;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;import junit.framework.TestCase;import org.apache.commons.mail.settings.EmailConfiguration;import org.subethamail.wiser.Wiser;import org.subethamail.wiser.WiserMessage;/** * Base test case for Email test classes * * @since 1.0 * @author <a href="mailto:corey.scott@gmail.com">Corey Scott</a> * @author <a href="mailto:epugh@opensourceconnections.com">Eric Pugh</a> * @version $Id: BaseEmailTestCase.java 544629 2007-06-05 20:55:13Z bspeakmon $ */public class BaseEmailTestCase extends TestCase{    /** Padding at end of body added by wiser/send */    public static final int BODY_END_PAD = 3;    /** Padding at start of body added by wiser/send */    public static final int BODY_START_PAD = 2;    /** Line separator in email messages */    private static final String LINE_SEPARATOR = "\r\n";    /** default port */    private static int mailServerPort = EmailConfiguration.MAIL_SERVER_PORT;    /** The fake Wiser email server */    protected Wiser fakeMailServer;    /** Mail server used for testing */    protected String strTestMailServer = EmailConfiguration.MAIL_SERVER;    /** From address for the test email */    protected String strTestMailFrom = EmailConfiguration.TEST_FROM;    /** Destination address for the test email */    protected String strTestMailTo = EmailConfiguration.TEST_TO;    /** Mailserver username (set if needed) */    protected String strTestUser = EmailConfiguration.TEST_USER;    /** Mailserver strTestPasswd (set if needed) */    protected String strTestPasswd = EmailConfiguration.TEST_PASSWD;    /** URL to used to test URL attachmetns (Must be valid) */    protected String strTestURL = EmailConfiguration.TEST_URL;    /** Test characters acceptable to email */    protected String[] testCharsValid =    {            " ",            "a",            "A",            "\uc5ec",            "0123456789",            "012345678901234567890",            "\n"    };    /** Array of test strings */    protected String[] testCharsNotValid = {"", null};    /** Where to save email output **/    private File emailOutputDir;    /**     * @param name name     */    public BaseEmailTestCase(String name)    {        super(name);        emailOutputDir = new File("target/test-emails");        if (!emailOutputDir.exists())        {            emailOutputDir.mkdirs();        }    }    /** */    protected void tearDown()    {        //stop the fake email server (if started)        if (this.fakeMailServer != null && !isMailServerStopped(fakeMailServer))        {            this.fakeMailServer.stop();            assertTrue("Mail server didn't stop", isMailServerStopped(fakeMailServer));        }        this.fakeMailServer = null;    }    /**     * Gets the mail server port.     * @return the port the server is running on.     */    protected int getMailServerPort()    {        return mailServerPort;    }    /**     *     * @param email email     * @throws IOException Exception     */    protected void saveEmailToFile(WiserMessage email) throws IOException    {        File emailFile =            new File(emailOutputDir, "email" + new Date().getTime() + ".txt");        FileWriter fw = new FileWriter(emailFile);        fw.write(email.toString());        fw.close();    }    /**     * @param intMsgNo the message to retrieve     * @return message as string     */    public String getMessageAsString(int intMsgNo)    {        List receivedMessages = fakeMailServer.getMessages();        assertTrue("mail server didn't get enough messages", receivedMessages.size() >= intMsgNo);        WiserMessage emailMessage = (WiserMessage) receivedMessages.get(intMsgNo);        if (emailMessage != null)        {            try            {                return serializeEmailMessage(emailMessage);            }            catch (Exception e)            {                // ignore, since the test will fail on an empty string return            }        }        fail("Message not found");        return "";    }    /**     * Initializes the stub mail server. Fails if the server cannot be proven     * to have started. If the server is already started, this method returns     * without changing the state of the server.     */    public void getMailServer()    {        if (this.fakeMailServer == null || isMailServerStopped(fakeMailServer))        {            mailServerPort++;            this.fakeMailServer = new Wiser();            this.fakeMailServer.setPort(getMailServerPort());            this.fakeMailServer.start();            assertFalse("fake mail server didn't start", isMailServerStopped(fakeMailServer));            Date dtStartWait = new Date();            while (isMailServerStopped(fakeMailServer))            {                // test for connected                if (this.fakeMailServer != null                    && !isMailServerStopped(fakeMailServer))                {                    break;                }                // test for timeout                if ((dtStartWait.getTime() + EmailConfiguration.TIME_OUT)                    <= new Date().getTime())                {                    fail("Mail server failed to start");                }            }        }    }    /**     * Validate the message was sent properly     * @param mailServer reference to the fake mail server     * @param strSubject expected subject     * @param fromAdd expected from address     * @param toAdd list of expected to addresses     * @param ccAdd list of expected cc addresses     * @param bccAdd list of expected bcc addresses     * @param boolSaveToFile true will output to file, false doesnt     * @return WiserMessage email to check     * @throws IOException Exception     */    protected WiserMessage validateSend(        Wiser mailServer,        String strSubject,        InternetAddress fromAdd,        List toAdd,        List ccAdd,        List bccAdd,        boolean boolSaveToFile)        throws IOException    {        assertTrue("mail server doesn't contain expected message",                mailServer.getMessages().size() == 1);        WiserMessage emailMessage = (WiserMessage) mailServer.getMessages().get(0);        if (boolSaveToFile)        {            this.saveEmailToFile(emailMessage);        }        try        {            // get the MimeMessage            MimeMessage mimeMessage = emailMessage.getMimeMessage();            // test subject            assertEquals("got wrong subject from mail",                    strSubject, mimeMessage.getHeader("Subject", null));

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -