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

File FileSystemMailContentStoreTest.java

 
saveMessageThrowsMailStoreExceptionWhenError: Failed to save message (id [ar1vm0Wca42E/dDn3dsH8ogs3/s=]...
loadMessageThrowsMailStoreExceptionWhenError: Failed to load message (id [ar1vm0Wca42E/dDn3dsH8ogs3/s=]...
 

Code metrics

0
90
9
1
249
178
9
0.1
10
9
1

Classes

Class Line # Actions
FileSystemMailContentStoreTest 61 90 0% 9 20
0.797979879.8%
 

Contributing tests

This file is covered by 7 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;
21   
22    import java.io.File;
23    import java.io.FileInputStream;
24    import java.io.FileWriter;
25    import java.io.IOException;
26    import java.io.InputStream;
27    import java.io.OutputStream;
28    import java.net.URLEncoder;
29    import java.util.Properties;
30    import java.util.UUID;
31   
32    import javax.mail.Session;
33    import javax.mail.internet.MimeMessage;
34   
35    import org.apache.commons.io.FileUtils;
36    import org.apache.commons.io.IOUtils;
37    import org.junit.Before;
38    import org.junit.Rule;
39    import org.junit.Test;
40    import org.junit.rules.ExpectedException;
41    import org.xwiki.environment.Environment;
42    import org.xwiki.mail.ExtendedMimeMessage;
43    import org.xwiki.mail.MailStoreException;
44    import org.xwiki.test.annotation.BeforeComponent;
45    import org.xwiki.test.mockito.MockitoComponentMockingRule;
46   
47    import static org.junit.Assert.assertEquals;
48    import static org.junit.Assert.assertTrue;
49    import static org.junit.Assert.fail;
50    import static org.mockito.ArgumentMatchers.any;
51    import static org.mockito.Mockito.doThrow;
52    import static org.mockito.Mockito.mock;
53    import static org.mockito.Mockito.when;
54   
55    /**
56    * Unit tests for {@link FileSystemMailContentStore}.
57    *
58    * @version $Id: 2be050b456d8cb953404afbe436f3b5f8e1a7a2a $
59    * @since 6.4M3
60    */
 
61    public class FileSystemMailContentStoreTest
62    {
63    // Passed at the Maven level in the pom.xml file.
64    private static final String TEMPORARY_DIRECTORY =
65    System.getProperty("temporaryDirectory", System.getProperty("java.io.tmpdir"));
66   
67    @Rule
68    public MockitoComponentMockingRule<FileSystemMailContentStore> mocker =
69    new MockitoComponentMockingRule<>(FileSystemMailContentStore.class);
70   
71    @Rule
72    public ExpectedException thrown = ExpectedException.none();
73   
 
74  7 toggle @Before
75    public void deleteMailStore() throws Exception
76    {
77    // Delete content of the mails store directory
78  7 FileUtils.deleteDirectory(
79    new File(TEMPORARY_DIRECTORY, this.mocker.getComponentUnderTest().ROOT_DIRECTORY));
80    }
81   
 
82  7 toggle @BeforeComponent
83    public void registerMockComponents() throws Exception
84    {
85  7 Environment environment = this.mocker.registerMockComponent(Environment.class);
86  7 when(environment.getPermanentDirectory()).thenReturn(new File(TEMPORARY_DIRECTORY));
87    }
88   
 
89  1 toggle @Test
90    public void saveMessage() throws Exception
91    {
92  1 String batchId = UUID.randomUUID().toString();
93   
94  1 ExtendedMimeMessage message = new ExtendedMimeMessage();
95  1 message.setText("Lorem ipsum dolor sit amet, consectetur adipiscing elit");
96   
97  1 this.mocker.getComponentUnderTest().save(batchId, message);
98  1 String messageId = message.getMessageID();
99   
100  1 File tempDir = new File(TEMPORARY_DIRECTORY);
101  1 File batchDirectory =
102    new File(new File(tempDir, this.mocker.getComponentUnderTest().ROOT_DIRECTORY),
103    URLEncoder.encode(batchId, "UTF-8"));
104  1 File messageFile = new File(batchDirectory, URLEncoder.encode(message.getUniqueMessageId(), "UTF-8"));
105  1 InputStream in = new FileInputStream(messageFile);
106  1 String messageContent = IOUtils.toString(in);
107   
108  1 assertTrue(messageContent.contains("Message-ID: " + messageId));
109  1 assertTrue(messageContent.contains("Lorem ipsum dolor sit amet, consectetur adipiscing elit"));
110    }
111   
 
112  1 toggle @Test
113    public void saveMessageWithCustomMessageId() throws Exception
114    {
115  1 String batchId = UUID.randomUUID().toString();
116  1 String mimeMessageId = "<1128820400.0.1419205781342.JavaMail.contact@xwiki.org>";
117   
118  1 ExtendedMimeMessage message = new ExtendedMimeMessage();
119  1 message.setMessageId(mimeMessageId);
120  1 message.setText("Lorem ipsum dolor sit amet, consectetur adipiscing elit");
121   
122  1 this.mocker.getComponentUnderTest().save(batchId, message);
123   
124  1 File tempDir = new File(TEMPORARY_DIRECTORY);
125  1 File batchDirectory =
126    new File(new File(tempDir, this.mocker.getComponentUnderTest().ROOT_DIRECTORY),
127    URLEncoder.encode(batchId, "UTF-8"));
128  1 File messageFile = new File(batchDirectory, URLEncoder.encode(message.getUniqueMessageId(), "UTF-8"));
129  1 InputStream in = new FileInputStream(messageFile);
130  1 String messageContent = IOUtils.toString(in);
131   
132  1 assertTrue(messageContent.contains("Message-ID: " + mimeMessageId));
133  1 assertTrue(messageContent.contains("Lorem ipsum dolor sit amet, consectetur adipiscing elit"));
134    }
135   
 
136  1 toggle @Test
137    public void saveMessageWhenInstableCustomMessageID() throws Exception
138    {
139  1 String batchId = UUID.randomUUID().toString();
140  1 String mimeMessageId = "<1128820400.0.1419205781342.JavaMail.contact@xwiki.org>";
141   
142  1 ExtendedMimeMessage message = new ExtendedMimeMessage();
143  1 message.setHeader("Message-ID", mimeMessageId);
144  1 message.setText("Lorem ipsum dolor sit amet, consectetur adipiscing elit");
145   
146  1 this.mocker.getComponentUnderTest().save(batchId, message);
147   
148  1 File tempDir = new File(TEMPORARY_DIRECTORY);
149  1 File batchDirectory =
150    new File(new File(tempDir, this.mocker.getComponentUnderTest().ROOT_DIRECTORY),
151    URLEncoder.encode(batchId, "UTF-8"));
152  1 File messageFile = new File(batchDirectory, URLEncoder.encode(message.getUniqueMessageId(), "UTF-8"));
153  1 InputStream in = new FileInputStream(messageFile);
154  1 String messageContent = IOUtils.toString(in);
155   
156  1 assertTrue(messageContent.contains("Message-ID: " + message.getMessageID()));
157  1 assertTrue(messageContent.contains("Lorem ipsum dolor sit amet, consectetur adipiscing elit"));
158    }
159   
 
160  0 toggle @Test
161    public void saveMessageThrowsMailStoreExceptionWhenError() throws Exception
162    {
163  0 Environment environment = this.mocker.getInstance(Environment.class);
164  0 when(environment.getPermanentDirectory()).thenReturn(new File(TEMPORARY_DIRECTORY));
165   
166  0 String batchId = UUID.randomUUID().toString();
167  0 String messageId = "ar1vm0Wca42E/dDn3dsH8ogs3/s=";
168   
169  0 ExtendedMimeMessage message = mock(ExtendedMimeMessage.class);
170  0 when(message.getUniqueMessageId()).thenReturn(messageId);
171   
172  0 this.thrown.expect(MailStoreException.class);
173  0 this.thrown.expectMessage(
174    "Failed to save message (id [" + messageId + "], batch id [" + batchId + "]) into file");
175   
176  0 when(message.getContent()).thenReturn("Lorem ipsum dolor sit amet, consectetur adipiscing elit");
177  0 doThrow(new IOException()).when(message).writeTo(any(OutputStream.class));
178  0 Test failure here this.mocker.getComponentUnderTest().save(batchId, message);
179    }
180   
 
181  1 toggle @Test
182    public void loadMessage() throws Exception
183    {
184  1 String batchId = UUID.randomUUID().toString();
185  1 String messageId = "ar1vm0Wca42E/dDn3dsH8ogs3/s=";
186  1 String mimeMessageId = "<1128820400.0.1419205781342.JavaMail.contact@xwiki.org>";
187   
188  1 File tempDir = new File(TEMPORARY_DIRECTORY);
189  1 File batchDirectory =
190    new File(new File(tempDir, this.mocker.getComponentUnderTest().ROOT_DIRECTORY),
191    URLEncoder.encode(batchId,"UTF-8"));
192  1 batchDirectory.mkdirs();
193  1 File messageFile = new File(batchDirectory, URLEncoder.encode(messageId,"UTF-8"));
194  1 messageFile.createNewFile();
195   
196  1 String newLine = System.getProperty("line.separator");
197   
198  1 FileWriter fileWriter = new FileWriter(messageFile, true);
199    // Unique string is <hashcode>.<id>.<currentTime>.JavaMail.<suffix>
200  1 fileWriter.append("Message-ID: " + mimeMessageId + newLine);
201  1 fileWriter.append("MIME-Version: 1.0" + newLine);
202  1 fileWriter.append("Content-Type: text/plain; charset=us-ascii" + newLine);
203  1 fileWriter.append("Content-Transfer-Encoding: 7bit" + newLine + newLine);
204  1 fileWriter.append("Lorem ipsum dolor sit amet, consectetur adipiscing elit");
205  1 fileWriter.close();
206   
207  1 Session session = Session.getInstance(new Properties());
208  1 MimeMessage message = this.mocker.getComponentUnderTest().load(session, batchId, messageId);
209   
210  1 assertEquals(mimeMessageId, message.getMessageID());
211  1 assertEquals("Lorem ipsum dolor sit amet, consectetur adipiscing elit", message.getContent());
212    }
213   
 
214  0 toggle @Test
215    public void loadMessageThrowsMailStoreExceptionWhenError() throws Exception
216    {
217  0 String batchId = UUID.randomUUID().toString();
218  0 String messageId = "ar1vm0Wca42E/dDn3dsH8ogs3/s=";
219  0 Session session = Session.getInstance(new Properties());
220   
221  0 this.thrown.expect(MailStoreException.class);
222  0 this.thrown.expectMessage(
223    "Failed to load message (id [" + messageId + "], batch id [" + batchId + "]) from file");
224   
225  0 Test failure here MimeMessage message = this.mocker.getComponentUnderTest().load(session, batchId, messageId);
226  0 fail("Should have thrown an exception here");
227    }
228   
 
229  1 toggle @Test
230    public void deleteMessage() throws Exception
231    {
232  1 Environment environment = this.mocker.getInstance(Environment.class);
233  1 when(environment.getPermanentDirectory()).thenReturn(new File(TEMPORARY_DIRECTORY));
234   
235  1 String batchId = UUID.randomUUID().toString();
236  1 String messageId = "ar1vm0Wca42E/dDn3dsH8ogs3/s=";
237   
238  1 File tempDir = new File(TEMPORARY_DIRECTORY);
239  1 File batchDirectory =
240    new File(new File(tempDir, this.mocker.getComponentUnderTest().ROOT_DIRECTORY), URLEncoder.encode(batchId, "UTF-8"));
241  1 batchDirectory.mkdirs();
242  1 File messageFile = new File(batchDirectory, URLEncoder.encode(messageId, "UTF-8"));
243  1 messageFile.createNewFile();
244   
245  1 this.mocker.getComponentUnderTest().delete(batchId, messageId);
246   
247  1 assertTrue(!messageFile.exists());
248    }
249    }