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);
}
}
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()));
}
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;
}
}
}
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());
}
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);
}
}
Aggregations