@Test
  public void testSetContentViaOutputStream() throws Exception {
    int attachLength = 20;
    int seed = (int) System.currentTimeMillis();
    final XWikiAttachment attach = new XWikiAttachment();
    final InputStream ris = new RandomInputStream(attachLength, seed);
    attach.setContent(ris);
    assertTrue(
        IOUtils.contentEquals(
            new RandomInputStream(attachLength, seed),
            attach.getAttachment_content().getContentInputStream()));
    // Now write to the attachment via an OutputStream.
    final XWikiAttachmentContent xac = attach.getAttachment_content();
    xac.setContentDirty(false);
    final OutputStream os = xac.getContentOutputStream();

    // Adding content with seed+1 will make a radically different set of content.
    IOUtils.copy(new RandomInputStream(attachLength, seed + 1), os);

    // It should still be the old content.
    assertTrue(
        IOUtils.contentEquals(
            new RandomInputStream(attachLength, seed), xac.getContentInputStream()));
    assertFalse(xac.isContentDirty());

    os.close();

    // Now it should be the new content.
    assertTrue(
        IOUtils.contentEquals(
            new RandomInputStream(attachLength, seed + 1), xac.getContentInputStream()));
    assertTrue(xac.isContentDirty());
  }
  @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()));
  }