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

📄 emailtest.java

📁 也是apache的
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
/* * 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.UnsupportedEncodingException;import java.nio.charset.Charset;import java.nio.charset.IllegalCharsetNameException;import java.util.ArrayList;import java.util.Calendar;import java.util.Date;import java.util.Hashtable;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Properties;import javax.mail.Authenticator;import javax.mail.Session;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;import javax.mail.internet.MimeMultipart;import javax.mail.internet.ParseException;import org.apache.commons.mail.mocks.MockEmailConcrete;/** * JUnit test case for Email Class * * @since 1.0 * @author <a href="mailto:corey.scott@gmail.com">Corey Scott</a> * @version $Id: EmailTest.java 513307 2007-03-01 13:22:34Z dion $ */public class EmailTest extends BaseEmailTestCase{    /** valid email addresses */    public static final String[] ARR_VALID_EMAILS =        {            "me@home.com",            "joe.doe@apache.org",            "someone_here@work-address.com.au"};    /** mock for testing */    private MockEmailConcrete email;    /**     * @param name test name     */    public EmailTest(String name)    {        super(name);    }    /**     * @throws Exception  */    protected void setUp() throws Exception    {        super.setUp();        // reusable objects to be used across multiple tests        this.email = new MockEmailConcrete();    }    /** */    public void testGetSetDebug()    {        // JUnitDoclet begin method setBoolTest isBoolTest        boolean[] tests = {true, false};        for (int i = 0; i < tests.length; i++)        {            this.email.setDebug(tests[i]);            assertEquals(tests[i], this.email.isDebug());        }    }    /**     *     * @throws Exception Exception     */    public void testGetSetSession() throws Exception    {        Properties properties = new Properties(System.getProperties());        properties.setProperty(Email.MAIL_TRANSPORT_PROTOCOL, Email.SMTP);        properties.setProperty(            Email.MAIL_PORT,            String.valueOf(this.getMailServerPort()));        properties.setProperty(Email.MAIL_HOST, this.strTestMailServer);        properties.setProperty(Email.MAIL_DEBUG, String.valueOf(false));        Session mySession = Session.getInstance(properties, null);        this.email.setMailSession(mySession);        assertEquals(mySession, this.email.getMailSession());    }    /** */    public void testGetSetAuthentication()    {        // setup        String strUsername = "user.name";        String strPassword = "user.pwd";        this.email.setAuthentication(strUsername, strPassword);        // this is cast into DefaultAuthenticator for convenience        // and give us access to the getPasswordAuthentication fn        DefaultAuthenticator retrievedAuth =            (DefaultAuthenticator) this.email.getAuthenticator();        // tests        assertTrue(            Authenticator.class.isInstance(this.email.getAuthenticator()));        assertEquals(            strUsername,            retrievedAuth.getPasswordAuthentication().getUserName());        assertEquals(            strPassword,            retrievedAuth.getPasswordAuthentication().getPassword());    }    /** */    public void testGetSetAuthenticator()    {        // setup        String strUsername = "user.name";        String strPassword = "user.pwd";        DefaultAuthenticator authenicator =            new DefaultAuthenticator(strUsername, strPassword);        this.email.setAuthenticator(authenicator);        // this is cast into DefaultAuthenticator for convenience        // and give us access to the getPasswordAuthentication fn        DefaultAuthenticator retrievedAuth =            (DefaultAuthenticator) this.email.getAuthenticator();        // tests        assertTrue(            Authenticator.class.isInstance(this.email.getAuthenticator()));        assertEquals(            strUsername,            retrievedAuth.getPasswordAuthentication().getUserName());        assertEquals(            strPassword,            retrievedAuth.getPasswordAuthentication().getPassword());    }    /** */    public void testGetSetCharset()    {        // test ASCII and UTF-8 charsets; since every JVM is required        // to support these, testing them should always succeed.        Charset set = Charset.forName("US-ASCII");        this.email.setCharset(set.name());        assertEquals(set.name(), this.email.getCharset());        set = Charset.forName("UTF-8");        this.email.setCharset(set.name());        assertEquals(set.name(), this.email.getCharset());    }    /** */    public void testSetContentMimeMultipart()    {        MimeMultipart[] tests =            {new MimeMultipart(), new MimeMultipart("abc123"), null};        for (int i = 0; i < tests.length; i++)        {            this.email.setContent(tests[i]);            assertEquals(tests[i], this.email.getContentMimeMultipart());        }    }    /** */    public void testSetContentObject()    {        // setup        String testObject = "test string object";        String testContentType = "";        // ====================================================================        // test (string object and valid content type)        testObject = "test string object";        testContentType = " ; charset=" + Email.US_ASCII;        this.email.setContent(testObject, testContentType);        assertEquals(testObject, this.email.getContentObject());        assertEquals(testContentType, this.email.getContentType());        // ====================================================================        // test (null string object and valid content type)        testObject = null;        testContentType = " ; charset=" + Email.US_ASCII + " some more here";        this.email.setContent(testObject, testContentType);        assertEquals(testObject, this.email.getContentObject());        assertEquals(testContentType, this.email.getContentType());        // ====================================================================        // test (string object and null content type)        testObject = "test string object";        testContentType = null;        this.email.setContent(testObject, testContentType);        assertEquals(testObject, this.email.getContentObject());        assertEquals(testContentType, this.email.getContentType());        // ====================================================================        // test (string object and invalid content type)        testObject = "test string object";        testContentType = " something incorrect ";        this.email.setContent(testObject, testContentType);        assertEquals(testObject, this.email.getContentObject());        assertEquals(testContentType, this.email.getContentType());    }    /** */    public void testGetSetHostName()    {        for (int i = 0; i < testCharsValid.length; i++)        {            this.email.setHostName(testCharsValid[i]);            assertEquals(testCharsValid[i], this.email.getHostName());        }    }    /** */    public void testGetSetSmtpPort()    {        // ====================================================================        // Test Success        // ====================================================================        int[] tests = {1, Integer.MAX_VALUE};        for (int i = 0; i < tests.length; i++)        {            this.email.setSmtpPort(tests[i]);            assertEquals(                tests[i],                Integer.valueOf(this.email.getSmtpPort()).intValue());        }        // ====================================================================        // Test Exceptions        // ====================================================================        int[] testExs = {Integer.MIN_VALUE, -1, 0};        for (int i = 0; i < testExs.length; i++)        {            try            {                this.email.setSmtpPort(testExs[i]);                fail("Should have thrown an exception");            }            catch (IllegalArgumentException e)            {                assertTrue(true);            }        }    }    /**     *     * @throws Exception Exception     */    public void testSetFrom() throws Exception    {        // ====================================================================        // Test Success        // ====================================================================        List arrExpected = new ArrayList();        arrExpected.add(new InternetAddress("me@home.com", "me@home.com"));        arrExpected.add(            new InternetAddress(                "joe.doe@apache.org",                "joe.doe@apache.org"));        arrExpected.add(            new InternetAddress(                "someone_here@work-address.com.au",                "someone_here@work-address.com.au"));        for (int i = 0; i < ARR_VALID_EMAILS.length; i++)        {            // set from            this.email.setFrom(ARR_VALID_EMAILS[i]);            // retrieve and verify            assertEquals(arrExpected.get(i), this.email.getFromAddress());        }    }    /**     *     * @throws Exception Exception     */    public void testSetFromWithEncoding() throws Exception    {        // ====================================================================        // Test Success (with charset set)        // ====================================================================        String testValidEmail = "me@home.com";        InternetAddress inetExpected =            new InternetAddress("me@home.com", "me@home.com", Email.ISO_8859_1);        // set from        this.email.setFrom(testValidEmail, testValidEmail, Email.ISO_8859_1);        // retrieve and verify        assertEquals(inetExpected, this.email.getFromAddress());    }    /**     *     * @throws Exception Exception     */    public void testSetFrom2() throws Exception    {        // ====================================================================        // Test Success        // ====================================================================        String[] testEmails =        {            "me@home.com",            "joe.doe@apache.org",            "someone_here@work-address.com.au"        };        String[] testEmailNames = {"Name1", "", null};        List arrExpected = new ArrayList();        arrExpected.add(new InternetAddress("me@home.com", "Name1"));        arrExpected.add(            new InternetAddress(                "joe.doe@apache.org",                "joe.doe@apache.org"));        arrExpected.add(            new InternetAddress(

⌨️ 快捷键说明

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