@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()); }