Search in sources :

Example 16 with DocumentModelBridge

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

the class SheetScriptService method getReadOnlyDocument.

/**
 * Note: This method accesses the low level XWiki document through reflection in order to bypass programming rights.
 *
 * @param document an instance of {@link Document} received from a script
 * @return an instance of {@link DocumentModelBridge} that wraps the low level document object exposed by the given
 *         document API and that <em>must</em> not be modified
 */
private DocumentModelBridge getReadOnlyDocument(Document document) {
    try {
        // HACK: We try to access the XWikiDocument instance wrapped by the document API using reflection because we
        // want to bypass the programming rights requirements.
        Field docField = Document.class.getDeclaredField("doc");
        docField.setAccessible(true);
        return (DocumentModelBridge) docField.get(document);
    } catch (Exception e) {
        throw new RuntimeException("Failed to access the XWikiDocument instance wrapped by the document API.", e);
    }
}
Also used : Field(java.lang.reflect.Field) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge)

Example 17 with DocumentModelBridge

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

the class DocumentTitleDisplayerTest method fallbackOnSpaceNameWhenSpaceHomePageTitleIsEmpty.

@Test
public void fallbackOnSpaceNameWhenSpaceHomePageTitleIsEmpty() throws Exception {
    EntityReferenceProvider defaultEntityReferenceProvider = this.mocker.getInstance(EntityReferenceProvider.class);
    when(defaultEntityReferenceProvider.getDefaultReference(EntityType.DOCUMENT)).thenReturn(new EntityReference("Page", EntityType.DOCUMENT));
    DocumentModelBridge document = mock(DocumentModelBridge.class);
    when(document.getDocumentReference()).thenReturn(new DocumentReference("wiki", Arrays.asList("Space"), "Page"));
    XDOM titleXDOM = new XDOM(Arrays.asList(new WordBlock("Space")));
    Parser plainTextParser = this.mocker.getInstance(Parser.class, "plain/1.0");
    when(plainTextParser.parse(any(StringReader.class))).thenReturn(titleXDOM);
    DocumentDisplayerParameters params = new DocumentDisplayerParameters();
    params.setTitleDisplayed(true);
    assertSame(titleXDOM, this.mocker.getComponentUnderTest().display(document, params));
    ArgumentCaptor<Reader> argument = ArgumentCaptor.forClass(Reader.class);
    verify(plainTextParser).parse(argument.capture());
    assertEquals("Space", IOUtils.toString(argument.getValue()));
}
Also used : EntityReferenceProvider(org.xwiki.model.reference.EntityReferenceProvider) XDOM(org.xwiki.rendering.block.XDOM) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) WordBlock(org.xwiki.rendering.block.WordBlock) EntityReference(org.xwiki.model.reference.EntityReference) StringReader(java.io.StringReader) Reader(java.io.Reader) StringReader(java.io.StringReader) DocumentReference(org.xwiki.model.reference.DocumentReference) Parser(org.xwiki.rendering.parser.Parser) Test(org.junit.Test)

Example 18 with DocumentModelBridge

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

the class DisplayMacroTest method testDisplayMacroWithRecursiveDisplay.

@Test
public void testDisplayMacroWithRecursiveDisplay() throws Exception {
    final DocumentDisplayer mockDocumentDisplayer = getMockery().mock(DocumentDisplayer.class);
    this.displayMacro.setDocumentAccessBridge(mockSetup.bridge);
    FieldUtils.writeField(this.displayMacro, "documentDisplayer", mockDocumentDisplayer, true);
    final MacroTransformationContext macroContext = createMacroTransformationContext("wiki:space.page", false);
    final DisplayMacroParameters parameters = new DisplayMacroParameters();
    parameters.setReference("wiki:space.page");
    getMockery().checking(new Expectations() {

        {
            allowing(mockDocumentReferenceResolver).resolve("wiki:space.page", macroContext.getCurrentMacroBlock());
            will(returnValue(new DocumentReference("wiki", "space", "page")));
            allowing(mockSetup.bridge).isDocumentViewable(with(any(DocumentReference.class)));
            will(returnValue(true));
            allowing(mockSetup.bridge).getDocumentInstance(with(any(DocumentReference.class)));
            will(returnValue(null));
            allowing(mockDocumentDisplayer).display(with(same((DocumentModelBridge) null)), with(any(DocumentDisplayerParameters.class)));
            will(new CustomAction("recursively call the include macro again") {

                @Override
                public Object invoke(Invocation invocation) throws Throwable {
                    try {
                        displayMacro.execute(parameters, null, macroContext);
                    } catch (Exception expected) {
                        if (expected.getMessage().contains("Found recursive display")) {
                            throw new ExpectedRecursiveInclusionException();
                        }
                    }
                    return true;
                }
            });
        }
    });
    try {
        this.displayMacro.execute(parameters, null, macroContext);
        Assert.fail("The display macro hasn't checked the recursive display");
    } catch (MacroExecutionException expected) {
        if (!(expected.getCause() instanceof ExpectedRecursiveInclusionException)) {
            throw expected;
        }
    }
}
Also used : Expectations(org.jmock.Expectations) DocumentDisplayerParameters(org.xwiki.display.internal.DocumentDisplayerParameters) DocumentDisplayer(org.xwiki.display.internal.DocumentDisplayer) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) Invocation(org.jmock.api.Invocation) CustomAction(org.jmock.lib.action.CustomAction) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) DisplayMacroParameters(org.xwiki.rendering.macro.display.DisplayMacroParameters) DocumentReference(org.xwiki.model.reference.DocumentReference) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) Test(org.junit.Test)

