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

File AttachmentMimeBodyPartFactoryTest.java

 

Code metrics

0
33
3
1
129
74
4
0.12
11
3
1.33

Classes

Class Line # Actions
AttachmentMimeBodyPartFactoryTest 50 33 0% 4 1
0.972222297.2%
 

Contributing tests

This file is covered by 3 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.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    * Unit tests for {@link org.xwiki.mail.internal.factory.attachment.AttachmentMimeBodyPartFactory}.
46    *
47    * @version $Id: 14572c222d8346fb0d55fa60b326e11f9443e47d $
48    * @since 6.1M2
49    */
 
50    public class AttachmentMimeBodyPartFactoryTest
51    {
52    // Passed at the Maven level in the pom.xml file.
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   
 
60  1 toggle @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    // JavaMail adds some extra params to the content-type header
76    // (e.g. image/png; name=image.png) , we just verify the content type that we passed.
77  1 assertTrue(part.getContentType().startsWith("image/png"));
78    // We verify that the Content-Disposition has the correct file namr
79  1 assertTrue(part.getFileName().matches("image\\.png"));
80   
81  1 assertEquals("Lorem Ipsum", IOUtils.toString(part.getDataHandler().getInputStream()));
82    }
83   
 
84  1 toggle @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    // JavaMail adds some extra params to the content-type header
102    // (e.g. image/png; name=image.png) , we just verify the content type that we passed.
103  1 assertTrue(part.getContentType().startsWith("image/png"));
104    // We verify that the Content-Disposition has the correct file namr
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   
 
112  1 toggle @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    }