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.MalformedURLException;
021: import java.net.URL;
022: import java.util.Hashtable;
023:
024: import javax.activation.URLDataSource;
025: import javax.mail.internet.MimeMultipart;
026:
027: import org.apache.commons.mail.mocks.MockMultiPartEmailConcrete;
028:
029: /**
030: * JUnit test case for MultiPartEmail Class
031: *
032: * @since 1.0
033: * @author <a href="mailto:corey.scott@gmail.com">Corey Scott</a>
034: * @version $Id: MultiPartEmailTest.java 279300 2005-09-07 11:43:52Z henning $
035: */
036:
037: public class MultiPartEmailTest extends BaseEmailTestCase {
038: /** */
039: private MockMultiPartEmailConcrete email = null;
040: /** File to used to test file attachmetns (Must be valid) */
041: private File testFile;
042:
043: /**
044: * @param name name
045: */
046: public MultiPartEmailTest(String name) {
047: super (name);
048: }
049:
050: /** */
051: protected void setUp() {
052: super .setUp();
053: // reusable objects to be used across multiple tests
054: this .email = new MockMultiPartEmailConcrete();
055: try {
056: testFile = File.createTempFile("testfile", ".txt");
057: } catch (IOException ioe) {
058: fail(ioe.getMessage());
059: }
060: }
061:
062: /** */
063: public void testSetMsg() {
064: // ====================================================================
065: // Test Success
066: // ====================================================================
067:
068: // without charset set
069: for (int i = 0; i < testCharsValid.length; i++) {
070: try {
071: this .email.setMsg(testCharsValid[i]);
072: assertEquals(testCharsValid[i], this .email.getMsg());
073: } catch (EmailException e) {
074: fail("Unexpected exception thrown");
075: }
076: }
077:
078: // with charset set
079: this .email.setCharset(Email.US_ASCII);
080: for (int i = 0; i < testCharsValid.length; i++) {
081: try {
082: this .email.setMsg(testCharsValid[i]);
083: assertEquals(testCharsValid[i], this .email.getMsg());
084: } catch (EmailException e) {
085: fail("Unexpected exception thrown");
086: }
087: }
088:
089: // ====================================================================
090: // Test Exceptions
091: // ====================================================================
092: for (int i = 0; i < testCharsNotValid.length; i++) {
093: try {
094: this .email.setMsg(testCharsNotValid[i]);
095: fail("Should have thrown an exception");
096: } catch (EmailException e) {
097: assertTrue(true);
098: } catch (Exception e) {
099: e.printStackTrace();
100: fail("Unexpected exception thrown");
101: }
102: }
103: }
104:
105: /** */
106: public void testSend() {
107: // ====================================================================
108: // Test Success
109: // ====================================================================
110: try {
111: this .getMailServer();
112:
113: String strSubject = "Test Multipart Send Subject";
114:
115: EmailAttachment attachment = new EmailAttachment();
116: attachment.setPath(testFile.getAbsolutePath());
117: attachment.setDisposition(EmailAttachment.ATTACHMENT);
118: attachment.setName("Test_Attachment");
119: attachment.setDescription("Test Attachment Desc");
120:
121: MockMultiPartEmailConcrete testEmail = new MockMultiPartEmailConcrete();
122: testEmail.setHostName(this .strTestMailServer);
123: testEmail.setSmtpPort(this .getMailServerPort());
124: testEmail.setFrom(this .strTestMailFrom);
125: testEmail.addTo(this .strTestMailTo);
126: testEmail.attach(attachment);
127: testEmail.setSubType("subType");
128:
129: if (EmailUtils.isNotEmpty(this .strTestUser)
130: && EmailUtils.isNotEmpty(this .strTestPasswd)) {
131: testEmail.setAuthentication(this .strTestUser,
132: this .strTestPasswd);
133: }
134:
135: testEmail.setSubject(strSubject);
136:
137: testEmail.setMsg("Test Message");
138:
139: Hashtable ht = new Hashtable();
140: ht.put("X-Priority", "2");
141: ht.put("Disposition-Notification-To", this .strTestMailFrom);
142: ht.put("X-Mailer", "Sendmail");
143:
144: testEmail.setHeaders(ht);
145:
146: testEmail.send();
147:
148: this .fakeMailServer.stop();
149: // validate message
150: validateSend(this .fakeMailServer, strSubject, testEmail
151: .getMsg(), testEmail.getFromAddress(), testEmail
152: .getToList(), testEmail.getCcList(), testEmail
153: .getBccList(), true);
154:
155: // validate attachment
156: validateSend(this .fakeMailServer, strSubject, attachment
157: .getName(), testEmail.getFromAddress(), testEmail
158: .getToList(), testEmail.getCcList(), testEmail
159: .getBccList(), false);
160: }
161:
162: catch (IOException e) {
163: e.printStackTrace();
164: fail("Failed to save email to output file");
165: } catch (Exception e) {
166: e.printStackTrace();
167: fail("Unexpected exception thrown");
168: }
169:
170: // ====================================================================
171: // Test Exceptions
172: // ====================================================================
173: try {
174: this .getMailServer();
175:
176: this .email.send();
177: fail("Should have thrown an exception");
178: } catch (EmailException e) {
179: this .fakeMailServer.stop();
180: assertTrue(true);
181: } catch (Exception e) {
182: e.printStackTrace();
183: fail("Unexpected exception thrown");
184: }
185: }
186:
187: /** */
188: public void testAttach() {
189: EmailAttachment attachment;
190: // ====================================================================
191: // Test Success - File
192: // ====================================================================
193: attachment = new EmailAttachment();
194: try {
195: attachment.setName("Test Attachment");
196: attachment.setDescription("Test Attachment Desc");
197: attachment.setPath(testFile.getAbsolutePath());
198: this .email.attach(attachment);
199: } catch (EmailException e) {
200: fail("Unexpected exception thrown");
201: }
202:
203: // ====================================================================
204: // Test Success - URL
205: // ====================================================================
206: attachment = new EmailAttachment();
207: try {
208: attachment.setName("Test Attachment");
209: attachment.setDescription("Test Attachment Desc");
210: attachment.setURL(new URL(this .strTestURL));
211: this .email.attach(attachment);
212: } catch (EmailException e) {
213: e.printStackTrace();
214: fail("Unexpected exception thrown");
215: } catch (Exception e) {
216: e.printStackTrace();
217: fail("Unexpected exception thrown");
218: }
219:
220: // ====================================================================
221: // Test Exceptions
222: // ====================================================================
223: // null attachment
224: try {
225: this .email.attach(null);
226: fail("Should have thrown an exception");
227: } catch (EmailException e) {
228: assertTrue(true);
229: } catch (Exception e) {
230: e.printStackTrace();
231: fail("Unexpected exception thrown");
232: }
233:
234: // bad url
235: attachment = new EmailAttachment();
236: try {
237: attachment.setURL(new URL("http://bad.url"));
238: this .email.attach(attachment);
239: fail("Should have thrown an exception");
240: } catch (EmailException e) {
241: assertTrue(true);
242: } catch (Exception e) {
243: e.printStackTrace();
244: fail("Unexpected exception thrown");
245: }
246:
247: // bad file
248: attachment = new EmailAttachment();
249: try {
250: attachment.setPath("");
251: this .email.attach(attachment);
252: fail("Should have thrown an exception");
253: } catch (EmailException e) {
254: assertTrue(true);
255: } catch (Exception e) {
256: e.printStackTrace();
257: fail("Unexpected exception thrown");
258: }
259: }
260:
261: /** */
262: public void testAttach2() {
263: // ====================================================================
264: // Test Success - URL
265: // ====================================================================
266: try {
267: this .email.attach(new URL(this .strTestURL),
268: "Test Attachment", "Test Attachment Desc");
269: } catch (EmailException e) {
270: e.printStackTrace();
271: fail("Unexpected exception thrown");
272: } catch (MalformedURLException e) {
273: e.printStackTrace();
274: fail("Unexpected exception thrown");
275: }
276:
277: // bad name
278: try {
279: this .email.attach(new URL(this .strTestURL), null,
280: "Test Attachment Desc");
281: } catch (EmailException e) {
282: e.printStackTrace();
283: fail("Unexpected exception thrown");
284: } catch (MalformedURLException e) {
285: e.printStackTrace();
286: fail("Unexpected exception thrown");
287: }
288: }
289:
290: /** */
291: public void testAttach3() {
292: // ====================================================================
293: // Test Success - URL
294: // ====================================================================
295: try {
296: this .email.attach(new URLDataSource(
297: new URL(this .strTestURL)), "Test Attachment",
298: "Test Attachment Desc");
299: } catch (EmailException e) {
300: e.printStackTrace();
301: fail("Unexpected exception thrown");
302: } catch (MalformedURLException e) {
303: e.printStackTrace();
304: fail("Unexpected exception thrown");
305: }
306:
307: // ====================================================================
308: // Test Exceptions
309: // ====================================================================
310: // null datasource
311: try {
312: URLDataSource urlDs = null;
313: this .email.attach(urlDs, "Test Attachment",
314: "Test Attachment Desc");
315: fail("Should have thrown an exception");
316: } catch (EmailException e) {
317: assertTrue(true);
318: } catch (Exception e) {
319: e.printStackTrace();
320: fail("Unexpected exception thrown");
321: }
322:
323: // invalid datasource
324: try {
325: URLDataSource urlDs = new URLDataSource(new URL(
326: "http://bad.url/"));
327: this .email.attach(urlDs, "Test Attachment",
328: "Test Attachment Desc");
329: fail("Should have thrown an exception");
330: } catch (EmailException e) {
331: assertTrue(true);
332: } catch (Exception e) {
333: e.printStackTrace();
334: fail("Unexpected exception thrown");
335: }
336: }
337:
338: /**
339: *
340: * @throws Exception Exception
341: */
342: public void testAddPart() throws Exception {
343:
344: // setup
345: this .email = new MockMultiPartEmailConcrete();
346: String strMessage = "hello";
347: String strContentType = "text/plain";
348:
349: // add part
350: this .email.addPart(strMessage, strContentType);
351:
352: // validate
353: assertEquals(strContentType, this .email.getContainer()
354: .getBodyPart(0).getContentType());
355: assertEquals(strMessage, this .email.getContainer().getBodyPart(
356: 0).getDataHandler().getContent());
357:
358: }
359:
360: /**
361: *
362: * @throws Exception Exception
363: */
364: public void testAddPart2() throws Exception {
365:
366: // setup
367: this .email = new MockMultiPartEmailConcrete();
368: String strSubtype = "subtype/abc123";
369:
370: // add part
371: this .email.addPart(new MimeMultipart(strSubtype));
372:
373: // validate
374: assertTrue(this .email.getContainer().getBodyPart(0)
375: .getDataHandler().getContentType().indexOf(strSubtype) != -1);
376:
377: }
378:
379: /** @todo implement test for GetContainer */
380: public void testGetContainer() {
381: assertTrue(true);
382: }
383:
384: /** */
385: public void testInit() {
386: // call the init function twice to trigger the IllegalStateException
387: try {
388: this .email.init();
389: this .email.init();
390: fail("Should have thrown an exception");
391: } catch (IllegalStateException e) {
392: assertTrue(true);
393: } catch (Exception e) {
394: e.printStackTrace();
395: fail("Unexpected exception thrown");
396: }
397: }
398:
399: /** */
400: public void testGetSetSubType() {
401: for (int i = 0; i < testCharsValid.length; i++) {
402: this.email.setSubType(testCharsValid[i]);
403: assertEquals(testCharsValid[i], this.email.getSubType());
404: }
405: }
406: }
|