Search in sources :

Example 66 with Execution

use of org.xwiki.context.Execution in project xwiki-platform by xwiki.

the class DefaultOfficeViewerScriptServiceTest method view.

@Test
public void view() throws Exception {
    Execution execution = mocker.getInstance(Execution.class);
    when(execution.getContext()).thenReturn(new ExecutionContext());
    AttachmentReference attachmentReference = new AttachmentReference("file.odt", new DocumentReference("wiki", "Space", "Page"));
    DocumentModelBridge document = mock(DocumentModelBridge.class);
    DocumentAccessBridge documentAccessBridge = mocker.getInstance(DocumentAccessBridge.class);
    when(documentAccessBridge.isDocumentViewable(attachmentReference.getDocumentReference())).thenReturn(true);
    when(documentAccessBridge.getTranslatedDocumentInstance(attachmentReference.getDocumentReference())).thenReturn(document);
    when(document.getSyntax()).thenReturn(Syntax.TEX_1_0);
    XDOM xdom = new XDOM(Collections.<Block>emptyList());
    OfficeViewer viewer = mocker.getInstance(OfficeViewer.class);
    when(viewer.createView(attachmentReference, Collections.<String, String>emptyMap())).thenReturn(xdom);
    BlockRenderer xhtmlRenderer = mocker.registerMockComponent(BlockRenderer.class, "xhtml/1.0");
    Assert.assertEquals("", mocker.getComponentUnderTest().view(attachmentReference));
    TransformationManager transformationManager = mocker.getInstance(TransformationManager.class);
    verify(transformationManager).performTransformations(eq(xdom), notNull(TransformationContext.class));
    verify(xhtmlRenderer).render(eq(xdom), notNull(WikiPrinter.class));
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) Execution(org.xwiki.context.Execution) ExecutionContext(org.xwiki.context.ExecutionContext) XDOM(org.xwiki.rendering.block.XDOM) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) OfficeViewer(org.xwiki.office.viewer.OfficeViewer) TransformationManager(org.xwiki.rendering.transformation.TransformationManager) TransformationContext(org.xwiki.rendering.transformation.TransformationContext) DocumentReference(org.xwiki.model.reference.DocumentReference) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) BlockRenderer(org.xwiki.rendering.renderer.BlockRenderer) Test(org.junit.Test)

Example 67 with Execution

use of org.xwiki.context.Execution in project xwiki-platform by xwiki.

the class DefaultOfficeViewerScriptServiceTest method viewThrowingException.

@Test
public void viewThrowingException() throws Exception {
    ExecutionContext executionContext = new ExecutionContext();
    executionContext.setProperty("officeView.caughtException", "before");
    Execution execution = mocker.getInstance(Execution.class);
    when(execution.getContext()).thenReturn(executionContext);
    Assert.assertNull(mocker.getComponentUnderTest().view(null));
    Exception e = mocker.getComponentUnderTest().getCaughtException();
    Assert.assertTrue(e instanceof NullPointerException);
    verify(mocker.getMockedLogger()).error("Failed to view office document: null", e);
}
Also used : ExecutionContext(org.xwiki.context.ExecutionContext) Execution(org.xwiki.context.Execution) Test(org.junit.Test)

Example 68 with Execution

use of org.xwiki.context.Execution in project xwiki-platform by xwiki.

the class DefaultWikiMacro method execute.

