home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Commons Email example source code file (InvalidAddressTest.java)

This example Commons Email source code file (InvalidAddressTest.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Commons Email tags/keywords

arr_invalid_emails, arr_invalid_emails, baseemailtestcase, emailexception, emailexception, exception, exception, invalidaddresstest, joe, joe, mockemailconcrete, string

The Commons Email InvalidAddressTest.java source code

/*
 * 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 org.apache.commons.mail.mocks.MockEmailConcrete;

/**
 * JUnit test case for invalid Addresses in Email Class
 *
 * @since 1.0
 * @author Niall Pemberton
 * @version $Id: InvalidAddressTest.java 512612 2007-02-28 05:39:57Z dion $
 */
public class InvalidAddressTest extends BaseEmailTestCase
{
    /** */
    private static final String [] ARR_INVALID_EMAILS = {
        "local name@domain.com",
        "local(name@domain.com",
        "local)name@domain.com",
        "local<name@domain.com",
        "local>name@domain.com",
        "local,name@domain.com",
        "local;name@domain.com",
        "local:name@domain.com",
        "local[name@domain.com",
        "local]name@domain.com",
        "local\\name@domain.com",

        //      "local\"name@domain.com",
        "local\tname@domain.com",
        "local\nname@domain.com",
        "local\rname@domain.com",
        "local.name@domain com",
        "local.name@domain(com",
        "local.name@domain)com",
        "local.name@domain<com",
        "local.name@domain>com",
        "local.name@domain,com",
        "local.name@domain;com",
        "local.name@domain:com",

        //      "local.name@domain[com",
        "local.name@domain]com",
        "local.name@domain\\com",
        "local.name@domain\tcom",
        "local.name@domain\ncom",
        "local.name@domain\rcom",
        "local.name@",
        "@domain.com"
    };

    /** */
    private MockEmailConcrete email;

    /**
     * @param name name
     */
    public InvalidAddressTest(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();
    }

    /**
     *
     * @throws Exception Exception
     */
    public void testSetInvalidFrom()
            throws Exception
    {
        // ====================================================================
        // Test setting invalid 'from' addresses
        // ====================================================================
        for (int i = 0; i < ARR_INVALID_EMAILS.length; i++)
        {
            try
            {
                // set from
                email.setFrom(ARR_INVALID_EMAILS[i]);

                // Expected an exception to be thrown
                fail("setFrom " + i + " passed: " + ARR_INVALID_EMAILS[i]);
            }
            catch (EmailException ignore)
            {
                // Expected Result
            }
        }
    }

    /**
     *
     * @throws Exception Exception
     */
    public void testAddInvalidTo()
            throws Exception
    {
        // ====================================================================
        // Test adding invalid 'to' addresses
        // ====================================================================
        for (int i = 0; i < ARR_INVALID_EMAILS.length; i++)
        {
            try
            {
                // Add To
                email.addTo(ARR_INVALID_EMAILS[i], "Joe");

                // Expected an exception to be thrown
                fail("addTo " + i + " passed: " + ARR_INVALID_EMAILS[i]);
            }
            catch (EmailException ignore)
            {
                // Expected Result
            }
        }
    }

    /**
     *
     * @throws Exception Exception
     */
    public void testAddInvalidCc()
            throws Exception
    {
        // ====================================================================
        // Test adding invalid 'cc' addresses
        // ====================================================================
        for (int i = 0; i < ARR_INVALID_EMAILS.length; i++)
        {
            try
            {
                // add cc
                email.addCc(ARR_INVALID_EMAILS[i], "Joe");

                // Expected an exception to be thrown
                fail("addCc " + i + " passed: " + ARR_INVALID_EMAILS[i]);
            }
            catch (EmailException ignore)
            {
                // Expected Result
            }
        }
    }

    /**
     *
     * @throws Exception Exception
     */
    public void testAddInvalidBcc()
            throws Exception
    {
        // ====================================================================
        // Test adding invalid 'Bcc' addresses
        // ====================================================================
        for (int i = 0; i < ARR_INVALID_EMAILS.length; i++)
        {
            try
            {
                // add bcc
                email.addBcc(ARR_INVALID_EMAILS[i], "Joe");

                // Expected an exception to be thrown
                fail("addBcc " + i + " passed: " + ARR_INVALID_EMAILS[i]);
            }
            catch (EmailException ignore)
            {
                // Expected Result
            }
        }
    }
}

Other Commons Email examples (source code examples)

Here is a short list of links related to this Commons Email InvalidAddressTest.java source code file:



my book on functional programming

 

new blog posts

 

Copyright 1998-2019 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.