1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
package test.atlassian.mail; |
9 |
|
|
10 |
|
import com.atlassian.core.ofbiz.test.AbstractOFBizTestCase; |
11 |
|
import com.atlassian.core.ofbiz.test.UtilsForTests; |
12 |
|
import com.atlassian.core.user.UserUtils; |
13 |
|
import com.atlassian.mail.MailUtils; |
14 |
|
import com.opensymphony.user.DuplicateEntityException; |
15 |
|
import com.opensymphony.user.ImmutableException; |
16 |
|
import com.opensymphony.user.User; |
17 |
|
import test.mock.mail.MockMessage; |
18 |
|
|
19 |
|
import java.io.ByteArrayInputStream; |
20 |
|
import java.io.FileInputStream; |
21 |
|
import java.io.IOException; |
22 |
|
import java.util.ArrayList; |
23 |
|
import java.util.Collection; |
24 |
|
import java.util.List; |
25 |
|
import java.util.Properties; |
26 |
|
import javax.mail.Address; |
27 |
|
import javax.mail.BodyPart; |
28 |
|
import javax.mail.Message; |
29 |
|
import javax.mail.MessagingException; |
30 |
|
import javax.mail.Multipart; |
31 |
|
import javax.mail.Part; |
32 |
|
import javax.mail.Session; |
33 |
|
import javax.mail.internet.AddressException; |
34 |
|
import javax.mail.internet.InternetAddress; |
35 |
|
import javax.mail.internet.MimeBodyPart; |
36 |
|
import javax.mail.internet.MimeMessage; |
37 |
|
import javax.mail.internet.NewsAddress; |
38 |
|
|
39 |
|
import org.apache.commons.io.IOUtils; |
40 |
|
|
|
|
| 99.3% |
Uncovered Elements: 2 (278) |
Complexity: 65 |
Complexity Density: 0.32 |
|
41 |
|
public class TestMailUtils extends AbstractOFBizTestCase |
42 |
|
{ |
43 |
|
private User testuser; |
44 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
45 |
38
|
public TestMailUtils(String s)... |
46 |
|
{ |
47 |
38
|
super(s); |
48 |
|
} |
49 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
50 |
38
|
protected void setUp() throws Exception... |
51 |
|
{ |
52 |
38
|
super.setUp(); |
53 |
38
|
testuser = UserUtils.createUser("test user", "password", "test@atlassian.com", "test user fullname"); |
54 |
|
} |
55 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
56 |
38
|
protected void tearDown() throws Exception... |
57 |
|
{ |
58 |
38
|
UtilsForTests.cleanUsers(); |
59 |
|
|
60 |
38
|
super.tearDown(); |
61 |
|
} |
62 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1
PASS
|
|
63 |
1
|
public void testParseAddresses() throws AddressException... |
64 |
|
{ |
65 |
1
|
InternetAddress[] addresses = MailUtils.parseAddresses("edwin@atlassian.com, mike@atlassian.com, owen@atlassian.com"); |
66 |
|
|
67 |
1
|
assertEquals(new InternetAddress("edwin@atlassian.com"), addresses[0]); |
68 |
1
|
assertEquals(new InternetAddress("mike@atlassian.com"), addresses[1]); |
69 |
1
|
assertEquals(new InternetAddress("owen@atlassian.com"), addresses[2]); |
70 |
|
} |
71 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1
PASS
|
|
72 |
1
|
public void testGetBody() throws MessagingException, IOException... |
73 |
|
{ |
74 |
1
|
Message msg = new MimeMessage(null, new ByteArrayInputStream("test".getBytes())); |
75 |
1
|
msg.setContent("test message", ""); |
76 |
1
|
msg.setHeader("Content-Type", "text/plain"); |
77 |
1
|
assertEquals("test message", MailUtils.getBody(msg)); |
78 |
|
|
79 |
1
|
msg.setContent(new Integer(1), "text/plain"); |
80 |
1
|
assertNull(MailUtils.getBody(msg)); |
81 |
|
} |
82 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1
PASS
|
|
83 |
1
|
public void testGetAuthorFromSender() throws MessagingException, ImmutableException, DuplicateEntityException... |
84 |
|
{ |
85 |
1
|
Message msg = new MimeMessage(null, new ByteArrayInputStream("test".getBytes())); |
86 |
1
|
assertNull(MailUtils.getAuthorFromSender(msg)); |
87 |
|
|
88 |
1
|
msg.setFrom(new InternetAddress("edwin@atlassian.com")); |
89 |
1
|
assertNull(MailUtils.getAuthorFromSender(msg)); |
90 |
|
|
91 |
1
|
msg.setFrom(new InternetAddress("test@atlassian.com")); |
92 |
1
|
assertNotNull(MailUtils.getAuthorFromSender(msg)); |
93 |
1
|
assertEquals(testuser, MailUtils.getAuthorFromSender(msg)); |
94 |
|
} |
95 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1
PASS
|
|
96 |
1
|
public void testGetFirstValidUser() throws ImmutableException, DuplicateEntityException, AddressException... |
97 |
|
{ |
98 |
1
|
Address[] blankaddresslist = { }; |
99 |
1
|
assertNull(MailUtils.getFirstValidUser(blankaddresslist)); |
100 |
|
|
101 |
1
|
Address[] nouseraddresslist = { new InternetAddress("edwin@atlassian.com"), new InternetAddress("owen@atlassian.coM") }; |
102 |
1
|
assertNull(MailUtils.getFirstValidUser(nouseraddresslist)); |
103 |
|
|
104 |
1
|
Address[] addresslist = { new InternetAddress("mike@atlassian.com"), new InternetAddress("test@atlassian.com") }; |
105 |
1
|
assertNotNull(MailUtils.getFirstValidUser(addresslist)); |
106 |
1
|
assertEquals(testuser, MailUtils.getFirstValidUser(addresslist)); |
107 |
|
} |
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
|
114 |
|
@throws |
115 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (18) |
Complexity: 1 |
Complexity Density: 0.06 |
1
PASS
|
|
116 |
1
|
public void testGetSenders() throws Exception... |
117 |
|
{ |
118 |
1
|
Address[] blankaddresslist = { }; |
119 |
|
|
120 |
1
|
Message msg = createMockMessageWithSenders(blankaddresslist); |
121 |
1
|
assertEquals(0, MailUtils.getSenders(msg).size()); |
122 |
|
|
123 |
|
|
124 |
1
|
Address[] allNulls = { null, null }; |
125 |
1
|
msg = createMockMessageWithSenders(allNulls); |
126 |
1
|
assertEquals(0, MailUtils.getSenders(msg).size()); |
127 |
|
|
128 |
|
|
129 |
1
|
String goodAddress = "good@address.com"; |
130 |
1
|
Address[] oneGood = { null, new InternetAddress(goodAddress) }; |
131 |
1
|
msg = createMockMessageWithSenders(oneGood); |
132 |
1
|
List senders = MailUtils.getSenders(msg); |
133 |
1
|
assertEquals(1, senders.size()); |
134 |
1
|
assertEquals(goodAddress, senders.get(0)); |
135 |
|
|
136 |
|
|
137 |
1
|
Address[] mixedBag = { |
138 |
|
new InternetAddress(goodAddress), |
139 |
|
new NewsAddress(), |
140 |
|
null, |
141 |
|
new InternetAddress(" " + goodAddress) }; |
142 |
1
|
msg = createMockMessageWithSenders(mixedBag); |
143 |
1
|
senders = MailUtils.getSenders(msg); |
144 |
1
|
assertEquals(2, senders.size()); |
145 |
|
|
146 |
1
|
assertEquals(goodAddress, senders.get(0)); |
147 |
1
|
assertEquals(goodAddress, senders.get(1)); |
148 |
|
} |
149 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
150 |
4
|
private Message createMockMessageWithSenders(final Address[] addresses) throws MessagingException... |
151 |
|
{ |
152 |
4
|
Message msg = new MockMessage() |
153 |
|
{ |
154 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
155 |
4
|
public Address[] getFrom() throws MessagingException... |
156 |
|
{ |
157 |
4
|
return addresses; |
158 |
|
} |
159 |
|
}; |
160 |
4
|
return msg; |
161 |
|
} |
162 |
|
|
163 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1
PASS
|
|
164 |
1
|
public void testHasRecipient() throws MessagingException... |
165 |
|
{ |
166 |
1
|
Message msg = new MimeMessage(null, new ByteArrayInputStream("test".getBytes())); |
167 |
1
|
assertTrue(!MailUtils.hasRecipient("edwin@atlassian.com", msg)); |
168 |
1
|
msg.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress("edwin@atlassian.com")); |
169 |
1
|
assertTrue(MailUtils.hasRecipient("edwin@atlassian.com", msg)); |
170 |
|
} |
171 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1
PASS
|
|
172 |
1
|
public void testCreateAttachmentMimeBodyPartFileNames() throws MessagingException... |
173 |
|
{ |
174 |
1
|
MimeBodyPart mbp = MailUtils.createAttachmentMimeBodyPart("C:/test/foo/bar/export.zip"); |
175 |
1
|
assertEquals("export.zip", mbp.getFileName()); |
176 |
|
} |
177 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1
PASS
|
|
178 |
1
|
public void testCreateAttachmentMimeBodyPartFileNames2() throws MessagingException... |
179 |
|
{ |
180 |
1
|
MimeBodyPart mbp = MailUtils.createAttachmentMimeBodyPart("C:\\test\\foo\\bar\\export.zip"); |
181 |
1
|
assertEquals("export.zip", mbp.getFileName()); |
182 |
|
} |
183 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1
PASS
|
|
184 |
1
|
public void testGetBodyText() throws MessagingException... |
185 |
|
{ |
186 |
1
|
assertEquals("This is a simple test mail, which isn't in HTML and has no attachments.", |
187 |
|
MailUtils.getBody(makeMessageFromResourceFile("/testmails/simplebody.txt"))); |
188 |
|
} |
189 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1
PASS
|
|
190 |
1
|
public void testGetBodyTextIfContentEncodingUnsupported() throws MessagingException... |
191 |
|
{ |
192 |
1
|
assertEquals("This is a simple test mail, which isn't in HTML and has no attachments.", |
193 |
|
MailUtils.getBody(makeMessageFromResourceFile("/testmails/simplebodyunsupportedencoding.txt"))); |
194 |
|
} |
195 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1
PASS
|
|
196 |
1
|
public void testGetBodyHtml() throws MessagingException... |
197 |
|
{ |
198 |
1
|
assertEquals("This is an HTML test mail\nwith a linebreak.", |
199 |
|
MailUtils.getBody(makeMessageFromResourceFile("/testmails/simplehtml.txt"))); |
200 |
|
} |
201 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1
PASS
|
|
202 |
1
|
public void testGetBodyMultipartAlternativeGetsTextMatch() throws MessagingException... |
203 |
|
{ |
204 |
1
|
assertEquals("This is the text part of the mail", |
205 |
|
MailUtils.getBody(makeMessageFromResourceFile("/testmails/multipart.txt"))); |
206 |
|
} |
207 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1
PASS
|
|
208 |
1
|
public void testGetBodyMultipartGetsFirstTextMatch() throws MessagingException... |
209 |
|
{ |
210 |
1
|
assertEquals("This is the first part", |
211 |
|
MailUtils.getBody(makeMessageFromResourceFile("/testmails/multipartbothtext.txt"))); |
212 |
|
} |
213 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1
PASS
|
|
214 |
1
|
public void testGetBodyMultipartGetsHtmlIfNoText() throws MessagingException... |
215 |
|
{ |
216 |
1
|
assertEquals("This is\nthe HTML part of the mail", |
217 |
|
MailUtils.getBody(makeMessageFromResourceFile("/testmails/multipartnotext.txt"))); |
218 |
|
} |
219 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1
PASS
|
|
220 |
1
|
public void testGetBodyMultipartNoMatch() throws MessagingException... |
221 |
|
{ |
222 |
1
|
assertEquals("", MailUtils.getBody(makeMessageFromResourceFile("/testmails/multipartnobody.txt"))); |
223 |
|
} |
224 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1
PASS
|
|
225 |
1
|
public void testGetBodyMultipartMixed() throws MessagingException... |
226 |
|
{ |
227 |
1
|
assertEquals("Text alternative", MailUtils.getBody(makeMessageFromResourceFile("/testmails/multipartmixedotherorder.txt"))); |
228 |
|
} |
229 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1
PASS
|
|
230 |
1
|
public void testGetBodyMultipartMixedWithLotsOfTextAndHtml() throws MessagingException... |
231 |
|
{ |
232 |
1
|
assertEquals("Text alternative\nThis is the first part after the alternative\nThis is the second part after the alternative", |
233 |
|
MailUtils.getBody(makeMessageFromResourceFile("/testmails/multipartmixedtextandhtml.txt"))); |
234 |
|
} |
235 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1
PASS
|
|
236 |
1
|
public void testGetAttachmentsNoAttachments() throws MessagingException, IOException... |
237 |
|
{ |
238 |
1
|
assertEquals("no attachments", 0, |
239 |
|
MailUtils.getAttachments(makeMessageFromResourceFile("/testmails/multipart.txt")).length); |
240 |
|
} |
241 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1
PASS
|
|
242 |
1
|
public void testGetAttachmentsIfContentEncodingIsNotSupported() throws MessagingException, IOException... |
243 |
|
{ |
244 |
1
|
Message message = makeMessageFromResourceFile("/testmails/multipartunsupportedencoding.txt"); |
245 |
1
|
assertEquals("This is the text part of the mail", MailUtils.getBody(message)); |
246 |
1
|
assertEquals("no attachments", 0, |
247 |
|
MailUtils.getAttachments(message).length); |
248 |
|
} |
249 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
1
PASS
|
|
250 |
1
|
public void testGetAttachmentsHasAttachments() throws MessagingException, IOException... |
251 |
|
{ |
252 |
1
|
MailUtils.Attachment[] attachments = MailUtils.getAttachments(makeMessageFromResourceFile("/testmails/multipartmixed.txt")); |
253 |
1
|
assertEquals("has attachments", 2, attachments.length); |
254 |
1
|
assertEquals("Attachment 1", new String(attachments[0].getContents())); |
255 |
1
|
assertEquals("image/jpeg", attachments[0].getContentType().substring(0, 10)); |
256 |
1
|
assertEquals("bugger.jpg", attachments[0].getFilename()); |
257 |
1
|
assertEquals("html<br>attachment", new String(attachments[1].getContents())); |
258 |
1
|
assertEquals("text/html", attachments[1].getContentType().substring(0, 9)); |
259 |
1
|
assertEquals("foo.html", attachments[1].getFilename()); |
260 |
|
} |
261 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
262 |
14
|
private Message makeMessageFromResourceFile(String resourceName) throws MessagingException... |
263 |
|
{ |
264 |
14
|
return new MimeMessage(null, this.getClass().getResourceAsStream(resourceName)); |
265 |
|
} |
266 |
|
|
267 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1
PASS
|
|
268 |
1
|
public void testGetContentTypeFromHeaderValue()... |
269 |
|
{ |
270 |
1
|
final String input = "text/plain"; |
271 |
1
|
final String actual = MailUtils.getContentType(input); |
272 |
1
|
final String expected = input; |
273 |
|
|
274 |
1
|
assertEquals(expected, actual); |
275 |
|
} |
276 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1
PASS
|
|
277 |
1
|
public void testGetContentTypeFromHeaderValueWithParameter()... |
278 |
|
{ |
279 |
1
|
final String input = "text/plain; something=somevalue"; |
280 |
1
|
final String actual = MailUtils.getContentType(input); |
281 |
1
|
final String expected = "text/plain"; |
282 |
|
|
283 |
1
|
assertEquals(expected, actual); |
284 |
|
} |
285 |
|
|
286 |
|
|
287 |
|
|
288 |
|
|
289 |
|
|
290 |
|
final static boolean PLAIN_TEXT_YES = true; |
291 |
|
final static boolean PLAIN_TEXT_NO = false; |
292 |
|
|
293 |
|
final static boolean HTML_YES = true; |
294 |
|
final static boolean HTML_NO = false; |
295 |
|
|
296 |
|
final static boolean INLINE_YES = true; |
297 |
|
final static boolean INLINE_NO = false; |
298 |
|
|
299 |
|
final static boolean ATTACHMENT_YES = true; |
300 |
|
final static boolean ATTACHMENT_NO = false; |
301 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1
PASS
|
|
302 |
1
|
public void testIsPlainTextPart() throws Exception... |
303 |
|
{ |
304 |
1
|
this.checkParts("OutlookPlainText.msg", PLAIN_TEXT_YES, HTML_YES, INLINE_NO, ATTACHMENT_NO); |
305 |
|
} |
306 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1
PASS
|
|
307 |
1
|
public void testIsHtmlPart() throws Exception... |
308 |
|
{ |
309 |
1
|
this.checkParts("OutlookHtml.msg", PLAIN_TEXT_YES, HTML_YES, INLINE_NO, ATTACHMENT_NO); |
310 |
|
} |
311 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1
PASS
|
|
312 |
1
|
public void testIsImageAttachedPart() throws Exception... |
313 |
|
{ |
314 |
1
|
this.checkParts("OutlookHtmlImageAttached.msg", PLAIN_TEXT_YES, HTML_YES, INLINE_NO, ATTACHMENT_YES); |
315 |
|
} |
316 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1
PASS
|
|
317 |
1
|
public void testIsInlineImagePart() throws Exception... |
318 |
|
{ |
319 |
1
|
this.checkParts("OutlookHtmlInlineImage.msg", PLAIN_TEXT_YES, HTML_YES, INLINE_YES, ATTACHMENT_NO); |
320 |
|
} |
321 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1
PASS
|
|
322 |
1
|
public void testIsInlineImagePartWherePartHasDisposition() throws Exception... |
323 |
|
{ |
324 |
1
|
this.checkParts("ThunderbirdHtmlAndPlainTextInlineImage.msg", PLAIN_TEXT_YES, HTML_YES, INLINE_YES, ATTACHMENT_NO); |
325 |
|
} |
326 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1
PASS
|
|
327 |
1
|
public void testIsAttachmentPart() throws Exception... |
328 |
|
{ |
329 |
1
|
this.checkParts("OutlookHtmlBinaryAttachment.msg", PLAIN_TEXT_YES, HTML_YES, INLINE_NO, ATTACHMENT_YES); |
330 |
|
} |
331 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1
PASS
|
|
332 |
1
|
public void testIsRelatedPart() throws Exception... |
333 |
|
{ |
334 |
1
|
assertFalse(MailUtils.isPartRelated(makeMessageFromResourceFile("/testmails/multipartmixedtextandhtml.txt"))); |
335 |
1
|
assertTrue(MailUtils.isPartRelated(makeMessageFromResourceFile("/testmails/multipartrelated.txt"))); |
336 |
|
} |
337 |
|
|
338 |
|
|
339 |
|
|
340 |
|
|
341 |
|
|
342 |
|
|
343 |
|
|
344 |
|
@param |
345 |
|
@param |
346 |
|
@param |
347 |
|
@param |
348 |
|
@param |
349 |
|
@throws |
350 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (41) |
Complexity: 6 |
Complexity Density: 0.19 |
|
351 |
6
|
void checkParts(final String filename, final boolean plainTextExpected, final boolean htmlExpected, final boolean inlineExpected, final boolean attachmentExpected)... |
352 |
|
throws Exception |
353 |
|
{ |
354 |
6
|
assertNotNull(filename); |
355 |
|
|
356 |
6
|
final Part[] parts = this.createPartsFromMessage(filename); |
357 |
6
|
assertNotNull("parts", parts); |
358 |
6
|
assertTrue("Expected atleast 1 part but got " + parts.length + " part(s)", parts.length > 0); |
359 |
|
|
360 |
|
|
361 |
6
|
boolean plainTextFound = false; |
362 |
6
|
boolean htmlFound = false; |
363 |
6
|
boolean inlineFound = false; |
364 |
6
|
boolean attachmentFound = false; |
365 |
|
|
366 |
22
|
for (int i = 0; i < parts.length; i++) |
367 |
|
{ |
368 |
16
|
final Part part = parts[i]; |
369 |
|
|
370 |
16
|
if (MailUtils.isPartPlainText(part)) |
371 |
|
{ |
372 |
6
|
assertTrue("PlainText part found when none was expected", plainTextExpected); |
373 |
6
|
plainTextFound = true; |
374 |
6
|
continue; |
375 |
|
} |
376 |
|
|
377 |
10
|
if (MailUtils.isPartHtml(part)) |
378 |
|
{ |
379 |
6
|
assertTrue("Html part found when none was expected", htmlExpected); |
380 |
6
|
htmlFound = true; |
381 |
|
} |
382 |
|
|
383 |
10
|
if (MailUtils.isPartInline(part)) |
384 |
|
{ |
385 |
2
|
assertTrue("Inline part found when none was expected", inlineExpected); |
386 |
2
|
inlineFound = true; |
387 |
|
|
388 |
2
|
final boolean reportedEmpty = MailUtils.isContentEmpty(part); |
389 |
2
|
assertFalse("All inline parts in the prepared msg files are never empty...", reportedEmpty); |
390 |
|
} |
391 |
|
|
392 |
10
|
if (MailUtils.isPartAttachment(part)) |
393 |
|
{ |
394 |
2
|
assertTrue("Attachment part found when none was expected", attachmentExpected); |
395 |
2
|
attachmentFound = true; |
396 |
|
|
397 |
2
|
final boolean reportedEmpty = MailUtils.isContentEmpty(part); |
398 |
2
|
assertFalse("All attachments parts in the prepared msg files are never empty...", reportedEmpty); |
399 |
|
} |
400 |
|
} |
401 |
|
|
402 |
6
|
assertEquals("Expected to find a plain text part but one was not found", plainTextExpected, plainTextFound); |
403 |
6
|
assertEquals("Expected to find a html part but one was not found", htmlExpected, htmlFound); |
404 |
6
|
assertEquals("Expected to find a inline part but one was not found", inlineExpected, inlineFound); |
405 |
6
|
assertEquals("Expected to find a attachment part but one was not found", attachmentExpected, attachmentFound); |
406 |
|
} |
407 |
|
|
408 |
|
|
409 |
|
|
410 |
|
|
411 |
|
|
412 |
|
@param |
413 |
|
@return |
414 |
|
@throws |
415 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
|
416 |
6
|
Part[] createPartsFromMessage(final String filename) throws Exception... |
417 |
|
{ |
418 |
6
|
assertNotNull(filename); |
419 |
|
|
420 |
6
|
FileInputStream fis = null; |
421 |
|
|
422 |
6
|
try |
423 |
|
{ |
424 |
6
|
fis = new FileInputStream("src/test/resources/testmails/" + filename); |
425 |
|
|
426 |
|
|
427 |
6
|
final Message message = new MimeMessage(Session.getDefaultInstance(new Properties()), fis); |
428 |
6
|
final Part[] parts = this.getAllParts(message); |
429 |
6
|
assertNotNull("parts", parts); |
430 |
6
|
assertTrue("Expected atleast 1 part but got " + parts.length + " part(s)", parts.length > 0); |
431 |
|
|
432 |
6
|
return parts; |
433 |
|
} |
434 |
|
finally |
435 |
|
{ |
436 |
6
|
IOUtils.closeQuietly(fis); |
437 |
|
} |
438 |
|
} |
439 |
|
|
440 |
|
|
441 |
|
|
442 |
|
|
443 |
|
|
444 |
|
@param |
445 |
|
@return |
446 |
|
@throws |
447 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
448 |
6
|
Part[] getAllParts(final Message message) throws Exception... |
449 |
|
{ |
450 |
6
|
final List partsList = new ArrayList(); |
451 |
6
|
this.processMessageParts(message, partsList); |
452 |
6
|
final Part[] parts = (Part[]) partsList.toArray(new Part[0]); |
453 |
6
|
return parts; |
454 |
|
} |
455 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
456 |
6
|
void processMessageParts(final Message message, final Collection parts) throws Exception... |
457 |
|
{ |
458 |
6
|
final Object content = message.getContent(); |
459 |
6
|
this.handlePartOrContent(content, parts); |
460 |
|
} |
461 |
|
|
|
|
| 88.2% |
Uncovered Elements: 2 (17) |
Complexity: 4 |
Complexity Density: 0.36 |
|
462 |
30
|
void handlePartOrContent(final Object partOrContent, final Collection parts) throws Exception... |
463 |
|
{ |
464 |
30
|
if (partOrContent instanceof Multipart) |
465 |
|
{ |
466 |
10
|
this.processMultipartParts((Multipart) partOrContent, parts); |
467 |
10
|
return; |
468 |
|
} |
469 |
|
|
470 |
20
|
if (partOrContent instanceof BodyPart) |
471 |
|
{ |
472 |
20
|
final BodyPart bodyPart = (BodyPart) partOrContent; |
473 |
20
|
final Object bodyPartContent = bodyPart.getContent(); |
474 |
|
|
475 |
20
|
if (bodyPartContent instanceof Multipart) |
476 |
|
{ |
477 |
4
|
this.handlePartOrContent(bodyPartContent, parts); |
478 |
|
} |
479 |
|
else |
480 |
|
{ |
481 |
16
|
parts.add(bodyPart); |
482 |
|
} |
483 |
20
|
return; |
484 |
|
} |
485 |
0
|
parts.add((Part) partOrContent); |
486 |
|
} |
487 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
488 |
10
|
void processMultipartParts(final Multipart multipart, final Collection parts) throws Exception... |
489 |
|
{ |
490 |
10
|
final int partsCount = multipart.getCount(); |
491 |
30
|
for (int i = 0; i < partsCount; i++) |
492 |
|
{ |
493 |
20
|
final BodyPart bodyPart = multipart.getBodyPart(i); |
494 |
20
|
this.handlePartOrContent(bodyPart, parts); |
495 |
|
} |
496 |
|
} |
497 |
|
|
498 |
|
|
499 |
|
static final String X_PKCS7_SIGNATURE = "application/x-pkcs7-signature"; |
500 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1
PASS
|
|
501 |
1
|
public void testPositivePartIsSignature() throws Exception... |
502 |
|
{ |
503 |
1
|
final Message message = createMessageWithAttachment(X_PKCS7_SIGNATURE, "attachment", "filename", "xxxx"); |
504 |
1
|
final boolean actual = MailUtils.isPartSignaturePKCS7(message); |
505 |
1
|
assertTrue("The message being tested is of \"" + X_PKCS7_SIGNATURE + "\" but MailUtils returned false instead of true", actual); |
506 |
|
} |
507 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1
PASS
|
|
508 |
1
|
public void testNegativePartIsSignature() throws Exception... |
509 |
|
{ |
510 |
1
|
final Message message = createMessageWithAttachment("text/plain", "", null, "xxxx"); |
511 |
1
|
final boolean actual = MailUtils.isPartSignaturePKCS7(message); |
512 |
1
|
assertFalse("The message being tested is of \"" + X_PKCS7_SIGNATURE + "\" but MailUtils returned true for a text/plain when it should have returned false.", actual); |
513 |
|
} |
514 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1
PASS
|
|
515 |
1
|
public void testTextPartContentIsEmpty() throws Exception... |
516 |
|
{ |
517 |
1
|
final Message message = createMessageWithAttachment("text/plain", "", null, ""); |
518 |
1
|
final boolean actual = MailUtils.isContentEmpty(message); |
519 |
1
|
assertTrue("The plaintext message contains a part that should be empty.", actual); |
520 |
|
} |
521 |
|
|
522 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1
PASS
|
|
523 |
1
|
public void testTextPartContentIsEmptyOnNullContent() throws Exception... |
524 |
|
{ |
525 |
1
|
final Message message = createMessageWithAttachment("text/plain", "", null, (String) null); |
526 |
1
|
final boolean actual = MailUtils.isContentEmpty(message); |
527 |
1
|
assertTrue("The plaintext message contains a part that should be empty.", actual); |
528 |
|
} |
529 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1
PASS
|
|
530 |
1
|
public void testTextPartContentIsEmptyBecauseContainsOnlyWhitespace() throws Exception... |
531 |
|
{ |
532 |
1
|
final Message message = createMessageWithAttachment("text/plain", "", null, " "); |
533 |
1
|
final boolean actual = MailUtils.isContentEmpty(message); |
534 |
1
|
assertTrue("The plaintext message contains a part that should be empty.", actual); |
535 |
|
} |
536 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1
PASS
|
|
537 |
1
|
public void testBinaryAttachmentPartContentIsEmpty() throws Exception... |
538 |
|
{ |
539 |
1
|
final Message message = createMessageWithAttachment("binary/octet-stream", "file.bin", Part.ATTACHMENT, new byte[0]); |
540 |
1
|
final boolean actual = MailUtils.isContentEmpty(message); |
541 |
1
|
assertTrue("The attachment part should be empty.", actual); |
542 |
|
} |
543 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1
PASS
|
|
544 |
1
|
public void testBinaryAttachmentPartContentIsNotEmpty() throws Exception... |
545 |
|
{ |
546 |
1
|
final Message message = createMessageWithAttachment("binary/octet-stream", "file.bin", Part.ATTACHMENT, " NOT EMPTY!!! ".getBytes()); |
547 |
1
|
final boolean actual = MailUtils.isContentEmpty(message); |
548 |
1
|
assertFalse("The attachment part should be empty.", actual); |
549 |
|
} |
550 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1
PASS
|
|
551 |
1
|
public void testInlineAttachment() throws Exception... |
552 |
|
{ |
553 |
1
|
final Message message = createMessageWithAttachment("image/jpeg", "image.jpeg", Part.INLINE, " NOT EMPTY!!! ".getBytes()); |
554 |
1
|
final boolean actual = MailUtils.isPartInline(message); |
555 |
1
|
assertFalse("The attachment part is an inline part.", actual); |
556 |
|
} |
557 |
|
|
558 |
|
|
559 |
|
|
560 |
|
|
561 |
|
|
562 |
|
@throws |
563 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1
PASS
|
|
564 |
1
|
public void testInlineAttachmentThatIsntBase64Encoded() throws Exception... |
565 |
|
{ |
566 |
1
|
final Message message = createMessageWithAttachment("image/jpeg", "image.jpeg", null, " NOT EMPTY!!! ".getBytes()); |
567 |
1
|
message.setHeader("Content-ID", "1234567890"); |
568 |
1
|
final boolean actual = MailUtils.isPartInline(message); |
569 |
1
|
assertFalse("The attachment part isnt not base64 encoded.", actual); |
570 |
|
} |
571 |
|
|
572 |
|
|
573 |
|
|
574 |
|
|
575 |
|
@param |
576 |
|
@param |
577 |
|
@param |
578 |
|
@param |
579 |
|
@return |
580 |
|
@throws |
581 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 3 |
Complexity Density: 0.33 |
|
582 |
5
|
Message createMessageWithAttachment(final String mimeType, final String disposition, final String filename, final String content)... |
583 |
|
throws Exception |
584 |
|
{ |
585 |
5
|
assertNotNull("mimeType", mimeType); |
586 |
5
|
assertTrue("mimeType must not be empty", mimeType.length() > 0); |
587 |
|
|
588 |
5
|
final Message message = new MimeMessage(Session.getDefaultInstance(new Properties())); |
589 |
5
|
message.setContent(content, mimeType); |
590 |
5
|
if (null != filename && filename.length() > 0) |
591 |
|
{ |
592 |
1
|
message.setFileName(filename); |
593 |
|
} |
594 |
5
|
message.setHeader("Content-Type", mimeType); |
595 |
5
|
message.setDisposition(disposition); |
596 |
5
|
return message; |
597 |
|
} |
598 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 3 |
Complexity Density: 0.33 |
|
599 |
4
|
Message createMessageWithAttachment(final String mimeType, final String disposition, final String filename, final byte[] content)... |
600 |
|
throws Exception |
601 |
|
{ |
602 |
4
|
assertNotNull("mimeType", mimeType); |
603 |
4
|
assertTrue("mimeType must not be empty", mimeType.length() > 0); |
604 |
|
|
605 |
4
|
final Message message = new MimeMessage(Session.getDefaultInstance(new Properties())); |
606 |
4
|
message.setContent(new ByteArrayInputStream(content), mimeType); |
607 |
4
|
if (null != filename && filename.length() > 0) |
608 |
|
{ |
609 |
3
|
message.setFileName(filename); |
610 |
|
} |
611 |
4
|
message.setHeader("Content-Type", mimeType); |
612 |
4
|
message.setDisposition(disposition); |
613 |
4
|
return message; |
614 |
|
} |
615 |
|
} |