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

File SerializedFilesMimeMessageIteratorTest.java

 

Code metrics

0
59
6
1
185
117
6
0.1
9.83
6
1

Classes

Class Line # Actions
SerializedFilesMimeMessageIteratorTest 54 59 0% 6 0
1.0100%
 

Contributing tests

This file is covered by 2 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.files;
21   
22    import java.io.File;
23    import java.io.FileWriter;
24    import java.io.IOException;
25    import java.net.URLEncoder;
26    import java.util.ArrayList;
27    import java.util.Collections;
28    import java.util.HashMap;
29    import java.util.Map;
30    import java.util.UUID;
31   
32    import javax.mail.internet.MimeMessage;
33   
34    import org.apache.commons.io.FileUtils;
35    import org.junit.After;
36    import org.junit.Before;
37    import org.junit.Rule;
38    import org.junit.Test;
39    import org.xwiki.component.manager.ComponentManager;
40    import org.xwiki.environment.Environment;
41    import org.xwiki.test.LogRule;
42   
43    import static org.junit.Assert.*;
44    import static org.mockito.ArgumentMatchers.eq;
45    import static org.mockito.Mockito.mock;
46    import static org.mockito.Mockito.when;
47   
48    /**
49    * Unit tests for {@link org.xwiki.mail.internal.factory.files.SerializedFilesMimeMessageIterator}.
50    *
51    * @version $Id: ca383e18d5186cb5451fcbb470859d47065ff8e7 $
52    * @since 6.4M3
53    */
 
54    public class SerializedFilesMimeMessageIteratorTest
55    {
56    @Rule
57    public LogRule logRule = new LogRule()
58    {
 
59  2 toggle {
60  2 record(LogLevel.ERROR);
61  2 recordLoggingForType(SerializedFilesMimeMessageIterator.class);
62    }
63    };
64   
65    // Passed at the Maven level in the pom.xml file.
66    private static final String TEMPORARY_DIRECTORY =
67    System.getProperty("temporaryDirectory", System.getProperty("java.io.tmpdir"));
68   
69    private String batchId;
70   
71    private File batchDirectory;
72   
 
73  2 toggle @Before
74    public void setUp() throws Exception
75    {
76  2 this.batchId = UUID.randomUUID().toString();
77   
78  2 File tempDir = new File(TEMPORARY_DIRECTORY);
79  2 this.batchDirectory = new File(new File(tempDir, "mailstore"), URLEncoder.encode(this.batchId, "UTF-8"));
80  2 this.batchDirectory.mkdirs();
81    }
82   
 
83  2 toggle @After
84    public void tearDown() throws Exception
85    {
86    // Delete created messages and directories
87  2 FileUtils.deleteDirectory(
88    new File(TEMPORARY_DIRECTORY, this.batchId));
89    }
90   
91    /**
92    * Error that can happen if the file has been locally deleted between the time the time the user executes a
93    * resend and the time the Mail Sender Thread reaches that file for processing (i.e. deserializing it).
94    */
 
95  1 toggle @Test
96    public void createMessageWhenFileNoLongerExists() throws Exception
97    {
98  1 Environment environment = mock(Environment.class);
99  1 when(environment.getPermanentDirectory()).thenReturn(new File(TEMPORARY_DIRECTORY));
100   
101  1 ComponentManager componentManager = mock(ComponentManager.class);
102  1 when(componentManager.getInstance(eq(Environment.class))).thenReturn(environment);
103   
104    // Create a serialized file before the iterator is initialized
105  1 String mailID = "<1128820400.0.1419205781342.JavaMail.contact@xwiki.org>";
106  1 createSerializedMessage(mailID);
107   
108  1 SerializedFilesMimeMessageIterator iterator = new SerializedFilesMimeMessageIterator(this.batchId,
109    Collections.<String, Object>emptyMap(), componentManager);
110   
111    // Remove the file before next() is called to generate the error
112  1 File messageFile = new File(this.batchDirectory, URLEncoder.encode(mailID, "UTF-8"));
113  1 messageFile.delete();
114   
115  1 MimeMessage message = iterator.next();
116   
117    // Verify that:
118    // 1) the returned message is null since there was an error
119    // 2) that the log contains the error
120  1 assertNull(message);
121  1 assertEquals("Failed to create Mime Message", this.logRule.getMessage(0));
122    }
123   
 
124  1 toggle @Test
125    public void createMessage() throws Exception
126    {
127  1 String mailID1 = "<1128820400.0.1419205781342.JavaMail.contact@xwiki.org>";
128  1 String mailID2 = "<1128820400.1.1419205781342.JavaMail.contact@xwiki.org>";
129  1 String mailID3 = "<1128820400.2.1419205781342.JavaMail.contact@xwiki.org>";
130   
131  1 createSerializedMessage(mailID1);
132  1 createSerializedMessage(mailID2);
133  1 createSerializedMessage(mailID3);
134   
135  1 Map<String, Object> parameters = new HashMap<>();
136  1 parameters.put("parameters", Collections.EMPTY_MAP);
137   
138  1 Environment environment = mock(Environment.class);
139  1 when(environment.getPermanentDirectory()).thenReturn(new File(TEMPORARY_DIRECTORY));
140   
141  1 ComponentManager componentManager = mock(ComponentManager.class);
142  1 when(componentManager.getInstance(eq(Environment.class))).thenReturn(environment);
143   
144  1 SerializedFilesMimeMessageIterator iterator =
145    new SerializedFilesMimeMessageIterator(this.batchId, parameters, componentManager);
146   
147  1 ArrayList<String> listID = new ArrayList<>();
148  1 listID.add(mailID1);
149  1 listID.add(mailID2);
150  1 listID.add(mailID3);
151   
152  1 assertTrue(iterator.hasNext());
153  1 MimeMessage message1 = iterator.next();
154  1 assertTrue(listID.contains(message1.getMessageID()));
155  1 listID.remove(message1.getMessageID());
156   
157  1 assertTrue(iterator.hasNext());
158  1 MimeMessage message2 = iterator.next();
159  1 assertTrue(listID.contains(message2.getMessageID()));
160  1 listID.remove(message1.getMessageID());
161   
162  1 assertTrue(iterator.hasNext());
163  1 MimeMessage message3 = iterator.next();
164  1 assertTrue(listID.contains(message2.getMessageID()));
165  1 listID.remove(message3.getMessageID());
166   
167  1 assertFalse(iterator.hasNext());
168    }
169   
 
170  4 toggle private void createSerializedMessage(String messageId) throws IOException
171    {
172  4 File messageFile = new File(this.batchDirectory, URLEncoder.encode(messageId, "UTF-8"));
173  4 messageFile.createNewFile();
174  4 String newLine = System.getProperty("line.separator");
175   
176  4 FileWriter fileWriter = new FileWriter(messageFile, true);
177    // Unique string is <hashcode>.<id>.<currentTime>.JavaMail.<suffix>
178  4 fileWriter.append("Message-ID: " + messageId + newLine);
179  4 fileWriter.append("MIME-Version: 1.0" + newLine);
180  4 fileWriter.append("Content-Type: text/plain; charset=us-ascii" + newLine);
181  4 fileWriter.append("Content-Transfer-Encoding: 7bit" + newLine);
182  4 fileWriter.append("Lorem ipsum dolor sit amet, consectetur adipiscing elit");
183  4 fileWriter.close();
184    }
185    }