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; |
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 |
|
@link |
57 |
|
|
58 |
|
@version |
59 |
|
@since |
60 |
|
|
|
|
| 79.8% |
Uncovered Elements: 20 (99) |
Complexity: 9 |
Complexity Density: 0.1 |
|
61 |
|
public class FileSystemMailContentStoreTest |
62 |
|
{ |
63 |
|
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
74 |
7 |
@Before... |
75 |
|
public void deleteMailStore() throws Exception |
76 |
|
{ |
77 |
|
|
78 |
7 |
FileUtils.deleteDirectory( |
79 |
|
new File(TEMPORARY_DIRECTORY, this.mocker.getComponentUnderTest().ROOT_DIRECTORY)); |
80 |
|
} |
81 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
82 |
7 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
89 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
112 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
136 |
1 |
@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 |
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
3FAIL
|
|
160 |
0 |
@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 |
this.mocker.getComponentUnderTest().save(batchId, message); |
179 |
|
} |
180 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (20) |
Complexity: 1 |
Complexity Density: 0.05 |
1PASS
|
|
181 |
1 |
@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 |
|
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
3FAIL
|
|
214 |
0 |
@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 |
MimeMessage message = this.mocker.getComponentUnderTest().load(session, batchId, messageId); |
226 |
0 |
fail("Should have thrown an exception here"); |
227 |
|
} |
228 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
1PASS
|
|
229 |
1 |
@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 |
|
} |