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

File FilesystemAttachmentStoreTest.java

 

Code metrics

6
125
18
1
324
242
24
0.19
6.94
18
1.33

Classes

Class Line # Actions
FilesystemAttachmentStoreTest 66 125 0% 24 4
0.9731543797.3%
 

Contributing tests

This file is covered by 6 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.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    * Tests for FilesystemAttachmentStore.
62    *
63    * @version $Id: fa69852ea671c1b2d0fe4e7a2c935dc61b723e33 $
64    * @since 3.0M2
65    */
 
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    * The file which will hold content for this attachment.
94    */
95    private File storeFile;
96   
97    /**
98    * The dir in /tmp/ which we use as our sandbox.
99    */
100    private File storageLocation;
101   
 
102  1 toggle 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   
 
111  6 toggle @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);
 
130  6 toggle 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   
 
167  6 toggle @After
168    public void tearDown() throws IOException
169    {
170  6 resursiveDelete(this.storageLocation);
171    }
172   
 
173  1 toggle @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   
 
192  1 toggle @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   
 
214  1 toggle @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   
 
222  1 toggle 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    {
 
226  1 toggle 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   
 
249  1 toggle @Test
250    public void deleteAttachmentTest() throws Exception
251    {
 
252  1 toggle 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   
 
263  1 toggle @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   
 
270  1 toggle 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    {
 
276  1 toggle 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   
 
290  1 toggle @Test
291    public void documentUpdateOnSaveTest() throws Exception
292    {
 
293  1 toggle 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    /* -------------------- Helpers -------------------- */
301   
 
302  2 toggle 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   
 
311  46 toggle 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    }