@Test public void errorWhenNoAttachment() throws Exception { XWikiDocument workspaceInstallDoc = mock(XWikiDocument.class); DocumentReference workspaceInstallDocRef = new DocumentReference("mainWiki", "WorkspaceManager", "Install"); when(xwiki.getDocument(eq(workspaceInstallDocRef), any(XWikiContext.class))).thenReturn(workspaceInstallDoc); when(workspaceInstallDoc.getAttachment("workspace-template.xar")).thenReturn(null); mocker.getComponentUnderTest().restoreDocumentFromAttachedXAR(new DocumentReference("mainWiki", "WorkspaceManager", "Install"), "workspace-template.xar", docsToRestore); verify(mocker.getMockedLogger()).warn("[{}] has no attachment named [{}].", workspaceInstallDocRef, "workspace-template.xar"); }
@Override public void restoreDocumentFromAttachedXAR(DocumentReference docReference, String attachmentName, List<DocumentReference> documentsToRestore) throws XWikiException { XWikiContext xcontext = xcontextProvider.get(); XWiki xwiki = xcontext.getWiki(); File tempZipFile = null; try { tempZipFile = getTemporaryZipFile(docReference, attachmentName); if (tempZipFile == null) { return; } ZipFile zipFile = new ZipFile(tempZipFile); Iterator<DocumentReference> itDocumentsToRestore = documentsToRestore.iterator(); while (itDocumentsToRestore.hasNext()) { DocumentReference docRef = itDocumentsToRestore.next(); String fileNameToRestore = String.format("%s/%s.xml", docRef.getLastSpaceReference().getName(), docRef.getName()); ZipArchiveEntry zipEntry = zipFile.getEntry(fileNameToRestore); if (zipEntry != null) { XWikiDocument docToRestore = xwiki.getDocument(docRef, xcontext); docToRestore.fromXML(zipFile.getInputStream(zipEntry)); xwiki.saveDocument(docToRestore, xcontext); itDocumentsToRestore.remove(); } } zipFile.close(); } catch (IOException e) { logger.error("Error during the decompression of [{}].", attachmentName, e); } finally { if (tempZipFile != null) { tempZipFile.delete(); } } }
DefaultDocumentRestorerFromAttachedXAR implements DocumentRestorerFromAttachedXAR { @Override public void restoreDocumentFromAttachedXAR(DocumentReference docReference, String attachmentName, List<DocumentReference> documentsToRestore) throws XWikiException { XWikiContext xcontext = xcontextProvider.get(); XWiki xwiki = xcontext.getWiki(); File tempZipFile = null; try { tempZipFile = getTemporaryZipFile(docReference, attachmentName); if (tempZipFile == null) { return; } ZipFile zipFile = new ZipFile(tempZipFile); Iterator<DocumentReference> itDocumentsToRestore = documentsToRestore.iterator(); while (itDocumentsToRestore.hasNext()) { DocumentReference docRef = itDocumentsToRestore.next(); String fileNameToRestore = String.format("%s/%s.xml", docRef.getLastSpaceReference().getName(), docRef.getName()); ZipArchiveEntry zipEntry = zipFile.getEntry(fileNameToRestore); if (zipEntry != null) { XWikiDocument docToRestore = xwiki.getDocument(docRef, xcontext); docToRestore.fromXML(zipFile.getInputStream(zipEntry)); xwiki.saveDocument(docToRestore, xcontext); itDocumentsToRestore.remove(); } } zipFile.close(); } catch (IOException e) { logger.error("Error during the decompression of [{}].", attachmentName, e); } finally { if (tempZipFile != null) { tempZipFile.delete(); } } } }
DefaultDocumentRestorerFromAttachedXAR implements DocumentRestorerFromAttachedXAR { @Override public void restoreDocumentFromAttachedXAR(DocumentReference docReference, String attachmentName, List<DocumentReference> documentsToRestore) throws XWikiException { XWikiContext xcontext = xcontextProvider.get(); XWiki xwiki = xcontext.getWiki(); File tempZipFile = null; try { tempZipFile = getTemporaryZipFile(docReference, attachmentName); if (tempZipFile == null) { return; } ZipFile zipFile = new ZipFile(tempZipFile); Iterator<DocumentReference> itDocumentsToRestore = documentsToRestore.iterator(); while (itDocumentsToRestore.hasNext()) { DocumentReference docRef = itDocumentsToRestore.next(); String fileNameToRestore = String.format("%s/%s.xml", docRef.getLastSpaceReference().getName(), docRef.getName()); ZipArchiveEntry zipEntry = zipFile.getEntry(fileNameToRestore); if (zipEntry != null) { XWikiDocument docToRestore = xwiki.getDocument(docRef, xcontext); docToRestore.fromXML(zipFile.getInputStream(zipEntry)); xwiki.saveDocument(docToRestore, xcontext); itDocumentsToRestore.remove(); } } zipFile.close(); } catch (IOException e) { logger.error("Error during the decompression of [{}].", attachmentName, e); } finally { if (tempZipFile != null) { tempZipFile.delete(); } } }  }
DefaultDocumentRestorerFromAttachedXAR implements DocumentRestorerFromAttachedXAR { @Override public void restoreDocumentFromAttachedXAR(DocumentReference docReference, String attachmentName, List<DocumentReference> documentsToRestore) throws XWikiException { XWikiContext xcontext = xcontextProvider.get(); XWiki xwiki = xcontext.getWiki(); File tempZipFile = null; try { tempZipFile = getTemporaryZipFile(docReference, attachmentName); if (tempZipFile == null) { return; } ZipFile zipFile = new ZipFile(tempZipFile); Iterator<DocumentReference> itDocumentsToRestore = documentsToRestore.iterator(); while (itDocumentsToRestore.hasNext()) { DocumentReference docRef = itDocumentsToRestore.next(); String fileNameToRestore = String.format("%s/%s.xml", docRef.getLastSpaceReference().getName(), docRef.getName()); ZipArchiveEntry zipEntry = zipFile.getEntry(fileNameToRestore); if (zipEntry != null) { XWikiDocument docToRestore = xwiki.getDocument(docRef, xcontext); docToRestore.fromXML(zipFile.getInputStream(zipEntry)); xwiki.saveDocument(docToRestore, xcontext); itDocumentsToRestore.remove(); } } zipFile.close(); } catch (IOException e) { logger.error("Error during the decompression of [{}].", attachmentName, e); } finally { if (tempZipFile != null) { tempZipFile.delete(); } } }  @Override void restoreDocumentFromAttachedXAR(DocumentReference docReference, String attachmentName,
            List<DocumentReference> documentsToRestore); }
