1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.mail;
18  
19  import java.io.IOException;
20  
21  import org.apache.commons.mail.mocks.MockSimpleEmail;
22  
23  /**
24   * JUnit test case for SimpleEmailTest
25   * @since 1.0
26   * @version $Revision: 512634 $ $Date: 2007-02-27 23:09:27 -0800 (Tue, 27 Feb 2007) $
27   */
28  
29  public class SimpleEmailTest extends BaseEmailTestCase
30  {
31      /** */
32      private MockSimpleEmail email;
33  
34      /**
35       * @param name name
36       */
37      public SimpleEmailTest(String name)
38      {
39          super(name);
40      }
41  
42      /**
43       * @throws Exception  */
44      protected void setUp() throws Exception
45      {
46          super.setUp();
47          // reusable objects to be used across multiple tests
48          this.email = new MockSimpleEmail();
49      }
50  
51      /**
52       * @throws EmailException  */
53      public void testGetSetMsg() throws EmailException
54      {
55          // ====================================================================
56          // Test Success
57          // ====================================================================
58          for (int i = 0; i < testCharsValid.length; i++)
59          {
60              this.email.setMsg(testCharsValid[i]);
61              assertEquals(testCharsValid[i], this.email.getMsg());
62          }
63  
64          // ====================================================================
65          // Test Exception
66          // ====================================================================
67          for (int i = 0; i < this.testCharsNotValid.length; i++)
68          {
69              try
70              {
71                  this.email.setMsg(this.testCharsNotValid[i]);
72                  fail("Should have thrown an exception");
73              }
74              catch (EmailException e)
75              {
76                  assertTrue(true);
77              }
78          }
79  
80      }
81  
82      /**
83       * @throws EmailException when a bad address is set.
84       * @throws IOException when sending fails
85       * @todo Add code to test the popBeforeSmtp() settings
86       */
87      public void testSend() throws EmailException, IOException
88      {
89          // ====================================================================
90          // Test Success
91          // ====================================================================
92          this.getMailServer();
93  
94          this.email = new MockSimpleEmail();
95          this.email.setHostName(this.strTestMailServer);
96          this.email.setSmtpPort(this.getMailServerPort());
97          this.email.setFrom(this.strTestMailFrom);
98          this.email.addTo(this.strTestMailTo);
99  
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 }