@Override
public List<Block> execute(WikiMacroParameters parameters, String macroContent, MacroTransformationContext context) throws MacroExecutionException {
    validate(parameters, macroContent);
    // Parse the wiki macro content.
    XDOM xdom = prepareWikiMacroContent(context);
    // Prepare macro context.
    Map<String, Object> macroBinding = new HashMap<>();
    macroBinding.put(MACRO_PARAMS_KEY, parameters);
    macroBinding.put(MACRO_CONTENT_KEY, macroContent);
    macroBinding.put(MACRO_DESCRIPTOR_KEY, getDescriptor());
    macroBinding.put(MACRO_CONTEXT_KEY, context);
    macroBinding.put(MACRO_RESULT_KEY, null);
    // Extension point to add more wiki macro bindings
    try {
        List<WikiMacroBindingInitializer> bindingInitializers = this.componentManager.getInstanceList(WikiMacroBindingInitializer.class);
        for (WikiMacroBindingInitializer bindingInitializer : bindingInitializers) {
            bindingInitializer.initialize(this.macroDocumentReference, parameters, macroContent, context, macroBinding);
        }
    } catch (ComponentLookupException e) {
    // TODO: we should probably log something but that should never happen
    }
    // Execute the macro
    ObservationManager observation = null;
    try {
        observation = this.componentManager.getInstance(ObservationManager.class);
    } catch (ComponentLookupException e) {
    // TODO: maybe log something
    }
    // Get XWiki context
    Map<String, Object> xwikiContext = null;
    try {
        Execution execution = this.componentManager.getInstance(Execution.class);
        ExecutionContext econtext = execution.getContext();
        if (econtext != null) {
            xwikiContext = (Map<String, Object>) execution.getContext().getProperty("xwikicontext");
        }
    } catch (ComponentLookupException e) {
    // TODO: maybe log something
    }
    try {
        Transformation macroTransformation = this.componentManager.getInstance(Transformation.class, MACRO_HINT);
        if (xwikiContext != null) {
            // Place macro context inside xwiki context ($xcontext.macro).
            xwikiContext.put(MACRO_KEY, macroBinding);
        }
        MacroBlock wikiMacroBlock = context.getCurrentMacroBlock();
        MacroMarkerBlock wikiMacroMarker = new MacroMarkerBlock(wikiMacroBlock.getId(), wikiMacroBlock.getParameters(), wikiMacroBlock.getContent(), xdom.getChildren(), wikiMacroBlock.isInline());
        // Make sure to use provided metadatas
        MetaDataBlock metaDataBlock = new MetaDataBlock(Collections.<Block>singletonList(wikiMacroMarker), xdom.getMetaData());
        // Make sure the context XDOM contains the wiki macro content
        wikiMacroBlock.getParent().replaceChild(metaDataBlock, wikiMacroBlock);
        // "Emulate" the fact that wiki macro block is still part of the XDOM (what is in the XDOM is a
        // MacroMarkerBlock and MacroTransformationContext current macro block only support MacroBlock so we can't
        // switch it without breaking some APIs)
        wikiMacroBlock.setParent(metaDataBlock.getParent());
        wikiMacroBlock.setNextSiblingBlock(metaDataBlock.getNextSibling());
        wikiMacroBlock.setPreviousSiblingBlock(metaDataBlock.getPreviousSibling());
        try {
            if (observation != null) {
                observation.notify(STARTEXECUTION_EVENT, this, macroBinding);
            }
            // Perform internal macro transformations.
            TransformationContext txContext = new TransformationContext(context.getXDOM(), this.syntax);
            txContext.setId(context.getId());
            RenderingContext renderingContext = componentManager.getInstance(RenderingContext.class);
            ((MutableRenderingContext) renderingContext).transformInContext(macroTransformation, txContext, wikiMacroMarker);
        } finally {
            // Restore context XDOM to its previous state
            metaDataBlock.getParent().replaceChild(wikiMacroBlock, metaDataBlock);
        }
        return extractResult(wikiMacroMarker.getChildren(), macroBinding, context);
    } catch (Exception ex) {
        throw new MacroExecutionException("Error while performing internal macro transformations", ex);
    } finally {
        if (xwikiContext != null) {
            xwikiContext.remove(MACRO_KEY);
        }
        if (observation != null) {
            observation.notify(ENDEXECUTION_EVENT, this);
        }
    }
}
Also used : RenderingContext(org.xwiki.rendering.transformation.RenderingContext) MutableRenderingContext(org.xwiki.rendering.internal.transformation.MutableRenderingContext) Transformation(org.xwiki.rendering.transformation.Transformation) XDOM(org.xwiki.rendering.block.XDOM) HashMap(java.util.HashMap) MacroMarkerBlock(org.xwiki.rendering.block.MacroMarkerBlock) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) ObservationManager(org.xwiki.observation.ObservationManager) TransformationContext(org.xwiki.rendering.transformation.TransformationContext) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) MacroParameterException(org.xwiki.rendering.macro.parameter.MacroParameterException) Execution(org.xwiki.context.Execution) ExecutionContext(org.xwiki.context.ExecutionContext) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) MutableRenderingContext(org.xwiki.rendering.internal.transformation.MutableRenderingContext) MacroBlock(org.xwiki.rendering.block.MacroBlock) MetaDataBlock(org.xwiki.rendering.block.MetaDataBlock)

