1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.refactoring.internal

File DefaultLinkRefactoringTest.java

 

Code metrics

0
233
10
1
494
357
10
0.04
23.3
10
1

Classes

Class Line # Actions
DefaultLinkRefactoringTest 74 233 0% 10 0
1.0100%
 

Contributing tests

This file is covered by 8 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.refactoring.internal;
21   
22    import java.util.Arrays;
23    import java.util.Collections;
24    import java.util.HashMap;
25    import java.util.List;
26    import java.util.Locale;
27    import java.util.Map;
28   
29    import javax.inject.Provider;
30   
31    import org.junit.Before;
32    import org.junit.Rule;
33    import org.junit.Test;
34    import org.xwiki.component.manager.ComponentManager;
35    import org.xwiki.component.util.DefaultParameterizedType;
36    import org.xwiki.model.reference.DocumentReference;
37    import org.xwiki.model.reference.DocumentReferenceResolver;
38    import org.xwiki.model.reference.EntityReference;
39    import org.xwiki.model.reference.EntityReferenceResolver;
40    import org.xwiki.model.reference.EntityReferenceSerializer;
41    import org.xwiki.model.reference.SpaceReference;
42    import org.xwiki.rendering.block.Block;
43    import org.xwiki.rendering.block.LinkBlock;
44    import org.xwiki.rendering.block.MacroBlock;
45    import org.xwiki.rendering.block.XDOM;
46    import org.xwiki.rendering.block.match.BlockMatcher;
47    import org.xwiki.rendering.internal.resolver.DefaultResourceReferenceEntityReferenceResolver;
48    import org.xwiki.rendering.listener.reference.ResourceReference;
49    import org.xwiki.rendering.listener.reference.ResourceType;
50    import org.xwiki.rendering.renderer.BlockRenderer;
51    import org.xwiki.rendering.syntax.Syntax;
52    import org.xwiki.test.annotation.ComponentList;
53    import org.xwiki.test.mockito.MockitoComponentMockingRule;
54   
55    import com.xpn.xwiki.XWiki;
56    import com.xpn.xwiki.XWikiContext;
57    import com.xpn.xwiki.doc.XWikiDocument;
58    import com.xpn.xwiki.internal.render.DefaultLinkedResourceHelper;
59   
60    import static org.junit.Assert.assertEquals;
61    import static org.mockito.ArgumentMatchers.any;
62    import static org.mockito.ArgumentMatchers.eq;
63    import static org.mockito.Mockito.atLeastOnce;
64    import static org.mockito.Mockito.mock;
65    import static org.mockito.Mockito.verify;
66    import static org.mockito.Mockito.when;
67   
68    /**
69    * Unit tests for {@link DefaultLinkRefactoring}.
70    *
71    * @version $Id: fa4407b72512049a25ecb4ecfc1644eb8874b105 $
72    */
73    @ComponentList(DefaultLinkedResourceHelper.class)
 
