1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package com.xpn.xwiki.doc

File XWikiDocumentArchiveTest.java

 

Code metrics

0
124
7
1
338
247
7
0.06
17.71
7
1

Classes

Class Line # Actions
XWikiDocumentArchiveTest 40 124 0% 7 0
1.0100%
 

Contributing tests

This file is covered by 5 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 com.xpn.xwiki.doc;
21   
22    import java.util.Date;
23   
24    import org.jmock.Mock;
25    import org.suigeneris.jrcs.rcs.Version;
26    import org.xwiki.model.reference.DocumentReference;
27   
28    import com.xpn.xwiki.XWiki;
29    import com.xpn.xwiki.XWikiConfig;
30    import com.xpn.xwiki.XWikiContext;
31    import com.xpn.xwiki.XWikiException;
32    import com.xpn.xwiki.test.AbstractBridgedXWikiComponentTestCase;
33    import com.xpn.xwiki.user.api.XWikiRightService;
34   
35    /**
36    * Unit tests for {@link XWikiDocumentArchive}.
37    *
38    * @version $Id: 7cb0ae11dff0005b2bc4bc63fdbb560b713544eb $
39    */
 
40    public class XWikiDocumentArchiveTest extends AbstractBridgedXWikiComponentTestCase
41    {
42    private XWikiContext context;
43   
44    private Mock mockXWiki;
45   
 
46  5 toggle @Override
47    protected void setUp() throws Exception
48    {
49  5 super.setUp();
50   
51  5 this.mockXWiki = mock(XWiki.class);
52  5 this.mockXWiki.stubs().method("getEncoding").will(returnValue("iso-8859-1"));
53  5 this.mockXWiki.stubs().method("getConfig").will(returnValue(new XWikiConfig()));
54   
55  5 this.context = new XWikiContext();
56  5 this.context.setWiki((XWiki) this.mockXWiki.proxy());
57    }
58   
59    /**
60    * JRCS uses the user.name system property to set the author of a change. Verify that it
61    * works if the user name has a space in its name. This used to fail and this test is here to
62    * ensure it'll continue to work fine in the future...
63    *
64    * @todo simplify this test. Not sure how to do it. I guess we could create a real document.
65    */
 
66  1 toggle public void testUpdateArchiveWhenSpaceInUsername() throws Exception
67    {
68  1 String originalArchive = "head\t1.1;\n" +
69    "access;\n" +
70    "symbols;\n" +
71    "locks; strict;\n" +
72    "comment\t@# @;\n" +
73    "\n" +
74    "\n" +
75    "1.1\n" +
76    "date\t2007.02.14.14.01.57;\tauthor vmassol;\tstate Exp;\n" +
77    "branches;\n" +
78    "next\t;\n" +
79    "\n" +
80    "\n" +
81    "desc\n" +
82    "@@\n" +
83    "\n" +
84    "\n" +
85    "1.1\n" +
86    "log\n" +
87    "@KnowledgeBase.WebHome\n" +
88    "@\n" +
89    "text\n" +
90    "@<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" +
91    "\n" +
92    "<xwikidoc>\n" +
93    "<web>KnowledgeBase</web>\n" +
94    "<name>WebHome</name>\n" +
95    "<language></language>\n" +
96    "<defaultLanguage>en</defaultLanguage>\n" +
97    "<translation>0</translation>\n" +
98    "<parent>Main.Notes</parent>\n" +
99    "<creator>XWiki.Admin</creator>\n" +
100    "<author>XWiki.Admin</author>\n" +
101    "<customClass></customClass>\n" +
102    "<contentAuthor>XWiki.Admin</contentAuthor>\n" +
103    "<creationDate>1165874272000</creationDate>\n" +
104    "<date>1166177448000</date>\n" +
105    "<contentUpdateDate>1171458116000</contentUpdateDate>\n" +
106    "<version>1.1</version>\n" +
107    "<title></title>\n" +
108    "<template></template>\n" +
109    "<defaultTemplate></defaultTemplate>\n" +
110    "<validationScript></validationScript>\n" +
111    "<object>\n" +
112    "<class>\n" +
113    "<name>XWiki.TagClass</name>\n" +
114    "<customClass></customClass>\n" +
115    "<customMapping></customMapping>\n" +
116    "<defaultViewSheet></defaultViewSheet>\n" +
117    "<defaultEditSheet></defaultEditSheet>\n" +
118    "<defaultWeb></defaultWeb>\n" +
119    "<nameField></nameField>\n" +
120    "<validationScript></validationScript>\n" +
121    "<tags>\n" +
122    "<name>tags</name>\n" +
123    "<prettyName>Tags</prettyName>\n" +
124    "<unmodifiable>0</unmodifiable>\n" +
125    "<relationalStorage>1</relationalStorage>\n" +
126    "<displayType>checkbox</displayType>\n" +
127    "<multiSelect>1</multiSelect>\n" +
128    "<size>30</size>\n" +
129    "<separator> </separator>\n" +
130    "<separators> ,|</separators>\n" +
131    "<values></values>\n" +
132    "<number>1</number>\n" +
133    "<classType>com.xpn.xwiki.objects.classes.StaticListClass</classType>\n" +
134    "</tags>\n" +
135    "</class>\n" +
136    "<name>KnowledgeBase.WebHome</name>\n" +
137    "<number>0</number>\n" +
138    "<className>XWiki.TagClass</className>\n" +
139    "<property>\n" +
140    "<tags/>\n" +
141    "</property>\n" +
142    "</object>\n" +
143    "<content>1 Wiki Knowledge Base\n" +
144    "\n" +
145    "This is the Wiki Knowledge Base, where you can start writing about your favorite subjects.\n" +
146    "\n" +
147    "To create new pages, click edit button and write links using brackets around words.\n" +
148    "\n" +
149    "* [Example Link 1]\n" +
150    "* [Example Link 2]</content>\n" +
151    "</xwikidoc>\n" +
152    "@";
153   
154  1 XWikiDocumentArchive archive = new XWikiDocumentArchive(123456789L);
155  1 archive.setArchive(originalArchive);
156   
157    // Set a username with a space
158  1 System.setProperty("user.name", "Vincent Massol");
159   
160  1 XWikiDocument doc = new XWikiDocument(new DocumentReference("Wiki", "KnowledgeBase", "WebHome"));
161  1 doc.setContent(doc.getContent() + "\nsomething added");
162  1 archive.updateArchive(doc, XWikiRightService.GUEST_USER_FULLNAME, new Date(), "some comment", null, context);
163   
164    // Try to construct again the archive from the last modification. This will happen when
165    // XWiki loads a document from the database for example. We verify here that a username
166    // with a space works.
167  1 new XWikiDocumentArchive(123456789L).setArchive(archive.getArchive(context));
168    }
169   
 
170  1 toggle public void testUpdateLoad() throws XWikiException
171    {
172  1 XWikiDocument doc = new XWikiDocument(new DocumentReference("Test", "Test", "Test"));
173  1 doc.setContent("content 1.1");
174   
175  1 XWikiDocumentArchive archive = new XWikiDocumentArchive(doc.getId());
176  1 assertEquals(0, archive.getNodes().size());
177   
178  1 String author = "XWiki.some author";
179  1 archive.updateArchive(doc, author, new Date(), "initial, 1.1", null, context);
180  1 assertEquals(new Version(1,1), archive.getLatestVersion());
181  1 String archive11 = archive.getArchive(context);
182  1 assertEquals(1, archive.getNodes().size());
183  1 assertEquals(1, archive.getUpdatedNodeInfos().size());
184  1 assertEquals(1, archive.getUpdatedNodeContents().size());
185   
186  1 XWikiDocumentArchive archive2 = new XWikiDocumentArchive(doc.getId());
187  1 archive2.setArchive(archive11);
188  1 assertEquals(new Version(1,1), archive2.getLatestVersion());
189  1 assertEquals(archive11, archive2.getArchive(context));
190  1 assertEquals(1, archive2.getNodes().size());
191  1 assertEquals(1, archive2.getUpdatedNodeInfos().size());
192  1 assertEquals(1, archive2.getUpdatedNodeContents().size());
193   
194  1 doc.setContent("content\n1.2");
195  1 archive.updateArchive(doc, author, new Date(), "1.2", new Version(1,2), context);
196  1 assertEquals(new Version(1,2), archive.getLatestVersion());
197  1 String archive12 = archive.getArchive(context);
198  1 assertEquals(2, archive.getNodes().size());
199  1 assertEquals(2, archive.getUpdatedNodeInfos().size());
200  1 assertEquals(2, archive.getUpdatedNodeContents().size());
201   
202  1 XWikiDocumentArchive archive3 = new XWikiDocumentArchive(doc.getId());
203  1 archive3.setArchive(archive12);
204  1 assertEquals(new Version(1,2), archive3.getLatestVersion());
205  1 assertEquals(2, archive3.getNodes().size());
206  1 assertEquals(2, archive3.getUpdatedNodeInfos().size());
207  1 assertEquals(2, archive3.getUpdatedNodeContents().size());
208   
209  1 doc.setContent("major change\ncontent\n2.1");
210  1 archive.updateArchive(doc, author, new Date(), "2.1", new Version(1,1), context);
211  1 assertEquals(new Version(2,1), archive.getLatestVersion());
212  1 assertEquals(3, archive.getNodes().size());
213  1 assertEquals(3, archive.getUpdatedNodeInfos().size());
214  1 assertEquals(3, archive.getUpdatedNodeContents().size());
215   
216  1 doc.setContent("major change\ncontent\n 3.3");
217  1 archive.updateArchive(doc, author, new Date(), "2.1", new Version(3,3), context);
218  1 assertEquals(new Version(3,3), archive.getLatestVersion());
219    }
220   
 
221  1 toggle public void testRemoveVersions() throws XWikiException
222    {
223  1 XWikiDocument doc = new XWikiDocument(new DocumentReference("Test", "Test", "Test"));
224  1 XWikiDocumentArchive archive = new XWikiDocumentArchive(doc.getId());
225  1 doc.setDocumentArchive(archive);
226  1 String author = "XWiki.some author";
227   
228  1 addRevisionToHistory(archive, doc, "content 1.1", author, "initial 1.1");
229  1 XWikiDocument doc11 = doc.clone();
230   
231  1 doc.setContent("content 2.1\nqwe @ ");
232  1 archive.updateArchive(doc, author, new Date(), "2.1", new Version(2,1), context);
233   
234  1 doc.setContent("content 2.2\nqweq@ ");
235  1 archive.updateArchive(doc, author, new Date(), "2.2", new Version(2,2), context);
236   
237  1 doc.setContent("content 2.3\nqweqe @@");
238  1 archive.updateArchive(doc, author, new Date(), "2.3", new Version(2,3), context);
239  1 assertEquals(new Version(2,3), archive.getLatestVersion());
240   
241  1 archive.removeVersions(new Version(2,1), new Version(2,2), context);
242   
243  1 assertEquals(2, archive.getNodes().size());
244  1 assertEquals(2, archive.getDeletedNodeInfo().size());
245  1 assertNull(archive.getNode(new Version(2,1)));
246  1 assertNull(archive.getNode(new Version(2,2)));
247   
248  1 XWikiDocument actual = archive.loadDocument(new Version(1,1), context);
249  1 assertEquals(doc11.getContent(), actual.getContent());
250  1 assertEquals(doc11.getDate(), actual.getDate());
251  1 assertEquals(doc11.getAuthor(), actual.getAuthor());
252  1 assertEquals(doc11.getComment(), actual.getComment());
253    }
254   
255    /**
256    * Verify issue "When loading a revision of a document the creation date is incorrectly set as the last
257    * modification date".
258    * @see <a href="https://jira.xwiki.org/browse/XWIKI-2029">XWIKI-2029</a>
259    */
 
260  1 toggle public void testVerifyCreationDateWhenLoadingDocumentFromArchive() throws Exception
261    {
262  1 XWikiDocument doc = new XWikiDocument(new DocumentReference("Test", "Test", "Test"));
263  1 XWikiDocumentArchive archive = new XWikiDocumentArchive(doc.getId());
264  1 doc.setDocumentArchive(archive);
265  1 String author = "XWiki.some author";
266   
267  1 addRevisionToHistory(archive, doc, "content 1.1", author, "initial 1.1");
268   
269  1 Date creationDate = doc.getCreationDate();
270   
271    // Wait for 2 seconds and make a change. We'll then load the last revision and verify it has a correct
272    // creation date.
273  1 Thread.sleep(1000L);
274   
275  1 doc.setContent("content 2.1\nqwe @ ");
276  1 archive.updateArchive(doc, author, new Date(), "2.1", new Version(2,1), context);
277   
278  1 XWikiDocument latest = archive.loadDocument(new Version(2,1), context);
279   
280  1 assertEquals(creationDate, latest.getCreationDate());
281    }
282   
 
283  1 toggle public void testVerifyDiffAndFullRevisionAlgorithm() throws Exception
284    {
285  1 XWikiDocument doc = new XWikiDocument(new DocumentReference("Test", "Test", "Test"));
286  1 XWikiDocumentArchive archive = new XWikiDocumentArchive(doc.getId());
287  1 doc.setDocumentArchive(archive);
288  1 String author = "XWiki.some author";
289   
290    // The first revision is always a full revision (not a diff)
291  1 addRevisionToHistory(archive, doc, "content 1.1", author, "1.1");
292  1 assertFalse(archive.getNode(new Version(1, 1)).isDiff());
293   
294    // When a new revision is added the new revision is always the full revision but the previous one is
295    // modified to be a diff.
296  1 addRevisionToHistory(archive, doc, "content 2.1", author, "2.1");
297  1 assertTrue(archive.getNode(new Version(1, 1)).isDiff());
298  1 assertFalse(archive.getNode(new Version(2, 1)).isDiff());
299   
300  1 addRevisionToHistory(archive, doc, "content 3.1", author, "3.1");
301  1 assertTrue(archive.getNode(new Version(1, 1)).isDiff());
302  1 assertTrue(archive.getNode(new Version(2, 1)).isDiff());
303  1 assertFalse(archive.getNode(new Version(3, 1)).isDiff());
304   
305  1 addRevisionToHistory(archive, doc, "content 4.1", author, "4.1");
306  1 assertTrue(archive.getNode(new Version(1, 1)).isDiff());
307  1 assertTrue(archive.getNode(new Version(2, 1)).isDiff());
308  1 assertTrue(archive.getNode(new Version(3, 1)).isDiff());
309  1 assertFalse(archive.getNode(new Version(4, 1)).isDiff());
310   
311    // Every 5th revision we save the full content and not a diff
312  1 addRevisionToHistory(archive, doc, "content 5.1", author, "5.1");
313  1 assertTrue(archive.getNode(new Version(1, 1)).isDiff());
314  1 assertTrue(archive.getNode(new Version(2, 1)).isDiff());
315  1 assertTrue(archive.getNode(new Version(3, 1)).isDiff());
316  1 assertTrue(archive.getNode(new Version(4, 1)).isDiff());
317  1 assertFalse(archive.getNode(new Version(5, 1)).isDiff());
318   
319    // Verify that the 5th revision is kept as a full content revision when the 6th is added
320  1 addRevisionToHistory(archive, doc, "content 6.1", author, "6.1");
321  1 assertTrue(archive.getNode(new Version(1, 1)).isDiff());
322  1 assertTrue(archive.getNode(new Version(2, 1)).isDiff());
323  1 assertTrue(archive.getNode(new Version(3, 1)).isDiff());
324  1 assertTrue(archive.getNode(new Version(4, 1)).isDiff());
325  1 assertFalse(archive.getNode(new Version(5, 1)).isDiff());
326  1 assertFalse(archive.getNode(new Version(6, 1)).isDiff());
327    }
328   
 
329  8 toggle private void addRevisionToHistory(XWikiDocumentArchive archive, XWikiDocument document, String content,
330    String author, String comment) throws XWikiException
331    {
332  8 document.setContent(content);
333  8 document.setAuthor(author);
334  8 document.setComment(comment);
335  8 document.setDate(new Date());
336  8 archive.updateArchive(document, document.getAuthor(), document.getDate(), document.getComment(), null, context);
337    }
338    }