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

File XWikiDocumentMergeTest.java

 

Code metrics

2
160
18
1
410
283
19
0.12
8.89
18
1.06

Classes

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