1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.test.storage

File AttachmentTest.java

 
testRollbackAfterDeleteAttachment: expected:<[This is content for a very small attachment.]>...
testRollbackAfterUpdateOfAttachment: expected:<[This is content for a very small attachment.]>...
testRollbackAfterDeleteAttachment: expected:<[This is content for a very small attachment.]>...
testRollbackAfterUpdateOfAttachment: expected:<[This is content for a very small attachment.]>...
 

Code metrics

0
113
25
1
391
217
25
0.22
4.52
25
1

Classes

Class Line # Actions
AttachmentTest 39 113 0% 25 60
0.565217456.5%
 

Contributing tests

This file is covered by 12 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.test.storage;
21   
22    import java.io.ByteArrayOutputStream;
23    import java.util.HashMap;
24   
25    import org.apache.commons.httpclient.HttpMethod;
26    import org.apache.commons.io.IOUtils;
27    import org.junit.Assert;
28    import org.junit.Ignore;
29    import org.junit.Test;
30    import org.xwiki.test.storage.framework.AbstractTest;
31    import org.xwiki.test.storage.framework.StoreTestUtils;
32   
33    /**
34    * Test saving and downloading of attachments.
35    *
36    * @version $Id: a3948f0c2ce543f3074c110d2082d2278c5cf0b6 $
37    * @since 3.0RC1
38    */
 
