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

File FilesystemAttachmentVersioningStoreTest.java

 

Code metrics

6
82
11
2
234
164
15
0.18
7.45
5.5
1.36

Classes

Class Line # Actions
FilesystemAttachmentVersioningStoreTest 55 78 0% 11 2
0.97802297.8%
FilesystemAttachmentVersioningStoreTest.StringAttachmentContent 210 4 0% 4 0
1.0100%
 

Contributing tests

This file is covered by 3 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.File;
24    import java.io.IOException;
25    import java.io.InputStream;
26    import java.util.ArrayList;
27   
28    import com.xpn.xwiki.doc.XWikiAttachment;
29    import com.xpn.xwiki.doc.XWikiAttachmentArchive;
30    import com.xpn.xwiki.doc.XWikiAttachmentContent;
31    import com.xpn.xwiki.doc.XWikiDocument;
32    import com.xpn.xwiki.store.AttachmentVersioningStore;
33    import com.xpn.xwiki.web.Utils;
34    import org.apache.commons.io.IOUtils;
35    import org.junit.After;
36    import org.junit.Assert;
37    import org.junit.Before;
38    import org.junit.Test;
39    import org.xwiki.model.internal.reference.PathStringEntityReferenceSerializer;
40    import org.xwiki.model.reference.DocumentReference;
41    import org.xwiki.store.filesystem.internal.AttachmentFileProvider;
42    import org.xwiki.store.filesystem.internal.DefaultFilesystemStoreTools;
43    import org.xwiki.store.filesystem.internal.FilesystemStoreTools;
44    import org.xwiki.store.legacy.doc.internal.ListAttachmentArchive;
45    import org.xwiki.store.locks.dummy.internal.DummyLockProvider;
46    import org.xwiki.store.serialization.xml.internal.AttachmentListMetadataSerializer;
47    import org.xwiki.store.serialization.xml.internal.AttachmentMetadataSerializer;
48   
49    /**
50    * Tests for FilesystemAttachmentVersioningStore.
51    *
52    * @version $Id: c7bfccec1a1c4704fc3e29fecc714d288eae57de $
53    * @since 3.0M2
54    */
 
