@Test void updateRelativeLinks() throws Exception { DocumentReference oldReference = new DocumentReference("wiki", "A", "B"); AttachmentReference oldImageTargetAttachment = new AttachmentReference("attachment.txt", oldReference); DocumentReference newReference = new DocumentReference("wiki", "X", "Y"); AttachmentReference newImageTargetAttachment = new AttachmentReference("attachment.txt", newReference); XWikiDocument newDocument = mock(XWikiDocument.class); when(this.xcontext.getWiki().getDocument(newReference, this.xcontext)).thenReturn(newDocument); when(newDocument.getDocumentReference()).thenReturn(newReference); when(newDocument.getSyntax()).thenReturn(Syntax.XWIKI_2_1); ResourceReference docLinkReference = new ResourceReference("C", ResourceType.DOCUMENT); LinkBlock docLinkBlock = new LinkBlock(Collections.emptyList(), docLinkReference, false); ResourceReference spaceLinkReference = new ResourceReference("Z", ResourceType.SPACE); LinkBlock spaceLinkBlock = new LinkBlock(Collections.emptyList(), spaceLinkReference, false); ResourceReference imageReference = new AttachmentResourceReference("attachment.txt"); ImageBlock imageBlock = new ImageBlock(imageReference, false); when(newDocument.getXDOM()) .thenReturn(new XDOM(Arrays.asList(docLinkBlock, spaceLinkBlock, imageBlock))); ResourceReference xobjectDocLinkReference = new ResourceReference("C", ResourceType.DOCUMENT); LinkBlock xobjectDocLinkBlock = new LinkBlock(Collections.emptyList(), xobjectDocLinkReference, false); ResourceReference xobjectSpaceLinkReference = new ResourceReference("Z", ResourceType.SPACE); LinkBlock xobjectSpaceLinkBlock = new LinkBlock(Collections.emptyList(), xobjectSpaceLinkReference, false); ResourceReference xobjectImageReference = new AttachmentResourceReference("attachment.txt"); ImageBlock xobjectImageBlock = new ImageBlock(imageReference, false); setTextarea(newDocument, new XDOM(Arrays.asList(xobjectDocLinkBlock, xobjectSpaceLinkBlock, xobjectImageBlock))); DocumentReference originalDocLinkReference = new DocumentReference("C", oldReference.getLastSpaceReference()); when(this.resourceReferenceResolver.resolve(docLinkReference, null, oldReference)) .thenReturn(originalDocLinkReference); when(this.resourceReferenceResolver.resolve(xobjectDocLinkReference, null, oldReference)) .thenReturn(originalDocLinkReference); when(this.resourceReferenceResolver.resolve(imageReference, null, oldReference)) .thenReturn(oldImageTargetAttachment); DocumentReference newDocLinkReference = new DocumentReference("C", newReference.getLastSpaceReference()); when(this.resourceReferenceResolver.resolve(docLinkReference, null, newReference)) .thenReturn(newDocLinkReference); when(this.resourceReferenceResolver.resolve(xobjectDocLinkReference, null, newReference)) .thenReturn(newDocLinkReference); when(this.resourceReferenceResolver.resolve(imageReference, null, newReference)) .thenReturn(newImageTargetAttachment); SpaceReference originalSpaceReference = new SpaceReference("wiki", "Z"); when(this.resourceReferenceResolver.resolve(spaceLinkReference, null, oldReference)) .thenReturn(originalSpaceReference); when(this.resourceReferenceResolver.resolve(xobjectSpaceLinkReference, null, oldReference)) .thenReturn(originalSpaceReference); when(this.resourceReferenceResolver.resolve(spaceLinkReference, null, newReference)) .thenReturn(originalSpaceReference); when(this.resourceReferenceResolver.resolve(xobjectSpaceLinkReference, null, newReference)) .thenReturn(originalSpaceReference); when(this.compactEntityReferenceSerializer.serialize(originalDocLinkReference, newReference)).thenReturn("A.C"); this.refactoring.updateRelativeLinks(oldReference, newReference); assertEquals("A.C", docLinkBlock.getReference().getReference()); assertEquals(ResourceType.DOCUMENT, docLinkBlock.getReference().getType()); assertEquals("Z", spaceLinkBlock.getReference().getReference()); assertEquals(ResourceType.SPACE, spaceLinkBlock.getReference().getType()); assertEquals("attachment.txt", imageBlock.getReference().getReference()); assertEquals("A.C", xobjectDocLinkBlock.getReference().getReference()); assertEquals(ResourceType.DOCUMENT, xobjectDocLinkBlock.getReference().getType()); assertEquals("Z", xobjectSpaceLinkBlock.getReference().getReference()); assertEquals(ResourceType.SPACE, xobjectSpaceLinkBlock.getReference().getType()); assertEquals("attachment.txt", xobjectImageBlock.getReference().getReference()); verifyDocumentSave(newDocument, "Updated the relative links.", true, true); }
@Override public void updateRelativeLinks(DocumentReference oldReference, DocumentReference newReference) { XWikiContext xcontext = this.xcontextProvider.get(); try { XWikiDocument document = xcontext.getWiki().getDocument(newReference, xcontext); renameLinks(document, oldReference, document.getDocumentReference(), xcontext, true); } catch (XWikiException e) { this.logger.error("Failed to update the relative links from [{}].", newReference, e); } }
DefaultLinkRefactoring implements LinkRefactoring { @Override public void updateRelativeLinks(DocumentReference oldReference, DocumentReference newReference) { XWikiContext xcontext = this.xcontextProvider.get(); try { XWikiDocument document = xcontext.getWiki().getDocument(newReference, xcontext); renameLinks(document, oldReference, document.getDocumentReference(), xcontext, true); } catch (XWikiException e) { this.logger.error("Failed to update the relative links from [{}].", newReference, e); } } }
DefaultLinkRefactoring implements LinkRefactoring { @Override public void updateRelativeLinks(DocumentReference oldReference, DocumentReference newReference) { XWikiContext xcontext = this.xcontextProvider.get(); try { XWikiDocument document = xcontext.getWiki().getDocument(newReference, xcontext); renameLinks(document, oldReference, document.getDocumentReference(), xcontext, true); } catch (XWikiException e) { this.logger.error("Failed to update the relative links from [{}].", newReference, e); } }  }
DefaultLinkRefactoring implements LinkRefactoring { @Override public void updateRelativeLinks(DocumentReference oldReference, DocumentReference newReference) { XWikiContext xcontext = this.xcontextProvider.get(); try { XWikiDocument document = xcontext.getWiki().getDocument(newReference, xcontext); renameLinks(document, oldReference, document.getDocumentReference(), xcontext, true); } catch (XWikiException e) { this.logger.error("Failed to update the relative links from [{}].", newReference, e); } }  @Override void renameLinks(DocumentReference documentReference, DocumentReference oldLinkTarget,
        DocumentReference newLinkTarget); @Override void updateRelativeLinks(DocumentReference oldReference, DocumentReference newReference); }
DefaultLinkRefactoring implements LinkRefactoring { @Override public void updateRelativeLinks(DocumentReference oldReference, DocumentReference newReference) { XWikiContext xcontext = this.xcontextProvider.get(); try { XWikiDocument document = xcontext.getWiki().getDocument(newReference, xcontext); renameLinks(document, oldReference, document.getDocumentReference(), xcontext, true); } catch (XWikiException e) { this.logger.error("Failed to update the relative links from [{}].", newReference, e); } }  @Override void renameLinks(DocumentReference documentReference, DocumentReference oldLinkTarget,
        DocumentReference newLinkTarget); @Override void updateRelativeLinks(DocumentReference oldReference, DocumentReference newReference);  }