74    public class DefaultLinkRefactoringTest
75    {
76    @Rule
77    public MockitoComponentMockingRule<LinkRefactoring> mocker = new MockitoComponentMockingRule<LinkRefactoring>(
78    DefaultLinkRefactoring.class);
79   
80    private XWikiContext xcontext = mock(XWikiContext.class);
81   
82    private EntityReferenceSerializer<String> compactEntityReferenceSerializer;
83   
84    private DocumentReferenceResolver<EntityReference> defaultReferenceDocumentReferenceResolver;
85   
86    private EntityReferenceResolver<ResourceReference> resourceReferenceResolver;
87   
 
88  8 toggle @Before
89    public void configure() throws Exception
90    {
91  8 XWiki xwiki = mock(XWiki.class);
92  8 when(this.xcontext.getWiki()).thenReturn(xwiki);
93   
94  8 Provider<XWikiContext> xcontextProvider = this.mocker.getInstance(XWikiContext.TYPE_PROVIDER);
95  8 when(xcontextProvider.get()).thenReturn(this.xcontext);
96   
97  8 this.resourceReferenceResolver =
98    this.mocker.getInstance(DefaultResourceReferenceEntityReferenceResolver.TYPE_RESOURCEREFERENCE);
99  8 this.defaultReferenceDocumentReferenceResolver =
100    this.mocker.getInstance(DocumentReferenceResolver.TYPE_REFERENCE);
101  8 this.compactEntityReferenceSerializer =
102    this.mocker.getInstance(EntityReferenceSerializer.TYPE_STRING, "compact");
103   
104  8 Provider<ComponentManager> contextComponentManagerProvider =
105    this.mocker.registerMockComponent(
106    new DefaultParameterizedType(null, Provider.class, ComponentManager.class), "context");
107  8 when(contextComponentManagerProvider.get()).thenReturn(this.mocker);
108    }
109   
 
110  1 toggle @Test
111    public void updateRelativeLinks() throws Exception
112    {
113  1 DocumentReference oldReference = new DocumentReference("wiki", "A", "B");
114  1 DocumentReference newReference = new DocumentReference("wiki", "X", "Y");
115   
116  1 XWikiDocument newDocument = mock(XWikiDocument.class);
117  1 when(this.xcontext.getWiki().getDocument(newReference, this.xcontext)).thenReturn(newDocument);
118  1 when(newDocument.getDocumentReference()).thenReturn(newReference);
119  1 when(newDocument.getSyntax()).thenReturn(Syntax.XWIKI_2_1);
120  1 this.mocker.registerMockComponent(BlockRenderer.class, Syntax.XWIKI_2_1.toIdString());
121   
122  1 XDOM xdom = mock(XDOM.class);
123  1 when(newDocument.getXDOM()).thenReturn(xdom);
124   
125  1 ResourceReference docLinkReference = new ResourceReference("C", ResourceType.DOCUMENT);
126  1 LinkBlock docLinkBlock = new LinkBlock(Collections.<Block>emptyList(), docLinkReference, false);
127   
128  1 ResourceReference spaceLinkReference = new ResourceReference("Z", ResourceType.SPACE);
129  1 LinkBlock spaceLinkBlock = new LinkBlock(Collections.<Block>emptyList(), spaceLinkReference, false);
130   
131  1 when(xdom.getBlocks(any(), eq(Block.Axes.DESCENDANT)))
132    .thenReturn(Arrays.<Block>asList(docLinkBlock, spaceLinkBlock));
133   
134  1 DocumentReference originalDocLinkReference = new DocumentReference("C", oldReference.getLastSpaceReference());
135  1 when(this.resourceReferenceResolver.resolve(docLinkReference, null, oldReference)).thenReturn(
136    originalDocLinkReference);
137  1 DocumentReference newDocLinkReference = new DocumentReference("C", newReference.getLastSpaceReference());
138  1 when(this.resourceReferenceResolver.resolve(docLinkReference, null, newReference)).thenReturn(
139    newDocLinkReference);
140   
141  1 SpaceReference originalSpaceReference = new SpaceReference("wiki", "Z");
142  1 when(this.resourceReferenceResolver.resolve(spaceLinkReference, null, oldReference)).thenReturn(
143    originalSpaceReference);
144  1 when(this.resourceReferenceResolver.resolve(spaceLinkReference, null, newReference)).thenReturn(
145    originalSpaceReference);
146   
147  1 when(this.compactEntityReferenceSerializer.serialize(originalDocLinkReference, newReference)).thenReturn("A.C");
148   
149  1 this.mocker.getComponentUnderTest().updateRelativeLinks(oldReference, newReference);
150   
151    // Document link block is updated.
152  1 assertEquals("A.C", docLinkBlock.getReference().getReference());
153  1 assertEquals(ResourceType.DOCUMENT, docLinkBlock.getReference().getType());
154    // Space link block stays the same, since they were on the same wiki.
155  1 assertEquals("Z", spaceLinkBlock.getReference().getReference());
156  1 assertEquals(ResourceType.SPACE, spaceLinkBlock.getReference().getType());
157  1 verifyDocumentSave(newDocument, "Updated the relative links.", true);
158    }
159   
 
160  1 toggle @Test
161    public void updateRelativeLinksAcrossWikis() throws Exception
162    {
163  1 DocumentReference oldReference = new DocumentReference("wiki1", "A", "B");
164  1 DocumentReference newReference = new DocumentReference("wiki2", "X", "Y");
165   
166  1 XWikiDocument newDocument = mock(XWikiDocument.class);
167  1 when(this.xcontext.getWiki().getDocument(newReference, this.xcontext)).thenReturn(newDocument);
168  1 when(newDocument.getDocumentReference()).thenReturn(newReference);
169  1 when(newDocument.getSyntax()).thenReturn(Syntax.XWIKI_2_1);
170  1 this.mocker.registerMockComponent(BlockRenderer.class, Syntax.XWIKI_2_1.toIdString());
171   
172  1 XDOM xdom = mock(XDOM.class);
173  1 when(newDocument.getXDOM()).thenReturn(xdom);
174   
175  1 ResourceReference docLinkReference = new ResourceReference("C", ResourceType.DOCUMENT);
176  1 LinkBlock docLinkBlock = new LinkBlock(Collections.<Block>emptyList(), docLinkReference, false);
177   
178  1 ResourceReference spaceLinkReference = new ResourceReference("Z", ResourceType.SPACE);
179  1 LinkBlock spaceLinkBlock = new LinkBlock(Collections.<Block>emptyList(), spaceLinkReference, false);
180   
181  1 when(xdom.getBlocks(any(), eq(Block.Axes.DESCENDANT)))
182    .thenReturn(Arrays.<Block>asList(docLinkBlock, spaceLinkBlock));
183   
184  1 DocumentReference originalDocLinkReference = new DocumentReference("C", oldReference.getLastSpaceReference());
185  1 when(this.resourceReferenceResolver.resolve(docLinkReference, null, oldReference)).thenReturn(
186    originalDocLinkReference);
187  1 DocumentReference newDocLinkReference = new DocumentReference("C", newReference.getLastSpaceReference());
188  1 when(this.resourceReferenceResolver.resolve(docLinkReference, null, newReference)).thenReturn(
189    newDocLinkReference);
190   
191  1 SpaceReference originalSpaceReference = new SpaceReference("wiki1", "Z");
192  1 when(this.resourceReferenceResolver.resolve(spaceLinkReference, null, oldReference)).thenReturn(
193    originalSpaceReference);
194  1 SpaceReference newSpaceReference = new SpaceReference("wiki2", "Z");
195  1 when(this.resourceReferenceResolver.resolve(spaceLinkReference, null, newReference)).thenReturn(
196    newSpaceReference);
197   
198  1 when(this.compactEntityReferenceSerializer.serialize(originalDocLinkReference, newReference)).thenReturn(
199    "wiki1:A.C");
200  1 when(this.compactEntityReferenceSerializer.serialize(originalSpaceReference, newReference)).thenReturn(
201    "wiki1:Z");
202   
203  1 this.mocker.getComponentUnderTest().updateRelativeLinks(oldReference, newReference);
204   
205    // Document link block is updated.
206  1 assertEquals("wiki1:A.C", docLinkBlock.getReference().getReference());
207  1 assertEquals(ResourceType.DOCUMENT, docLinkBlock.getReference().getType());
208    // Space link is also updated, since they were referring entities on a different wiki.
209  1 assertEquals("wiki1:Z", spaceLinkBlock.getReference().getReference());
210  1 assertEquals(ResourceType.SPACE, spaceLinkBlock.getReference().getType());
211  1 verifyDocumentSave(newDocument, "Updated the relative links.", true);
212    }
213   
 
214  1 toggle @Test
215    public void renameLinks() throws Exception
216    {
217  1 DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page");
218  1 XWikiDocument document = mock(XWikiDocument.class);
219  1 when(this.xcontext.getWiki().getDocument(documentReference, this.xcontext)).thenReturn(document);
220  1 when(document.getDocumentReference()).thenReturn(documentReference);
221  1 when(document.getSyntax()).thenReturn(Syntax.XWIKI_2_1);
222  1 this.mocker.registerMockComponent(BlockRenderer.class, Syntax.XWIKI_2_1.toIdString());
223   
224    // From a terminal document to another terminal document.
225  1 DocumentReference oldLinkTarget = new DocumentReference("wiki", "A", "B");
226  1 DocumentReference newLinkTarget = new DocumentReference("wiki", "X", "Y");
227   
228  1 XDOM xdom = mock(XDOM.class);
229  1 when(document.getXDOM()).thenReturn(xdom);
230   
231  1 ResourceReference linkReference = new ResourceReference("A.B", ResourceType.DOCUMENT);
232  1 LinkBlock linkBlock = new LinkBlock(Collections.<Block>emptyList(), linkReference, false);
233  1 when(xdom.getBlocks(any(), eq(Block.Axes.DESCENDANT))).thenReturn(Arrays.<Block>asList(linkBlock));
234   
235  1 when(this.resourceReferenceResolver.resolve(linkReference, null, documentReference)).thenReturn(oldLinkTarget);
236  1 when(this.defaultReferenceDocumentReferenceResolver.resolve(oldLinkTarget)).thenReturn(oldLinkTarget);
237   
238  1 when(this.compactEntityReferenceSerializer.serialize(newLinkTarget, documentReference)).thenReturn("X.Y");
239   
240  1 this.mocker.getComponentUnderTest().renameLinks(documentReference, oldLinkTarget, newLinkTarget);
241   
242  1 assertEquals("X.Y", linkBlock.getReference().getReference());
243  1 assertEquals(ResourceType.DOCUMENT, linkBlock.getReference().getType());
244  1 verifyDocumentSave(document, "Renamed back-links.", false);
245    }
246   
 
247  1 toggle @Test
248    public void renameNonTerminalDocumentLinks() throws Exception
249    {
250  1 DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page");
251  1 XWikiDocument document = mock(XWikiDocument.class);
252  1 when(this.xcontext.getWiki().getDocument(documentReference, this.xcontext)).thenReturn(document);
253  1 when(document.getDocumentReference()).thenReturn(documentReference);
254  1 when(document.getSyntax()).thenReturn(Syntax.XWIKI_2_1);
255  1 this.mocker.registerMockComponent(BlockRenderer.class, Syntax.XWIKI_2_1.toIdString());
256   
257    // From a non-terminal document to another non-terminal document.
258  1 DocumentReference oldLinkTarget = new DocumentReference("wiki", "A", "WebHome");
259  1 DocumentReference newLinkTarget = new DocumentReference("wiki", "X", "WebHome");
260   
261  1 XDOM xdom = mock(XDOM.class);
262  1 when(document.getXDOM()).thenReturn(xdom);
263   
264  1 ResourceReference docLinkReference = new ResourceReference("A.WebHome", ResourceType.DOCUMENT);
265  1 LinkBlock documentLinkBlock = new LinkBlock(Collections.<Block>emptyList(), docLinkReference, false);
266   
267  1 ResourceReference spaceLinkReference = new ResourceReference("A", ResourceType.SPACE);
268  1 LinkBlock spaceLinkBlock = new LinkBlock(Collections.<Block>emptyList(), spaceLinkReference, false);
269   
270  1 when(xdom.getBlocks(any(), eq(Block.Axes.DESCENDANT)))
271    .thenReturn(Arrays.<Block>asList(documentLinkBlock, spaceLinkBlock));
272   
273    // Doc link
274  1 when(this.resourceReferenceResolver.resolve(docLinkReference, null, documentReference)).thenReturn(
275    oldLinkTarget);
276  1 when(this.defaultReferenceDocumentReferenceResolver.resolve(oldLinkTarget)).thenReturn(oldLinkTarget);
277  1 when(this.compactEntityReferenceSerializer.serialize(newLinkTarget, documentReference)).thenReturn("X.WebHome");
278   
279    // Space link
280  1 SpaceReference spaceReference = oldLinkTarget.getLastSpaceReference();
281  1 when(this.resourceReferenceResolver.resolve(spaceLinkReference, null, documentReference)).thenReturn(
282    spaceReference);
283  1 when(this.defaultReferenceDocumentReferenceResolver.resolve(spaceReference)).thenReturn(oldLinkTarget);
284  1 when(this.compactEntityReferenceSerializer.serialize(newLinkTarget.getLastSpaceReference(), documentReference))
285    .thenReturn("X");
286   
287  1 this.mocker.getComponentUnderTest().renameLinks(documentReference, oldLinkTarget, newLinkTarget);
288   
289  1 assertEquals("X.WebHome", documentLinkBlock.getReference().getReference());
290  1 assertEquals(ResourceType.DOCUMENT, documentLinkBlock.getReference().getType());
291  1 assertEquals("X", spaceLinkBlock.getReference().getReference());
292  1 assertEquals(ResourceType.SPACE, spaceLinkBlock.getReference().getType());
293  1 verifyDocumentSave(document, "Renamed back-links.", false);
294    }
295   
 
296  1 toggle @Test
297    public void renameNonTerminalToTerminalDocumentLinks() throws Exception
298    {
299  1 DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page");
300  1 XWikiDocument document = mock(XWikiDocument.class);
301  1 when(this.xcontext.getWiki().getDocument(documentReference, this.xcontext)).thenReturn(document);
302  1 when(document.getDocumentReference()).thenReturn(documentReference);
303  1 when(document.getSyntax()).thenReturn(Syntax.XWIKI_2_1);
304  1 this.mocker.registerMockComponent(BlockRenderer.class, Syntax.XWIKI_2_1.toIdString());
305   
306    // From a non-terminal document to a terminal document.
307  1 DocumentReference oldLinkTarget = new DocumentReference("wiki", "A", "WebHome");
308  1 DocumentReference newLinkTarget = new DocumentReference("wiki", "X", "Y");
309   
310  1 XDOM xdom = mock(XDOM.class);
311  1 when(document.getXDOM()).thenReturn(xdom);
312   
313  1 ResourceReference docLinkReference = new ResourceReference("A.WebHome", ResourceType.DOCUMENT);
314  1 LinkBlock documentLinkBlock = new LinkBlock(Collections.<Block>emptyList(), docLinkReference, false);
315   
316  1 ResourceReference spaceLinkReference = new ResourceReference("A", ResourceType.SPACE);
317  1 LinkBlock spaceLinkBlock = new LinkBlock(Collections.<Block>emptyList(), spaceLinkReference, false);
318   
319  1 when(xdom.getBlocks(any(), eq(Block.Axes.DESCENDANT)))
320    .thenReturn(Arrays.<Block>asList(documentLinkBlock, spaceLinkBlock));
321   
322    // Doc link
323  1 when(this.resourceReferenceResolver.resolve(docLinkReference, null, documentReference)).thenReturn(
324    oldLinkTarget);
325  1 when(this.defaultReferenceDocumentReferenceResolver.resolve(oldLinkTarget)).thenReturn(oldLinkTarget);
326  1 when(this.compactEntityReferenceSerializer.serialize(newLinkTarget, documentReference)).thenReturn("X.Y");
327   
328    // Space link
329  1 SpaceReference spaceReference = oldLinkTarget.getLastSpaceReference();
330  1 when(this.resourceReferenceResolver.resolve(spaceLinkReference, null, documentReference)).thenReturn(
331    spaceReference);
332  1 when(this.defaultReferenceDocumentReferenceResolver.resolve(spaceReference)).thenReturn(oldLinkTarget);
333  1 when(this.compactEntityReferenceSerializer.serialize(newLinkTarget.getLastSpaceReference(), documentReference))
334    .thenReturn("X");
335   
336  1 this.mocker.getComponentUnderTest().renameLinks(documentReference, oldLinkTarget, newLinkTarget);
337   
338    // Note that both resulting renamed back-links are of type document. (i.e. the space link was converted to a doc
339    // link)
340  1 assertEquals("X.Y", documentLinkBlock.getReference().getReference());
341  1 assertEquals(ResourceType.DOCUMENT, documentLinkBlock.getReference().getType());
342  1 assertEquals("X.Y", spaceLinkBlock.getReference().getReference());
343  1 assertEquals(ResourceType.DOCUMENT, spaceLinkBlock.getReference().getType());
344  1 verifyDocumentSave(document, "Renamed back-links.", false);
345    }
346   
 
347  1 toggle @Test
348    public void renameLinksFromMacros() throws Exception
349    {
350  1 DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page");
351  1 XWikiDocument document = mock(XWikiDocument.class);
352  1 when(this.xcontext.getWiki().getDocument(documentReference, this.xcontext)).thenReturn(document);
353  1 when(document.getDocumentReference()).thenReturn(documentReference);
354  1 when(document.getSyntax()).thenReturn(Syntax.XWIKI_2_1);
355  1 this.mocker.registerMockComponent(BlockRenderer.class, Syntax.XWIKI_2_1.toIdString());
356   
357    // From a terminal document to another terminal document.
358  1 DocumentReference oldLinkTarget = new DocumentReference("wiki", "A", "B");
359  1 DocumentReference newLinkTarget = new DocumentReference("wiki", "X", "Y");
360   
361  1 XDOM xdom = mock(XDOM.class);
362  1 when(document.getXDOM()).thenReturn(xdom);
363   
364  1 Map<String, String> includeParameters = new HashMap<String, String>();
365  1 includeParameters.put("reference", "A.B");
366  1 MacroBlock includeMacroBlock1 = new MacroBlock("include", includeParameters, false);
367   
368  1 Map<String, String> includeOldParameters = new HashMap<String, String>();
369  1 includeOldParameters.put("document", "A.B");
370  1 MacroBlock includeMacroBlock2 = new MacroBlock("include", includeOldParameters, false);
371   
372  1 Map<String, String> displayParameters = new HashMap<String, String>();
373  1 displayParameters.put("reference", "A.B");
374  1 MacroBlock displayMacroBlock = new MacroBlock("display", displayParameters, false);
375   
376  1 when(xdom.getBlocks(any(), eq(Block.Axes.DESCENDANT)))
377    .thenReturn(Arrays.<Block>asList(includeMacroBlock1, includeMacroBlock2, displayMacroBlock));
378   
379  1 ResourceReference macroResourceReference = new ResourceReference("A.B", ResourceType.DOCUMENT);
380   
381  1 when(this.resourceReferenceResolver.resolve(macroResourceReference, null, documentReference)).thenReturn(
382    oldLinkTarget);
383  1 when(this.defaultReferenceDocumentReferenceResolver.resolve(oldLinkTarget)).thenReturn(oldLinkTarget);
384  1 when(this.compactEntityReferenceSerializer.serialize(newLinkTarget, documentReference)).thenReturn("X.Y");
385   
386  1 this.mocker.getComponentUnderTest().renameLinks(documentReference, oldLinkTarget, newLinkTarget);
387   
388  1 assertEquals("X.Y", includeMacroBlock1.getParameter("reference"));
389  1 assertEquals("X.Y", includeMacroBlock2.getParameter("document"));
390  1 assertEquals("X.Y", displayMacroBlock.getParameter("reference"));
391  1 verifyDocumentSave(document, "Renamed back-links.", false);
392    }
393   
 
394  1 toggle @Test
395    public void renameLinksFromLinksAndMacros() throws Exception
396    {
397  1 DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page");
398  1 XWikiDocument document = mock(XWikiDocument.class);
399  1 when(this.xcontext.getWiki().getDocument(documentReference, this.xcontext)).thenReturn(document);
400  1 when(document.getDocumentReference()).thenReturn(documentReference);
401  1 when(document.getSyntax()).thenReturn(Syntax.XWIKI_2_1);
402  1 this.mocker.registerMockComponent(BlockRenderer.class, Syntax.XWIKI_2_1.toIdString());
403   
404    // From a terminal document to another terminal document.
405  1 DocumentReference oldLinkTarget = new DocumentReference("wiki", "A", "B");
406  1 DocumentReference newLinkTarget = new DocumentReference("wiki", "X", "Y");
407   
408  1 XDOM xdom = mock(XDOM.class);
409  1 when(document.getXDOM()).thenReturn(xdom);
410   
411  1 Map<String, String> includeParameters = new HashMap<String, String>();
412  1 includeParameters.put("reference", "A.B");
413  1 MacroBlock includeMacroBlock = new MacroBlock("include", includeParameters, false);
414   
415  1 ResourceReference resourceReference = new ResourceReference("A.B", ResourceType.DOCUMENT);
416  1 LinkBlock documentLinkBlock = new LinkBlock(Collections.<Block>emptyList(), resourceReference, false);
417   
418  1 when(xdom.getBlocks(any(), eq(Block.Axes.DESCENDANT)))
419    .thenReturn(Arrays.<Block>asList(includeMacroBlock, documentLinkBlock));
420   
421  1 when(this.resourceReferenceResolver.resolve(resourceReference, null, documentReference)).thenReturn(
422    oldLinkTarget);
423  1 when(this.defaultReferenceDocumentReferenceResolver.resolve(oldLinkTarget)).thenReturn(oldLinkTarget);
424  1 when(this.compactEntityReferenceSerializer.serialize(newLinkTarget, documentReference)).thenReturn("X.Y");
425   
426  1 this.mocker.getComponentUnderTest().renameLinks(documentReference, oldLinkTarget, newLinkTarget);
427   
428  1 assertEquals("X.Y", includeMacroBlock.getParameter("reference"));
429  1 assertEquals("X.Y", documentLinkBlock.getReference().getReference());
430  1 assertEquals(ResourceType.DOCUMENT, documentLinkBlock.getReference().getType());
431  1 verifyDocumentSave(document, "Renamed back-links.", false);
432    }
433   
 
434  10 toggle private void verifyDocumentSave(XWikiDocument document, String comment, boolean minorEdit) throws Exception
435    {
436    // Verify we preserve the content author.
437  10 verify(document).setContentDirty(false);
438    // Verify the version is going to be incremented.
439  10 verify(document).setMetaDataDirty(true);
440  10 verify(this.xcontext.getWiki()).saveDocument(document, comment, minorEdit, this.xcontext);
441    }
442   
 
443  1 toggle @Test
444    public void renameLinksAndTranslations() throws Exception
445    {
446  1 DocumentReference baseDocumentReference = new DocumentReference("wiki", "Space", "Page");
447  1 XWikiDocument baseDocument = mock(XWikiDocument.class);
448  1 when(this.xcontext.getWiki().getDocument(baseDocumentReference, this.xcontext)).thenReturn(baseDocument);
449  1 when(baseDocument.getDocumentReference()).thenReturn(baseDocumentReference);
450  1 this.mocker.registerMockComponent(BlockRenderer.class, Syntax.XWIKI_2_1.toIdString());
451   
452  1 when(baseDocument.getTranslationLocales(xcontext)).thenReturn(Arrays.asList(Locale.FRENCH, Locale.ENGLISH));
453  1 DocumentReference frenchDocumentReference = new DocumentReference("wiki", "Space", "Page", Locale.FRENCH);
454  1 XWikiDocument frenchDocument = mock(XWikiDocument.class);
455  1 when(baseDocument.getTranslatedDocument(Locale.FRENCH, xcontext)).thenReturn(frenchDocument);
456  1 when(frenchDocument.getDocumentReference()).thenReturn(frenchDocumentReference);
457   
458  1 DocumentReference englishDocumentReference = new DocumentReference("wiki", "Space", "Page", Locale.ENGLISH);
459  1 XWikiDocument englishDocument = mock(XWikiDocument.class);
460  1 when(baseDocument.getTranslatedDocument(Locale.ENGLISH, xcontext)).thenReturn(englishDocument);
461  1 when(englishDocument.getDocumentReference()).thenReturn(englishDocumentReference);
462   
463    // From a terminal document to another terminal document.
464  1 DocumentReference oldLinkTarget = new DocumentReference("wiki", "A", "B");
465  1 DocumentReference newLinkTarget = new DocumentReference("wiki", "X", "Y");
466   
467  1 List<XWikiDocument> documentsToUpdate = Arrays.asList(baseDocument, frenchDocument, englishDocument);
468   
469  1 for (XWikiDocument xWikiDocument : documentsToUpdate) {
470  3 DocumentReference documentReference = xWikiDocument.getDocumentReference();
471  3 when(xWikiDocument.getSyntax()).thenReturn(Syntax.XWIKI_2_1);
472  3 XDOM xdom = mock(XDOM.class);
473  3 when(xWikiDocument.getXDOM()).thenReturn(xdom);
474   
475  3 ResourceReference linkReference = new ResourceReference("A.B", ResourceType.DOCUMENT);
476  3 LinkBlock linkBlock = new LinkBlock(Collections.<Block>emptyList(), linkReference, false);
477  3 when(xdom.getBlocks(any(), eq(Block.Axes.DESCENDANT))).thenReturn(Arrays.<Block>asList(linkBlock));
478   
479  3 when(this.resourceReferenceResolver.resolve(linkReference, null, documentReference)).thenReturn(oldLinkTarget);
480  3 when(this.defaultReferenceDocumentReferenceResolver.resolve(oldLinkTarget)).thenReturn(oldLinkTarget);
481   
482  3 when(this.compactEntityReferenceSerializer.serialize(newLinkTarget, documentReference)).thenReturn("X.Y");
483    }
484  1 this.mocker.getComponentUnderTest().renameLinks(baseDocumentReference, oldLinkTarget, newLinkTarget);
485   
486  1 for (XWikiDocument xWikiDocument : documentsToUpdate) {
487  3 verifyDocumentSave(xWikiDocument, "Renamed back-links.", false);
488  3 LinkBlock linkBlock = (LinkBlock) xWikiDocument.getXDOM().
489    getBlocks(mock(BlockMatcher.class), Block.Axes.DESCENDANT).get(0);
490  3 assertEquals("X.Y", linkBlock.getReference().getReference());
491  3 assertEquals(ResourceType.DOCUMENT, linkBlock.getReference().getType());
492    }
493    }
494    }