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.Test;
28  
29  /**
30   * JUnit test case for SimpleEmailTest
31   * @since 1.0
32   * @version $Id: SimpleEmailTest.java 1533924 2013-10-20 16:24:38Z tn $
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 (String validChar : testCharsValid)
52          {
53              this.email.setMsg(validChar);
54              assertEquals(validChar, this.email.getMsg());
55          }
56  
57          // ====================================================================
58          // Test Exception
59          // ====================================================================
60          for (String invalidChar : this.testCharsNotValid)
61          {
62              try
63              {
64                  this.email.setMsg(invalidChar);
65                  fail("Should have thrown an exception");
66              }
67              catch (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         String strSubject = "Test Msg Subject";
102         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     public void testDefaultMimeCharset() throws EmailException, IOException
125     {
126         System.setProperty(EmailConstants.MAIL_MIME_CHARSET, "utf-8");
127 
128         // ====================================================================
129         // Test Success
130         // ====================================================================
131         this.getMailServer();
132 
133         this.email = new MockSimpleEmail();
134         this.email.setHostName(this.strTestMailServer);
135         this.email.setSmtpPort(this.getMailServerPort());
136         this.email.setFrom(this.strTestMailFrom);
137         this.email.addTo(this.strTestMailTo);
138 
139         if (this.strTestUser != null && this.strTestPasswd != null)
140         {
141             this.email.setAuthentication(
142                 this.strTestUser,
143                 this.strTestPasswd);
144         }
145 
146         String strSubject = "Test Msg Subject";
147         String strMessage = "Test Msg Body รค"; // add non-ascii character, otherwise us-ascii will be used
148 
149         this.email.setSubject(strSubject);
150         this.email.setMsg(strMessage);
151 
152         this.email.send();
153 
154         this.fakeMailServer.stop();
155 
156         String message = getMessageAsString(0);
157         // check that the charset has been correctly set
158         assertTrue(message.contains("Content-Type: text/plain; charset=utf-8"));
159 
160         System.clearProperty(EmailConstants.MAIL_MIME_CHARSET);
161     }
162 }