Ejemplo n.º 1
0
  @Test
  public void getContentInputStreamFromArchive() throws Exception {
    XWikiDocument document = mock(XWikiDocument.class);
    when(document.getDocumentReference())
        .thenReturn(new DocumentReference("wiki", "Space", "Page"));

    when(this.oldcore
            .getXWikiContext()
            .getWiki()
            .getDocument(document.getDocumentReference(), this.oldcore.getXWikiContext()))
        .thenReturn(document);

    XWikiAttachment attachment = new XWikiAttachment(document, "file.txt");
    attachment.setVersion("3.5");

    XWikiAttachment newAttachment = new XWikiAttachment(document, attachment.getFilename());
    newAttachment.setVersion("5.1");
    when(document.getAttachment(attachment.getFilename())).thenReturn(newAttachment);

    XWikiAttachmentContent content = mock(XWikiAttachmentContent.class);
    when(content.getContentInputStream()).thenReturn(mock(InputStream.class));

    XWikiAttachment archivedAttachment = new XWikiAttachment(document, attachment.getFilename());
    archivedAttachment.setAttachment_content(content);

    XWikiAttachmentArchive archive = mock(XWikiAttachmentArchive.class);
    when(archive.getRevision(attachment, attachment.getVersion(), this.oldcore.getXWikiContext()))
        .thenReturn(archivedAttachment);

    AttachmentVersioningStore store = mock(AttachmentVersioningStore.class);
    when(this.oldcore.getXWikiContext().getWiki().getAttachmentVersioningStore()).thenReturn(store);
    when(store.loadArchive(attachment, this.oldcore.getXWikiContext(), true)).thenReturn(archive);

    assertSame(
        content.getContentInputStream(),
        attachment.getContentInputStream(this.oldcore.getXWikiContext()));
  }
Ejemplo n.º 2
0
  /**
   * Unit test for <a href="http://jira.xwiki.org/browse/XWIKI-9075">XWIKI-9075</a> to prove that
   * calling {@code fromXML} doesn't set the metadata dirty flag.
   *
   * <p>Note: I think there's a bug in that fromXML should return a new instance of XWikiAttachment
   * and not modify the current one as this would mean changing its identity...
   */
  @Test
  public void fromXML() throws Exception {
    XWikiAttachment attachment = new XWikiAttachment();
    attachment.fromXML(
        "<attachment>\n"
            + "<filename>XWikiLogo.png</filename>\n"
            + "<filesize>1390</filesize>\n"
            + "<mimetype>image/png2</mimetype>\n"
            + "<author>xwiki:XWiki.Admin</author>\n"
            + "<date>1252454400000</date>\n"
            + "<version>1.1</version>\n"
            + "<comment/>\n"
            + "<content>MDEyMzQ1Njc4OQ==</content>\n"
            + "</attachment>");

    assertEquals("XWikiLogo.png", attachment.getFilename());
    assertEquals(new Date(1252454400000L), attachment.getDate());
    assertEquals("1.1", attachment.getVersion());
    assertEquals("0123456789", IOUtils.toString(attachment.getContentInputStream(null)));
    assertEquals("image/png2", attachment.getMimeType());
    assertEquals("image/png2", attachment.getMimeType(null));

    assertFalse(attachment.isMetaDataDirty());
  }