Search in sources :

Example 16 with XWikiAttachment

use of com.xpn.xwiki.doc.XWikiAttachment in project xwiki-platform by xwiki.

the class AttachmentMetadataSerializerTest method testParseSerialize.

@Test
public void testParseSerialize() throws Exception {
    final ByteArrayInputStream bais = new ByteArrayInputStream(TEST_CONTENT.getBytes("US-ASCII"));
    final XWikiAttachment attach = this.serializer.parse(bais);
    bais.close();
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    IOUtils.copy(this.serializer.serialize(attach), baos);
    final String test = new String(baos.toByteArray(), "US-ASCII");
    final String control = TEST_CONTENT.replaceAll("[0-9][0-9][0-9]</date>", "000</date>");
    Assert.assertEquals("Parsing and serializing yields a different output.", control, test);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 17 with XWikiAttachment

use of com.xpn.xwiki.doc.XWikiAttachment in project xwiki-platform by xwiki.

the class AttachmentMetadataSerializerTest method testParse.

@Test
public void testParse() throws Exception {
    final ByteArrayInputStream bais = new ByteArrayInputStream(TEST_CONTENT.getBytes("US-ASCII"));
    final XWikiAttachment attach = this.serializer.parse(bais);
    bais.close();
    Assert.assertEquals("Attachment1 had wrong name", "file1", attach.getFilename());
    Assert.assertEquals("Attachment1 had wrong author", "me", attach.getAuthor());
    Assert.assertEquals("Attachment1 had wrong version", "1.1", attach.getVersion());
    Assert.assertEquals("Attachment1 had wrong comment", attach.getComment(), "something whitty");
    // We drop milliseconds for consistency with the database so last 3 digits are 0.
    Assert.assertEquals("Attachment1 had wrong date.", attach.getDate().getTime() + "", "1293045632000");
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Test(org.junit.Test)

Example 18 with XWikiAttachment

use of com.xpn.xwiki.doc.XWikiAttachment in project xwiki-platform by xwiki.

the class ListAttachmentArchive method fromRCS.

/**
 * @param rcsArchive the RCS archive to import.
 * @throws Exception if getting a revision from the RCS archive or deserializing an attachment from XML fails
 */
private void fromRCS(final Archive rcsArchive) throws Exception {
    if (rcsArchive == null) {
        return;
    }
    final Node[] nodes = rcsArchive.changeLog();
    for (int i = nodes.length - 1; i > -1; i--) {
        final Object[] lines = rcsArchive.getRevision(nodes[i].getVersion());
        final StringBuilder content = new StringBuilder();
        for (int j = 0; j < lines.length; j++) {
            String line = lines[j].toString();
            content.append(line);
            if (j != lines.length - 1) {
                content.append("\n");
            }
        }
        final XWikiAttachment rev = new XWikiAttachment();
        rev.fromXML(content.toString());
        rev.setDoc(getAttachment().getDoc());
        rev.setAttachment_archive(this);
        // this should not be necessary, keeping to maintain behavior.
        rev.setVersion(nodes[i].getVersion().toString());
        revisions.add(rev);
    }
}
Also used : Node(org.suigeneris.jrcs.rcs.impl.Node) ToString(org.suigeneris.jrcs.util.ToString) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment)

Example 19 with XWikiAttachment

use of com.xpn.xwiki.doc.XWikiAttachment in project xwiki-platform by xwiki.

the class ListAttachmentArchive method clone.

@Override
public Object clone() {
    final ListAttachmentArchive out = new ListAttachmentArchive(cloneAttachment(this.attachment));
    out.attachment.setAttachment_archive(out);
    for (XWikiAttachment revision : this.revisions) {
        final XWikiAttachment revClone = cloneAttachment(revision);
        revClone.setAttachment_archive(out);
        out.revisions.add(revClone);
    }
    return out;
}
Also used : XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment)

Example 20 with XWikiAttachment

use of com.xpn.xwiki.doc.XWikiAttachment in project xwiki-platform by xwiki.

the class DefaultDocumentRestorerFromAttachedXARTest method restoreDocumentFromAttachedXAR.

@Test
public void restoreDocumentFromAttachedXAR() throws Exception {
    // Mocks
    XWikiDocument workspaceInstallDoc = mock(XWikiDocument.class);
    when(xwiki.getDocument(eq(new DocumentReference("mainWiki", "WorkspaceManager", "Install")), any(XWikiContext.class))).thenReturn(workspaceInstallDoc);
    XWikiAttachment xeXar = mock(XWikiAttachment.class);
    when(workspaceInstallDoc.getAttachment("workspace-template.xar")).thenReturn(xeXar);
    when(xeXar.getContentInputStream(any(XWikiContext.class))).thenReturn(getClass().getResourceAsStream("/test-restore-documents.xar"));
    // Run
    mocker.getComponentUnderTest().restoreDocumentFromAttachedXAR(new DocumentReference("mainWiki", "WorkspaceManager", "Install"), "workspace-template.xar", docsToRestore);
    // Verify the document to restore has been restored from the xar
    verify(docToRestore1).fromXML(any(InputStream.class));
    verify(xwiki, times(1)).saveDocument(docToRestore1, xcontext);
    verify(docToRestore2).fromXML(any(InputStream.class));
    verify(xwiki, times(1)).saveDocument(docToRestore2, xcontext);
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) InputStream(java.io.InputStream) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Aggregations

XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)133 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)71 DocumentReference (org.xwiki.model.reference.DocumentReference)51 Test (org.junit.Test)40 XWikiContext (com.xpn.xwiki.XWikiContext)35 XWikiException (com.xpn.xwiki.XWikiException)25 ByteArrayInputStream (java.io.ByteArrayInputStream)20 Attachment (com.xpn.xwiki.api.Attachment)18 Date (java.util.Date)17 IOException (java.io.IOException)15 ArrayList (java.util.ArrayList)15 Document (com.xpn.xwiki.api.Document)14 XWiki (com.xpn.xwiki.XWiki)13 BaseObject (com.xpn.xwiki.objects.BaseObject)13 AttachmentReference (org.xwiki.model.reference.AttachmentReference)13 InputStream (java.io.InputStream)11 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)10 File (java.io.File)10 URL (java.net.URL)7 List (java.util.List)7