1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.extension.xar.internal.handler.packager

File DefaultDocumentMergeImporterTest.java

 

Code metrics

0
131
20
1
450
295
20
0.15
6.55
20
1

Classes

Class Line # Actions
DefaultDocumentMergeImporterTest 70 131 0% 20 0
1.0100%
 

Contributing tests

This file is covered by 15 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.extension.xar.internal.handler.packager;
21   
22    import java.util.Locale;
23   
24    import javax.inject.Provider;
25   
26    import org.junit.Before;
27    import org.junit.Rule;
28    import org.junit.Test;
29    import org.mockito.invocation.InvocationOnMock;
30    import org.mockito.stubbing.Answer;
31    import org.xwiki.component.internal.ContextComponentManagerProvider;
32    import org.xwiki.component.manager.ComponentLookupException;
33    import org.xwiki.context.Execution;
34    import org.xwiki.context.ExecutionContext;
35    import org.xwiki.extension.xar.question.ConflictQuestion;
36    import org.xwiki.extension.xar.question.ConflictQuestion.ConflictType;
37    import org.xwiki.extension.xar.question.ConflictQuestion.GlobalAction;
38    import org.xwiki.job.Job;
39    import org.xwiki.job.JobContext;
40    import org.xwiki.job.Request;
41    import org.xwiki.job.event.status.JobStatus;
42    import org.xwiki.model.reference.DocumentReference;
43    import org.xwiki.test.annotation.BeforeComponent;
44    import org.xwiki.test.annotation.ComponentList;
45    import org.xwiki.test.mockito.MockitoComponentMockingRule;
46   
47    import com.xpn.xwiki.XWiki;
48    import com.xpn.xwiki.XWikiContext;
49    import com.xpn.xwiki.doc.MandatoryDocumentInitializerManager;
50    import com.xpn.xwiki.doc.XWikiDocument;
51    import com.xpn.xwiki.doc.merge.MergeConfiguration;
52    import com.xpn.xwiki.doc.merge.MergeResult;
53   
54    import static org.mockito.ArgumentMatchers.any;
55    import static org.mockito.ArgumentMatchers.eq;
56    import static org.mockito.ArgumentMatchers.same;
57    import static org.mockito.Mockito.doAnswer;
58    import static org.mockito.Mockito.mock;
59    import static org.mockito.Mockito.times;
60    import static org.mockito.Mockito.verify;
61    import static org.mockito.Mockito.verifyZeroInteractions;
62    import static org.mockito.Mockito.when;
63   
64    /**
65    * Validate {@link DocumentMergeImporter}.
66    *
67    * @version $Id: 613bd1f2583f933a70c313a5c9756735d80f197a $
68    */
69    @ComponentList({ ContextComponentManagerProvider.class, DefaultXWikiDocumentMerger.class })
 