Example 19 with DocumentModelBridge

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

the class DefaultIconSetLoaderTest method loadIconSetFromWikiDocument.

@Test
public void loadIconSetFromWikiDocument() throws Exception {
    DocumentReference iconSetRef = new DocumentReference("xwiki", "IconThemes", "Default");
    DocumentReference iconClassRef = new DocumentReference("wikiId", "IconThemesCode", "IconThemeClass");
    when(documentAccessBridge.getProperty(eq(iconSetRef), eq(iconClassRef), eq("name"))).thenReturn("MyIconTheme");
    DocumentModelBridge doc = mock(DocumentModelBridge.class);
    when(documentAccessBridge.getDocumentInstance(iconSetRef)).thenReturn(doc);
    StringWriter content = new StringWriter();
    IOUtils.copyLarge(new InputStreamReader(getClass().getResourceAsStream("/test.iconset")), content);
    when(doc.getContent()).thenReturn(content.toString());
    // Test
    IconSet result = mocker.getComponentUnderTest().loadIconSet(iconSetRef);
    // Verify
    verifies(result);
    assertEquals("MyIconTheme", result.getName());
}
Also used : DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) StringWriter(java.io.StringWriter) InputStreamReader(java.io.InputStreamReader) DocumentReference(org.xwiki.model.reference.DocumentReference) IconSet(org.xwiki.icon.IconSet) Test(org.junit.Test)

Example 20 with DocumentModelBridge

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

the class DefaultIconSetLoader method loadIconSet.

@Override
public IconSet loadIconSet(DocumentReference iconSetReference) throws IconException {
    try {
        // Get the document
        DocumentModelBridge doc = documentAccessBridge.getDocumentInstance(iconSetReference);
        String content = doc.getContent();
        // The name of the icon set is stored in the IconThemesCode.IconThemeClass XObject of the document
        DocumentReference iconClassRef = new DocumentReference(wikiDescriptorManager.getCurrentWikiId(), "IconThemesCode", "IconThemeClass");
        String name = (String) documentAccessBridge.getProperty(iconSetReference, iconClassRef, "name");
        // Load the icon set
        return loadIconSet(new StringReader(content), name);
    } catch (Exception e) {
        throw new IconException(String.format(ERROR_MSG, iconSetReference), e);
    }
}
Also used : DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) StringReader(java.io.StringReader) DocumentReference(org.xwiki.model.reference.DocumentReference) IOException(java.io.IOException) IconException(org.xwiki.icon.IconException) IconException(org.xwiki.icon.IconException)

Aggregations

DocumentModelBridge (org.xwiki.bridge.DocumentModelBridge)58 DocumentReference (org.xwiki.model.reference.DocumentReference)43 Test (org.junit.Test)39 Expectations (org.jmock.Expectations)18 XDOM (org.xwiki.rendering.block.XDOM)18 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)14 Syntax (org.xwiki.rendering.syntax.Syntax)11 MacroTransformationContext (org.xwiki.rendering.transformation.MacroTransformationContext)10 MacroBlock (org.xwiki.rendering.block.MacroBlock)9 DocumentDisplayerParameters (org.xwiki.display.internal.DocumentDisplayerParameters)8 ResourceReference (org.xwiki.rendering.listener.reference.ResourceReference)7 MacroExecutionException (org.xwiki.rendering.macro.MacroExecutionException)7 Block (org.xwiki.rendering.block.Block)6 HashMap (java.util.HashMap)5 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)5 DocumentDisplayer (org.xwiki.display.internal.DocumentDisplayer)5 MetaDataBlock (org.xwiki.rendering.block.MetaDataBlock)5 MetaData (org.xwiki.rendering.listener.MetaData)5 DocumentResourceReference (org.xwiki.rendering.listener.reference.DocumentResourceReference)5 StringReader (java.io.StringReader)4