use of org.xwiki.bridge.DocumentModelBridge 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));
}
use of org.xwiki.bridge.DocumentModelBridge in project xwiki-platform by xwiki.
the class XWikiWikiModelTest method getXDOM.
@Test
public void getXDOM() throws Exception {
ResourceReference reference = new ResourceReference("reference", ResourceType.DOCUMENT);
DocumentReference documentReference = new DocumentReference("wiki", "space", "page");
when(this.referenceResolver.resolve(reference, EntityType.DOCUMENT)).thenReturn(documentReference);
DocumentModelBridge dmb = mock(DocumentModelBridge.class);
when(this.documentAccessBridge.getTranslatedDocumentInstance(documentReference)).thenReturn(dmb);
XDOM xdom = new XDOM(Collections.emptyList());
when(dmb.getXDOM()).thenReturn(xdom);
assertEquals(xdom, this.mocker.getComponentUnderTest().getXDOM(reference));
}
use of org.xwiki.bridge.DocumentModelBridge in project xwiki-platform by xwiki.
the class OfficeImporterScriptServiceTest method saveWithAppend.
@Test
public void saveWithAppend() throws Exception {
XDOMOfficeDocument doc = mock(XDOMOfficeDocument.class);
DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page");
String syntaxId = "test/1.0";
when(documentAccessBridge.isDocumentEditable(documentReference)).thenReturn(true);
when(documentAccessBridge.exists(documentReference)).thenReturn(true);
DocumentModelBridge document = mock(DocumentModelBridge.class);
when(documentAccessBridge.getTranslatedDocumentInstance(documentReference)).thenReturn(document);
when(document.getSyntax()).thenReturn(new Syntax(new SyntaxType("test", "Test"), "1.0"));
when(documentAccessBridge.getDocumentContent(documentReference, null)).thenReturn("before");
when(doc.getContentAsString(syntaxId)).thenReturn("after");
assertTrue(officeImporterScriptService.save(doc, documentReference, syntaxId, null, null, true));
verify(documentAccessBridge).setDocumentContent(documentReference, "before\nafter", "Updated by office importer.", false);
}
use of org.xwiki.bridge.DocumentModelBridge 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));
}
use of org.xwiki.bridge.DocumentModelBridge in project xwiki-platform by xwiki.
the class DisplayScriptService method getDocument.
/**
* 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
*/
private DocumentModelBridge getDocument(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);
}
}
Aggregations