1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.mail.internal.factory.attachment; |
21 |
|
|
22 |
|
import java.io.File; |
23 |
|
import java.util.Collections; |
24 |
|
import java.util.Map; |
25 |
|
|
26 |
|
import javax.mail.MessagingException; |
27 |
|
import javax.mail.internet.MimeBodyPart; |
28 |
|
|
29 |
|
import org.apache.commons.io.IOUtils; |
30 |
|
import org.junit.Rule; |
31 |
|
import org.junit.Test; |
32 |
|
import org.xwiki.environment.Environment; |
33 |
|
import org.xwiki.test.mockito.MockitoComponentMockingRule; |
34 |
|
|
35 |
|
import com.xpn.xwiki.api.Attachment; |
36 |
|
|
37 |
|
import static org.junit.Assert.assertArrayEquals; |
38 |
|
import static org.junit.Assert.assertEquals; |
39 |
|
import static org.junit.Assert.assertTrue; |
40 |
|
import static org.junit.Assert.fail; |
41 |
|
import static org.mockito.Mockito.mock; |
42 |
|
import static org.mockito.Mockito.when; |
43 |
|
|
44 |
|
|
45 |
|
@link |
46 |
|
|
47 |
|
@version |
48 |
|
@since |
49 |
|
|
|
|
| 97.2% |
Uncovered Elements: 1 (36) |
Complexity: 4 |
Complexity Density: 0.12 |
|
50 |
|
public class AttachmentMimeBodyPartFactoryTest |
51 |
|
{ |
52 |
|
|
53 |
|
private static final String TEMPORARY_DIRECTORY = |
54 |
|
System.getProperty("temporaryDirectory", System.getProperty("java.io.tmpdir")); |
55 |
|
|
56 |
|
@Rule |
57 |
|
public MockitoComponentMockingRule<AttachmentMimeBodyPartFactory> mocker = |
58 |
|
new MockitoComponentMockingRule<>(AttachmentMimeBodyPartFactory.class); |
59 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
1PASS
|
|
60 |
1 |
@Test... |
61 |
|
public void createAttachmentBodyPart() throws Exception |
62 |
|
{ |
63 |
1 |
Environment environment = this.mocker.getInstance(Environment.class); |
64 |
1 |
when(environment.getTemporaryDirectory()).thenReturn(new File(TEMPORARY_DIRECTORY)); |
65 |
|
|
66 |
1 |
Attachment attachment = mock(Attachment.class); |
67 |
1 |
when(attachment.getContent()).thenReturn("Lorem Ipsum".getBytes()); |
68 |
1 |
when(attachment.getFilename()).thenReturn("image.png"); |
69 |
1 |
when(attachment.getMimeType()).thenReturn("image/png"); |
70 |
|
|
71 |
1 |
MimeBodyPart part = this.mocker.getComponentUnderTest().create(attachment, |
72 |
|
Collections.<String, Object>emptyMap()); |
73 |
|
|
74 |
1 |
assertEquals("<image.png>", part.getContentID()); |
75 |
|
|
76 |
|
|
77 |
1 |
assertTrue(part.getContentType().startsWith("image/png")); |
78 |
|
|
79 |
1 |
assertTrue(part.getFileName().matches("image\\.png")); |
80 |
|
|
81 |
1 |
assertEquals("Lorem Ipsum", IOUtils.toString(part.getDataHandler().getInputStream())); |
82 |
|
} |
83 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
84 |
1 |
@Test... |
85 |
|
public void createAttachmentBodyPartWithHeader() throws Exception |
86 |
|
{ |
87 |
1 |
Environment environment = this.mocker.getInstance(Environment.class); |
88 |
1 |
when(environment.getTemporaryDirectory()).thenReturn(new File(TEMPORARY_DIRECTORY)); |
89 |
|
|
90 |
1 |
Attachment attachment = mock(Attachment.class); |
91 |
1 |
when(attachment.getContent()).thenReturn("Lorem Ipsum".getBytes()); |
92 |
1 |
when(attachment.getFilename()).thenReturn("image.png"); |
93 |
1 |
when(attachment.getMimeType()).thenReturn("image/png"); |
94 |
|
|
95 |
1 |
Map<String, Object> parameters = Collections.singletonMap("headers", (Object) |
96 |
|
Collections.singletonMap("Content-Transfer-Encoding", "quoted-printable")); |
97 |
|
|
98 |
1 |
MimeBodyPart part = this.mocker.getComponentUnderTest().create(attachment, parameters); |
99 |
|
|
100 |
1 |
assertEquals("<image.png>", part.getContentID()); |
101 |
|
|
102 |
|
|
103 |
1 |
assertTrue(part.getContentType().startsWith("image/png")); |
104 |
|
|
105 |
1 |
assertTrue(part.getFileName().matches("image\\.png")); |
106 |
|
|
107 |
1 |
assertArrayEquals(new String[]{ "quoted-printable" }, part.getHeader("Content-Transfer-Encoding")); |
108 |
|
|
109 |
1 |
assertEquals("Lorem Ipsum", IOUtils.toString(part.getDataHandler().getInputStream())); |
110 |
|
} |
111 |
|
|
|
|
| 88.9% |
Uncovered Elements: 1 (9) |
Complexity: 2 |
Complexity Density: 0.22 |
1PASS
|
|
112 |
1 |
@Test... |
113 |
|
public void createAttachmentBodyPartWhenWriteError() throws Exception |
114 |
|
{ |
115 |
1 |
Environment environment = this.mocker.getInstance(Environment.class); |
116 |
1 |
when(environment.getTemporaryDirectory()).thenReturn(new File(TEMPORARY_DIRECTORY)); |
117 |
|
|
118 |
1 |
Attachment attachment = mock(Attachment.class); |
119 |
1 |
when(attachment.getFilename()).thenReturn("image.png"); |
120 |
1 |
when(attachment.getContent()).thenThrow(new RuntimeException("error")); |
121 |
|
|
122 |
1 |
try { |
123 |
1 |
this.mocker.getComponentUnderTest().create(attachment, Collections.<String, Object>emptyMap()); |
124 |
0 |
fail("Should have thrown an exception here!"); |
125 |
|
} catch (MessagingException expected) { |
126 |
1 |
assertEquals("Failed to save attachment [image.png] to the file system", expected.getMessage()); |
127 |
|
} |
128 |
|
} |
129 |
|
} |