1   /*
2    * Copyright 2001-2005 The Apache Software Foundation
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.mail;
17  
18  import java.io.IOException;
19  
20  import org.apache.commons.mail.mocks.MockSimpleEmail;
21  
22  /**
23   * JUnit test case for SimpleEmailTest
24   * @since 1.0
25   * @version $Revision: 279300 $ $Date: 2005-09-07 13:43:52 +0200 (Wed, 07 Sep 2005) $
26   */
27  
28  public class SimpleEmailTest extends BaseEmailTestCase
29  {
30      /** */
31      private MockSimpleEmail email = null;
32  
33      /**
34       * @param name name
35       */
36      public SimpleEmailTest(String name)
37      {
38          super(name);
39      }
40  
41      /** */
42      protected void setUp()
43      {
44          super.setUp();
45          // reusable objects to be used across multiple tests
46          this.email = new MockSimpleEmail();
47      }
48  
49      /** */
50      public void testGetSetMsg()
51      {
52          // ====================================================================
53          // Test Success
54          // ====================================================================
55          try
56          {
57              for (int i = 0; i < testCharsValid.length; i++)
58              {
59                  this.email.setMsg(testCharsValid[i]);
60                  assertEquals(testCharsValid[i], this.email.getMsg());
61              }
62          }
63          catch (EmailException e)
64          {
65              e.printStackTrace();
66              fail("Unexpected exception thrown");
67          }
68  
69          // ====================================================================
70          // Test Exception
71          // ====================================================================
72          for (int i = 0; i < this.testCharsNotValid.length; i++)
73          {
74              try
75              {
76                  this.email.setMsg(this.testCharsNotValid[i]);
77                  fail("Should have thrown an exception");
78              }
79              catch (EmailException e)
80              {
81                  assertTrue(true);
82              }
83              catch (Exception e)
84              {
85                  e.printStackTrace();
86                  fail("Unexpected exception thrown");
87              }
88          }
89  
90      }
91  
92      /**
93       * @todo Add code to test the popBeforeSmtp() settings
94       */
95      public void testSend()
96      {
97          // ====================================================================
98          // Test Success
99          // ====================================================================
100         try
101         {
102             this.getMailServer();
103 
104             this.email = new MockSimpleEmail();
105             this.email.setHostName(this.strTestMailServer);
106             this.email.setSmtpPort(this.getMailServerPort());
107             this.email.setFrom(this.strTestMailFrom);
108             this.email.addTo(this.strTestMailTo);
109 
110             if (this.strTestUser != null && this.strTestPasswd != null)
111             {
112                 this.email.setAuthentication(
113                     this.strTestUser,
114                     this.strTestPasswd);
115             }
116 
117             String strSubject = "Test Msg Subject";
118             String strMessage = "Test Msg Body";
119 
120             this.email.setCharset(Email.ISO_8859_1);
121             this.email.setSubject(strSubject);
122 
123             this.email.setMsg(strMessage);
124 
125             this.email.send();
126 
127             this.fakeMailServer.stop();
128             validateSend(
129                 this.fakeMailServer,
130                 strSubject,
131                 this.email.getMsg(),
132                 this.email.getFromAddress(),
133                 this.email.getToList(),
134                 this.email.getCcList(),
135                 this.email.getBccList(),
136                 true);
137         }
138 
139         catch (IOException e)
140         {
141             e.printStackTrace();
142             fail("failed to save email to output file");
143         }
144         catch (Exception e)
145         {
146             e.printStackTrace();
147             fail("Unexpected exception thrown");
148         }
149     }
150 }