001: /*
002: * Copyright 2001-2005 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.mail;
017:
018: import java.io.File;
019: import java.io.IOException;
020: import java.net.URL;
021:
022: import org.apache.commons.mail.mocks.MockHtmlEmailConcrete;
023: import org.apache.commons.mail.settings.EmailConfiguration;
024:
025: /**
026: * JUnit test case for HtmlEmail Class
027: *
028: * @since 1.0
029: * @author <a href="mailto:corey.scott@gmail.com">Corey Scott</a>
030: * @version $Id: HtmlEmailTest.java 279300 2005-09-07 11:43:52Z henning $
031: */
032:
033: public class HtmlEmailTest extends BaseEmailTestCase {
034: /** */
035: private MockHtmlEmailConcrete email = null;
036:
037: /**
038: * @param name name
039: */
040: public HtmlEmailTest(String name) {
041: super (name);
042: }
043:
044: /** */
045: protected void setUp() {
046: super .setUp();
047: // reusable objects to be used across multiple tests
048: this .email = new MockHtmlEmailConcrete();
049: }
050:
051: /** */
052: public void testGetSetTextMsg() {
053: // ====================================================================
054: // Test Success
055: // ====================================================================
056: try {
057: for (int i = 0; i < testCharsValid.length; i++) {
058: this .email.setTextMsg(testCharsValid[i]);
059: assertEquals(testCharsValid[i], this .email.getTextMsg());
060: }
061: } catch (EmailException e) {
062: fail("Shoudn't have thrown exception");
063: }
064:
065: // ====================================================================
066: // Test Exception
067: // ====================================================================
068: for (int i = 0; i < this .testCharsNotValid.length; i++) {
069: try {
070: this .email.setTextMsg(this .testCharsNotValid[i]);
071: fail("Should have thrown an exception");
072: } catch (EmailException e) {
073: assertTrue(true);
074: } catch (Exception e) {
075: fail("Unexpected exception thrown");
076: }
077: }
078:
079: }
080:
081: /** */
082: public void testGetSetHtmlMsg() {
083: // ====================================================================
084: // Test Success
085: // ====================================================================
086: try {
087: for (int i = 0; i < testCharsValid.length; i++) {
088: this .email.setHtmlMsg(testCharsValid[i]);
089: assertEquals(testCharsValid[i], this .email.getHtmlMsg());
090: }
091: } catch (EmailException e) {
092: fail("Shoudn't have thrown exception");
093: }
094:
095: // ====================================================================
096: // Test Exception
097: // ====================================================================
098: for (int i = 0; i < this .testCharsNotValid.length; i++) {
099: try {
100: this .email.setHtmlMsg(this .testCharsNotValid[i]);
101: fail("Should have thrown an exception");
102: } catch (EmailException e) {
103: assertTrue(true);
104: } catch (Exception e) {
105: fail("Unexpected exception thrown");
106: }
107: }
108:
109: }
110:
111: /** */
112: public void testGetSetMsg() {
113: // ====================================================================
114: // Test Success
115: // ====================================================================
116: try {
117: for (int i = 0; i < testCharsValid.length; i++) {
118: this .email.setMsg(testCharsValid[i]);
119: assertEquals(testCharsValid[i], this .email.getTextMsg());
120:
121: assertTrue(this .email.getHtmlMsg().indexOf(
122: testCharsValid[i]) != -1);
123: }
124: } catch (EmailException e) {
125: fail("Shoudn't have thrown exception");
126: }
127:
128: // ====================================================================
129: // Test Exception
130: // ====================================================================
131: for (int i = 0; i < this .testCharsNotValid.length; i++) {
132: try {
133: this .email.setMsg(this .testCharsNotValid[i]);
134: fail("Should have thrown an exception");
135: } catch (EmailException e) {
136: assertTrue(true);
137: } catch (Exception e) {
138: fail("Unexpected exception thrown");
139: }
140: }
141:
142: }
143:
144: /**
145: *
146: * @throws Exception Exception
147: */
148: public void testEmbed() throws Exception {
149: // ====================================================================
150: // Test Success
151: // ====================================================================
152:
153: String strEmbed = this .email.embed(new URL(this .strTestURL),
154: "Test name");
155: assertNotNull(strEmbed);
156: assertEquals(HtmlEmail.CID_LENGTH, strEmbed.length());
157:
158: // ====================================================================
159: // Test Exceptions
160: // ====================================================================
161: // bad URL
162: try {
163: this .email.embed(new URL("http://bad.url"), "Test name");
164: fail("Should have thrown an exception");
165: } catch (EmailException e) {
166: assertTrue(true);
167: } catch (Exception e) {
168: fail("Unexpected exception thrown");
169: }
170: }
171:
172: /** */
173: public void testSend() {
174: EmailAttachment attachment = new EmailAttachment();
175: File testFile = null;
176:
177: try {
178: /** File to used to test file attachments (Must be valid) */
179: testFile = File.createTempFile("commons-email-testfile",
180: ".txt");
181: } catch (IOException e) {
182: fail("Test file cannot be found");
183: }
184:
185: // ====================================================================
186: // Test Success
187: // ====================================================================
188: try {
189: this .getMailServer();
190:
191: String strSubject = "Test HTML Send #1 Subject (w charset)";
192:
193: this .email = new MockHtmlEmailConcrete();
194: this .email.setHostName(this .strTestMailServer);
195: this .email.setSmtpPort(this .getMailServerPort());
196: this .email.setFrom(this .strTestMailFrom);
197: this .email.addTo(this .strTestMailTo);
198:
199: /** File to used to test file attachmetns (Must be valid) */
200: attachment.setName("Test Attachment");
201: attachment.setDescription("Test Attachment Desc");
202: attachment.setPath(testFile.getAbsolutePath());
203: this .email.attach(attachment);
204:
205: this .email.setAuthentication(this .strTestUser,
206: this .strTestPasswd);
207:
208: this .email.setCharset(Email.ISO_8859_1);
209: this .email.setSubject(strSubject);
210:
211: URL url = new URL(EmailConfiguration.TEST_URL);
212: String cid = this .email.embed(url, "Apache Logo");
213:
214: String strHtmlMsg = "<html>The Apache logo - <img src=\"cid:"
215: + cid + "\"><html>";
216:
217: this .email.setHtmlMsg(strHtmlMsg);
218: this .email
219: .setTextMsg("Your email client does not support HTML emails");
220:
221: this .email.send();
222: this .fakeMailServer.stop();
223: // validate txt message
224: validateSend(this .fakeMailServer, strSubject, this .email
225: .getTextMsg(), this .email.getFromAddress(),
226: this .email.getToList(), this .email.getCcList(),
227: this .email.getBccList(), true);
228:
229: // validate html message
230: validateSend(this .fakeMailServer, strSubject, this .email
231: .getHtmlMsg(), this .email.getFromAddress(),
232: this .email.getToList(), this .email.getCcList(),
233: this .email.getBccList(), false);
234:
235: // validate attachment
236: validateSend(this .fakeMailServer, strSubject, attachment
237: .getName(), this .email.getFromAddress(), this .email
238: .getToList(), this .email.getCcList(), this .email
239: .getBccList(), false);
240: } catch (Exception e) {
241: fail("Unexpected exception thrown");
242: }
243:
244: try {
245: this .getMailServer();
246:
247: this .email = new MockHtmlEmailConcrete();
248: this .email.setHostName(this .strTestMailServer);
249: this .email.setSmtpPort(this .getMailServerPort());
250: this .email.setFrom(this .strTestMailFrom);
251: this .email.addTo(this .strTestMailTo);
252:
253: if (this .strTestUser != null && this .strTestPasswd != null) {
254: this .email.setAuthentication(this .strTestUser,
255: this .strTestPasswd);
256: }
257:
258: String strSubject = "Test HTML Send #1 Subject (wo charset)";
259: this .email.setSubject(strSubject);
260: this .email.setTextMsg("Test message");
261:
262: this .email.send();
263: this .fakeMailServer.stop();
264: // validate txt message
265: validateSend(this .fakeMailServer, strSubject, this .email
266: .getTextMsg(), this .email.getFromAddress(),
267: this .email.getToList(), this .email.getCcList(),
268: this .email.getBccList(), true);
269: }
270:
271: catch (IOException e) {
272: fail("Failed to save email to output file");
273: } catch (Exception e) {
274: e.printStackTrace();
275: fail("Unexpected exception thrown");
276: }
277: }
278:
279: /**
280: *
281: * @throws Exception Exception
282: */
283: public void testSend2() throws Exception {
284: // ====================================================================
285: // Test Success
286: // ====================================================================
287:
288: this .getMailServer();
289:
290: this .email = new MockHtmlEmailConcrete();
291: this .email.setHostName(this .strTestMailServer);
292: this .email.setSmtpPort(this .getMailServerPort());
293: this .email.setFrom(this .strTestMailFrom);
294: this .email.addTo(this .strTestMailTo);
295:
296: if (this .strTestUser != null && this .strTestPasswd != null) {
297: this .email.setAuthentication(this .strTestUser,
298: this .strTestPasswd);
299: }
300:
301: String strSubject = "Test HTML Send #2 Subject (wo charset)";
302: this .email.setSubject(strSubject);
303: this .email.setMsg("Test txt msg");
304:
305: this .email.send();
306: this .fakeMailServer.stop();
307: // validate txt message
308: validateSend(this .fakeMailServer, strSubject, this .email
309: .getTextMsg(), this .email.getFromAddress(), this .email
310: .getToList(), this .email.getCcList(), this .email
311: .getBccList(), true);
312:
313: // validate html message
314: validateSend(this .fakeMailServer, strSubject, this .email
315: .getHtmlMsg(), this .email.getFromAddress(), this .email
316: .getToList(), this .email.getCcList(), this .email
317: .getBccList(), false);
318:
319: this .getMailServer();
320:
321: this .email = new MockHtmlEmailConcrete();
322: this .email.setHostName(this .strTestMailServer);
323: this .email.setFrom(this .strTestMailFrom);
324: this .email.setSmtpPort(this .getMailServerPort());
325: this .email.addTo(this .strTestMailTo);
326:
327: if (this .strTestUser != null && this .strTestPasswd != null) {
328: this .email.setAuthentication(this .strTestUser,
329: this .strTestPasswd);
330: }
331:
332: strSubject = "Test HTML Send #2 Subject (w charset)";
333: this .email.setCharset(Email.ISO_8859_1);
334: this .email.setSubject(strSubject);
335: this .email.setMsg("Test txt msg");
336:
337: this .email.send();
338: this .fakeMailServer.stop();
339: // validate txt message
340: validateSend(this .fakeMailServer, strSubject, this .email
341: .getTextMsg(), this .email.getFromAddress(), this .email
342: .getToList(), this .email.getCcList(), this .email
343: .getBccList(), true);
344:
345: // validate html message
346: validateSend(this .fakeMailServer, strSubject, this .email
347: .getHtmlMsg(), this .email.getFromAddress(), this .email
348: .getToList(), this .email.getCcList(), this .email
349: .getBccList(), false);
350:
351: }
352:
353: }
|