Search in sources :

Example 61 with AttachmentReference

use of org.xwiki.model.reference.AttachmentReference in project xwiki-platform by xwiki.

the class MockitoDownloadActionTest method renderWhenAttachmentIsInANestedSpace.

@Test
public void renderWhenAttachmentIsInANestedSpace() throws Exception {
    DownloadAction action = new DownloadAction();
    XWikiContext xcontext = this.oldcore.getXWikiContext();
    // Set the Request URL
    XWikiServletRequestStub request = new XWikiServletRequestStub();
    request.setRequestURI("http://localhost:8080/xwiki/bin/download/space1/space2/page/file.ext");
    xcontext.setRequest(request);
    // Setup the returned attachment
    XWikiAttachment attachment = mock(XWikiAttachment.class);
    when(attachment.getContentSize(xcontext)).thenReturn(100);
    Date now = new Date();
    when(attachment.getDate()).thenReturn(now);
    when(attachment.getFilename()).thenReturn("file.ext");
    when(attachment.getContentInputStream(xcontext)).thenReturn(new ByteArrayInputStream("test".getBytes()));
    when(attachment.getMimeType(xcontext)).thenReturn("mimetype");
    // Set the current doc
    XWikiDocument document = mock(XWikiDocument.class);
    when(document.getAttachment("file.ext")).thenReturn(attachment);
    xcontext.setDoc(document);
    // Set the Plugin Manager
    XWikiPluginManager pluginManager = mock(XWikiPluginManager.class);
    when(pluginManager.downloadAttachment(attachment, xcontext)).thenReturn(attachment);
    doReturn(pluginManager).when(this.oldcore.getSpyXWiki()).getPluginManager();
    // Set the Response
    XWikiResponse response = mock(XWikiResponse.class);
    StubServletOutputStream ssos = new StubServletOutputStream();
    when(response.getOutputStream()).thenReturn(ssos);
    xcontext.setResponse(response);
    // Set the Resource Reference Manager used to parse the URL and extract the attachment name
    ResourceReferenceManager rrm = this.oldcore.getMocker().registerMockComponent(ResourceReferenceManager.class);
    when(rrm.getResourceReference()).thenReturn(new EntityResourceReference(new AttachmentReference("file.ext", new DocumentReference("wiki", Arrays.asList("space1", "space2"), "page")), EntityResourceAction.VIEW));
    // Note: we don't give PR and the attachment is not an authorized mime type.
    assertNull(action.render(xcontext));
    // This is the test, we verify what is set in the response
    verify(response).setContentType("mimetype");
    verify(response).setHeader("Accept-Ranges", "bytes");
    verify(response).addHeader("Content-disposition", "attachment; filename*=utf-8''file.ext");
    verify(response).setDateHeader("Last-Modified", now.getTime());
    verify(response).setContentLength(100);
    assertEquals("test", ssos.baos.toString());
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) XWikiContext(com.xpn.xwiki.XWikiContext) EntityResourceReference(org.xwiki.resource.entity.EntityResourceReference) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Date(java.util.Date) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ByteArrayInputStream(java.io.ByteArrayInputStream) XWikiPluginManager(com.xpn.xwiki.plugin.XWikiPluginManager) ResourceReferenceManager(org.xwiki.resource.ResourceReferenceManager) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 62 with AttachmentReference

use of org.xwiki.model.reference.AttachmentReference in project xwiki-platform by xwiki.

the class AttachmentQueryFilter method filterResults.

@Override
public List filterResults(List results) {
    // We need at least 2 columns in the select: the document full name and the attachment file name.
    if (results.size() > 0 && results.get(0).getClass().isArray()) {
        List<AttachmentReference> attachmentReferences = new ArrayList<>();
        for (Object result : results) {
            Object[] actualResult = (Object[]) result;
            String documentFullName = String.valueOf(actualResult[0]);
            String attachmentFileName = String.valueOf(actualResult[1]);
            DocumentReference documentReference = this.documentReferenceResolver.resolve(documentFullName);
            attachmentReferences.add(new AttachmentReference(attachmentFileName, documentReference));
        }
        return attachmentReferences;
    }
    return results;
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) ArrayList(java.util.ArrayList) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 63 with AttachmentReference

use of org.xwiki.model.reference.AttachmentReference in project xwiki-platform by xwiki.

the class FilesystemAttachmentStoreTest method setUp.

