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.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 |
|
@link |
50 |
|
|
51 |
|
@version |
52 |
|
@since |
53 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (65) |
Complexity: 6 |
Complexity Density: 0.1 |
|
54 |
|
public class SerializedFilesMimeMessageIteratorTest |
55 |
|
{ |
56 |
|
@Rule |
57 |
|
public LogRule logRule = new LogRule() |
58 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
59 |
2 |
{... |
60 |
2 |
record(LogLevel.ERROR); |
61 |
2 |
recordLoggingForType(SerializedFilesMimeMessageIterator.class); |
62 |
|
} |
63 |
|
}; |
64 |
|
|
65 |
|
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
73 |
2 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
83 |
2 |
@After... |
84 |
|
public void tearDown() throws Exception |
85 |
|
{ |
86 |
|
|
87 |
2 |
FileUtils.deleteDirectory( |
88 |
|
new File(TEMPORARY_DIRECTORY, this.batchId)); |
89 |
|
} |
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
95 |
1 |
@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 |
|
|
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 |
|
|
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 |
|
|
118 |
|
|
119 |
|
|
120 |
1 |
assertNull(message); |
121 |
1 |
assertEquals("Failed to create Mime Message", this.logRule.getMessage(0)); |
122 |
|
} |
123 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (30) |
Complexity: 1 |
Complexity Density: 0.03 |
1PASS
|
|
124 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
|
170 |
4 |
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 |
|
|
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 |
|
} |