@Test void renameLinksAndTranslations() throws Exception { DocumentReference baseDocumentReference = new DocumentReference("wiki", "Space", "Page"); XWikiDocument baseDocument = mock(XWikiDocument.class); when(this.xcontext.getWiki().getDocument(baseDocumentReference, this.xcontext)).thenReturn(baseDocument); when(baseDocument.getDocumentReference()).thenReturn(baseDocumentReference); when(baseDocument.getTranslationLocales(xcontext)).thenReturn(Arrays.asList(Locale.FRENCH, Locale.ENGLISH)); DocumentReference frenchDocumentReference = new DocumentReference("wiki", "Space", "Page", Locale.FRENCH); XWikiDocument frenchDocument = mock(XWikiDocument.class); when(baseDocument.getTranslatedDocument(Locale.FRENCH, xcontext)).thenReturn(frenchDocument); when(frenchDocument.getDocumentReference()).thenReturn(frenchDocumentReference); DocumentReference englishDocumentReference = new DocumentReference("wiki", "Space", "Page", Locale.ENGLISH); XWikiDocument englishDocument = mock(XWikiDocument.class); when(baseDocument.getTranslatedDocument(Locale.ENGLISH, xcontext)).thenReturn(englishDocument); when(englishDocument.getDocumentReference()).thenReturn(englishDocumentReference); DocumentReference oldLinkTarget = new DocumentReference("wiki", "A", "B"); DocumentReference newLinkTarget = new DocumentReference("wiki", "X", "Y"); List<XWikiDocument> documentsToUpdate = Arrays.asList(baseDocument, frenchDocument, englishDocument); for (XWikiDocument xWikiDocument : documentsToUpdate) { DocumentReference documentReference = xWikiDocument.getDocumentReference(); when(xWikiDocument.getSyntax()).thenReturn(Syntax.XWIKI_2_1); XDOM xdom = mock(XDOM.class); when(xWikiDocument.getXDOM()).thenReturn(xdom); ResourceReference linkReference = new ResourceReference("A.B", ResourceType.DOCUMENT); LinkBlock linkBlock = new LinkBlock(Collections.emptyList(), linkReference, false); when(xdom.getBlocks(any(), eq(Block.Axes.DESCENDANT))).thenReturn(Arrays.asList(linkBlock)); when(this.resourceReferenceResolver.resolve(linkReference, null, documentReference)) .thenReturn(oldLinkTarget); when(this.defaultReferenceDocumentReferenceResolver.resolve(oldLinkTarget)).thenReturn(oldLinkTarget); when(this.compactEntityReferenceSerializer.serialize(newLinkTarget, documentReference)).thenReturn("X.Y"); } this.refactoring.renameLinks(baseDocumentReference, oldLinkTarget, newLinkTarget); for (XWikiDocument xWikiDocument : documentsToUpdate) { verifyDocumentSave(xWikiDocument, "Renamed back-links.", false, false); LinkBlock linkBlock = (LinkBlock) xWikiDocument.getXDOM().getBlocks(mock(BlockMatcher.class), Block.Axes.DESCENDANT).get(0); assertEquals("X.Y", linkBlock.getReference().getReference()); assertEquals(ResourceType.DOCUMENT, linkBlock.getReference().getType()); } }
@Override public void renameLinks(DocumentReference documentReference, DocumentReference oldLinkTarget, DocumentReference newLinkTarget) { boolean popLevelProgress = false; XWikiContext xcontext = this.xcontextProvider.get(); String previousWikiId = xcontext.getWikiId(); try { xcontext.setWikiId(documentReference.getWikiReference().getName()); XWikiDocument document = xcontext.getWiki().getDocument(documentReference, xcontext); List<Locale> locales = document.getTranslationLocales(xcontext); this.progressManager.pushLevelProgress(1 + locales.size(), this); popLevelProgress = true; this.progressManager.startStep(this); renameLinks(document, oldLinkTarget, newLinkTarget, xcontext, false); this.progressManager.endStep(this); for (Locale locale : locales) { this.progressManager.startStep(this); renameLinks(document.getTranslatedDocument(locale, xcontext), oldLinkTarget, newLinkTarget, xcontext, false); this.progressManager.endStep(this); } } catch (XWikiException e) { this.logger.error("Failed to rename the links that target [{}] from [{}].", oldLinkTarget, documentReference, e); } finally { if (popLevelProgress) { this.progressManager.popLevelProgress(this); } xcontext.setWikiId(previousWikiId); } }
DefaultLinkRefactoring implements LinkRefactoring { @Override public void renameLinks(DocumentReference documentReference, DocumentReference oldLinkTarget, DocumentReference newLinkTarget) { boolean popLevelProgress = false; XWikiContext xcontext = this.xcontextProvider.get(); String previousWikiId = xcontext.getWikiId(); try { xcontext.setWikiId(documentReference.getWikiReference().getName()); XWikiDocument document = xcontext.getWiki().getDocument(documentReference, xcontext); List<Locale> locales = document.getTranslationLocales(xcontext); this.progressManager.pushLevelProgress(1 + locales.size(), this); popLevelProgress = true; this.progressManager.startStep(this); renameLinks(document, oldLinkTarget, newLinkTarget, xcontext, false); this.progressManager.endStep(this); for (Locale locale : locales) { this.progressManager.startStep(this); renameLinks(document.getTranslatedDocument(locale, xcontext), oldLinkTarget, newLinkTarget, xcontext, false); this.progressManager.endStep(this); } } catch (XWikiException e) { this.logger.error("Failed to rename the links that target [{}] from [{}].", oldLinkTarget, documentReference, e); } finally { if (popLevelProgress) { this.progressManager.popLevelProgress(this); } xcontext.setWikiId(previousWikiId); } } }
DefaultLinkRefactoring implements LinkRefactoring { @Override public void renameLinks(DocumentReference documentReference, DocumentReference oldLinkTarget, DocumentReference newLinkTarget) { boolean popLevelProgress = false; XWikiContext xcontext = this.xcontextProvider.get(); String previousWikiId = xcontext.getWikiId(); try { xcontext.setWikiId(documentReference.getWikiReference().getName()); XWikiDocument document = xcontext.getWiki().getDocument(documentReference, xcontext); List<Locale> locales = document.getTranslationLocales(xcontext); this.progressManager.pushLevelProgress(1 + locales.size(), this); popLevelProgress = true; this.progressManager.startStep(this); renameLinks(document, oldLinkTarget, newLinkTarget, xcontext, false); this.progressManager.endStep(this); for (Locale locale : locales) { this.progressManager.startStep(this); renameLinks(document.getTranslatedDocument(locale, xcontext), oldLinkTarget, newLinkTarget, xcontext, false); this.progressManager.endStep(this); } } catch (XWikiException e) { this.logger.error("Failed to rename the links that target [{}] from [{}].", oldLinkTarget, documentReference, e); } finally { if (popLevelProgress) { this.progressManager.popLevelProgress(this); } xcontext.setWikiId(previousWikiId); } }  }
DefaultLinkRefactoring implements LinkRefactoring { @Override public void renameLinks(DocumentReference documentReference, DocumentReference oldLinkTarget, DocumentReference newLinkTarget) { boolean popLevelProgress = false; XWikiContext xcontext = this.xcontextProvider.get(); String previousWikiId = xcontext.getWikiId(); try { xcontext.setWikiId(documentReference.getWikiReference().getName()); XWikiDocument document = xcontext.getWiki().getDocument(documentReference, xcontext); List<Locale> locales = document.getTranslationLocales(xcontext); this.progressManager.pushLevelProgress(1 + locales.size(), this); popLevelProgress = true; this.progressManager.startStep(this); renameLinks(document, oldLinkTarget, newLinkTarget, xcontext, false); this.progressManager.endStep(this); for (Locale locale : locales) { this.progressManager.startStep(this); renameLinks(document.getTranslatedDocument(locale, xcontext), oldLinkTarget, newLinkTarget, xcontext, false); this.progressManager.endStep(this); } } catch (XWikiException e) { this.logger.error("Failed to rename the links that target [{}] from [{}].", oldLinkTarget, documentReference, e); } finally { if (popLevelProgress) { this.progressManager.popLevelProgress(this); } xcontext.setWikiId(previousWikiId); } }  @Override void renameLinks(DocumentReference documentReference, DocumentReference oldLinkTarget,
        DocumentReference newLinkTarget); @Override void updateRelativeLinks(DocumentReference oldReference, DocumentReference newReference); }
