1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.doc

File XWikiDocumentMergeTest.java

 

Code metrics

2
160
18
1
421
294
19
0.12
8.89
18
1.06

Classes

Class Line # Actions
XWikiDocumentMergeTest 78 160 0% 19 2
0.9888888698.9%
 

Contributing tests

This file is covered by 16 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.Arrays;
23    import java.util.List;
24    import java.util.Locale;
25   
26    import org.junit.Assert;
27    import org.junit.Before;
28    import org.junit.Rule;
29    import org.junit.Test;
30    import org.xwiki.diff.internal.DefaultDiffManager;
31    import org.xwiki.logging.LogLevel;
32    import org.xwiki.logging.event.LogEvent;
33    import org.xwiki.model.internal.DefaultModelConfiguration;
34    import org.xwiki.model.internal.reference.DefaultEntityReferenceProvider;
35    import org.xwiki.model.internal.reference.DefaultStringDocumentReferenceResolver;
36    import org.xwiki.model.internal.reference.DefaultStringEntityReferenceResolver;
37    import org.xwiki.model.internal.reference.DefaultStringEntityReferenceSerializer;
38    import org.xwiki.model.internal.reference.DefaultSymbolScheme;
39    import org.xwiki.model.internal.reference.LocalStringEntityReferenceSerializer;
40    import org.xwiki.model.internal.reference.LocalUidStringEntityReferenceSerializer;
41    import org.xwiki.model.reference.DocumentReference;
42    import org.xwiki.test.annotation.ComponentList;
43   
44    import com.xpn.xwiki.doc.merge.MergeConfiguration;
45    import com.xpn.xwiki.doc.merge.MergeException;
46    import com.xpn.xwiki.doc.merge.MergeResult;
47    import com.xpn.xwiki.internal.model.reference.CurrentEntityReferenceProvider;
48    import com.xpn.xwiki.internal.model.reference.CurrentReferenceDocumentReferenceResolver;
49    import com.xpn.xwiki.internal.model.reference.CurrentReferenceEntityReferenceResolver;
50    import com.xpn.xwiki.internal.model.reference.CurrentStringDocumentReferenceResolver;
51    import com.xpn.xwiki.internal.model.reference.CurrentStringEntityReferenceResolver;
52    import com.xpn.xwiki.objects.BaseObject;
53    import com.xpn.xwiki.objects.classes.BaseClass;
54    import com.xpn.xwiki.objects.classes.TextAreaClass;
55    import com.xpn.xwiki.test.MockitoOldcoreRule;
56   
57    /**
58    * Validate {@link XWikiDocument#merge(XWikiDocument, XWikiDocument, MergeConfiguration, com.xpn.xwiki.XWikiContext)}.
59    *
60    * @version $Id: 6c83d4cbd487890108268c36011575ce53f6679a $
61    */
62    @ComponentList(value = {
63    DefaultDiffManager.class,
64    LocalStringEntityReferenceSerializer.class,
65    CurrentReferenceDocumentReferenceResolver.class,
66    CurrentReferenceEntityReferenceResolver.class,
67    CurrentEntityReferenceProvider.class,
68    DefaultModelConfiguration.class,
69    CurrentStringDocumentReferenceResolver.class,
70    CurrentStringEntityReferenceResolver.class,
71    LocalUidStringEntityReferenceSerializer.class,
72    DefaultStringEntityReferenceSerializer.class,
73    DefaultStringDocumentReferenceResolver.class,
74    DefaultStringEntityReferenceResolver.class,
75    DefaultEntityReferenceProvider.class,
76    DefaultSymbolScheme.class
77    })
 
