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.html; |
21 |
|
|
22 |
|
import java.util.Arrays; |
23 |
|
import java.util.Collections; |
24 |
|
import java.util.HashMap; |
25 |
|
import java.util.Map; |
26 |
|
|
27 |
|
import javax.mail.internet.MimeBodyPart; |
28 |
|
import javax.mail.internet.MimeMultipart; |
29 |
|
|
30 |
|
import org.junit.Rule; |
31 |
|
import org.junit.Test; |
32 |
|
import org.xwiki.component.util.DefaultParameterizedType; |
33 |
|
import org.xwiki.mail.MimeBodyPartFactory; |
34 |
|
import org.xwiki.test.mockito.MockitoComponentMockingRule; |
35 |
|
|
36 |
|
import com.xpn.xwiki.api.Attachment; |
37 |
|
|
38 |
|
import static org.junit.Assert.*; |
39 |
|
import static org.mockito.Mockito.*; |
40 |
|
|
41 |
|
|
42 |
|
@link |
43 |
|
|
44 |
|
@version |
45 |
|
@since |
46 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (72) |
Complexity: 6 |
Complexity Density: 0.09 |
|
47 |
|
public class HTMLMimeBodyPartFactoryTest |
48 |
|
{ |
49 |
|
@Rule |
50 |
|
public MockitoComponentMockingRule<MimeBodyPartFactory> mocker = |
51 |
|
new MockitoComponentMockingRule<MimeBodyPartFactory>(HTMLMimeBodyPartFactory.class); |
52 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
53 |
1 |
@Test... |
54 |
|
public void createWhenOnlyHTMLContent() throws Exception |
55 |
|
{ |
56 |
1 |
MimeBodyPart bodyPart = this.mocker.getComponentUnderTest().create("<p>some html</p>", |
57 |
|
Collections.<String, Object>emptyMap()); |
58 |
|
|
59 |
1 |
assertEquals("<p>some html</p>", bodyPart.getContent()); |
60 |
1 |
assertEquals("text/html; charset=UTF-8", bodyPart.getContentType()); |
61 |
|
} |
62 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
63 |
1 |
@Test... |
64 |
|
public void createWhenHTMLContentAndHeader() throws Exception |
65 |
|
{ |
66 |
1 |
MimeBodyPart bodyPart = this.mocker.getComponentUnderTest().create("<p>some html</p>", |
67 |
|
Collections.<String, Object>singletonMap("headers", Collections.singletonMap("key", "value"))); |
68 |
|
|
69 |
1 |
assertEquals("<p>some html</p>", bodyPart.getContent()); |
70 |
1 |
assertArrayEquals(new String[]{ "value" }, bodyPart.getHeader("key")); |
71 |
|
} |
72 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
1PASS
|
|
73 |
1 |
@Test... |
74 |
|
public void createWhenHTMLAndAlternateTextContent() throws Exception |
75 |
|
{ |
76 |
1 |
MimeBodyPart textBodyPart = mock(MimeBodyPart.class); |
77 |
1 |
MimeBodyPartFactory defaultBodyPartFactory = this.mocker.getInstance( |
78 |
|
new DefaultParameterizedType(null, MimeBodyPartFactory.class, String.class)); |
79 |
1 |
when(defaultBodyPartFactory.create(eq("some text"), any(Map.class))).thenReturn(textBodyPart); |
80 |
|
|
81 |
1 |
MimeBodyPart bodyPart = this.mocker.getComponentUnderTest().create("<p>some html</p>", |
82 |
|
Collections.<String, Object>singletonMap("alternate", "some text")); |
83 |
|
|
84 |
1 |
MimeMultipart multipart = ((MimeMultipart) bodyPart.getContent()); |
85 |
1 |
assertEquals(2, multipart.getCount()); |
86 |
1 |
assertSame(textBodyPart, multipart.getBodyPart(0)); |
87 |
1 |
assertEquals("<p>some html</p>", multipart.getBodyPart(1).getContent()); |
88 |
|
} |
89 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
1PASS
|
|
90 |
1 |
@Test... |
91 |
|
public void createWhenHTMLAndEmbeddedImages() throws Exception |
92 |
|
{ |
93 |
1 |
Attachment attachment = mock(Attachment.class); |
94 |
|
|
95 |
1 |
MimeBodyPart attachmentBodyPart = mock(MimeBodyPart.class); |
96 |
1 |
MimeBodyPartFactory attachmentBodyPartFactory = this.mocker.getInstance( |
97 |
|
new DefaultParameterizedType(null, MimeBodyPartFactory.class, Attachment.class), "xwiki/attachment"); |
98 |
1 |
when(attachmentBodyPartFactory.create(same(attachment), any(Map.class))).thenReturn(attachmentBodyPart); |
99 |
|
|
100 |
1 |
MimeBodyPart bodyPart = this.mocker.getComponentUnderTest().create("<p>some html</p>", |
101 |
|
Collections.<String, Object>singletonMap("attachments", Arrays.asList(attachment))); |
102 |
|
|
103 |
1 |
MimeMultipart multipart = ((MimeMultipart) bodyPart.getContent()); |
104 |
1 |
assertEquals(2, multipart.getCount()); |
105 |
1 |
assertEquals("<p>some html</p>", multipart.getBodyPart(0).getContent()); |
106 |
1 |
assertSame(attachmentBodyPart, multipart.getBodyPart(1)); |
107 |
|
} |
108 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (17) |
Complexity: 1 |
Complexity Density: 0.06 |
1PASS
|
|
109 |
1 |
@Test... |
110 |
|
public void createWhenHTMLAndEmbeddedImagesAndNormalAttachments() throws Exception |
111 |
|
{ |
112 |
1 |
Attachment normalAttachment = mock(Attachment.class, "normalAttachment"); |
113 |
1 |
when(normalAttachment.getFilename()).thenReturn("attachment1.png"); |
114 |
1 |
Attachment embeddedAttachment = mock(Attachment.class, "embeddedAttachment"); |
115 |
1 |
when(embeddedAttachment.getFilename()).thenReturn("embeddedAttachment.png"); |
116 |
|
|
117 |
1 |
MimeBodyPart embeddedAttachmentBodyPart = mock(MimeBodyPart.class); |
118 |
1 |
MimeBodyPartFactory attachmentBodyPartFactory = this.mocker.getInstance( |
119 |
|
new DefaultParameterizedType(null, MimeBodyPartFactory.class, Attachment.class), "xwiki/attachment"); |
120 |
1 |
when(attachmentBodyPartFactory.create(same(embeddedAttachment), any(Map.class))).thenReturn( |
121 |
|
embeddedAttachmentBodyPart); |
122 |
|
|
123 |
1 |
MimeBodyPart normalAttachmentBodyPart = mock(MimeBodyPart.class); |
124 |
1 |
when(attachmentBodyPartFactory.create(same(normalAttachment), any(Map.class))).thenReturn( |
125 |
|
normalAttachmentBodyPart); |
126 |
|
|
127 |
1 |
MimeBodyPart bodyPart = this.mocker.getComponentUnderTest().create( |
128 |
|
"<p>html... <img src='cid:embeddedAttachment.png'/></p>", |
129 |
|
Collections.<String, Object>singletonMap("attachments", |
130 |
|
Arrays.asList(normalAttachment, embeddedAttachment))); |
131 |
|
|
132 |
1 |
MimeMultipart multipart = (MimeMultipart) bodyPart.getContent(); |
133 |
1 |
assertEquals(2, multipart.getCount()); |
134 |
|
|
135 |
1 |
MimeMultipart htmlMultipart = (MimeMultipart) multipart.getBodyPart(0).getContent(); |
136 |
1 |
assertEquals(2, htmlMultipart.getCount()); |
137 |
1 |
assertEquals("<p>html... <img src='cid:embeddedAttachment.png'/></p>", |
138 |
|
htmlMultipart.getBodyPart(0).getContent()); |
139 |
1 |
assertSame(embeddedAttachmentBodyPart, htmlMultipart.getBodyPart(1)); |
140 |
|
|
141 |
1 |
assertSame(normalAttachmentBodyPart, multipart.getBodyPart(1)); |
142 |
|
} |
143 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (26) |
Complexity: 1 |
Complexity Density: 0.04 |
1PASS
|
|
144 |
1 |
@Test... |
145 |
|
public void createWhenHTMLAndAlternateTextAndEmbeddedImagesAndNormalAttachments() throws Exception |
146 |
|
{ |
147 |
1 |
Attachment normalAttachment = mock(Attachment.class, "normalAttachment"); |
148 |
1 |
when(normalAttachment.getFilename()).thenReturn("attachment1.png"); |
149 |
1 |
Attachment embeddedAttachment = mock(Attachment.class, "embeddedAttachment"); |
150 |
1 |
when(embeddedAttachment.getFilename()).thenReturn("embeddedAttachment.png"); |
151 |
|
|
152 |
1 |
MimeBodyPart embeddedAttachmentBodyPart = mock(MimeBodyPart.class); |
153 |
1 |
MimeBodyPartFactory attachmentBodyPartFactory = this.mocker.getInstance( |
154 |
|
new DefaultParameterizedType(null, MimeBodyPartFactory.class, Attachment.class), "xwiki/attachment"); |
155 |
1 |
when(attachmentBodyPartFactory.create(same(embeddedAttachment), any(Map.class))).thenReturn( |
156 |
|
embeddedAttachmentBodyPart); |
157 |
|
|
158 |
1 |
MimeBodyPart normalAttachmentBodyPart = mock(MimeBodyPart.class); |
159 |
1 |
when(attachmentBodyPartFactory.create(same(normalAttachment), any(Map.class))).thenReturn( |
160 |
|
normalAttachmentBodyPart); |
161 |
|
|
162 |
1 |
MimeBodyPart textBodyPart = mock(MimeBodyPart.class); |
163 |
1 |
MimeBodyPartFactory defaultBodyPartFactory = this.mocker.getInstance( |
164 |
|
new DefaultParameterizedType(null, MimeBodyPartFactory.class, String.class)); |
165 |
1 |
when(defaultBodyPartFactory.create(eq("some text"), any(Map.class))).thenReturn(textBodyPart); |
166 |
|
|
167 |
1 |
Map<String, Object> parameters = new HashMap<>(); |
168 |
1 |
parameters.put("attachments", Arrays.asList(normalAttachment, embeddedAttachment)); |
169 |
1 |
parameters.put("alternate", "some text"); |
170 |
1 |
MimeBodyPart bodyPart = this.mocker.getComponentUnderTest().create( |
171 |
|
"<p>html... <img src='cid:embeddedAttachment.png'/></p>", parameters); |
172 |
|
|
173 |
1 |
MimeMultipart multipart = (MimeMultipart) bodyPart.getContent(); |
174 |
1 |
assertEquals(2, multipart.getCount()); |
175 |
|
|
176 |
1 |
MimeMultipart alternateMultipart = (MimeMultipart) multipart.getBodyPart(0).getContent(); |
177 |
1 |
assertEquals(2, alternateMultipart.getCount()); |
178 |
1 |
assertSame(textBodyPart, alternateMultipart.getBodyPart(0)); |
179 |
|
|
180 |
1 |
MimeMultipart relatedMultipart = (MimeMultipart) alternateMultipart.getBodyPart(1).getContent(); |
181 |
1 |
assertEquals(2, relatedMultipart.getCount()); |
182 |
1 |
assertEquals("<p>html... <img src='cid:embeddedAttachment.png'/></p>", |
183 |
|
relatedMultipart.getBodyPart(0).getContent()); |
184 |
1 |
assertSame(embeddedAttachmentBodyPart, relatedMultipart.getBodyPart(1)); |
185 |
|
|
186 |
1 |
assertSame(normalAttachmentBodyPart, multipart.getBodyPart(1)); |
187 |
|
} |
188 |
|
|
189 |
|
} |