Example 69 with Execution

use of org.xwiki.context.Execution in project xwiki-platform by xwiki.

the class AbstractWikiTestCase method setUp.

@Before
@Override
public void setUp() throws Exception {
    super.setUp();
    this.executionContext = new ExecutionContext();
    Execution execution = getComponentManager().getInstance(Execution.class);
    execution.setContext(this.executionContext);
    Utils.setComponentManager(getComponentManager());
}
Also used : ExecutionContext(org.xwiki.context.ExecutionContext) Execution(org.xwiki.context.Execution) Before(org.junit.Before)

Example 70 with Execution

use of org.xwiki.context.Execution in project xwiki-platform by xwiki.

the class DefaultSheetManagerTest method configure.

@Before
public void configure() throws Exception {
    documentAccessBridge = getComponentManager().getInstance(DocumentAccessBridge.class);
    documentSheetBinder = getComponentManager().getInstance(SheetBinder.class, "document");
    classSheetBinder = getComponentManager().getInstance(SheetBinder.class, "class");
    modelBridge = getComponentManager().getInstance(ModelBridge.class);
    document = getMockery().mock(DocumentModelBridge.class);
    final Execution execution = getComponentManager().getInstance(Execution.class);
    getMockery().checking(new Expectations() {

        {
            allowing(execution).getContext();
            will(returnValue(context));
            allowing(document).getDocumentReference();
            will(returnValue(new DocumentReference(WIKI_NAME, "Space", "Page")));
        }
    });
}
Also used : Expectations(org.jmock.Expectations) Execution(org.xwiki.context.Execution) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) SheetBinder(org.xwiki.sheet.SheetBinder) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) DocumentReference(org.xwiki.model.reference.DocumentReference) Before(org.junit.Before)

Aggregations

Execution (org.xwiki.context.Execution)82 ExecutionContext (org.xwiki.context.ExecutionContext)58 Before (org.junit.Before)36 XWikiContext (com.xpn.xwiki.XWikiContext)30 Test (org.junit.Test)19 DocumentReference (org.xwiki.model.reference.DocumentReference)17 XWiki (com.xpn.xwiki.XWiki)13 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)13 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)11 WikiDescriptorManager (org.xwiki.wiki.descriptor.WikiDescriptorManager)10 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)8 ComponentManager (org.xwiki.component.manager.ComponentManager)8 HashMap (java.util.HashMap)7 Expectations (org.jmock.Expectations)7 VelocityContext (org.apache.velocity.VelocityContext)5 ExecutionContextManager (org.xwiki.context.ExecutionContextManager)5 BaseObject (com.xpn.xwiki.objects.BaseObject)4 DefaultComponentDescriptor (org.xwiki.component.descriptor.DefaultComponentDescriptor)4 XDOM (org.xwiki.rendering.block.XDOM)4 AuthorizationManager (org.xwiki.security.authorization.AuthorizationManager)4