Search in sources :

Example 6 with DocumentAccessBridge

use of org.xwiki.bridge.DocumentAccessBridge in project xwiki-platform by xwiki.

the class XWikiLinkLabelGeneratorTest method generateWhithRegexpSyntax.

@Test
public void generateWhithRegexpSyntax() throws Exception {
    ResourceReference resourceReference = new DocumentResourceReference("HelloWorld");
    DocumentReference documentReference = new DocumentReference("$0", "\\", "$0");
    EntityReferenceResolver<ResourceReference> resourceReferenceResolver = this.mocker.getInstance(new DefaultParameterizedType(null, EntityReferenceResolver.class, ResourceReference.class));
    when(resourceReferenceResolver.resolve(resourceReference, EntityType.DOCUMENT)).thenReturn(documentReference);
    DocumentAccessBridge dab = this.mocker.getInstance(DocumentAccessBridge.class);
    DocumentModelBridge dmb = mock(DocumentModelBridge.class);
    when(dab.getTranslatedDocumentInstance(documentReference)).thenReturn(dmb);
    when(dmb.getTitle()).thenReturn("$0");
    EntityReferenceSerializer<String> localSerializer = this.mocker.getInstance(EntityReferenceSerializer.TYPE_STRING, "local");
    when(localSerializer.serialize(new SpaceReference("$0", "\\"))).thenReturn("\\");
    assertEquals("%l%la%n%na%N%NA [$0:\\.$0] \\ $0 $0 $0 ($0) [$0:\\.$0] \\ $0 $0 $0 ($0)", this.mocker.getComponentUnderTest().generate(resourceReference));
}
Also used : DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) EntityReferenceResolver(org.xwiki.model.reference.EntityReferenceResolver) SpaceReference(org.xwiki.model.reference.SpaceReference) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) DocumentResourceReference(org.xwiki.rendering.listener.reference.DocumentResourceReference) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) DocumentResourceReference(org.xwiki.rendering.listener.reference.DocumentResourceReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 7 with DocumentAccessBridge

use of org.xwiki.bridge.DocumentAccessBridge in project xwiki-platform by xwiki.

the class DefaultOfficeResourceViewerTest method configure.

/**
 * Test fixture.
 *
 * @throws Exception in case of an exception raised during the fixture preparation
 */
@Before
public void configure() throws Exception {
    final CacheManager cacheManager = mocker.getInstance(CacheManager.class);
    attachmentCache = mock(Cache.class, "attachment");
    externalCache = mock(Cache.class, "external");
    when(cacheManager.<OfficeDocumentView>createNewCache(notNull(CacheConfiguration.class))).thenReturn(attachmentCache, externalCache);
    EntityReferenceSerializer<String> entityReferenceSerializer = mocker.getInstance(EntityReferenceSerializer.TYPE_STRING);
    when(entityReferenceSerializer.serialize(ATTACHMENT_REFERENCE)).thenReturn(STRING_ATTACHMENT_REFERENCE);
    when(entityReferenceSerializer.serialize(ATTACHMENT_REFERENCE.getDocumentReference())).thenReturn(STRING_DOCUMENT_REFERENCE);
    AttachmentReferenceResolver<String> attachmentReferenceResolver = mocker.getInstance(AttachmentReferenceResolver.TYPE_STRING, "current");
    when(attachmentReferenceResolver.resolve(STRING_ATTACHMENT_REFERENCE)).thenReturn(ATTACHMENT_REFERENCE);
    this.resourceReferenceSerializer = mocker.getInstance(ResourceReferenceTypeSerializer.class);
    when(this.resourceReferenceSerializer.serialize(ATTACHMENT_RESOURCE_REFERENCE)).thenReturn(STRING_ATTACHMENT_RESOURCE_REFERENCE);
    ConverterManager converterManager = mocker.getInstance(ConverterManager.class);
    when(converterManager.convert(boolean.class, null)).thenReturn(false);
    when(converterManager.convert(DocumentReference.class, ATTACHMENT_REFERENCE.getDocumentReference())).thenReturn(ATTACHMENT_REFERENCE.getDocumentReference());
    documentAccessBridge = mocker.getInstance(DocumentAccessBridge.class);
    officeDocumentBuilder = mocker.getInstance(XDOMOfficeDocumentBuilder.class);
    OfficeServer officeServer = mocker.getInstance(OfficeServer.class);
    OfficeConverter officeConverter = mock(OfficeConverter.class);
    when(officeServer.getConverter()).thenReturn(officeConverter);
    when(officeConverter.getFormatRegistry()).thenReturn(new DefaultDocumentFormatRegistry());
}
Also used : DefaultDocumentFormatRegistry(org.artofsolving.jodconverter.document.DefaultDocumentFormatRegistry) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) OfficeServer(org.xwiki.officeimporter.server.OfficeServer) OfficeConverter(org.xwiki.officeimporter.converter.OfficeConverter) ConverterManager(org.xwiki.properties.ConverterManager) ResourceReferenceTypeSerializer(org.xwiki.rendering.renderer.reference.ResourceReferenceTypeSerializer) CacheManager(org.xwiki.cache.CacheManager) XDOMOfficeDocumentBuilder(org.xwiki.officeimporter.builder.XDOMOfficeDocumentBuilder) CacheConfiguration(org.xwiki.cache.config.CacheConfiguration) Cache(org.xwiki.cache.Cache) Before(org.junit.Before)