DefaultDocumentRestorerFromAttachedXAR implements DocumentRestorerFromAttachedXAR { @Override public void restoreDocumentFromAttachedXAR(DocumentReference docReference, String attachmentName, List<DocumentReference> documentsToRestore) throws XWikiException { XWikiContext xcontext = xcontextProvider.get(); XWiki xwiki = xcontext.getWiki(); File tempZipFile = null; try { tempZipFile = getTemporaryZipFile(docReference, attachmentName); if (tempZipFile == null) { return; } ZipFile zipFile = new ZipFile(tempZipFile); Iterator<DocumentReference> itDocumentsToRestore = documentsToRestore.iterator(); while (itDocumentsToRestore.hasNext()) { DocumentReference docRef = itDocumentsToRestore.next(); String fileNameToRestore = String.format("%s/%s.xml", docRef.getLastSpaceReference().getName(), docRef.getName()); ZipArchiveEntry zipEntry = zipFile.getEntry(fileNameToRestore); if (zipEntry != null) { XWikiDocument docToRestore = xwiki.getDocument(docRef, xcontext); docToRestore.fromXML(zipFile.getInputStream(zipEntry)); xwiki.saveDocument(docToRestore, xcontext); itDocumentsToRestore.remove(); } } zipFile.close(); } catch (IOException e) { logger.error("Error during the decompression of [{}].", attachmentName, e); } finally { if (tempZipFile != null) { tempZipFile.delete(); } } }  @Override void restoreDocumentFromAttachedXAR(DocumentReference docReference, String attachmentName,
            List<DocumentReference> documentsToRestore);  }