001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.mail;
018    
019    import java.io.IOException;
020    
021    import org.apache.commons.mail.mocks.MockSimpleEmail;
022    
023    /**
024     * JUnit test case for SimpleEmailTest
025     * @since 1.0
026     * @version $Revision: 512634 $ $Date: 2007-02-27 23:09:27 -0800 (Tue, 27 Feb 2007) $
027     */
028    
029    public class SimpleEmailTest extends BaseEmailTestCase
030    {
031        /** */
032        private MockSimpleEmail email;
033    
034        /**
035         * @param name name
036         */
037        public SimpleEmailTest(String name)
038        {
039            super(name);
040        }
041    
042        /**
043         * @throws Exception  */
044        protected void setUp() throws Exception
045        {
046            super.setUp();
047            // reusable objects to be used across multiple tests
048            this.email = new MockSimpleEmail();
049        }
050    
051        /**
052         * @throws EmailException  */
053        public void testGetSetMsg() throws EmailException
054        {
055            // ====================================================================
056            // Test Success
057            // ====================================================================
058            for (int i = 0; i < testCharsValid.length; i++)
059            {
060                this.email.setMsg(testCharsValid[i]);
061                assertEquals(testCharsValid[i], this.email.getMsg());
062            }
063    
064            // ====================================================================
065            // Test Exception
066            // ====================================================================
067            for (int i = 0; i < this.testCharsNotValid.length; i++)
068            {
069                try
070                {
071                    this.email.setMsg(this.testCharsNotValid[i]);
072                    fail("Should have thrown an exception");
073                }
074                catch (EmailException e)
075                {
076                    assertTrue(true);
077                }
078            }
079    
080        }
081    
082        /**
083         * @throws EmailException when a bad address is set.
084         * @throws IOException when sending fails
085         * @todo Add code to test the popBeforeSmtp() settings
086         */
087        public void testSend() throws EmailException, IOException
088        {
089            // ====================================================================
090            // Test Success
091            // ====================================================================
092            this.getMailServer();
093    
094            this.email = new MockSimpleEmail();
095            this.email.setHostName(this.strTestMailServer);
096            this.email.setSmtpPort(this.getMailServerPort());
097            this.email.setFrom(this.strTestMailFrom);
098            this.email.addTo(this.strTestMailTo);
099    
100            if (this.strTestUser != null && this.strTestPasswd != null)
101            {
102                this.email.setAuthentication(
103                    this.strTestUser,
104                    this.strTestPasswd);
105            }
106    
107            String strSubject = "Test Msg Subject";
108            String strMessage = "Test Msg Body";
109    
110            this.email.setCharset(Email.ISO_8859_1);
111            this.email.setSubject(strSubject);
112    
113            this.email.setMsg(strMessage);
114    
115            this.email.send();
116    
117            this.fakeMailServer.stop();
118            validateSend(
119                this.fakeMailServer,
120                strSubject,
121                this.email.getMsg(),
122                this.email.getFromAddress(),
123                this.email.getToList(),
124                this.email.getCcList(),
125                this.email.getBccList(),
126                true);
127        }
128    }