1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.mail;
17
18 import java.io.File;
19 import java.io.IOException;
20 import java.net.MalformedURLException;
21 import java.net.URL;
22 import java.util.Hashtable;
23
24 import javax.activation.URLDataSource;
25 import javax.mail.internet.MimeMultipart;
26
27 import org.apache.commons.mail.mocks.MockMultiPartEmailConcrete;
28
29
30
31
32
33
34
35
36
37 public class MultiPartEmailTest extends BaseEmailTestCase
38 {
39
40 private MockMultiPartEmailConcrete email = null;
41
42 private File testFile;
43
44
45
46
47 public MultiPartEmailTest(String name)
48 {
49 super(name);
50 }
51
52
53 protected void setUp()
54 {
55 super.setUp();
56
57 this.email = new MockMultiPartEmailConcrete();
58 try
59 {
60 testFile = File.createTempFile("testfile", ".txt");
61 }
62 catch (IOException ioe)
63 {
64 fail(ioe.getMessage());
65 }
66 }
67
68
69 public void testSetMsg()
70 {
71
72
73
74
75
76 for (int i = 0; i < testCharsValid.length; i++)
77 {
78 try
79 {
80 this.email.setMsg(testCharsValid[i]);
81 assertEquals(testCharsValid[i], this.email.getMsg());
82 }
83 catch (EmailException e)
84 {
85 fail("Unexpected exception thrown");
86 }
87 }
88
89
90 this.email.setCharset(Email.US_ASCII);
91 for (int i = 0; i < testCharsValid.length; i++)
92 {
93 try
94 {
95 this.email.setMsg(testCharsValid[i]);
96 assertEquals(testCharsValid[i], this.email.getMsg());
97 }
98 catch (EmailException e)
99 {
100 fail("Unexpected exception thrown");
101 }
102 }
103
104
105
106
107 for (int i = 0; i < testCharsNotValid.length; i++)
108 {
109 try
110 {
111 this.email.setMsg(testCharsNotValid[i]);
112 fail("Should have thrown an exception");
113 }
114 catch (EmailException e)
115 {
116 assertTrue(true);
117 }
118 catch (Exception e)
119 {
120 e.printStackTrace();
121 fail("Unexpected exception thrown");
122 }
123 }
124 }
125
126
127 public void testSend()
128 {
129
130
131
132 try
133 {
134 this.getMailServer();
135
136 String strSubject = "Test Multipart Send Subject";
137
138 EmailAttachment attachment = new EmailAttachment();
139 attachment.setPath(testFile.getAbsolutePath());
140 attachment.setDisposition(EmailAttachment.ATTACHMENT);
141 attachment.setName("Test_Attachment");
142 attachment.setDescription("Test Attachment Desc");
143
144 MockMultiPartEmailConcrete testEmail =
145 new MockMultiPartEmailConcrete();
146 testEmail.setHostName(this.strTestMailServer);
147 testEmail.setSmtpPort(this.getMailServerPort());
148 testEmail.setFrom(this.strTestMailFrom);
149 testEmail.addTo(this.strTestMailTo);
150 testEmail.attach(attachment);
151 testEmail.setSubType("subType");
152
153 if (EmailUtils.isNotEmpty(this.strTestUser)
154 && EmailUtils.isNotEmpty(this.strTestPasswd))
155 {
156 testEmail.setAuthentication(
157 this.strTestUser,
158 this.strTestPasswd);
159 }
160
161 testEmail.setSubject(strSubject);
162
163 testEmail.setMsg("Test Message");
164
165 Hashtable ht = new Hashtable();
166 ht.put("X-Priority", "2");
167 ht.put("Disposition-Notification-To", this.strTestMailFrom);
168 ht.put("X-Mailer", "Sendmail");
169
170 testEmail.setHeaders(ht);
171
172 testEmail.send();
173
174 this.fakeMailServer.stop();
175
176 validateSend(
177 this.fakeMailServer,
178 strSubject,
179 testEmail.getMsg(),
180 testEmail.getFromAddress(),
181 testEmail.getToList(),
182 testEmail.getCcList(),
183 testEmail.getBccList(),
184 true);
185
186
187 validateSend(
188 this.fakeMailServer,
189 strSubject,
190 attachment.getName(),
191 testEmail.getFromAddress(),
192 testEmail.getToList(),
193 testEmail.getCcList(),
194 testEmail.getBccList(),
195 false);
196 }
197
198 catch (IOException e)
199 {
200 e.printStackTrace();
201 fail("Failed to save email to output file");
202 }
203 catch (Exception e)
204 {
205 e.printStackTrace();
206 fail("Unexpected exception thrown");
207 }
208
209
210
211
212 try
213 {
214 this.getMailServer();
215
216 this.email.send();
217 fail("Should have thrown an exception");
218 }
219 catch (EmailException e)
220 {
221 this.fakeMailServer.stop();
222 assertTrue(true);
223 }
224 catch (Exception e)
225 {
226 e.printStackTrace();
227 fail("Unexpected exception thrown");
228 }
229 }
230
231
232 public void testAttach()
233 {
234 EmailAttachment attachment;
235
236
237
238 attachment = new EmailAttachment();
239 try
240 {
241 attachment.setName("Test Attachment");
242 attachment.setDescription("Test Attachment Desc");
243 attachment.setPath(testFile.getAbsolutePath());
244 this.email.attach(attachment);
245 }
246 catch (EmailException e)
247 {
248 fail("Unexpected exception thrown");
249 }
250
251
252
253
254 attachment = new EmailAttachment();
255 try
256 {
257 attachment.setName("Test Attachment");
258 attachment.setDescription("Test Attachment Desc");
259 attachment.setURL(new URL(this.strTestURL));
260 this.email.attach(attachment);
261 }
262 catch (EmailException e)
263 {
264 e.printStackTrace();
265 fail("Unexpected exception thrown");
266 }
267 catch (Exception e)
268 {
269 e.printStackTrace();
270 fail("Unexpected exception thrown");
271 }
272
273
274
275
276
277 try
278 {
279 this.email.attach(null);
280 fail("Should have thrown an exception");
281 }
282 catch (EmailException e)
283 {
284 assertTrue(true);
285 }
286 catch (Exception e)
287 {
288 e.printStackTrace();
289 fail("Unexpected exception thrown");
290 }
291
292
293 attachment = new EmailAttachment();
294 try
295 {
296 attachment.setURL(new URL("http://bad.url"));
297 this.email.attach(attachment);
298 fail("Should have thrown an exception");
299 }
300 catch (EmailException e)
301 {
302 assertTrue(true);
303 }
304 catch (Exception e)
305 {
306 e.printStackTrace();
307 fail("Unexpected exception thrown");
308 }
309
310
311 attachment = new EmailAttachment();
312 try
313 {
314 attachment.setPath("");
315 this.email.attach(attachment);
316 fail("Should have thrown an exception");
317 }
318 catch (EmailException e)
319 {
320 assertTrue(true);
321 }
322 catch (Exception e)
323 {
324 e.printStackTrace();
325 fail("Unexpected exception thrown");
326 }
327 }
328
329
330 public void testAttach2()
331 {
332
333
334
335 try
336 {
337 this.email.attach(
338 new URL(this.strTestURL),
339 "Test Attachment",
340 "Test Attachment Desc");
341 }
342 catch (EmailException e)
343 {
344 e.printStackTrace();
345 fail("Unexpected exception thrown");
346 }
347 catch (MalformedURLException e)
348 {
349 e.printStackTrace();
350 fail("Unexpected exception thrown");
351 }
352
353
354 try
355 {
356 this.email.attach(
357 new URL(this.strTestURL),
358 null,
359 "Test Attachment Desc");
360 }
361 catch (EmailException e)
362 {
363 e.printStackTrace();
364 fail("Unexpected exception thrown");
365 }
366 catch (MalformedURLException e)
367 {
368 e.printStackTrace();
369 fail("Unexpected exception thrown");
370 }
371 }
372
373
374 public void testAttach3()
375 {
376
377
378
379 try
380 {
381 this.email.attach(
382 new URLDataSource(new URL(this.strTestURL)),
383 "Test Attachment",
384 "Test Attachment Desc");
385 }
386 catch (EmailException e)
387 {
388 e.printStackTrace();
389 fail("Unexpected exception thrown");
390 }
391 catch (MalformedURLException e)
392 {
393 e.printStackTrace();
394 fail("Unexpected exception thrown");
395 }
396
397
398
399
400
401 try
402 {
403 URLDataSource urlDs = null;
404 this.email.attach(urlDs, "Test Attachment", "Test Attachment Desc");
405 fail("Should have thrown an exception");
406 }
407 catch (EmailException e)
408 {
409 assertTrue(true);
410 }
411 catch (Exception e)
412 {
413 e.printStackTrace();
414 fail("Unexpected exception thrown");
415 }
416
417
418 try
419 {
420 URLDataSource urlDs = new URLDataSource(new URL("http://bad.url/"));
421 this.email.attach(urlDs, "Test Attachment", "Test Attachment Desc");
422 fail("Should have thrown an exception");
423 }
424 catch (EmailException e)
425 {
426 assertTrue(true);
427 }
428 catch (Exception e)
429 {
430 e.printStackTrace();
431 fail("Unexpected exception thrown");
432 }
433 }
434
435
436
437
438
439 public void testAddPart() throws Exception
440 {
441
442
443 this.email = new MockMultiPartEmailConcrete();
444 String strMessage = "hello";
445 String strContentType = "text/plain";
446
447
448 this.email.addPart(strMessage, strContentType);
449
450
451 assertEquals(
452 strContentType,
453 this.email.getContainer().getBodyPart(0).getContentType());
454 assertEquals(
455 strMessage,
456 this
457 .email
458 .getContainer()
459 .getBodyPart(0)
460 .getDataHandler()
461 .getContent());
462
463 }
464
465
466
467
468
469 public void testAddPart2() throws Exception
470 {
471
472
473 this.email = new MockMultiPartEmailConcrete();
474 String strSubtype = "subtype/abc123";
475
476
477 this.email.addPart(new MimeMultipart(strSubtype));
478
479
480 assertTrue(
481 this
482 .email
483 .getContainer()
484 .getBodyPart(0)
485 .getDataHandler()
486 .getContentType()
487 .indexOf(strSubtype)
488 != -1);
489
490 }
491
492
493 public void testGetContainer()
494 {
495 assertTrue(true);
496 }
497
498
499 public void testInit()
500 {
501
502 try
503 {
504 this.email.init();
505 this.email.init();
506 fail("Should have thrown an exception");
507 }
508 catch (IllegalStateException e)
509 {
510 assertTrue(true);
511 }
512 catch (Exception e)
513 {
514 e.printStackTrace();
515 fail("Unexpected exception thrown");
516 }
517 }
518
519
520 public void testGetSetSubType()
521 {
522 for (int i = 0; i < testCharsValid.length; i++)
523 {
524 this.email.setSubType(testCharsValid[i]);
525 assertEquals(testCharsValid[i], this.email.getSubType());
526 }
527 }
528 }