Search in sources :

Example 11 with XWikiAttachment

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

the class AttachmentListMetadataSerializer method serialize.

@Override
public void serialize(final List<XWikiAttachment> attachments, final XMLWriter writer) throws IOException {
    final Element docel = new DOMElement(ROOT_ELEMENT_NAME);
    docel.addAttribute(SERIALIZER_PARAM, THIS_SERIALIZER);
    writer.writeOpen(docel);
    for (XWikiAttachment attachment : attachments) {
        this.attachSerializer.serialize(attachment, writer);
    }
    writer.writeClose(docel);
}
Also used : Element(org.dom4j.Element) DOMElement(org.dom4j.dom.DOMElement) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) DOMElement(org.dom4j.dom.DOMElement)

Example 12 with XWikiAttachment

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

the class FilesystemAttachmentStoreTest method documentUpdateOnDeleteTest.

@Test
public void documentUpdateOnDeleteTest() throws Exception {
    final List<XWikiAttachment> attachList = new ArrayList<XWikiAttachment>();
    attachList.add(this.mockAttach);
    this.doc.setAttachmentList(attachList);
    getMockery().checking(new Expectations() {

        {
            oneOf(mockAttachVersionStore).deleteArchive(mockAttach, mockContext, false);
            exactly(2).of(mockHibernateSession).delete(with(any(Object.class)));
            oneOf(mockHibernate).saveXWikiDoc(doc, mockContext, false);
            will(new CustomAction("Make sure the attachment has been removed from the list.") {

                public Object invoke(final Invocation invoc) {
                    final XWikiDocument document = (XWikiDocument) invoc.getParameter(0);
                    Assert.assertTrue("Attachment was not removed from the list.", document.getAttachmentList().size() == 0);
                    return null;
                }
            });
        }
    });
    this.createFile();
    this.attachStore.deleteXWikiAttachment(this.mockAttach, true, this.mockContext, false);
}
Also used : Expectations(org.jmock.Expectations) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) Invocation(org.jmock.api.Invocation) CustomAction(org.jmock.lib.action.CustomAction) ArrayList(java.util.ArrayList) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Test(org.junit.Test)

Example 13 with XWikiAttachment

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

the class FilesystemAttachmentVersioningStoreTest method setUp.

@Before
@Override
public void setUp() throws Exception {
    super.setUp();
    Utils.setComponentManager(this.getComponentManager());
    final File tmpDir = new File(System.getProperty("java.io.tmpdir"));
    this.storageLocation = new File(tmpDir, "test-storage-location");
    this.fileTools = new FilesystemStoreTools(new FileStringEntityReferenceSerializer(), storageLocation, new DummyLockProvider());
    final AttachmentListMetadataSerializer serializer = new AttachmentListMetadataSerializer(new AttachmentMetadataSerializer());
    this.versionStore = new FilesystemAttachmentVersioningStore();
    FieldUtils.writeDeclaredField(this.versionStore, "fileTools", this.fileTools, true);
    FieldUtils.writeDeclaredField(this.versionStore, "metaSerializer", serializer, true);
    final XWikiDocument doc = new XWikiDocument(new DocumentReference("xwiki", "Main", "WebHome"));
    final XWikiAttachment version1 = new XWikiAttachment();
    version1.setVersion("1.1");
    version1.setFilename("attachment.txt");
    version1.setDoc(doc);
    version1.setAttachment_content(new StringAttachmentContent("I am version 1.1"));
    final XWikiAttachment version2 = new XWikiAttachment();
    version2.setVersion("1.2");
    version2.setFilename("attachment.txt");
    version2.setDoc(doc);
    version2.setAttachment_content(new StringAttachmentContent("I am version 1.2"));
    final XWikiAttachment version3 = new XWikiAttachment();
    version3.setVersion("1.3");
    version3.setFilename("attachment.txt");
    version3.setDoc(doc);
    version3.setAttachment_content(new StringAttachmentContent("I am version 1.3"));
    this.provider = this.fileTools.getAttachmentFileProvider(version1.getReference());
    this.archive = new ListAttachmentArchive(new ArrayList<XWikiAttachment>() {

        {
            add(version1);
            add(version2);
            add(version3);
        }
    });
}
Also used : FileStringEntityReferenceSerializer(org.xwiki.store.internal.FileStringEntityReferenceSerializer) FilesystemStoreTools(org.xwiki.store.filesystem.internal.FilesystemStoreTools) ArrayList(java.util.ArrayList) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) AttachmentListMetadataSerializer(org.xwiki.store.serialization.xml.internal.AttachmentListMetadataSerializer) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DummyLockProvider(org.xwiki.store.locks.dummy.internal.DummyLockProvider) File(java.io.File) DocumentReference(org.xwiki.model.reference.DocumentReference) ListAttachmentArchive(org.xwiki.store.legacy.doc.internal.ListAttachmentArchive) AttachmentMetadataSerializer(org.xwiki.store.serialization.xml.internal.AttachmentMetadataSerializer) Before(org.junit.Before)