55    public class FilesystemAttachmentVersioningStoreTest extends AbstractFilesystemAttachmentStoreTest
56    {
57    private FilesystemStoreTools fileTools;
58   
59    private AttachmentVersioningStore versionStore;
60   
61    private XWikiAttachmentArchive archive;
62   
63    private AttachmentFileProvider provider;
64   
65    private File storageLocation;
66   
 
67  3 toggle @Before
68    public void setUp() throws Exception
69    {
70  3 super.setUp();
71  3 Utils.setComponentManager(this.getComponentManager());
72   
73  3 final File tmpDir = new File(System.getProperty("java.io.tmpdir"));
74  3 this.storageLocation = new File(tmpDir, "test-storage-location");
75   
76  3 this.fileTools =
77    new DefaultFilesystemStoreTools(new PathStringEntityReferenceSerializer(),
78    storageLocation,
79    new DummyLockProvider());
80  3 final AttachmentListMetadataSerializer serializer =
81    new AttachmentListMetadataSerializer(new AttachmentMetadataSerializer());
82  3 this.versionStore = new FilesystemAttachmentVersioningStore(this.fileTools, serializer);
83   
84  3 final XWikiDocument doc = new XWikiDocument(new DocumentReference("xwiki", "Main", "WebHome"));
85   
86  3 final XWikiAttachment version1 = new XWikiAttachment();
87  3 version1.setVersion("1.1");
88  3 version1.setFilename("attachment.txt");
89  3 version1.setDoc(doc);
90  3 version1.setAttachment_content(new StringAttachmentContent("I am version 1.1"));
91   
92  3 final XWikiAttachment version2 = new XWikiAttachment();
93  3 version2.setVersion("1.2");
94  3 version2.setFilename("attachment.txt");
95  3 version2.setDoc(doc);
96  3 version2.setAttachment_content(new StringAttachmentContent("I am version 1.2"));
97   
98  3 final XWikiAttachment version3 = new XWikiAttachment();
99  3 version3.setVersion("1.3");
100  3 version3.setFilename("attachment.txt");
101  3 version3.setDoc(doc);
102  3 version3.setAttachment_content(new StringAttachmentContent("I am version 1.3"));
103   
104  3 this.provider = this.fileTools.getAttachmentFileProvider(version1);
 
105  3 toggle this.archive = new ListAttachmentArchive(new ArrayList<XWikiAttachment>() {{
106  3 add(version1);
107  3 add(version2);
108  3 add(version3);
109    }});
110    }
111   
 
112  3 toggle @After
113    public void tearDown() throws IOException
114    {
115  3 resursiveDelete(this.storageLocation);
116    }
117   
 
118  1 toggle @Test
119    public void saveArchiveTest() throws Exception
120    {
121  1 final XWikiAttachmentContent content = this.archive.getAttachment().getAttachment_content();
122  1 final XWikiAttachment attach = this.archive.getAttachment();
123   
124  1 Assert.assertFalse(this.provider.getAttachmentVersioningMetaFile().exists());
125  1 Assert.assertFalse(this.provider.getAttachmentVersionContentFile("1.1").exists());
126  1 Assert.assertFalse(this.provider.getAttachmentVersionContentFile("1.2").exists());
127  1 Assert.assertFalse(this.provider.getAttachmentVersionContentFile("1.3").exists());
128   
129    // Because the context is only used by the legacy implementation, it is safe to pass null.
130  1 this.versionStore.saveArchive(this.archive, null, false);
131   
132  1 Assert.assertTrue(this.provider.getAttachmentVersioningMetaFile().exists());
133   
134    // Make sure it's not just:
135    // <?xml version="1.0" encoding="UTF-8"?>
136    // <attachment-list serializer="attachment-list-meta/1.0">
137    // </attachment-list>
138  1 Assert.assertTrue(this.provider.getAttachmentVersioningMetaFile().length() > 120);
139   
140  1 Assert.assertTrue(this.provider.getAttachmentVersionContentFile("1.1").exists());
141  1 Assert.assertTrue(this.provider.getAttachmentVersionContentFile("1.2").exists());
142  1 Assert.assertTrue(this.provider.getAttachmentVersionContentFile("1.3").exists());
143   
144    // Prove that the attachment and attachment content are the same after saving.
145  1 Assert.assertSame(attach, this.archive.getAttachment());
146  1 Assert.assertSame(content, this.archive.getAttachment().getAttachment_content());
147    }
148   
 
149  1 toggle @Test
150    public void loadArchiveTest() throws Exception
151    {
152  1 this.versionStore.saveArchive(this.archive, null, false);
153  1 final XWikiAttachmentArchive newArch =
154    this.versionStore.loadArchive(archive.getAttachment(), null, false);
155  1 Assert.assertTrue(newArch.getVersions().length == 3);
156  1 final XWikiAttachment version1 = newArch.getRevision(archive.getAttachment(), "1.1", null);
157  1 final XWikiAttachment version2 = newArch.getRevision(archive.getAttachment(), "1.2", null);
158  1 final XWikiAttachment version3 = newArch.getRevision(archive.getAttachment(), "1.3", null);
159   
160  1 Assert.assertTrue(version1.getVersion().equals("1.1"));
161  1 Assert.assertTrue(version1.getFilename().equals("attachment.txt"));
162  1 Assert.assertEquals("I am version 1.1", IOUtils.toString(version1.getContentInputStream(null)));
163  1 Assert.assertSame(version1.getDoc(), this.archive.getAttachment().getDoc());
164   
165  1 Assert.assertTrue(version2.getVersion().equals("1.2"));
166  1 Assert.assertTrue(version2.getFilename().equals("attachment.txt"));
167  1 Assert.assertEquals("I am version 1.2", IOUtils.toString(version2.getContentInputStream(null)));
168  1 Assert.assertSame(version2.getDoc(), this.archive.getAttachment().getDoc());
169   
170  1 Assert.assertTrue(version3.getVersion().equals("1.3"));
171  1 Assert.assertTrue(version3.getFilename().equals("attachment.txt"));
172  1 Assert.assertEquals("I am version 1.3", IOUtils.toString(version3.getContentInputStream(null)));
173  1 Assert.assertSame(version3.getDoc(), this.archive.getAttachment().getDoc());
174    }
175   
 
176  1 toggle @Test
177    public void deleteArchiveTest() throws Exception
178    {
179  1 this.versionStore.saveArchive(this.archive, null, false);
180   
181  1 Assert.assertTrue(this.provider.getAttachmentVersioningMetaFile().exists());
182  1 Assert.assertTrue(this.provider.getAttachmentVersionContentFile("1.1").exists());
183  1 Assert.assertTrue(this.provider.getAttachmentVersionContentFile("1.2").exists());
184  1 Assert.assertTrue(this.provider.getAttachmentVersionContentFile("1.3").exists());
185   
186  1 this.versionStore.deleteArchive(this.archive.getAttachment(), null, false);
187   
188  1 Assert.assertFalse(this.provider.getAttachmentVersioningMetaFile().exists());
189  1 Assert.assertFalse(this.provider.getAttachmentVersionContentFile("1.1").exists());
190  1 Assert.assertFalse(this.provider.getAttachmentVersionContentFile("1.2").exists());
191  1 Assert.assertFalse(this.provider.getAttachmentVersionContentFile("1.3").exists());
192    }
193   
194    /* -------------------- Helpers -------------------- */
195   
 
196  29 toggle private static void resursiveDelete(final File toDelete) throws IOException
197    {
198  29 if (toDelete == null || !toDelete.exists()) {
199  0 return;
200    }
201  29 if (toDelete.isDirectory()) {
202  21 final File[] children = toDelete.listFiles();
203  47 for (int i = 0; i < children.length; i++) {
204  26 resursiveDelete(children[i]);
205    }
206    }
207  29 toDelete.delete();
208    }
209   
 
210    private static class StringAttachmentContent extends XWikiAttachmentContent
211    {
212    private final String content;
213   
 
214  9 toggle public StringAttachmentContent(final String content)
215    {
216  9 this.content = content;
217    }
218   
 
219  9 toggle public InputStream getContentInputStream()
220    {
221  9 return new ByteArrayInputStream(this.content.getBytes());
222    }
223   
 
224  9 toggle public boolean isContentDirty()
225    {
226  9 return true;
227    }
228   
 
229  9 toggle public StringAttachmentContent clone()
230    {
231  9 return this;
232    }
233    }
234    }