DefaultLinkRefactoring implements LinkRefactoring { @Override public void renameLinks(DocumentReference documentReference, DocumentReference oldLinkTarget, DocumentReference newLinkTarget) { boolean popLevelProgress = false; XWikiContext xcontext = this.xcontextProvider.get(); String previousWikiId = xcontext.getWikiId(); try { xcontext.setWikiId(documentReference.getWikiReference().getName()); XWikiDocument document = xcontext.getWiki().getDocument(documentReference, xcontext); List<Locale> locales = document.getTranslationLocales(xcontext); this.progressManager.pushLevelProgress(1 + locales.size(), this); popLevelProgress = true; this.progressManager.startStep(this); renameLinks(document, oldLinkTarget, newLinkTarget, xcontext, false); this.progressManager.endStep(this); for (Locale locale : locales) { this.progressManager.startStep(this); renameLinks(document.getTranslatedDocument(locale, xcontext), oldLinkTarget, newLinkTarget, xcontext, false); this.progressManager.endStep(this); } } catch (XWikiException e) { this.logger.error("Failed to rename the links that target [{}] from [{}].", oldLinkTarget, documentReference, e); } finally { if (popLevelProgress) { this.progressManager.popLevelProgress(this); } xcontext.setWikiId(previousWikiId); } }  @Override void renameLinks(DocumentReference documentReference, DocumentReference oldLinkTarget,
        DocumentReference newLinkTarget); @Override void updateRelativeLinks(DocumentReference oldReference, DocumentReference newReference);  }