1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.mail.internal.factory.html

File HTMLMimeBodyPartFactoryTest.java

 

Code metrics

0
66
6
1
189
129
6
0.09
11
6
1

Classes

Class Line # Actions
HTMLMimeBodyPartFactoryTest 47 66 0% 6 0
1.0100%
 

Contributing tests

This file is covered by 6 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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    * Unit tests for {@link org.xwiki.mail.internal.factory.html.HTMLMimeBodyPartFactory}.
43    *
44    * @version $Id: 28fe8a3a9c697410e1bca2dd830b74fcd75aa7b0 $
45    * @since 6.1M2
46    */
 
47    public class HTMLMimeBodyPartFactoryTest
48    {
49    @Rule
50    public MockitoComponentMockingRule<MimeBodyPartFactory> mocker =
51    new MockitoComponentMockingRule<MimeBodyPartFactory>(HTMLMimeBodyPartFactory.class);
52   
 
53  1 toggle @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   
 
63  1 toggle @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   
 
73  1 toggle @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   
 
90  1 toggle @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   
 
109  1 toggle @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   
 
144  1 toggle @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    }