1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.store.legacy.store.internal; |
21 |
|
|
22 |
|
import java.io.ByteArrayInputStream; |
23 |
|
import java.io.ByteArrayOutputStream; |
24 |
|
import java.io.File; |
25 |
|
import java.io.FileInputStream; |
26 |
|
import java.io.FileOutputStream; |
27 |
|
import java.io.IOException; |
28 |
|
import java.io.InputStream; |
29 |
|
import java.io.OutputStream; |
30 |
|
import java.io.UnsupportedEncodingException; |
31 |
|
import java.util.ArrayList; |
32 |
|
import java.util.List; |
33 |
|
|
34 |
|
import org.xwiki.store.legacy.doc.internal.FilesystemAttachmentContent; |
35 |
|
import com.xpn.xwiki.doc.XWikiAttachment; |
36 |
|
import com.xpn.xwiki.doc.XWikiAttachmentArchive; |
37 |
|
import com.xpn.xwiki.doc.XWikiAttachmentContent; |
38 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
39 |
|
import com.xpn.xwiki.store.AttachmentVersioningStore; |
40 |
|
import com.xpn.xwiki.store.XWikiHibernateStore; |
41 |
|
import com.xpn.xwiki.web.Utils; |
42 |
|
import com.xpn.xwiki.XWiki; |
43 |
|
import com.xpn.xwiki.XWikiContext; |
44 |
|
import org.apache.commons.io.IOUtils; |
45 |
|
import org.hibernate.Session; |
46 |
|
import org.jmock.Expectations; |
47 |
|
import org.jmock.api.Invocation; |
48 |
|
import org.jmock.lib.action.CustomAction; |
49 |
|
import org.jmock.lib.legacy.ClassImposteriser; |
50 |
|
import org.junit.After; |
51 |
|
import org.junit.Assert; |
52 |
|
import org.junit.Before; |
53 |
|
import org.junit.Test; |
54 |
|
import org.xwiki.model.internal.reference.PathStringEntityReferenceSerializer; |
55 |
|
import org.xwiki.model.reference.DocumentReference; |
56 |
|
import org.xwiki.store.filesystem.internal.DefaultFilesystemStoreTools; |
57 |
|
import org.xwiki.store.filesystem.internal.FilesystemStoreTools; |
58 |
|
import org.xwiki.store.locks.dummy.internal.DummyLockProvider; |
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
@version |
64 |
|
@since |
65 |
|
|
|
|
| 97.3% |
Uncovered Elements: 4 (149) |
Complexity: 24 |
Complexity Density: 0.19 |
|
66 |
|
public class FilesystemAttachmentStoreTest extends AbstractFilesystemAttachmentStoreTest |
67 |
|
{ |
68 |
|
private static final String HELLO = "Hello World"; |
69 |
|
|
70 |
|
private static final byte[] HELLO_BYTES; |
71 |
|
|
72 |
|
private static final InputStream HELLO_STREAM; |
73 |
|
|
74 |
|
private XWikiContext mockContext; |
75 |
|
|
76 |
|
private XWikiAttachment mockAttach; |
77 |
|
|
78 |
|
private FilesystemAttachmentStore attachStore; |
79 |
|
|
80 |
|
private FilesystemStoreTools fileTools; |
81 |
|
|
82 |
|
private AttachmentVersioningStore mockAttachVersionStore; |
83 |
|
|
84 |
|
private XWikiAttachmentArchive mockArchive; |
85 |
|
|
86 |
|
private XWikiHibernateStore mockHibernate; |
87 |
|
|
88 |
|
private Session mockHibernateSession; |
89 |
|
|
90 |
|
private XWikiDocument doc; |
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
private File storeFile; |
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
private File storageLocation; |
101 |
|
|
|
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
|
102 |
1 |
static {... |
103 |
1 |
try { |
104 |
1 |
HELLO_BYTES = HELLO.getBytes("UTF-8"); |
105 |
1 |
HELLO_STREAM = new ByteArrayInputStream(HELLO_BYTES); |
106 |
|
} catch (UnsupportedEncodingException e) { |
107 |
0 |
throw new RuntimeException("No UTF-8!!"); |
108 |
|
} |
109 |
|
} |
110 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (19) |
Complexity: 1 |
Complexity Density: 0.05 |
|
111 |
6 |
@Before... |
112 |
|
public void setUp() throws Exception |
113 |
|
{ |
114 |
6 |
super.setUp(); |
115 |
6 |
getMockery().setImposteriser(ClassImposteriser.INSTANCE); |
116 |
|
|
117 |
6 |
Utils.setComponentManager(this.getComponentManager()); |
118 |
|
|
119 |
6 |
this.mockContext = getMockery().mock(XWikiContext.class); |
120 |
6 |
final XWiki mockXWiki = getMockery().mock(XWiki.class); |
121 |
6 |
this.mockHibernate = getMockery().mock(XWikiHibernateStore.class); |
122 |
6 |
final XWikiAttachmentContent mockDirtyContent = |
123 |
|
getMockery().mock(XWikiAttachmentContent.class); |
124 |
6 |
this.mockAttachVersionStore = getMockery().mock(AttachmentVersioningStore.class); |
125 |
6 |
this.mockArchive = getMockery().mock(XWikiAttachmentArchive.class); |
126 |
6 |
this.mockHibernateSession = getMockery().mock(Session.class); |
127 |
6 |
this.doc = new XWikiDocument(new DocumentReference("xwiki", "Main", "WebHome")); |
128 |
|
|
129 |
6 |
this.mockAttach = getMockery().mock(XWikiAttachment.class); |
|
|
| 100% |
Uncovered Elements: 0 (28) |
Complexity: 1 |
Complexity Density: 0.04 |
|
130 |
6 |
getMockery().checking(new Expectations() {{... |
131 |
6 |
allowing(mockContext).getWiki(); will(returnValue(mockXWiki)); |
132 |
|
|
133 |
6 |
allowing(mockXWiki).getStore(); will(returnValue(mockHibernate)); |
134 |
6 |
allowing(mockXWiki).getHibernateStore(); will(returnValue(mockHibernate)); |
135 |
6 |
allowing(mockHibernate).checkHibernate(mockContext); |
136 |
6 |
allowing(mockHibernate).beginTransaction(mockContext); |
137 |
|
|
138 |
6 |
allowing(mockHibernate).getSession(mockContext); will(returnValue(mockHibernateSession)); |
139 |
|
|
140 |
6 |
allowing(mockXWiki).getAttachmentVersioningStore(); will(returnValue(mockAttachVersionStore)); |
141 |
6 |
allowing(mockAttachVersionStore).saveArchive(mockArchive, mockContext, false); |
142 |
|
|
143 |
6 |
allowing(mockAttach).getContentInputStream(mockContext); will(returnValue(HELLO_STREAM)); |
144 |
6 |
allowing(mockAttach).getDoc(); will(returnValue(doc)); |
145 |
6 |
allowing(mockAttach).getFilename(); will(returnValue("file.name")); |
146 |
6 |
allowing(mockAttach).updateContentArchive(mockContext); |
147 |
6 |
allowing(mockAttach).getAttachment_archive(); will(returnValue(mockArchive)); |
148 |
6 |
allowing(mockAttach).getAttachment_content(); will(returnValue(mockDirtyContent)); |
149 |
6 |
allowing(mockAttach).isContentDirty(); will(returnValue(true)); |
150 |
6 |
allowing(mockDirtyContent).isContentDirty(); will(returnValue(true)); |
151 |
|
}}); |
152 |
|
|
153 |
6 |
final File tmpDir = new File(System.getProperty("java.io.tmpdir")); |
154 |
6 |
this.storageLocation = new File(tmpDir, "test-storage-location"); |
155 |
|
|
156 |
6 |
this.fileTools = |
157 |
|
new DefaultFilesystemStoreTools(new PathStringEntityReferenceSerializer(), |
158 |
|
storageLocation, |
159 |
|
new DummyLockProvider()); |
160 |
|
|
161 |
6 |
this.attachStore = new FilesystemAttachmentStore(fileTools); |
162 |
6 |
this.storeFile = |
163 |
|
this.fileTools.getAttachmentFileProvider(this.mockAttach).getAttachmentContentFile(); |
164 |
6 |
HELLO_STREAM.reset(); |
165 |
|
} |
166 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
167 |
6 |
@After... |
168 |
|
public void tearDown() throws IOException |
169 |
|
{ |
170 |
6 |
resursiveDelete(this.storageLocation); |
171 |
|
} |
172 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
1PASS
|
|
173 |
1 |
@Test... |
174 |
|
public void saveContentTest() throws Exception |
175 |
|
{ |
176 |
1 |
final File storeFile = |
177 |
|
this.fileTools.getAttachmentFileProvider(this.mockAttach).getAttachmentContentFile(); |
178 |
1 |
Assert.assertFalse(this.storeFile.exists()); |
179 |
1 |
this.attachStore.saveAttachmentContent(this.mockAttach, false, this.mockContext, false); |
180 |
1 |
Assert.assertTrue("The attachment file was not created.", this.storeFile.exists()); |
181 |
|
|
182 |
1 |
final InputStream is = new FileInputStream(storeFile); |
183 |
1 |
final ByteArrayOutputStream os = new ByteArrayOutputStream(); |
184 |
1 |
IOUtils.copy(is, os); |
185 |
1 |
is.close(); |
186 |
1 |
byte[] array = os.toByteArray(); |
187 |
1 |
Assert.assertEquals("The attachment file contained the wrong content", |
188 |
|
HELLO, |
189 |
|
new String(array, "UTF-8")); |
190 |
|
} |
191 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
192 |
1 |
@Test... |
193 |
|
public void saveTwoOfSameAttachmentInOneTransactionTest() throws Exception |
194 |
|
{ |
195 |
1 |
final File storeFile = |
196 |
|
this.fileTools.getAttachmentFileProvider(this.mockAttach).getAttachmentContentFile(); |
197 |
1 |
Assert.assertFalse(this.storeFile.exists()); |
198 |
1 |
final List<XWikiAttachment> attachments = new ArrayList<XWikiAttachment>(); |
199 |
1 |
attachments.add(this.mockAttach); |
200 |
1 |
attachments.add(this.mockAttach); |
201 |
1 |
this.attachStore.saveAttachmentsContent(attachments, this.doc, false, this.mockContext, false); |
202 |
1 |
Assert.assertTrue("The attachment file was not created.", this.storeFile.exists()); |
203 |
|
|
204 |
1 |
final InputStream is = new FileInputStream(storeFile); |
205 |
1 |
final ByteArrayOutputStream os = new ByteArrayOutputStream(); |
206 |
1 |
IOUtils.copy(is, os); |
207 |
1 |
is.close(); |
208 |
1 |
byte[] array = os.toByteArray(); |
209 |
1 |
Assert.assertEquals("The attachment file contained the wrong content", |
210 |
|
HELLO, |
211 |
|
new String(array, "UTF-8")); |
212 |
|
} |
213 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
214 |
1 |
@Test... |
215 |
|
public void loadContentTest() throws Exception |
216 |
|
{ |
217 |
1 |
this.storeFile.getParentFile().mkdirs(); |
218 |
1 |
OutputStream os = new FileOutputStream(this.storeFile, false); |
219 |
1 |
IOUtils.copy(HELLO_STREAM, os); |
220 |
1 |
os.close(); |
221 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
222 |
1 |
getMockery().checking(new Expectations() {{... |
223 |
1 |
oneOf(mockAttach).setAttachment_content(with(any(FilesystemAttachmentContent.class))); |
224 |
1 |
will(new CustomAction("Check to make sure the attachment content is correct.") |
225 |
|
{ |
|
|
| 87.5% |
Uncovered Elements: 1 (8) |
Complexity: 2 |
Complexity Density: 0.25 |
|
226 |
1 |
public Object invoke(final Invocation invoc)... |
227 |
|
{ |
228 |
1 |
final FilesystemAttachmentContent content = |
229 |
|
(FilesystemAttachmentContent) invoc.getParameter(0); |
230 |
|
|
231 |
1 |
try { |
232 |
1 |
final ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
233 |
1 |
IOUtils.copy(content.getContentInputStream(), baos); |
234 |
|
|
235 |
1 |
final String output = new String(baos.toByteArray(), "UTF-8"); |
236 |
|
|
237 |
1 |
Assert.assertEquals("Not the same attachment content.", HELLO, output); |
238 |
1 |
return null; |
239 |
|
} catch (IOException e) { |
240 |
0 |
throw new RuntimeException("Exception getting attachment content.", e); |
241 |
|
} |
242 |
|
} |
243 |
|
}); |
244 |
|
}}); |
245 |
|
|
246 |
1 |
this.attachStore.loadAttachmentContent(this.mockAttach, this.mockContext, false); |
247 |
|
} |
248 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
249 |
1 |
@Test... |
250 |
|
public void deleteAttachmentTest() throws Exception |
251 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
252 |
1 |
getMockery().checking(new Expectations() {{... |
253 |
1 |
oneOf(mockAttachVersionStore).deleteArchive(mockAttach, mockContext, false); |
254 |
1 |
exactly(2).of(mockHibernateSession).delete(with(any(Object.class))); |
255 |
|
}}); |
256 |
1 |
this.createFile(); |
257 |
|
|
258 |
1 |
this.attachStore.deleteXWikiAttachment(this.mockAttach, false, this.mockContext, false); |
259 |
|
|
260 |
1 |
Assert.assertFalse("The attachment file was not deleted.", this.storeFile.exists()); |
261 |
|
} |
262 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
263 |
1 |
@Test... |
264 |
|
public void documentUpdateOnDeleteTest() throws Exception |
265 |
|
{ |
266 |
1 |
final List<XWikiAttachment> attachList = new ArrayList<XWikiAttachment>(); |
267 |
1 |
attachList.add(this.mockAttach); |
268 |
1 |
this.doc.setAttachmentList(attachList); |
269 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
270 |
1 |
getMockery().checking(new Expectations() {{... |
271 |
1 |
oneOf(mockAttachVersionStore).deleteArchive(mockAttach, mockContext, false); |
272 |
1 |
exactly(2).of(mockHibernateSession).delete(with(any(Object.class))); |
273 |
1 |
oneOf(mockHibernate).saveXWikiDoc(doc, mockContext, false); |
274 |
1 |
will(new CustomAction("Make sure the attachment has been removed from the list.") |
275 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
276 |
1 |
public Object invoke(final Invocation invoc)... |
277 |
|
{ |
278 |
1 |
final XWikiDocument document = (XWikiDocument) invoc.getParameter(0); |
279 |
1 |
Assert.assertTrue("Attachment was not removed from the list.", |
280 |
|
document.getAttachmentList().size() == 0); |
281 |
1 |
return null; |
282 |
|
} |
283 |
|
}); |
284 |
|
}}); |
285 |
1 |
this.createFile(); |
286 |
|
|
287 |
1 |
this.attachStore.deleteXWikiAttachment(this.mockAttach, true, this.mockContext, false); |
288 |
|
} |
289 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
290 |
1 |
@Test... |
291 |
|
public void documentUpdateOnSaveTest() throws Exception |
292 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
293 |
1 |
getMockery().checking(new Expectations() {{... |
294 |
1 |
oneOf(mockHibernate).saveXWikiDoc(doc, mockContext, false); |
295 |
|
}}); |
296 |
|
|
297 |
1 |
this.attachStore.saveAttachmentContent(this.mockAttach, true, this.mockContext, false); |
298 |
|
} |
299 |
|
|
300 |
|
|
301 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
302 |
2 |
private void createFile() throws IOException... |
303 |
|
{ |
304 |
2 |
this.storeFile.getParentFile().mkdirs(); |
305 |
2 |
OutputStream os = new FileOutputStream(this.storeFile, false); |
306 |
2 |
IOUtils.copy(HELLO_STREAM, os); |
307 |
2 |
os.close(); |
308 |
2 |
Assert.assertTrue("The attachment file not created for the test.", this.storeFile.exists()); |
309 |
|
} |
310 |
|
|
|
|
| 84.6% |
Uncovered Elements: 2 (13) |
Complexity: 5 |
Complexity Density: 0.71 |
|
311 |
46 |
private static void resursiveDelete(final File toDelete) throws IOException... |
312 |
|
{ |
313 |
46 |
if (toDelete == null || !toDelete.exists()) { |
314 |
0 |
return; |
315 |
|
} |
316 |
46 |
if (toDelete.isDirectory()) { |
317 |
42 |
final File[] children = toDelete.listFiles(); |
318 |
82 |
for (int i = 0; i < children.length; i++) { |
319 |
40 |
resursiveDelete(children[i]); |
320 |
|
} |
321 |
|
} |
322 |
46 |
toDelete.delete(); |
323 |
|
} |
324 |
|
} |