Search in sources :

Example 41 with AttachmentReference

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);
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) SpaceReference(org.xwiki.model.reference.SpaceReference) EntityReference(org.xwiki.model.reference.EntityReference) WikiReference(org.xwiki.model.reference.WikiReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 42 with AttachmentReference

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)));
        }
    });
}
Also used : Expectations(org.jmock.Expectations) AttachmentReference(org.xwiki.model.reference.AttachmentReference) JUnit4Mockery(org.jmock.integration.junit4.JUnit4Mockery) ImageStorage(org.xwiki.formula.ImageStorage) ImageData(org.xwiki.formula.ImageData) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) JUnit4Mockery(org.jmock.integration.junit4.JUnit4Mockery) Mockery(org.jmock.Mockery) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 43 with AttachmentReference

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());
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 44 with AttachmentReference

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());
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) Comment(org.w3c.dom.Comment) Element(org.w3c.dom.Element) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) MimeTypeException(org.apache.tika.mime.MimeTypeException)

Example 45 with AttachmentReference

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());
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) HashMap(java.util.HashMap) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) Document(org.w3c.dom.Document) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

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