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.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 |
|
|
51 |
|
|
52 |
|
@version |
53 |
|
@since |
54 |
|
|
|
|
| 97.8% |
Uncovered Elements: 2 (91) |
Complexity: 11 |
Complexity Density: 0.14 |
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (25) |
Complexity: 1 |
Complexity Density: 0.04 |
|
67 |
3 |
@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); |
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
105 |
3 |
this.archive = new ListAttachmentArchive(new ArrayList<XWikiAttachment>() {{... |
106 |
3 |
add(version1); |
107 |
3 |
add(version2); |
108 |
3 |
add(version3); |
109 |
|
}}); |
110 |
|
} |
111 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
112 |
3 |
@After... |
113 |
|
public void tearDown() throws IOException |
114 |
|
{ |
115 |
3 |
resursiveDelete(this.storageLocation); |
116 |
|
} |
117 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (14) |
Complexity: 1 |
Complexity Density: 0.07 |
1PASS
|
|
118 |
1 |
@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 |
|
|
130 |
1 |
this.versionStore.saveArchive(this.archive, null, false); |
131 |
|
|
132 |
1 |
Assert.assertTrue(this.provider.getAttachmentVersioningMetaFile().exists()); |
133 |
|
|
134 |
|
|
135 |
|
|
136 |
|
|
137 |
|
|
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 |
|
|
145 |
1 |
Assert.assertSame(attach, this.archive.getAttachment()); |
146 |
1 |
Assert.assertSame(content, this.archive.getAttachment().getAttachment_content()); |
147 |
|
} |
148 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (18) |
Complexity: 1 |
Complexity Density: 0.06 |
1PASS
|
|
149 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
1PASS
|
|
176 |
1 |
@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 |
|
|
195 |
|
|
|
|
| 84.6% |
Uncovered Elements: 2 (13) |
Complexity: 5 |
Complexity Density: 0.71 |
|
196 |
29 |
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 4 |
Complexity Density: 1 |
|
210 |
|
private static class StringAttachmentContent extends XWikiAttachmentContent |
211 |
|
{ |
212 |
|
private final String content; |
213 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
214 |
9 |
public StringAttachmentContent(final String content)... |
215 |
|
{ |
216 |
9 |
this.content = content; |
217 |
|
} |
218 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
219 |
9 |
public InputStream getContentInputStream()... |
220 |
|
{ |
221 |
9 |
return new ByteArrayInputStream(this.content.getBytes()); |
222 |
|
} |
223 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
224 |
9 |
public boolean isContentDirty()... |
225 |
|
{ |
226 |
9 |
return true; |
227 |
|
} |
228 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
229 |
9 |
public StringAttachmentContent clone()... |
230 |
|
{ |
231 |
9 |
return this; |
232 |
|
} |
233 |
|
} |
234 |
|
} |