39    public class AttachmentTest extends AbstractTest
40    {
41    private static final String ATTACHMENT_CONTENT = "This is content for a very small attachment.";
42   
43    private static final String FILENAME = "littleAttachment.txt";
44   
45    /**
46    * Tests that XWIKI-5405 remains fixed.
47    * This test proves that when an attachment is saved using Document.addAttachment and
48    * then Document.save() the attachment is actually persisted to the database.
49    */
 
50  2 toggle @Test
51    public void testDocumentAddAttachment() throws Exception
52    {
53  2 final String test =
54    "{{groovy}}\n"
55    // First the attacher.
56    + "doc.addAttachment('" + FILENAME + "', '" + ATTACHMENT_CONTENT
57    + "'.getBytes('UTF-8'));\n"
58    + "doc.saveAsAuthor();"
59    // then the validator.
60    + "println(xwiki.getDocument(doc.getDocumentReference()).getAttachment('" + FILENAME
61    + "').getContentAsString());"
62    + "{{/groovy}}";
63   
64    // Delete the document if it exists.
65  2 doPostAsAdmin("Test", "Attachment", null, "delete", "confirm=1", null);
66   
67    // Create a document.
68  2 doPostAsAdmin("Test", "Attachment", null, "save", null,
 
69  2 toggle new HashMap<String, String>() {{
70  2 put("content", test);
71    }});
72   
73  2 HttpMethod ret = null;
74   
75    // Test getAttachment()
76  2 ret = doPostAsAdmin("Test", "Attachment", null, "view", "xpage=plain", null);
77  2 Assert.assertEquals("<p>" + ATTACHMENT_CONTENT + "</p>", ret.getResponseBodyAsString());
78   
79    // Test downloadAction.
80  2 ret = doPostAsAdmin("Test", "Attachment", FILENAME, "download", null, null);
81  2 Assert.assertEquals(ATTACHMENT_CONTENT, new String(ret.getResponseBody(), "UTF-8"));
82  2 Assert.assertEquals(200, ret.getStatusCode());
83   
84    // Make sure there is exactly 1 version of this attachment.
85  2 ret = doPostAsAdmin("Test", "Attachment", null, "preview", "xpage=plain",
 
86  2 toggle new HashMap<String, String>() {{
87  2 put("content", "{{velocity}}$doc.getAttachment('"
88    + FILENAME + "').getVersions().size(){{/velocity}}");
89    }});
90  2 Assert.assertEquals("<p>1</p>", ret.getResponseBodyAsString());
91   
92    // Make sure that version contains the correct content.
93  2 ret = doPostAsAdmin("Test", "Attachment", null, "preview", "xpage=plain",
 
94  2 toggle new HashMap<String, String>() {{
95  2 put("content", "{{velocity}}$doc.getAttachment('" + FILENAME
96    + "').getAttachmentRevision('1.1').getContentAsString(){{/velocity}}");
97    }});
98  2 Assert.assertEquals("<p>" + ATTACHMENT_CONTENT + "</p>", ret.getResponseBodyAsString());
99    }
100   
101    /**
102    * XWIKI-6126
103    */
 
104  2 toggle @Test
105    public void testImportDocumentWithAttachment() throws Exception
106    {
107  2 final String spaceName = "Test";
108  2 final String pageName = "Attachment2";
109   
110  2 final String attachURL = this.getAddressPrefix() + "download/" + spaceName + "/" + pageName + "/" + FILENAME;
111   
112    // Delete the document if it exists.
113  2 doPostAsAdmin(spaceName, pageName, null, "delete", "confirm=1", null);
114   
115    // Upload the XAR to import.
116  2 final ByteArrayOutputStream baos = new ByteArrayOutputStream();
117  2 IOUtils.copy(this.getClass().getResourceAsStream("/Test.Attachment2.xar"), baos);
118  2 doUploadAsAdmin("XWiki", "XWikiPreferences",
 
119  2 toggle new HashMap<String, byte[]>() {{
120  2 put("Test.Attachment2.xar", baos.toByteArray());
121    }});
122   
123    // Do the import.
124  2 doPostAsAdmin("XWiki", "XWikiPreferences", null, "import", null,
 
125  2 toggle new HashMap<String, String>() {{
126  2 put("action", "import");
127  2 put("name", "Test.Attachment2.xar");
128  2 put("historyStrategy", "add");
129  2 put("pages", "Test.Attachment2");
130    }});
131   
132    // Check for attachment content.
133  2 Assert.assertEquals(ATTACHMENT_CONTENT, StoreTestUtils.getPageAsString(attachURL));
134    }
135   
 
136  0 toggle @Test
137    public void testRollbackAfterDeleteAttachment() throws Exception
138    {
139  0 final String spaceName = "Test";
140  0 final String pageName = "testRollbackAfterDeleteAttachment";
141   
142  0 final String attachURL = this.getAddressPrefix() + "download/" + spaceName + "/" + pageName + "/" + FILENAME;
143   
144    // Delete the document if it exists.
145  0 doPostAsAdmin(spaceName, pageName, null, "delete", "confirm=1", null);
146   
147    // Create a document.
148  0 doPostAsAdmin(spaceName, pageName, null, "save", null, null);
149   
150    // Upload the attachment
151  0 doUploadAsAdmin(spaceName, pageName,
 
152  0 toggle new HashMap<String, byte[]>() {{
153  0 put(FILENAME, ATTACHMENT_CONTENT.getBytes());
154    }});
155   
156    // Make sure it's there.
157  0 Assert.assertEquals(ATTACHMENT_CONTENT, StoreTestUtils.getPageAsString(attachURL));
158   
159    // Delete it
160  0 doPostAsAdmin(spaceName, pageName, FILENAME, "delattachment", null, null);
161   
162    // Make sure it's nolonger there.
163  0 Assert.assertFalse(ATTACHMENT_CONTENT.equals(StoreTestUtils.getPageAsString(attachURL)));
164   
165    // Do a rollback.
166  0 doPostAsAdmin(spaceName, pageName, null, "rollback", "rev=2.1&confirm=1", null);
167   
168    // Make sure the content is back again.
169  0 Test failure here Assert.assertEquals(ATTACHMENT_CONTENT, StoreTestUtils.getPageAsString(attachURL));
170    }
171   
 
172  2 toggle @Test
173    public void testRollbackAfterNewAttachment() throws Exception
174    {
175  2 final String spaceName = "Test";
176  2 final String pageName = "testRollbackAfterNewAttachment";
177   
178  2 final String attachURL = this.getAddressPrefix() + "download/" + spaceName + "/" + pageName + "/" + FILENAME;
179   
180    // Delete the document if it exists.
181  2 doPostAsAdmin(spaceName, pageName, null, "delete", "?confirm=1", null);
182   
183    // Create a document.
184  2 doPostAsAdmin(spaceName, pageName, null, "save", null, null);
185   
186  2 HttpMethod ret;
187   
188    // Upload the attachment
189  2 ret = doUploadAsAdmin(spaceName, pageName,
 
190  2 toggle new HashMap<String, byte[]>() {{
191  2 put(FILENAME, ATTACHMENT_CONTENT.getBytes());
192    }});
193   
194    // Make sure it's there.
195  2 Assert.assertEquals(ATTACHMENT_CONTENT, StoreTestUtils.getPageAsString(attachURL));
196   
197    // Do a rollback.
198  2 doPostAsAdmin(spaceName, pageName, null, "rollback", "?rev=1.1&confirm=1", null);
199   
200    // Make sure it's nolonger there.
201  2 ret = doPostAsAdmin(spaceName, pageName, FILENAME, "download", null, null);
202  2 Assert.assertFalse(ATTACHMENT_CONTENT.equals(new String(ret.getResponseBody(), "UTF-8")));
203  2 Assert.assertEquals(404, ret.getStatusCode());
204    }
205   
 
206  0 toggle @Test
207    public void testRollbackAfterUpdateOfAttachment() throws Exception
208    {
209  0 final String spaceName = "Test";
210  0 final String pageName = "testRollbackAfterUpdateOfAttachment";
211   
212  0 final String versionTwo = "This is some different content";
213   
214  0 final String attachURL = this.getAddressPrefix() + "download/" + spaceName + "/" + pageName + "/" + FILENAME;
215   
216    // Delete the document if it exists.
217  0 doPostAsAdmin(spaceName, pageName, null, "delete", "confirm=1", null);
218   
219    // Create a document.
220  0 doPostAsAdmin(spaceName, pageName, null, "save", null, null);
221   
222    // Upload the attachment
223  0 doUploadAsAdmin(spaceName, pageName,
 
224  0 toggle new HashMap<String, byte[]>() {{
225  0 put(FILENAME, ATTACHMENT_CONTENT.getBytes());
226    }});
227   
228    // Make sure it's there.
229  0 Assert.assertEquals(ATTACHMENT_CONTENT, StoreTestUtils.getPageAsString(attachURL));
230   
231    // Overwrite
232  0 doUploadAsAdmin(spaceName, pageName,
 
233  0 toggle new HashMap<String, byte[]>() {{
234  0 put(FILENAME, versionTwo.getBytes());
235    }});
236   
237    // Make sure it is now version2
238  0 Assert.assertEquals(versionTwo, StoreTestUtils.getPageAsString(attachURL));
239   
240    // Do a rollback.
241  0 doPostAsAdmin(spaceName, pageName, null, "rollback", "rev=2.1&confirm=1", null);
242   
243    // Make sure it is version1
244  0 Test failure here Assert.assertEquals(ATTACHMENT_CONTENT, StoreTestUtils.getPageAsString(attachURL));
245    }
246   
247    /**
248    * If the user saves an attachment, then deletes it, then saves another with the same name,
249    * then rolls the version back, the new attachment should be trashed and the old attachment should be
250    * restored.
251    */
 
252  0 toggle @Test
253    // This test flickers. see: http://jira.xwiki.org/jira/browse/XE-934
254    @Ignore
255    public void testRollbackAfterSaveDeleteSaveAttachment() throws Exception
256    {
257  0 final String spaceName = "Test";
258  0 final String pageName = "testRollbackAfterSaveDeleteSaveAttachment";
259   
260  0 final String versionTwo = "This is some different content";
261   
262  0 final String attachURL = this.getAddressPrefix() + "download/" + spaceName + "/" + pageName + "/" + FILENAME;
263   
264    // Delete the document if it exists.
265  0 doPostAsAdmin(spaceName, pageName, null, "delete", "confirm=1", null);
266   
267    // Create a document. v1.1
268  0 doPostAsAdmin(spaceName, pageName, null, "save", null, null);
269   
270  0 HttpMethod ret;
271   
272    // Upload the attachment v2.1
273  0 ret = doUploadAsAdmin(spaceName, pageName,
 
274  0 toggle new HashMap<String, byte[]>() {{
275  0 put(FILENAME, ATTACHMENT_CONTENT.getBytes());
276    }});
277   
278    // Make sure it's there.
279  0 Assert.assertEquals(ATTACHMENT_CONTENT, StoreTestUtils.getPageAsString(attachURL));
280   
281    // Delete it v3.1
282  0 doPostAsAdmin(spaceName, pageName, FILENAME, "delattachment", null, null);
283   
284    // Upload again v4.1
285  0 ret = doUploadAsAdmin(spaceName, pageName,
 
286  0 toggle new HashMap<String, byte[]>() {{
287  0 put(FILENAME, versionTwo.getBytes());
288    }});
289   
290    // Make sure it's there.
291  0 Assert.assertEquals(versionTwo, StoreTestUtils.getPageAsString(attachURL));
292   
293    // Do a rollback. v5.1
294  0 doPostAsAdmin(spaceName, pageName, null, "rollback", "rev=2.1&confirm=1", null);
295   
296    // Make sure the latest current version is actually v5.1
297  0 ret = doPostAsAdmin(spaceName, pageName, null, "preview", "xpage=plain",
 
298  0 toggle new HashMap<String, String>() {{
299  0 put("content", "{{velocity}}$doc.getVersion(){{/velocity}}");
300    }});
301  0 Assert.assertEquals("<p>5.1</p>", new String(ret.getResponseBody(), "UTF-8"));
302   
303    // Make sure it is version1
304  0 Assert.assertEquals(ATTACHMENT_CONTENT, StoreTestUtils.getPageAsString(attachURL));
305   
306    // Do rollback to version2. v6.1
307  0 doPostAsAdmin(spaceName, pageName, null, "rollback", "rev=4.1&confirm=1", null);
308   
309    // Make sure it is version2
310  0 Assert.assertEquals(versionTwo, StoreTestUtils.getPageAsString(attachURL));
311   
312    // Make sure the latest current version is actually v6.1
313  0 ret = doPostAsAdmin(spaceName, pageName, null, "preview", "xpage=plain",
 
314  0 toggle new HashMap<String, String>() {{
315  0 put("content", "{{velocity}}$doc.getVersion(){{/velocity}}");
316    }});
317  0 Assert.assertEquals("<p>6.1</p>", new String(ret.getResponseBody(), "UTF-8"));
318    }
319   
320    /**
321    * Tests that XWIKI-5436 remains fixed.
322    * This test proves that when an attachment is saved using Document.addAttachment
323    * and then the document is saved a number of times after, the attachment verstion is not incremented.
324    * It also checks that XWikiAttachment.isContentDirty() is false unless the attachment has
325    * just been modified.
326    */
 
327  2 toggle @Test
328    public void testAttachmentContentDirty() throws Exception
329    {
330  2 final String test1 =
331    "{{groovy}}\n"
332    + "content = '" + ATTACHMENT_CONTENT + "'\n"
333    + "doc.addAttachment('" + FILENAME + "', content.getBytes('UTF-8'));\n"
334    + "print(doc.getDocument().getAttachmentList().get(0).isContentDirty());\n"
335    + "doc.saveAsAuthor();\n"
336    + "print(' ');\n"
337    + "print(xwiki.getDocument(doc.getFullName())"
338    + ".getDocument().getAttachmentList().get(0).isContentDirty());\n"
339    + "{{/groovy}}\n";
340   
341  2 final String spaceName = "Test";
342  2 final String pageName = "testAttachmentContentDirty";
343   
344    // Delete the document if it exists.
345  2 doPostAsAdmin(spaceName, pageName, null, "delete", "confirm=1", null);
346   
347    // Create a document.
348  2 doPostAsAdmin(spaceName, pageName, null, "save", null,
 
349  2 toggle new HashMap<String, String>(){{
350  2 put("content", test1);
351    }});
352   
353  2 Assert.assertEquals("<p>true false</p>",
354    StoreTestUtils.getPageAsString(this.getAddressPrefix() + "view/" + spaceName + "/" + pageName + "?xpage=plain"));
355   
356   
357    // Make sure that on load the attach content isn't dirty.
358  2 final String test2 =
359    "{{groovy}}print(doc.getDocument().getAttachmentList().get(0).isContentDirty());{{/groovy}}\n";
360   
361  2 doPostAsAdmin(spaceName, pageName, null, "save", null,
 
362  2 toggle new HashMap<String, String>(){{
363  2 put("content", test2);
364    }});
365  2 Assert.assertEquals("<p>false</p>",
366    StoreTestUtils.getPageAsString(this.getAddressPrefix() + "view/" + spaceName + "/" + pageName + "?xpage=plain"));
367   
368    // Make sure the version of the attachment has not been incremented.
369  2 final String test3 =
370    "{{groovy}}print(doc.getDocument().getAttachmentList().get(0).getVersion());{{/groovy}}";
371  2 doPostAsAdmin(spaceName, pageName, null, "save", null,
 
372  2 toggle new HashMap<String, String>(){{
373  2 put("content", test3);
374    }});
375  2 Assert.assertEquals("<p>1.1</p>",
376    StoreTestUtils.getPageAsString(this.getAddressPrefix() + "view/" + spaceName + "/" + pageName + "?xpage=plain"));
377   
378    // Reupload the attachment and make sure the version is incremented.
379  2 doPostAsAdmin(spaceName, pageName, null, "preview", null,
 
380  2 toggle new HashMap<String, String>(){{
381  2 put("content", test1);
382    }});
383   
384  2 doPostAsAdmin(spaceName, pageName, null, "save", null,
 
385  2 toggle new HashMap<String, String>(){{
386  2 put("content", test3);
387    }});
388  2 Assert.assertEquals("<p>1.2</p>",
389    StoreTestUtils.getPageAsString(this.getAddressPrefix() + "view/" + spaceName + "/" + pageName + "?xpage=plain"));
390    }
391    }