View Javadoc
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 static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertTrue;
21  import static org.junit.Assert.fail;
22  
23  import java.io.IOException;
24  
25  import org.apache.commons.mail.mocks.MockSimpleEmail;
26  import org.junit.Before;
27  import org.junit.Ignore;
28  import org.junit.Test;
29  
30  /**
31   * JUnit test case for SimpleEmailTest
32   * @since 1.0
33   */
34  public class SimpleEmailTest extends AbstractEmailTest
35  {
36      private MockSimpleEmail email;
37  
38      @Before
39      public void setUpSimpleEmailTest()
40      {
41          // reusable objects to be used across multiple tests
42          this.email = new MockSimpleEmail();
43      }
44  
45      @Test
46      public void testGetSetMsg() throws EmailException
47      {
48          // ====================================================================
49          // Test Success
50          // ====================================================================
51          for (final String validChar : testCharsValid)
52          {
53              this.email.setMsg(validChar);
54              assertEquals(validChar, this.email.getMsg());
55          }
56  
57          // ====================================================================
58          // Test Exception
59          // ====================================================================
60          for (final String invalidChar : this.testCharsNotValid)
61          {
62              try
63              {
64                  this.email.setMsg(invalidChar);
65                  fail("Should have thrown an exception");
66              }
67              catch (final EmailException e)
68              {
69                  assertTrue(true);
70              }
71          }
72  
73      }
74  
75      /**
76       * @throws EmailException when a bad address is set.
77       * @throws IOException when sending fails
78       * TODO Add code to test the popBeforeSmtp() settings
79       */
80      @Test
81      public void testSend() throws EmailException, IOException
82      {
83          // ====================================================================
84          // Test Success
85          // ====================================================================
86          this.getMailServer();
87  
88          this.email = new MockSimpleEmail();
89          this.email.setHostName(this.strTestMailServer);
90          this.email.setSmtpPort(this.getMailServerPort());
91          this.email.setFrom(this.strTestMailFrom);
92          this.email.addTo(this.strTestMailTo);
93  
94          if (this.strTestUser != null && this.strTestPasswd != null)
95          {
96              this.email.setAuthentication(
97                  this.strTestUser,
98                  this.strTestPasswd);
99          }
100 
101         final String strSubject = "Test Msg Subject";
102         final String strMessage = "Test Msg Body";
103 
104         this.email.setCharset(EmailConstants.ISO_8859_1);
105         this.email.setSubject(strSubject);
106 
107         this.email.setMsg(strMessage);
108 
109         this.email.send();
110 
111         this.fakeMailServer.stop();
112         validateSend(
113             this.fakeMailServer,
114             strSubject,
115             this.email.getMsg(),
116             this.email.getFromAddress(),
117             this.email.getToAddresses(),
118             this.email.getCcAddresses(),
119             this.email.getBccAddresses(),
120             true);
121     }
122 
123     @Test
124     @Ignore
125     public void testDefaultMimeCharset() throws EmailException, IOException
126     {
127         /*
128          * disabling this test as it is dependent on execution order.
129          * MimeUtility.getDefaultMIMECharset does some internal caching and if
130          * mail.mime.charset is not defined, reverts to the default java charset
131          * which is basically the system default file encoding.
132          */
133         System.setProperty(EmailConstants.MAIL_MIME_CHARSET, "utf-8");
134 
135         // ====================================================================
136         // Test Success
137         // ====================================================================
138         this.getMailServer();
139 
140         this.email = new MockSimpleEmail();
141         this.email.setHostName(this.strTestMailServer);
142         this.email.setSmtpPort(this.getMailServerPort());
143         this.email.setFrom(this.strTestMailFrom);
144         this.email.addTo(this.strTestMailTo);
145 
146         if (this.strTestUser != null && this.strTestPasswd != null)
147         {
148             this.email.setAuthentication(
149                 this.strTestUser,
150                 this.strTestPasswd);
151         }
152 
153         final String strSubject = "Test Msg Subject";
154         final String strMessage = "Test Msg Body ä"; // add non-ascii character, otherwise us-ascii will be used
155 
156         this.email.setSubject(strSubject);
157         this.email.setMsg(strMessage);
158 
159         this.email.send();
160 
161         this.fakeMailServer.stop();
162 
163         validateSend(
164                 this.fakeMailServer,
165                 strSubject,
166                 this.email.getMsg().substring(0, 13), // only check the start, the ä will be encoded
167                 this.email.getFromAddress(),
168                 this.email.getToAddresses(),
169                 this.email.getCcAddresses(),
170                 this.email.getBccAddresses(),
171                 true);
172 
173         final String message = getMessageAsString(0);
174         // check that the charset has been correctly set
175         assertTrue(message.toLowerCase().contains("content-type: text/plain; charset=utf-8"));
176 
177         System.clearProperty(EmailConstants.MAIL_MIME_CHARSET);
178     }
179 }