70    public class DefaultDocumentMergeImporterTest
71    {
72    @Rule
73    public MockitoComponentMockingRule<DocumentMergeImporter> mocker =
74    new MockitoComponentMockingRule<DocumentMergeImporter>(DocumentMergeImporter.class);
75   
76    private DocumentReference documentReference = new DocumentReference("wiki", "space", "page", Locale.ROOT);
77   
78    private MandatoryDocumentInitializerManager initializerManager;
79   
80    private JobContext jobContext;
81   
82    private XWikiDocument previousDocument;
83   
84    private XWikiDocument currentDocument;
85   
86    private XWikiDocument nextDocument;
87   
88    private XWikiDocument mergedDocument;
89   
90    private PackageConfiguration configuration;
91   
92    private MergeResult mergeResult;
93   
94    private XWikiContext xcontext;
95   
96    private XWiki xwiki;
97   
98    private JobStatus jobStatus;
99   
100    private Execution execution;
101   
102    private ExecutionContext econtext;
103   
 
104  15 toggle @BeforeComponent
105    public void registerComponents() throws Exception
106    {
107  15 this.xcontext = mock(XWikiContext.class);
108   
109  15 Provider<XWikiContext> xcontextProvider = this.mocker.registerMockComponent(XWikiContext.TYPE_PROVIDER);
110  15 when(xcontextProvider.get()).thenReturn(this.xcontext);
111   
112  15 this.initializerManager = this.mocker.registerMockComponent(MandatoryDocumentInitializerManager.class);
113  15 this.jobContext = this.mocker.registerMockComponent(JobContext.class);
114    }
115   
 
116  15 toggle @Before
117    public void setUp() throws Exception
118    {
119  15 this.xwiki = mock(XWiki.class);
120   
121  15 when(this.xcontext.getWiki()).thenReturn(this.xwiki);
122   
123    // documents
124   
125  15 this.previousDocument = mock(XWikiDocument.class, "previous");
126  15 when(this.previousDocument.isNew()).thenReturn(false);
127  15 when(this.previousDocument.getDocumentReferenceWithLocale()).thenReturn(this.documentReference);
128   
129  15 this.currentDocument = mock(XWikiDocument.class, "current");
130  15 when(this.currentDocument.isNew()).thenReturn(false);
131  15 when(this.currentDocument.getDocumentReferenceWithLocale()).thenReturn(this.documentReference);
132  15 when(this.xwiki.getDocument(same(this.documentReference), same(xcontext))).thenReturn(this.currentDocument);
133   
134  15 this.nextDocument = mock(XWikiDocument.class, "next");
135  15 when(this.nextDocument.isNew()).thenReturn(false);
136  15 when(this.nextDocument.getDocumentReferenceWithLocale()).thenReturn(this.documentReference);
137   
138  15 this.mergedDocument = mock(XWikiDocument.class, "merged");
139  15 when(this.mergedDocument.isNew()).thenReturn(false);
140  15 when(this.mergedDocument.getDocumentReferenceWithLocale()).thenReturn(this.documentReference);
141   
142  15 when(this.currentDocument.clone()).thenReturn(this.mergedDocument);
143   
144    // merge
145   
146  15 this.configuration = new PackageConfiguration();
147   
148  15 this.mergeResult = new MergeResult();
149  15 when(this.mergedDocument.merge(same(this.previousDocument), same(this.nextDocument),
150    any(MergeConfiguration.class), any(XWikiContext.class))).thenReturn(this.mergeResult);
151   
152    // job status
153   
154  15 this.jobStatus = mock(JobStatus.class);
155  15 this.configuration.setJobStatus(this.jobStatus);
156  15 Job job = mock(Job.class);
157  15 when(this.jobContext.getCurrentJob()).thenReturn(job);
158  15 when(job.getStatus()).thenReturn(this.jobStatus);
159   
160    // execution
161   
162  15 this.econtext = new ExecutionContext();
163  15 this.execution = this.mocker.registerMockComponent(Execution.class);
164  15 when(this.execution.getContext()).thenReturn(this.econtext);
165    }
166   
 
167  8 toggle private void setInteractive()
168    {
169  8 Request request = mock(Request.class);
170   
171  8 when(this.jobStatus.getRequest()).thenReturn(request);
172  8 when(request.isInteractive()).thenReturn(true);
173    }
174   
175    // Merge
176   
 
177  1 toggle @Test
178    public void testMergeNoChange() throws ComponentLookupException, Exception
179    {
180  1 this.mergeResult.setModified(false);
181   
182  1 this.mocker.getComponentUnderTest().importDocument("comment", this.previousDocument, this.currentDocument,
183    this.nextDocument, this.configuration);
184   
185  1 verifyZeroInteractions(this.xwiki, this.xcontext);
186    }
187   
 
188  1 toggle @Test
189    public void testMergeNoCurrent() throws ComponentLookupException, Exception
190    {
191  1 this.mocker.getComponentUnderTest().importDocument("comment", this.previousDocument, null, this.nextDocument,
192    this.configuration);
193   
194  1 verifyZeroInteractions(this.xwiki, this.xcontext);
195    }
196   
 
197  1 toggle @Test
198    public void testMergeChanges() throws ComponentLookupException, Exception
199    {
200  1 this.mergeResult.setModified(true);
201   
202  1 this.mocker.getComponentUnderTest().importDocument("comment", this.previousDocument, this.currentDocument,
203    this.nextDocument, this.configuration);
204   
205  1 verify(this.xwiki).saveDocument(same(this.mergedDocument), eq("comment"), eq(false), same(this.xcontext));
206    }
207   
 
208  1 toggle @Test
209    public void testMergeChangesNoCustom() throws ComponentLookupException, Exception
210    {
211  1 this.mergeResult.setModified(true);
212  1 when(this.currentDocument.equalsData(same(this.previousDocument))).thenReturn(true);
213   
214  1 this.mocker.getComponentUnderTest().importDocument("comment", this.previousDocument, this.currentDocument,
215    this.nextDocument, this.configuration);
216   
217  1 verify(this.xwiki).saveDocument(same(this.nextDocument), eq("comment"), eq(false), same(this.xcontext));
218    }
219   
220    // Merge interactive
221   
 
222  1 toggle @Test
223    public void testMergeInteractiveChangesNoConflict() throws ComponentLookupException, Exception
224    {
225  1 setInteractive();
226  1 this.configuration.setUser(new DocumentReference("wiki", "space", "user"));
227   
228  1 this.mergeResult.setModified(true);
229   
230  1 this.mocker.getComponentUnderTest().importDocument("comment", this.previousDocument, this.currentDocument,
231    this.nextDocument, this.configuration);
232   
233  1 verifyZeroInteractions(this.jobStatus);
234  1 verify(this.xwiki).saveDocument(same(this.mergedDocument), eq("comment"), eq(false), same(this.xcontext));
235    }
236   
 
237  1 toggle @Test
238    public void testMergeInteractiveChangesNoConflictAsk() throws ComponentLookupException, Exception
239    {
240  1 setInteractive();
241  1 this.configuration.setUser(new DocumentReference("wiki", "space", "user"));
242   
243  1 this.mergeResult.setModified(true);
244   
245  1 this.configuration.setConflictAction(ConflictType.MERGE_SUCCESS, GlobalAction.ASK);
246   
247  1 this.mocker.getComponentUnderTest().importDocument("comment", this.previousDocument, this.currentDocument,
248    this.nextDocument, this.configuration);
249   
250  1 verify(this.jobStatus, times(1)).ask(any());
251  1 verify(this.xwiki).saveDocument(same(this.mergedDocument), eq("comment"), eq(false), same(this.xcontext));
252    }
253   
 
254  1 toggle @Test
255    public void testMergeInteractiveChangesNoCustomNoConflictAsk() throws ComponentLookupException, Exception
256    {
257  1 setInteractive();
258  1 this.configuration.setUser(new DocumentReference("wiki", "space", "user"));
259   
260  1 this.mergeResult.setModified(true);
261  1 when(this.currentDocument.equalsData(same(this.previousDocument))).thenReturn(true);
262   
263  1 this.configuration.setConflictAction(ConflictType.MERGE_SUCCESS, GlobalAction.ASK);
264   
265  1 this.mocker.getComponentUnderTest().importDocument("comment", this.previousDocument, this.currentDocument,
266    this.nextDocument, this.configuration);
267   
268  1 verifyZeroInteractions(this.jobStatus);
269  1 verify(this.xwiki).saveDocument(same(this.nextDocument), eq("comment"), eq(false), same(this.xcontext));
270    }
271   
 
272  5 toggle private void answerGlobalAction(final GlobalAction action, final boolean always) throws InterruptedException
273    {
274  5 doAnswer(new Answer<Object>()
275    {
 
276  9 toggle @Override
277    public Object answer(InvocationOnMock invocation)
278    {
279  9 ConflictQuestion question = (ConflictQuestion) invocation.getArguments()[0];
280  9 question.setGlobalAction(action);
281  9 question.setAlways(always);
282  9 return null;
283    }
284    }).when(this.jobStatus).ask(any());
285    }
286   
 
287  1 toggle @Test
288    public void testMergeInteractiveChangesConflictAnswerCurrent() throws ComponentLookupException, Exception
289    {
290  1 setInteractive();
291  1 this.configuration.setUser(new DocumentReference("wiki", "space", "user"));
292   
293  1 this.mergeResult.setModified(true);
294  1 this.mergeResult.getLog().error("error");
295   
296  1 answerGlobalAction(GlobalAction.CURRENT, false);
297   
298  1 this.mocker.getComponentUnderTest().importDocument("comment", this.previousDocument, this.currentDocument,
299    this.nextDocument, this.configuration);
300   
301    // another try
302   
303  1 this.mocker.getComponentUnderTest().importDocument("comment", this.previousDocument, this.currentDocument,
304    this.nextDocument, this.configuration);
305   
306  1 verify(this.jobStatus, times(2)).ask(any());
307   
308  1 verifyZeroInteractions(this.xwiki, this.xcontext);
309    }
310   
 
311  1 toggle @Test
312    public void testMergeInteractiveChangesConflictAnswerNext() throws ComponentLookupException, Exception
313    {
314  1 setInteractive();
315  1 this.configuration.setUser(new DocumentReference("wiki", "space", "user"));
316   
317  1 this.mergeResult.setModified(true);
318  1 this.mergeResult.getLog().error("error");
319   
320  1 answerGlobalAction(GlobalAction.NEXT, false);
321   
322  1 this.mocker.getComponentUnderTest().importDocument("comment", this.previousDocument, this.currentDocument,
323    this.nextDocument, this.configuration);
324   
325  1 verify(this.xwiki).saveDocument(same(this.nextDocument), eq("comment"), eq(false), same(this.xcontext));
326   
327    // another try
328   
329  1 this.mocker.getComponentUnderTest().importDocument("comment", this.previousDocument, this.currentDocument,
330    this.nextDocument, this.configuration);
331   
332  1 verify(this.jobStatus, times(2)).ask(any());
333  1 verify(this.xwiki, times(2)).saveDocument(same(this.nextDocument), eq("comment"), eq(false),
334    same(this.xcontext));
335    }
336   
 
337  1 toggle @Test
338    public void testMergeInteractiveChangesConflictAnswerMerged() throws ComponentLookupException, Exception
339    {
340  1 setInteractive();
341  1 this.configuration.setUser(new DocumentReference("wiki", "space", "user"));
342   
343  1 this.mergeResult.setModified(true);
344  1 this.mergeResult.getLog().error("error");
345   
346  1 answerGlobalAction(GlobalAction.MERGED, false);
347   
348  1 this.mocker.getComponentUnderTest().importDocument("comment", this.previousDocument, this.currentDocument,
349    this.nextDocument, this.configuration);
350   
351  1 verify(this.xwiki).saveDocument(same(this.mergedDocument), eq("comment"), eq(false), same(this.xcontext));
352   
353    // another try
354   
355  1 this.mocker.getComponentUnderTest().importDocument("comment", this.previousDocument, this.currentDocument,
356    this.nextDocument, this.configuration);
357   
358  1 verify(this.jobStatus, times(2)).ask(any());
359  1 verify(this.xwiki, times(2)).saveDocument(same(this.mergedDocument), eq("comment"), eq(false),
360    same(this.xcontext));
361    }
362   
 
363  1 toggle @Test
364    public void testMergeInteractiveChangesConflictAnswerPrevious() throws ComponentLookupException, Exception
365    {
366  1 setInteractive();
367  1 this.configuration.setUser(new DocumentReference("wiki", "space", "user"));
368   
369  1 this.mergeResult.setModified(true);
370  1 this.mergeResult.getLog().error("error");
371   
372  1 answerGlobalAction(GlobalAction.PREVIOUS, false);
373   
374  1 this.mocker.getComponentUnderTest().importDocument("comment", this.previousDocument, this.currentDocument,
375    this.nextDocument, this.configuration);
376   
377  1 verify(this.xwiki).saveDocument(same(this.previousDocument), eq("comment"), eq(false), same(this.xcontext));
378   
379    // another try
380   
381  1 this.mocker.getComponentUnderTest().importDocument("comment", this.previousDocument, this.currentDocument,
382    this.nextDocument, this.configuration);
383   
384  1 verify(this.jobStatus, times(2)).ask(any());
385  1 verify(this.xwiki, times(2)).saveDocument(same(this.previousDocument), eq("comment"), eq(false),
386    same(this.xcontext));
387    }
388   
 
389  1 toggle @Test
390    public void testMergeInteractiveChangesConflictAnswerPreviousAlways() throws ComponentLookupException, Exception
391    {
392  1 setInteractive();
393  1 this.configuration.setUser(new DocumentReference("wiki", "space", "user"));
394   
395  1 this.mergeResult.setModified(true);
396  1 this.mergeResult.getLog().error("error");
397   
398  1 answerGlobalAction(GlobalAction.PREVIOUS, true);
399   
400  1 this.mocker.getComponentUnderTest().importDocument("comment", this.previousDocument, this.currentDocument,
401    this.nextDocument, this.configuration);
402   
403  1 verify(this.xwiki).saveDocument(same(this.previousDocument), eq("comment"), eq(false), same(this.xcontext));
404   
405    // another try
406   
407  1 this.mocker.getComponentUnderTest().importDocument("comment", this.previousDocument, this.currentDocument,
408    this.nextDocument, this.configuration);
409   
410    // Make sure we don't ask the job status this time
411  1 verify(this.jobStatus, times(1)).ask(any());
412  1 verify(this.xwiki, times(2)).saveDocument(same(this.previousDocument), eq("comment"), eq(false),
413    same(this.xcontext));
414    }
415   
416    // No merge
417   
 
418  1 toggle @Test
419    public void testNoMergeNoCurrent() throws ComponentLookupException, Exception
420    {
421  1 when(this.currentDocument.isNew()).thenReturn(true);
422   
423  1 this.mocker.getComponentUnderTest().importDocument("comment", null, null, this.nextDocument,
424    this.configuration);
425   
426  1 verify(this.xwiki).saveDocument(same(this.nextDocument), eq("comment"), eq(false), same(this.xcontext));
427    }
428   
 
429  1 toggle @Test
430    public void testNoMergeDifferent() throws ComponentLookupException, Exception
431    {
432  1 when(this.currentDocument.equalsData(same(this.nextDocument))).thenReturn(false);
433   
434  1 this.mocker.getComponentUnderTest().importDocument("comment", null, this.currentDocument, this.nextDocument,
435    this.configuration);
436   
437  1 verify(this.xwiki).saveDocument(same(this.nextDocument), eq("comment"), eq(false), same(this.xcontext));
438    }
439   
 
440  1 toggle @Test
441    public void testNoMergeNoChange() throws ComponentLookupException, Exception
442    {
443  1 when(this.currentDocument.equalsData(same(this.nextDocument))).thenReturn(true);
444   
445  1 this.mocker.getComponentUnderTest().importDocument("comment", null, this.currentDocument, this.nextDocument,
446    this.configuration);
447   
448  1 verifyZeroInteractions(this.xwiki, this.xcontext);
449    }
450    }