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