Search in sources :

Example 6 with TemporaryResourceReference

use of org.xwiki.resource.temporary.TemporaryResourceReference in project xwiki-platform by xwiki.

the class DefaultOfficeResourceViewerTest method testViewPresentation.

@Test
public void testViewPresentation() throws Exception {
    AttachmentResourceReference attachResourceRef = new AttachmentResourceReference("xwiki:Some.Page@presentation.odp");
    DocumentReference documentReference = new DocumentReference("wiki", "Some", "Page");
    AttachmentReference attachmentReference = new AttachmentReference("presentation.odp", documentReference);
    AttachmentReferenceResolver<String> attachmentReferenceResolver = mocker.getInstance(AttachmentReferenceResolver.TYPE_STRING, "current");
    when(attachmentReferenceResolver.resolve(attachResourceRef.getReference())).thenReturn(attachmentReference);
    when(documentAccessBridge.getAttachmentReferences(attachmentReference.getDocumentReference())).thenReturn(Arrays.asList(attachmentReference));
    when(documentAccessBridge.getAttachmentVersion(attachmentReference)).thenReturn("3.2");
    ByteArrayInputStream attachmentContent = new ByteArrayInputStream(new byte[256]);
    when(documentAccessBridge.getAttachmentContent(attachmentReference)).thenReturn(attachmentContent);
    ResourceReference imageReference = new ResourceReference("slide0.png", ResourceType.URL);
    ExpandedMacroBlock galleryMacro = new ExpandedMacroBlock("gallery", Collections.singletonMap("width", "300px"), null, false);
    galleryMacro.addChild(new ImageBlock(imageReference, true));
    XDOM xdom = new XDOM(Collections.<Block>singletonList(galleryMacro));
    Map<String, byte[]> artifacts = Collections.singletonMap("slide0.png", new byte[8]);
    XDOMOfficeDocument xdomOfficeDocument = new XDOMOfficeDocument(xdom, artifacts, mocker);
    PresentationBuilder presentationBuilder = mocker.getInstance(PresentationBuilder.class);
    when(presentationBuilder.build(attachmentContent, attachmentReference.getName(), documentReference)).thenReturn(xdomOfficeDocument);
    Map<String, ?> viewParameters = Collections.singletonMap("ownerDocument", documentReference);
    TemporaryResourceReference temporaryResourceReference = new TemporaryResourceReference("officeviewer", Arrays.asList(String.valueOf(viewParameters.hashCode()), "slide0.png"), documentReference);
    Type type = new DefaultParameterizedType(null, ResourceReferenceSerializer.class, TemporaryResourceReference.class, ExtendedURL.class);
    ResourceReferenceSerializer<TemporaryResourceReference, ExtendedURL> urlTemporaryResourceReferenceSerializer = mocker.getInstance(type, "standard/tmp");
    ExtendedURL extendedURL = new ExtendedURL(Arrays.asList("url", "to", "slide0.png"));
    when(urlTemporaryResourceReferenceSerializer.serialize(temporaryResourceReference)).thenReturn(extendedURL);
    XDOM output = this.mocker.getComponentUnderTest().createView(attachResourceRef, viewParameters);
    ImageBlock imageBlock = (ImageBlock) output.getBlocks(new ClassBlockMatcher(ImageBlock.class), Block.Axes.DESCENDANT).get(0);
    assertEquals("/url/to/slide0.png", imageBlock.getReference().getReference());
    galleryMacro = (ExpandedMacroBlock) output.getBlocks(new ClassBlockMatcher(ExpandedMacroBlock.class), Block.Axes.DESCENDANT).get(0);
    assertFalse(galleryMacro.getParent() instanceof XDOM);
    assertEquals(Syntax.XWIKI_2_1, ((MetaDataBlock) galleryMacro.getParent()).getMetaData().getMetaData(MetaData.SYNTAX));
    TemporaryResourceStore store = mocker.getInstance(TemporaryResourceStore.class);
    verify(store).createTemporaryFile(eq(temporaryResourceReference), any(InputStream.class));
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) XDOM(org.xwiki.rendering.block.XDOM) ImageBlock(org.xwiki.rendering.block.ImageBlock) AttachmentResourceReference(org.xwiki.rendering.listener.reference.AttachmentResourceReference) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ExtendedURL(org.xwiki.url.ExtendedURL) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) ResourceType(org.xwiki.rendering.listener.reference.ResourceType) Type(java.lang.reflect.Type) TemporaryResourceReference(org.xwiki.resource.temporary.TemporaryResourceReference) ByteArrayInputStream(java.io.ByteArrayInputStream) ExpandedMacroBlock(org.xwiki.rendering.block.ExpandedMacroBlock) ClassBlockMatcher(org.xwiki.rendering.block.match.ClassBlockMatcher) PresentationBuilder(org.xwiki.officeimporter.builder.PresentationBuilder) TemporaryResourceStore(org.xwiki.resource.temporary.TemporaryResourceStore) TemporaryResourceReference(org.xwiki.resource.temporary.TemporaryResourceReference) AttachmentResourceReference(org.xwiki.rendering.listener.reference.AttachmentResourceReference) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) DocumentReference(org.xwiki.model.reference.DocumentReference) XDOMOfficeDocument(org.xwiki.officeimporter.document.XDOMOfficeDocument) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock) Test(org.junit.Test)

