@Test void createRedirect() throws Exception { DocumentReference oldReference = new DocumentReference("wiki", "Space", "Old"); DocumentReference newReference = new DocumentReference("wiki", "Space", "New"); DocumentReference redirectClassReference = new DocumentReference("wiki", "XWiki", "RedirectClass"); when(this.xcontext.getWiki().exists(redirectClassReference, this.xcontext)).thenReturn(true); XWikiDocument oldDocument = mock(XWikiDocument.class); when(this.xcontext.getWiki().getDocument(oldReference, this.xcontext)).thenReturn(oldDocument); when(oldDocument.getXObject(eq(redirectClassReference), anyInt())).thenReturn(mock(BaseObject.class)); this.modelBridge.createRedirect(oldReference, newReference); verify(oldDocument).setHidden(true); verify(this.xcontext.getWiki()).saveDocument(oldDocument, "Create automatic redirect.", this.xcontext); assertLog(Level.INFO, "Created automatic redirect from [{}] to [{}].", oldReference, newReference); }
@Override public void createRedirect(DocumentReference oldReference, DocumentReference newReference) { XWikiContext xcontext = this.xcontextProvider.get(); DocumentReference redirectClassReference = new DocumentReference(REDIRECT_CLASS_REFERENCE, oldReference.getWikiReference()); if (xcontext.getWiki().exists(redirectClassReference, xcontext)) { try { XWikiDocument oldDocument = xcontext.getWiki().getDocument(oldReference, xcontext); int number = oldDocument.createXObject(redirectClassReference, xcontext); String location = this.defaultEntityReferenceSerializer.serialize(newReference); oldDocument.getXObject(redirectClassReference, number).setStringValue("location", location); oldDocument.setHidden(true); xcontext.getWiki().saveDocument(oldDocument, "Create automatic redirect.", xcontext); this.logger.info("Created automatic redirect from [{}] to [{}].", oldReference, newReference); } catch (XWikiException e) { this.logger.error("Failed to create automatic redirect from [{}] to [{}].", oldReference, newReference, e); } } else { this.logger.warn("We can't create an automatic redirect from [{}] to [{}] because [{}] is missing.", oldReference, newReference, redirectClassReference); } }
DefaultModelBridge implements ModelBridge { @Override public void createRedirect(DocumentReference oldReference, DocumentReference newReference) { XWikiContext xcontext = this.xcontextProvider.get(); DocumentReference redirectClassReference = new DocumentReference(REDIRECT_CLASS_REFERENCE, oldReference.getWikiReference()); if (xcontext.getWiki().exists(redirectClassReference, xcontext)) { try { XWikiDocument oldDocument = xcontext.getWiki().getDocument(oldReference, xcontext); int number = oldDocument.createXObject(redirectClassReference, xcontext); String location = this.defaultEntityReferenceSerializer.serialize(newReference); oldDocument.getXObject(redirectClassReference, number).setStringValue("location", location); oldDocument.setHidden(true); xcontext.getWiki().saveDocument(oldDocument, "Create automatic redirect.", xcontext); this.logger.info("Created automatic redirect from [{}] to [{}].", oldReference, newReference); } catch (XWikiException e) { this.logger.error("Failed to create automatic redirect from [{}] to [{}].", oldReference, newReference, e); } } else { this.logger.warn("We can't create an automatic redirect from [{}] to [{}] because [{}] is missing.", oldReference, newReference, redirectClassReference); } } }
DefaultModelBridge implements ModelBridge { @Override public void createRedirect(DocumentReference oldReference, DocumentReference newReference) { XWikiContext xcontext = this.xcontextProvider.get(); DocumentReference redirectClassReference = new DocumentReference(REDIRECT_CLASS_REFERENCE, oldReference.getWikiReference()); if (xcontext.getWiki().exists(redirectClassReference, xcontext)) { try { XWikiDocument oldDocument = xcontext.getWiki().getDocument(oldReference, xcontext); int number = oldDocument.createXObject(redirectClassReference, xcontext); String location = this.defaultEntityReferenceSerializer.serialize(newReference); oldDocument.getXObject(redirectClassReference, number).setStringValue("location", location); oldDocument.setHidden(true); xcontext.getWiki().saveDocument(oldDocument, "Create automatic redirect.", xcontext); this.logger.info("Created automatic redirect from [{}] to [{}].", oldReference, newReference); } catch (XWikiException e) { this.logger.error("Failed to create automatic redirect from [{}] to [{}].", oldReference, newReference, e); } } else { this.logger.warn("We can't create an automatic redirect from [{}] to [{}] because [{}] is missing.", oldReference, newReference, redirectClassReference); } }  }
DefaultModelBridge implements ModelBridge { @Override public void createRedirect(DocumentReference oldReference, DocumentReference newReference) { XWikiContext xcontext = this.xcontextProvider.get(); DocumentReference redirectClassReference = new DocumentReference(REDIRECT_CLASS_REFERENCE, oldReference.getWikiReference()); if (xcontext.getWiki().exists(redirectClassReference, xcontext)) { try { XWikiDocument oldDocument = xcontext.getWiki().getDocument(oldReference, xcontext); int number = oldDocument.createXObject(redirectClassReference, xcontext); String location = this.defaultEntityReferenceSerializer.serialize(newReference); oldDocument.getXObject(redirectClassReference, number).setStringValue("location", location); oldDocument.setHidden(true); xcontext.getWiki().saveDocument(oldDocument, "Create automatic redirect.", xcontext); this.logger.info("Created automatic redirect from [{}] to [{}].", oldReference, newReference); } catch (XWikiException e) { this.logger.error("Failed to create automatic redirect from [{}] to [{}].", oldReference, newReference, e); } } else { this.logger.warn("We can't create an automatic redirect from [{}] to [{}] because [{}] is missing.", oldReference, newReference, redirectClassReference); } }  @Override boolean create(DocumentReference documentReference); @Override boolean copy(DocumentReference source, DocumentReference destination); @Override boolean delete(DocumentReference reference); @Override boolean delete(DocumentReference reference, boolean skipRecycleBin); @Override boolean removeLock(DocumentReference reference); @Override void createRedirect(DocumentReference oldReference, DocumentReference newReference); @Override boolean canOverwriteSilently(DocumentReference documentReference); @Override boolean exists(DocumentReference reference); @Override List<DocumentReference> getBackLinkedReferences(DocumentReference documentReference, String wikiId); @Override List<DocumentReference> getDocumentReferences(SpaceReference spaceReference); @Override boolean updateParentField(final DocumentReference oldParentReference,
        final DocumentReference newParentReference); @Override DocumentReference setContextUserReference(DocumentReference userReference); @Override void update(DocumentReference documentReference, Map<String, String> parameters); @Override boolean restoreDeletedDocument(long deletedDocumentId, AbstractCheckRightsRequest request); @Override List<Long> getDeletedDocumentIds(String batchId); @Override boolean permanentlyDeleteDocument(long deletedDocumentId, AbstractCheckRightsRequest request); @Override boolean permanentlyDeleteAllDocuments(PermanentlyDeleteJob deleteJob, AbstractCheckRightsRequest request); @Override boolean rename(DocumentReference source, DocumentReference destination); }