Example 8 with DocumentAccessBridge

use of org.xwiki.bridge.DocumentAccessBridge in project xwiki-platform by xwiki.

the class DefaultPresentationBuilderTest method build.

@Test
public void build() throws Exception {
    DocumentReference documentReference = new DocumentReference("wiki", Arrays.asList("Path", "To"), "Page");
    when(this.entityReferenceSerializer.serialize(documentReference)).thenReturn("wiki:Path.To.Page");
    DocumentModelBridge document = mock(DocumentModelBridge.class);
    DocumentAccessBridge dab = this.mocker.getInstance(DocumentAccessBridge.class);
    when(dab.getTranslatedDocumentInstance(documentReference)).thenReturn(document);
    when(document.getSyntax()).thenReturn(Syntax.XWIKI_2_1);
    InputStream officeFileStream = new ByteArrayInputStream("Presentation content".getBytes());
    Map<String, byte[]> artifacts = new HashMap<String, byte[]>();
    byte[] firstSlide = "first slide".getBytes();
    byte[] secondSlide = "second slide".getBytes();
    artifacts.put("img0.jpg", firstSlide);
    artifacts.put("img0.html", new byte[0]);
    artifacts.put("text0.html", new byte[0]);
    artifacts.put("img1.jpg", secondSlide);
    artifacts.put("img1.html", new byte[0]);
    artifacts.put("text1.html", new byte[0]);
    when(this.officeConverter.convert(Collections.singletonMap("file.odp", officeFileStream), "file.odp", "img0.html")).thenReturn(artifacts);
    HTMLCleanerConfiguration config = mock(HTMLCleanerConfiguration.class);
    when(this.officeHTMLCleaner.getDefaultConfiguration()).thenReturn(config);
    Document xhtmlDoc = XMLUtils.createDOMDocument();
    xhtmlDoc.appendChild(xhtmlDoc.createElement("html"));
    String presentationHTML = "<p><img src=\"file-slide0.jpg\"/></p><p><img src=\"file-slide1.jpg\"/></p>";
    when(this.officeHTMLCleaner.clean(any(Reader.class), eq(config))).then(returnMatchingDocument(presentationHTML, xhtmlDoc));
    XDOM galleryContent = new XDOM(Collections.<Block>emptyList());
    when(this.xhtmlParser.parse(any(Reader.class))).thenReturn(galleryContent);
    XDOMOfficeDocument result = this.mocker.getComponentUnderTest().build(officeFileStream, "file.odp", documentReference);
    verify(config).setParameters(Collections.singletonMap("targetDocument", "wiki:Path.To.Page"));
    Map<String, byte[]> expectedArtifacts = new HashMap<String, byte[]>();
    expectedArtifacts.put("file-slide0.jpg", firstSlide);
    expectedArtifacts.put("file-slide1.jpg", secondSlide);
    assertEquals(expectedArtifacts, result.getArtifacts());
    assertEquals("wiki:Path.To.Page", result.getContentDocument().getMetaData().getMetaData(MetaData.BASE));
    List<ExpandedMacroBlock> macros = result.getContentDocument().getBlocks(new ClassBlockMatcher(ExpandedMacroBlock.class), Block.Axes.CHILD);
    Assert.assertEquals(1, macros.size());
    Assert.assertEquals("gallery", macros.get(0).getId());
    Assert.assertEquals(galleryContent, macros.get(0).getChildren().get(0));
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) Reader(java.io.Reader) Document(org.w3c.dom.Document) XDOMOfficeDocument(org.xwiki.officeimporter.document.XDOMOfficeDocument) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) ByteArrayInputStream(java.io.ByteArrayInputStream) ExpandedMacroBlock(org.xwiki.rendering.block.ExpandedMacroBlock) ClassBlockMatcher(org.xwiki.rendering.block.match.ClassBlockMatcher) DocumentReference(org.xwiki.model.reference.DocumentReference) XDOMOfficeDocument(org.xwiki.officeimporter.document.XDOMOfficeDocument) HTMLCleanerConfiguration(org.xwiki.xml.html.HTMLCleanerConfiguration) Test(org.junit.Test)

