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