DefaultModelBridge implements ModelBridge { @Override public void createRedirect(DocumentReference oldReference, DocumentReference newReference) { XWikiContext xcontext = this.xcontextProvider.get(); DocumentReference redirectClassReference = new DocumentReference(REDIRECT_CLASS_REFERENCE, oldReference.getWikiReference()); if (xcontext.getWiki().exists(redirectClassReference, xcontext)) { try { XWikiDocument oldDocument = xcontext.getWiki().getDocument(oldReference, xcontext); int number = oldDocument.createXObject(redirectClassReference, xcontext); String location = this.defaultEntityReferenceSerializer.serialize(newReference); oldDocument.getXObject(redirectClassReference, number).setStringValue("location", location); oldDocument.setHidden(true); xcontext.getWiki().saveDocument(oldDocument, "Create automatic redirect.", xcontext); this.logger.info("Created automatic redirect from [{}] to [{}].", oldReference, newReference); } catch (XWikiException e) { this.logger.error("Failed to create automatic redirect from [{}] to [{}].", oldReference, newReference, e); } } else { this.logger.warn("We can't create an automatic redirect from [{}] to [{}] because [{}] is missing.", oldReference, newReference, redirectClassReference); } }  @Override boolean create(DocumentReference documentReference); @Override boolean copy(DocumentReference source, DocumentReference destination); @Override boolean delete(DocumentReference reference); @Override boolean delete(DocumentReference reference, boolean skipRecycleBin); @Override boolean removeLock(DocumentReference reference); @Override void createRedirect(DocumentReference oldReference, DocumentReference newReference); @Override boolean canOverwriteSilently(DocumentReference documentReference); @Override boolean exists(DocumentReference reference); @Override List<DocumentReference> getBackLinkedReferences(DocumentReference documentReference, String wikiId); @Override List<DocumentReference> getDocumentReferences(SpaceReference spaceReference); @Override boolean updateParentField(final DocumentReference oldParentReference,
        final DocumentReference newParentReference); @Override DocumentReference setContextUserReference(DocumentReference userReference); @Override void update(DocumentReference documentReference, Map<String, String> parameters); @Override boolean restoreDeletedDocument(long deletedDocumentId, AbstractCheckRightsRequest request); @Override List<Long> getDeletedDocumentIds(String batchId); @Override boolean permanentlyDeleteDocument(long deletedDocumentId, AbstractCheckRightsRequest request); @Override boolean permanentlyDeleteAllDocuments(PermanentlyDeleteJob deleteJob, AbstractCheckRightsRequest request); @Override boolean rename(DocumentReference source, DocumentReference destination);  }