Example 7 with TemporaryResourceReference

use of org.xwiki.resource.temporary.TemporaryResourceReference in project xwiki-platform by xwiki.

the class BatikSVGRasterizer method rasterizeToTemporaryResource.

@Override
public TemporaryResourceReference rasterizeToTemporaryResource(String content, int width, int height, DocumentReference targetContext) throws IOException {
    String fileName = getTemporaryFileName(content, width, height);
    TemporaryResourceReference reference = new TemporaryResourceReference(TEMP_DIR_NAME, fileName, targetContext);
    File out = this.temporaryResourceStore.getTemporaryFile(reference);
    if (rasterizeToFile(content, out, width, height)) {
        return reference;
    }
    return null;
}
Also used : TemporaryResourceReference(org.xwiki.resource.temporary.TemporaryResourceReference) File(java.io.File)

Example 8 with TemporaryResourceReference

use of org.xwiki.resource.temporary.TemporaryResourceReference in project xwiki-platform by xwiki.

the class BatikSVGRasterizerTest method rasterizeToTemporaryResourceReturnsNullWhenBaseTempDirCannotBeCreated.

@Test
public void rasterizeToTemporaryResourceReturnsNullWhenBaseTempDirCannotBeCreated() throws Exception {
    this.baseDirectory.getRoot().mkdirs();
    writeTestFile(new File(this.baseDirectory.getRoot(), "temp"));
    TemporaryResourceReference tref = this.mocker.getComponentUnderTest().rasterizeToTemporaryResource(VALID_SVG, 100, 200);
    Assert.assertNull(tref);
}
Also used : TemporaryResourceReference(org.xwiki.resource.temporary.TemporaryResourceReference) File(java.io.File) Test(org.junit.Test)

Example 9 with TemporaryResourceReference

use of org.xwiki.resource.temporary.TemporaryResourceReference in project xwiki-platform by xwiki.

the class BatikSVGRasterizerTest method rasterizeToTemporaryResourceUsesContextDocument.

@Test
public void rasterizeToTemporaryResourceUsesContextDocument() throws Exception {
    TemporaryResourceReference tref = this.mocker.getComponentUnderTest().rasterizeToTemporaryResource(VALID_SVG, 100, 200);
    Assert.assertEquals("svg", tref.getModuleId());
    Assert.assertTrue(tref.getParameters().isEmpty());
    Assert.assertEquals(this.dref, tref.getOwningEntityReference());
    Assert.assertEquals(RASTER_FILE_NAME, tref.getResourceName());
}
Also used : TemporaryResourceReference(org.xwiki.resource.temporary.TemporaryResourceReference) Test(org.junit.Test)

Example 10 with TemporaryResourceReference

use of org.xwiki.resource.temporary.TemporaryResourceReference in project xwiki-platform by xwiki.

the class BatikSVGRasterizerTest method rasterizeToTemporaryResourceReusesFile.

@Test
public void rasterizeToTemporaryResourceReusesFile() throws Exception {
    writeTestFile(this.rasterFile);
    TemporaryResourceReference tref = this.mocker.getComponentUnderTest().rasterizeToTemporaryResource(VALID_SVG, 100, 200);
    Assert.assertEquals("svg", tref.getModuleId());
    Assert.assertTrue(tref.getParameters().isEmpty());
    Assert.assertEquals(this.dref, tref.getOwningEntityReference());
    Assert.assertEquals(Math.abs(VALID_SVG.hashCode()) + ".png", tref.getResourceName());
    Assert.assertTrue(isTestFile(this.rasterFile));
}
Also used : TemporaryResourceReference(org.xwiki.resource.temporary.TemporaryResourceReference) Test(org.junit.Test)

Aggregations

TemporaryResourceReference (org.xwiki.resource.temporary.TemporaryResourceReference)11 File (java.io.File)6 Test (org.junit.Test)6 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ExpandedMacroBlock (org.xwiki.rendering.block.ExpandedMacroBlock)2 ImageBlock (org.xwiki.rendering.block.ImageBlock)2 MetaDataBlock (org.xwiki.rendering.block.MetaDataBlock)2 ClassBlockMatcher (org.xwiki.rendering.block.match.ClassBlockMatcher)2 ResourceReference (org.xwiki.rendering.listener.reference.ResourceReference)2 TemporaryResourceStore (org.xwiki.resource.temporary.TemporaryResourceStore)2 InputStream (java.io.InputStream)1 Type (java.lang.reflect.Type)1 HashSet (java.util.HashSet)1 Before (org.junit.Before)1 CacheException (org.xwiki.cache.CacheException)1 InitializationException (org.xwiki.component.phase.InitializationException)1 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)1 Container (org.xwiki.container.Container)1 AttachmentReference (org.xwiki.model.reference.AttachmentReference)1 DocumentReference (org.xwiki.model.reference.DocumentReference)1