Example 9 with DocumentAccessBridge

use of org.xwiki.bridge.DocumentAccessBridge in project xwiki-platform by xwiki.

the class OfficeMacroImporterTest method buildXDOM.

@Test
public void buildXDOM() throws Exception {
    DocumentAccessBridge documentAccessBridge = this.mocker.getInstance(DocumentAccessBridge.class);
    DocumentReference documentReference = new DocumentReference("wiki", "space", "page");
    XWikiDocument xwikiDocument = mock(XWikiDocument.class);
    when(documentAccessBridge.getTranslatedDocumentInstance(documentReference)).thenReturn(xwikiDocument);
    when(xwikiDocument.getSyntax()).thenReturn(Syntax.XWIKI_2_1);
    XDOM xdom = this.mocker.getComponentUnderTest().buildXDOM(new AttachmentReference("file", documentReference), true);
    assertNotNull(xdom);
    // Verify we have the proper METADATA set
    assertEquals("xwiki/2.1", ((Syntax) xdom.getMetaData().getMetaData(MetaData.SYNTAX)).toIdString());
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XDOM(org.xwiki.rendering.block.XDOM) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 10 with DocumentAccessBridge

use of org.xwiki.bridge.DocumentAccessBridge in project xwiki-platform by xwiki.

the class DefaultCSRFTokenTest method userIsLogged.

/**
 * Add a mocking role to have a logged user.
 * @throws Exception if problems occur
 */
private void userIsLogged() throws Exception {
    // document access bridge
    final DocumentAccessBridge mockDocumentAccessBridge = getComponentManager().getInstance(DocumentAccessBridge.class);
    getMockery().checking(new Expectations() {

        {
            allowing(mockDocumentAccessBridge).getCurrentUserReference();
            will(returnValue(new DocumentReference("mainWiki", "XWiki", "Admin")));
        }
    });
}
Also used : Expectations(org.jmock.Expectations) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) DocumentReference(org.xwiki.model.reference.DocumentReference)

Aggregations

DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)56 DocumentReference (org.xwiki.model.reference.DocumentReference)39 Test (org.junit.Test)33 Expectations (org.jmock.Expectations)17 DocumentModelBridge (org.xwiki.bridge.DocumentModelBridge)15 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)11 Before (org.junit.Before)10 XDOM (org.xwiki.rendering.block.XDOM)10 Execution (org.xwiki.context.Execution)7 SpaceReference (org.xwiki.model.reference.SpaceReference)7 ResourceReference (org.xwiki.rendering.listener.reference.ResourceReference)7 EntityReferenceResolver (org.xwiki.model.reference.EntityReferenceResolver)6 MacroBlock (org.xwiki.rendering.block.MacroBlock)6 DocumentResourceReference (org.xwiki.rendering.listener.reference.DocumentResourceReference)6 HashMap (java.util.HashMap)5 VelocityContext (org.apache.velocity.VelocityContext)5 Event (org.xwiki.eventstream.Event)5 DefaultEvent (org.xwiki.eventstream.internal.DefaultEvent)5 VelocityEngine (org.xwiki.velocity.VelocityEngine)5 VelocityManager (org.xwiki.velocity.VelocityManager)5