@Before
@Override
public void setUp() throws Exception {
    super.setUp();
    getMockery().setImposteriser(ClassImposteriser.INSTANCE);
    Utils.setComponentManager(this.getComponentManager());
    this.mockContext = getMockery().mock(XWikiContext.class);
    final XWiki mockXWiki = getMockery().mock(XWiki.class);
    this.mockHibernate = getMockery().mock(XWikiHibernateStore.class);
    final XWikiAttachmentContent mockDirtyContent = getMockery().mock(XWikiAttachmentContent.class);
    this.mockAttachVersionStore = getMockery().mock(AttachmentVersioningStore.class);
    this.mockArchive = getMockery().mock(XWikiAttachmentArchive.class);
    this.mockHibernateSession = getMockery().mock(Session.class);
    this.doc = new XWikiDocument(new DocumentReference("xwiki", "Main", "WebHome"));
    this.mockAttachReference = new AttachmentReference("file.name", doc.getDocumentReference());
    this.mockAttach = getMockery().mock(XWikiAttachment.class);
    getMockery().checking(new Expectations() {

        {
            allowing(mockContext).getWiki();
            will(returnValue(mockXWiki));
            allowing(mockXWiki).getStore();
            will(returnValue(mockHibernate));
            allowing(mockXWiki).getHibernateStore();
            will(returnValue(mockHibernate));
            allowing(mockHibernate).checkHibernate(mockContext);
            allowing(mockHibernate).beginTransaction(mockContext);
            allowing(mockHibernate).getSession(mockContext);
            will(returnValue(mockHibernateSession));
            allowing(mockXWiki).getDefaultAttachmentArchiveStore();
            will(returnValue(mockAttachVersionStore));
            allowing(mockAttachVersionStore).saveArchive(mockArchive, mockContext, false);
            allowing(mockAttach).getContentInputStream(mockContext);
            will(returnValue(HELLO_STREAM));
            allowing(mockAttach).setDoc(doc);
            allowing(mockAttach).getDoc();
            will(returnValue(doc));
            allowing(mockAttach).getFilename();
            will(returnValue(mockAttachReference.getName()));
            allowing(mockAttach).getReference();
            will(returnValue(mockAttachReference));
            allowing(mockAttach).updateContentArchive(mockContext);
            allowing(mockAttach).getAttachment_archive();
            will(returnValue(mockArchive));
            allowing(mockAttach).isArchiveStoreSet();
            will(returnValue(false));
            allowing(mockAttach).getArchiveStore();
            will(returnValue("file"));
            allowing(mockAttach).getAttachment_content();
            will(returnValue(mockDirtyContent));
            allowing(mockAttach).isContentStoreSet();
            will(returnValue(false));
            allowing(mockAttach).getContentStore();
            will(returnValue("file"));
            allowing(mockAttach).isContentDirty();
            will(returnValue(true));
            allowing(mockDirtyContent).isContentDirty();
            will(returnValue(true));
            allowing(mockAttach).addNameModifiedListener(with(any(AttachmentNameChanged.class)));
            allowing(mockAttach).removeNameModifiedListener(with(any(AttachmentNameChanged.class)));
        }
    });
    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());
    this.attachStore = new FilesystemAttachmentStore();
    FieldUtils.writeField(this.attachStore, "fileTools", this.fileTools, true);
    this.storeFile = this.fileTools.getAttachmentFileProvider(this.mockAttachReference).getAttachmentContentFile();
    HELLO_STREAM.reset();
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) Expectations(org.jmock.Expectations) FileStringEntityReferenceSerializer(org.xwiki.store.internal.FileStringEntityReferenceSerializer) AttachmentVersioningStore(com.xpn.xwiki.store.AttachmentVersioningStore) XWikiAttachmentArchive(com.xpn.xwiki.doc.XWikiAttachmentArchive) FilesystemStoreTools(org.xwiki.store.filesystem.internal.FilesystemStoreTools) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) XWikiAttachmentContent(com.xpn.xwiki.doc.XWikiAttachmentContent) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiHibernateStore(com.xpn.xwiki.store.XWikiHibernateStore) AttachmentNameChanged(com.xpn.xwiki.doc.XWikiAttachment.AttachmentNameChanged) DummyLockProvider(org.xwiki.store.locks.dummy.internal.DummyLockProvider) File(java.io.File) DocumentReference(org.xwiki.model.reference.DocumentReference) Session(org.hibernate.Session) Before(org.junit.Before)

Example 64 with AttachmentReference

use of org.xwiki.model.reference.AttachmentReference in project xwiki-platform by xwiki.

the class ZipExplorerTest method createAttachment.

private XWikiAttachment createAttachment(String filename, byte[] content, XWikiDocument document) throws Exception {
    Mock mockAttachment = mock(XWikiAttachment.class);
    mockAttachment.stubs().method("getFilename").will(returnValue(filename));
    mockAttachment.stubs().method("getDoc").will(returnValue(document));
    mockAttachment.stubs().method("getAuthor").will(returnValue("Vincent"));
    mockAttachment.stubs().method("getAuthorReference").will(returnValue(new DocumentReference("wiki", "XWiki", "Vincent")));
    mockAttachment.stubs().method("getDate").will(returnValue(new Date()));
    mockAttachment.stubs().method("getFilesize").will(returnValue((content == null) ? 0 : content.length));
    mockAttachment.stubs().method("getContentSize").will(returnValue((content == null) ? 0 : content.length));
    mockAttachment.stubs().method("getContent").will(returnValue((content == null) ? new byte[0] : content));
    mockAttachment.stubs().method("getContentInputStream").will(returnValue(new ByteArrayInputStream((content == null) ? new byte[0] : content)));
    mockAttachment.stubs().method("getReference").will(returnValue(new AttachmentReference(filename, document.getDocumentReference())));
    return (XWikiAttachment) mockAttachment.proxy();
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) ByteArrayInputStream(java.io.ByteArrayInputStream) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Mock(org.jmock.Mock) DocumentReference(org.xwiki.model.reference.DocumentReference) Date(java.util.Date)

Aggregations

AttachmentReference (org.xwiki.model.reference.AttachmentReference)64 DocumentReference (org.xwiki.model.reference.DocumentReference)44 Test (org.junit.Test)31 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)17 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)14 ByteArrayInputStream (java.io.ByteArrayInputStream)10 ResourceReference (org.xwiki.rendering.listener.reference.ResourceReference)9 XWikiContext (com.xpn.xwiki.XWikiContext)8 EntityReference (org.xwiki.model.reference.EntityReference)7 Expectations (org.jmock.Expectations)6 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)4 XWikiException (com.xpn.xwiki.XWikiException)3 InputStream (java.io.InputStream)3 URL (java.net.URL)3 Mockery (org.jmock.Mockery)3 JUnit4Mockery (org.jmock.integration.junit4.JUnit4Mockery)3 XDOMOfficeDocument (org.xwiki.officeimporter.document.XDOMOfficeDocument)3 ImageBlock (org.xwiki.rendering.block.ImageBlock)3