use of org.xwiki.model.reference.AttachmentReference in project xwiki-platform by xwiki.
the class CurrentAttachmentReferenceResolverTest method resolveTest.
@Test
public void resolveTest() throws Exception {
// Mocks
EntityReference entityReference = new EntityReference("hello.txt", EntityType.ATTACHMENT);
AttachmentReference attachmentReference = new AttachmentReference("hello.txt", new DocumentReference("Document", new SpaceReference("Space", new WikiReference("wiki"))));
when(entityReferenceResolver.resolve(entityReference, EntityType.ATTACHMENT)).thenReturn(attachmentReference);
// Test
AttachmentReference result = mocker.getComponentUnderTest().resolve(entityReference);
// Verify
assertEquals(attachmentReference, result);
}
use of org.xwiki.model.reference.AttachmentReference in project xwiki-platform by xwiki.
the class IntegrationTests method initialize.
@RenderingTestSuite.Initialized
public void initialize(MockingComponentManager componentManager) throws Exception {
Mockery mockery = new JUnit4Mockery();
// Document Access Bridge Mock
final DocumentAccessBridge mockDocumentAccessBridge = componentManager.registerMockComponent(mockery, DocumentAccessBridge.class);
// Image Storage Mock
final ImageStorage mockImageStorage = componentManager.registerMockComponent(mockery, ImageStorage.class);
// Configuration Mock
final FormulaMacroConfiguration mockConfiguration = componentManager.registerMockComponent(mockery, FormulaMacroConfiguration.class);
mockery.checking(new Expectations() {
{
atLeast(1).of(mockDocumentAccessBridge).getCurrentDocumentReference();
DocumentReference documentReference = new DocumentReference("wiki", "space", "page");
will(returnValue(documentReference));
AttachmentReference attachmentReference = new AttachmentReference("06fbba0acf130efd9e147fdfe91a943cc4f3e29972c6cd1d972e9aabf0900966", documentReference);
atLeast(2).of(mockDocumentAccessBridge).getAttachmentURL(attachmentReference, false);
will(returnValue("/xwiki/bin/view/space/page/06fbba0acf130efd9e147fdfe91a943cc4f3e29972c6cd1d972e9aabf0900966"));
atLeast(2).of(mockConfiguration).getRenderer();
will(returnValue("snuggletex"));
atLeast(2).of(mockImageStorage).get(with(any(String.class)));
will(returnValue(null));
atLeast(2).of(mockImageStorage).put(with(any(String.class)), with(any(ImageData.class)));
}
});
}
use of org.xwiki.model.reference.AttachmentReference in project xwiki-platform by xwiki.
the class ExplicitStringAttachmentReferenceResolverTest method resolveWithExplicitAttachmentReference.
@Test
public void resolveWithExplicitAttachmentReference() {
DocumentReference documentReference = new DocumentReference("wiki", "space", "page");
AttachmentReference reference = this.resolver.resolve("", new AttachmentReference("file", documentReference));
Assert.assertEquals("file", reference.getName());
Assert.assertEquals(documentReference, reference.getDocumentReference());
}
use of org.xwiki.model.reference.AttachmentReference in project xwiki-platform by xwiki.
the class ImageFilter method filterImageSource.
private void filterImageSource(Attr source, DocumentReference targetDocumentReference) {
String fileName = null;
try {
fileName = getFileName(source);
} catch (Exception e) {
this.logger.warn("Failed to extract the image file name. Root cause is [{}]", ExceptionUtils.getRootCauseMessage(e));
this.logger.debug("Full stacktrace is: ", e);
}
if (StringUtils.isEmpty(fileName)) {
return;
}
// Set image source attribute relative to the reference document.
AttachmentReference attachmentReference = new AttachmentReference(fileName, targetDocumentReference);
source.setValue(this.documentAccessBridge.getAttachmentURL(attachmentReference, false));
ResourceReference imageReference = new ResourceReference(fileName, ResourceType.ATTACHMENT);
imageReference.setTyped(false);
Comment beforeComment = source.getOwnerDocument().createComment(XMLUtils.escapeXMLComment("startimage:" + this.xhtmlMarkerSerializer.serialize(imageReference)));
Comment afterComment = source.getOwnerDocument().createComment("stopimage");
Element image = source.getOwnerElement();
image.getParentNode().insertBefore(beforeComment, image);
image.getParentNode().insertBefore(afterComment, image.getNextSibling());
}
use of org.xwiki.model.reference.AttachmentReference in project xwiki-platform by xwiki.
the class ImageFilterTest method filterCollectsEmbeddedImages.
@Test
public void filterCollectsEmbeddedImages() {
AttachmentReference attachmentReference = new AttachmentReference("foo.png", this.documentReference);
when(this.dab.getAttachmentURL(attachmentReference, false)).thenReturn("/path/to/foo.png");
ResourceReference resourceReference = new ResourceReference("foo.png", ResourceType.ATTACHMENT);
resourceReference.setTyped(false);
when(this.xhtmlMarkerSerializer.serialize(resourceReference)).thenReturn("false|-|attach|-|foo.png");
String fileName = DataUri.parse("data:image/jpeg;base64,GgoAAAAN==", Charset.forName("UTF-8")).hashCode() + ".jpg";
attachmentReference = new AttachmentReference(fileName, this.documentReference);
when(this.dab.getAttachmentURL(attachmentReference, false)).thenReturn("/path/to/" + fileName);
resourceReference = new ResourceReference(fileName, ResourceType.ATTACHMENT);
resourceReference.setTyped(false);
when(this.xhtmlMarkerSerializer.serialize(resourceReference)).thenReturn("false|-|attach|-|" + fileName);
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("targetDocument", "Path.To.Page");
parameters.put("attachEmbeddedImages", "true");
Document document = filterAndAssertOutput("<img src=\"data:image/png;fileName=foo.png;base64,iVBORw0K==\"/>" + "<img src=\"data:image/jpeg;base64,GgoAAAAN==\"/>", parameters, "<!--startimage:false|-|attach|-|foo.png--><img src=\"/path/to/foo.png\"/><!--stopimage-->" + "<!--startimage:false|-|attach|-|" + fileName + "--><img src=\"/path/to/" + fileName + "\"/><!--stopimage-->");
@SuppressWarnings("unchecked") Map<String, byte[]> embeddedImages = (Map<String, byte[]>) document.getUserData("embeddedImages");
assertEquals(new HashSet<String>(Arrays.asList("foo.png", fileName)), embeddedImages.keySet());
}
Aggregations