78    public class XWikiDocumentMergeTest
79    {
80    @Rule
81    public MockitoOldcoreRule oldcore = new MockitoOldcoreRule();
82   
83    private XWikiDocument currentDocument;
84   
85    private XWikiDocument previousDocument;
86   
87    private XWikiDocument nextDocument;
88   
89    private BaseObject xobject;
90   
91    private BaseClass xclass;
92   
93    private MergeConfiguration configuration;
94   
 
95  16 toggle @Before
96    public void before() throws Exception
97    {
98  16 this.oldcore.registerMockEnvironment();
99   
100  16 this.currentDocument = new XWikiDocument(new DocumentReference("wiki", "space", "page"));
101  16 this.previousDocument = this.currentDocument.clone();
102  16 this.nextDocument = this.currentDocument.clone();
103   
104  16 this.xclass = new BaseClass();
105  16 this.xclass.setDocumentReference(new DocumentReference("wiki", "classspace", "class"));
106  16 this.xclass.addTextField("string", "String", 30);
107  16 this.xclass.addTextAreaField("area", "Area", 10, 10);
108  16 this.xclass.addTextAreaField("puretextarea", "Pure text area", 10, 10);
109    // set the text areas an non interpreted content
110  16 ((TextAreaClass) this.xclass.getField("puretextarea")).setContentType("puretext");
111  16 this.xclass.addPasswordField("passwd", "Password", 30);
112  16 this.xclass.addBooleanField("boolean", "Boolean", "yesno");
113  16 this.xclass.addNumberField("int", "Int", 10, "integer");
114  16 this.xclass.addStaticListField("stringlist", "StringList", "value1, value2");
115   
116  16 this.xobject = new BaseObject();
117  16 this.xobject.setXClassReference(this.xclass.getDocumentReference());
118  16 this.xobject.setStringValue("string", "string");
119  16 this.xobject.setLargeStringValue("area", "area");
120  16 this.xobject.setStringValue("passwd", "passwd");
121  16 this.xobject.setIntValue("boolean", 1);
122  16 this.xobject.setIntValue("int", 42);
123  16 this.xobject.setStringListValue("stringlist", Arrays.asList("VALUE1", "VALUE2"));
124   
125  16 this.configuration = new MergeConfiguration();
126    }
127   
 
128  15 toggle private MergeResult merge() throws Exception
129    {
130  15 MergeResult result =
131    this.currentDocument.merge(this.previousDocument, this.nextDocument, this.configuration,
132    this.oldcore.getXWikiContext());
133   
134  15 List<LogEvent> exception = result.getLog().getLogs(LogLevel.ERROR);
135  15 if (!exception.isEmpty()) {
136  0 throw new MergeException(exception.get(0).getFormattedMessage(), exception.get(0).getThrowable());
137    }
138   
139  15 return result;
140    }
141   
142    // Tests
143   
144    // #merge
145   
 
146  1 toggle @Test
147    public void testMergeContent() throws Exception
148    {
149  1 this.previousDocument.setContent("some content");
150  1 this.nextDocument.setContent("some new content");
151  1 this.currentDocument.setContent("some content");
152   
153  1 merge();
154   
155  1 Assert.assertEquals("some new content", this.currentDocument.getContent());
156    }
157   
 
158  1 toggle @Test
159    public void testMergeDefaultLocale() throws Exception
160    {
161  1 this.previousDocument.setDefaultLocale(Locale.ENGLISH);
162  1 this.nextDocument.setDefaultLocale(Locale.FRENCH);
163  1 this.currentDocument.setDefaultLocale(Locale.ENGLISH);
164   
165  1 merge();
166   
167  1 Assert.assertEquals(Locale.FRENCH, this.currentDocument.getDefaultLocale());
168    }
169   
 
170  1 toggle @Test
171    public void testMergeContentModified() throws Exception
172    {
173  1 this.previousDocument.setContent("some content");
174  1 this.nextDocument.setContent("some content\nafter");
175  1 this.currentDocument.setContent("before\nsome content");
176   
177  1 merge();
178   
179  1 Assert.assertEquals("before\nsome content\nafter", this.currentDocument.getContent());
180   
181  1 this.previousDocument.setContent("some content");
182  1 this.nextDocument.setContent("some content\nafter");
183  1 this.currentDocument.setContent("some content");
184   
185  1 merge();
186   
187  1 Assert.assertEquals("some content\nafter", this.currentDocument.getContent());
188    }
189   
 
190  1 toggle @Test
191    public void testMergeNewObjectAdded() throws Exception
192    {
193  1 this.nextDocument.addXObject(this.xobject);
194   
195  1 merge();
196   
197  1 Assert.assertSame(this.xobject, this.currentDocument.getXObject(this.xclass.getReference(), 0));
198    }
199   
 
200  1 toggle @Test
201    public void testMergeNewObjectRemoved() throws Exception
202    {
203  1 this.previousDocument.addXObject(this.xobject);
204  1 this.currentDocument.addXObject(this.xobject.clone());
205   
206  1 merge();
207   
208  1 Assert.assertNull(this.currentDocument.getXObject(this.xclass.getReference(), 0));
209    }
210   
 
211  1 toggle @Test
212    public void testMergeObjectModified() throws Exception
213    {
214  1 BaseObject previousobj = this.xobject;
215  1 previousobj.setStringValue("test", "test1");
216  1 this.previousDocument.addXObject(previousobj);
217   
218  1 BaseObject obj = this.xobject.clone();
219  1 obj.setStringValue("test", "test1");
220  1 this.currentDocument.addXObject(obj);
221   
222  1 BaseObject newobj = this.xobject.clone();
223  1 newobj.setStringValue("test", "test2");
224  1 this.nextDocument.addXObject(newobj);
225   
226  1 merge();
227   
228  1 BaseObject mergedobj = this.currentDocument.getXObject(this.xclass.getReference(), 0);
229   
230  1 Assert.assertNotNull(mergedobj);
231  1 Assert.assertEquals("test2", mergedobj.getStringValue("test"));
232    }
233   
 
234  1 toggle @Test
235    public void testMergeCurrentObjectRemoved() throws Exception
236    {
237  1 this.xobject.setStringValue("test", "");
238  1 this.xobject.setStringValue("previoustest", "previoustest");
239  1 this.previousDocument.addXObject(this.xobject);
240   
241  1 BaseObject newobj = this.xobject.clone();
242  1 newobj.setStringValue("test", "test2");
243  1 newobj.setStringValue("newtest", "newtest");
244  1 this.nextDocument.addXObject(newobj);
245   
246  1 merge();
247   
248  1 BaseObject mergedobj = this.currentDocument.getXObject(this.xclass.getReference(), 0);
249   
250  1 Assert.assertNull(mergedobj);
251    }
252   
 
253  1 toggle @Test
254    public void testMergeAttachmentEquals() throws Exception
255    {
256  1 XWikiAttachment attachment = new XWikiAttachment();
257   
258  1 attachment.setContent(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
259  1 attachment.setFilesize(10);
260  1 attachment.setFilename("file");
261   
262  1 this.previousDocument.addAttachment(attachment);
263  1 this.nextDocument.addAttachment((XWikiAttachment) attachment.clone());
264  1 this.currentDocument.addAttachment((XWikiAttachment) attachment.clone());
265   
266  1 MergeResult result = merge();
267   
268  1 Assert.assertFalse(result.isModified());
269    }
270   
 
271  1 toggle @Test
272    public void testMergeAttachmentEqualsDeletedCurrent() throws Exception
273    {
274  1 XWikiAttachment attachment = new XWikiAttachment();
275   
276  1 attachment.setContent(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
277  1 attachment.setFilesize(10);
278  1 attachment.setFilename("file");
279   
280  1 this.previousDocument.addAttachment(attachment);
281  1 this.nextDocument.addAttachment((XWikiAttachment) attachment.clone());
282   
283  1 MergeResult result = merge();
284   
285  1 Assert.assertFalse(result.isModified());
286    }
287   
 
288  1 toggle @Test
289    public void testMergeAttachmentEqualsAddedCurrent() throws Exception
290    {
291  1 XWikiAttachment attachment = new XWikiAttachment();
292   
293  1 attachment.setContent(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
294  1 attachment.setFilesize(10);
295  1 attachment.setFilename("file");
296   
297  1 this.currentDocument.addAttachment(attachment);
298   
299  1 MergeResult result = merge();
300   
301  1 Assert.assertFalse(result.isModified());
302    }
303   
 
304  1 toggle @Test
305    public void testMergeAttachmentEqualsModifiedCurrent() throws Exception
306    {
307  1 XWikiAttachment attachment = new XWikiAttachment();
308   
309  1 attachment.setContent(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
310  1 attachment.setFilesize(10);
311  1 attachment.setFilename("file");
312   
313  1 this.previousDocument.addAttachment(attachment);
314  1 this.nextDocument.addAttachment((XWikiAttachment) attachment.clone());
315   
316  1 attachment = (XWikiAttachment) attachment.clone();
317  1 attachment.setContent(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 });
318  1 attachment.setFilesize(9);
319   
320  1 this.currentDocument.addAttachment(attachment);
321   
322  1 MergeResult result = merge();
323   
324  1 Assert.assertFalse(result.isModified());
325    }
326   
 
327  1 toggle @Test
328    public void testMergeAttachmentNew() throws Exception
329    {
330  1 XWikiAttachment attachment = new XWikiAttachment();
331   
332  1 attachment.setContent(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
333  1 attachment.setFilesize(10);
334  1 attachment.setFilename("file");
335   
336  1 this.nextDocument.addAttachment(attachment);
337   
338  1 MergeResult result = merge();
339   
340  1 Assert.assertTrue(result.isModified());
341   
342  1 XWikiAttachment newAttachment = this.currentDocument.getAttachment("file");
343   
344  1 Assert.assertNotNull(newAttachment);
345  1 Assert.assertEquals(10, newAttachment.getFilesize());
346  1 Assert.assertArrayEquals(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, newAttachment.getContent(null));
347    }
348   
 
349  1 toggle @Test
350    public void testMergeAttachmentDeleted() throws Exception
351    {
352  1 XWikiAttachment attachment = new XWikiAttachment();
353   
354  1 attachment.setContent(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
355  1 attachment.setFilesize(10);
356  1 attachment.setFilename("file");
357   
358  1 this.currentDocument.addAttachment(attachment);
359  1 this.previousDocument.addAttachment((XWikiAttachment) attachment.clone());
360   
361  1 MergeResult result = merge();
362   
363  1 Assert.assertTrue(result.isModified());
364   
365  1 XWikiAttachment newAttachment = this.currentDocument.getAttachment("file");
366   
367  1 Assert.assertNull(newAttachment);
368    }
369   
 
370  1 toggle @Test
371    public void testMergeAttachmentModified() throws Exception
372    {
373  1 XWikiAttachment attachment = new XWikiAttachment();
374   
375  1 attachment.setContent(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
376  1 attachment.setFilesize(10);
377  1 attachment.setFilename("file");
378   
379  1 this.currentDocument.addAttachment(attachment);
380  1 this.previousDocument.addAttachment((XWikiAttachment) attachment.clone());
381   
382  1 attachment = (XWikiAttachment) attachment.clone();
383  1 attachment.setContent(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 });
384  1 attachment.setFilesize(9);
385   
386  1 this.nextDocument.addAttachment(attachment);
387   
388  1 MergeResult result = merge();
389   
390  1 Assert.assertTrue(result.isModified());
391   
392  1 XWikiAttachment newAttachment = this.currentDocument.getAttachment("file");
393   
394  1 Assert.assertNotNull(newAttachment);
395  1 Assert.assertEquals(9, newAttachment.getFilesize());
396  1 Assert.assertArrayEquals(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 }, newAttachment.getContent(null));
397    }
398   
399    // #apply
400   
 
401  1 toggle @Test
402    public void testApplyWithUnmodifiedObject()
403    {
404  1 this.previousDocument.addXObject(this.xobject);
405  1 this.currentDocument.addXObject(this.xobject.clone());
406   
407  1 Assert.assertFalse(this.previousDocument.apply(this.currentDocument, true));
408    }
409   
 
410  1 toggle @Test
411    public void testApplyWithModifiedObjectAndClean()
412    {
413  1 this.previousDocument.addXObject(this.xobject);
414  1 BaseObject modifiedObject = this.xobject.clone();
415  1 modifiedObject.setStringValue("string", "string2");
416  1 this.currentDocument.addXObject(modifiedObject);
417   
418  1 Assert.assertTrue(this.previousDocument.apply(this.currentDocument, true));
419  1 Assert.assertEquals("string2", this.xobject.getStringValue("string"));
420    }
421    }