Example 14 with XWikiAttachment

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

the class FilesystemAttachmentVersioningStoreTest method saveArchiveTest.

@Test
public void saveArchiveTest() throws Exception {
    final XWikiAttachmentContent content = this.archive.getAttachment().getAttachment_content();
    final XWikiAttachment attach = this.archive.getAttachment();
    Assert.assertFalse(this.provider.getAttachmentVersioningMetaFile().exists());
    Assert.assertFalse(this.provider.getAttachmentVersionContentFile("1.1").exists());
    Assert.assertFalse(this.provider.getAttachmentVersionContentFile("1.2").exists());
    Assert.assertFalse(this.provider.getAttachmentVersionContentFile("1.3").exists());
    // Because the context is only used by the legacy implementation, it is safe to pass null.
    this.versionStore.saveArchive(this.archive, null, false);
    Assert.assertTrue(this.provider.getAttachmentVersioningMetaFile().exists());
    // Make sure it's not just:
    // <?xml version="1.0" encoding="UTF-8"?>
    // <attachment-list serializer="attachment-list-meta/1.0">
    // </attachment-list>
    Assert.assertTrue(this.provider.getAttachmentVersioningMetaFile().length() > 120);
    Assert.assertTrue(this.provider.getAttachmentVersionContentFile("1.1").exists());
    Assert.assertTrue(this.provider.getAttachmentVersionContentFile("1.2").exists());
    Assert.assertTrue(this.provider.getAttachmentVersionContentFile("1.3").exists());
    // Prove that the attachment and attachment content are the same after saving.
    Assert.assertSame(attach, this.archive.getAttachment());
    Assert.assertSame(content, this.archive.getAttachment().getAttachment_content());
}
Also used : XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) XWikiAttachmentContent(com.xpn.xwiki.doc.XWikiAttachmentContent) Test(org.junit.Test)

Example 15 with XWikiAttachment

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

the class AttachmentListMetadataSerializerTest method testParse.

@Test
public void testParse() throws Exception {
    final ByteArrayInputStream bais = new ByteArrayInputStream(TEST_CONTENT.getBytes("US-ASCII"));
    final List<XWikiAttachment> attachList = this.serializer.parse(bais);
    bais.close();
    Assert.assertTrue("Attachment list was wrong size", 3 == attachList.size());
    Assert.assertEquals("Attachment1 had wrong name", "file1", attachList.get(0).getFilename());
    Assert.assertEquals("Attachment2 had wrong name", "file1", attachList.get(1).getFilename());
    Assert.assertEquals("Attachment3 had wrong name", "file1", attachList.get(2).getFilename());
    Assert.assertEquals("Attachment1 had wrong author", "me", attachList.get(0).getAuthor());
    Assert.assertEquals("Attachment2 had wrong author", "you", attachList.get(1).getAuthor());
    Assert.assertEquals("Attachment3 had wrong author", "them", attachList.get(2).getAuthor());
    Assert.assertEquals("Attachment1 had wrong version", "1.1", attachList.get(0).getVersion());
    Assert.assertEquals("Attachment2 had wrong version", "1.2", attachList.get(1).getVersion());
    Assert.assertEquals("Attachment3 had wrong version", "1.3", attachList.get(2).getVersion());
    Assert.assertEquals("Attachment1 had wrong comment", attachList.get(0).getComment(), "something whitty");
    Assert.assertEquals("Attachment2 had wrong comment", "a comment", attachList.get(1).getComment());
    Assert.assertEquals("Attachment3 had wrong comment", "i saved it", attachList.get(2).getComment());
    // We drop milliseconds for consistency with the database so last 3 digits are 0.
    Assert.assertEquals("Attachment1 had wrong date.", attachList.get(0).getDate().getTime() + "", "1293045632000");
    Assert.assertEquals("Attachment2 had wrong date.", attachList.get(1).getDate().getTime() + "", "1293789456000");
    Assert.assertEquals("Attachment3 had wrong date.", attachList.get(2).getDate().getTime() + "", "1293012345000");
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) 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