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