1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
|
@link |
64 |
|
|
65 |
|
@version |
66 |
|
|
67 |
|
@ComponentList({ContextComponentManagerProvider.class}) |
|
|
| 100% |
Uncovered Elements: 0 (120) |
Complexity: 16 |
Complexity Density: 0.15 |
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
98 |
12 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (24) |
Complexity: 1 |
Complexity Density: 0.04 |
|
107 |
12 |
@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 |
|
|
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 |
|
|
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 |
|
|
145 |
|
|
146 |
12 |
this.jobStatus = mock(JobStatus.class); |
147 |
12 |
this.configuration.setJobStatus(this.jobStatus); |
148 |
|
|
149 |
|
|
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 |
|
|
157 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
158 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
169 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
178 |
1 |
@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 |
|
|
190 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
191 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
206 |
5 |
private void answerGlobalAction(final GlobalAction action, final boolean always) throws InterruptedException... |
207 |
|
{ |
208 |
5 |
doAnswer(new Answer<Object>() |
209 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
210 |
9 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
1PASS
|
|
221 |
1 |
@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 |
|
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
1PASS
|
|
245 |
1 |
@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 |
|
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
1PASS
|
|
271 |
1 |
@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 |
|
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
1PASS
|
|
297 |
1 |
@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 |
|
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
1PASS
|
|
323 |
1 |
@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 |
|
|
340 |
|
|
341 |
1 |
this.mocker.getComponentUnderTest().saveDocument("comment", this.previousDocument, this.currentDocument, |
342 |
|
this.nextDocument, this.configuration); |
343 |
|
|
344 |
|
|
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 |
|
|
351 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
352 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
362 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
373 |
1 |
@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 |
|
} |