1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.extension.xar.internal.handler.packager

File DefaultDocumentMergeImporterTest.java

 

Code metrics

0
104
16
1
383
246
16
0.15
6.5
16
1

Classes

Class Line # Actions
DefaultDocumentMergeImporterTest 68 104 0% 16 0
1.0100%
 

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