Search in sources :

Example 61 with ExecutionContext

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

the class EventAndFactoryTest method configure.

@Before
public void configure() throws Exception {
    final DocumentAccessBridge mockDocumentAccessBridge = getComponentManager().getInstance(DocumentAccessBridge.class);
    getMockery().checking(new Expectations() {

        {
            allowing(mockDocumentAccessBridge).getCurrentUser();
            will(returnValue("XWiki.Admin"));
        }
    });
    final ExecutionContext context = new ExecutionContext();
    final Execution mockExecution = getComponentManager().getInstance(Execution.class);
    getMockery().checking(new Expectations() {

        {
            allowing(mockExecution).getContext();
            will(returnValue(context));
        }
    });
    final EntityReferenceResolver<String> mockResolver = getComponentManager().getInstance(EntityReferenceResolver.TYPE_STRING);
    getMockery().checking(new Expectations() {

        {
            allowing(mockResolver).resolve("XWiki.Admin", EntityType.DOCUMENT);
            will(returnValue(new DocumentReference("xwiki", "XWiki", "Admin")));
        }
    });
    this.factory = getComponentManager().getInstance(EventFactory.class);
    this.defaultEvent = this.factory.createEvent();
    this.rawEvent = this.factory.createRawEvent();
}
Also used : Expectations(org.jmock.Expectations) ExecutionContext(org.xwiki.context.ExecutionContext) Execution(org.xwiki.context.Execution) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) DefaultEventFactory(org.xwiki.eventstream.internal.DefaultEventFactory) DocumentReference(org.xwiki.model.reference.DocumentReference) Before(org.junit.Before)

Example 62 with ExecutionContext

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

the class DocumentContentDisplayerTest method testBaseMetaDataIsSetBeforeExecutingTransformations.

@Test
public void testBaseMetaDataIsSetBeforeExecutingTransformations() throws Exception {
    // The execution context is expected to have the "xwikicontext" property set.
    Execution mockExecution = mocker.getInstance(Execution.class);
    ExecutionContext executionContext = new ExecutionContext();
    executionContext.setProperty("xwikicontext", new HashMap<String, Object>());
    Mockito.when(mockExecution.getContext()).thenReturn(executionContext);
    // The document being displayed.
    DocumentModelBridge mockDocument = Mockito.mock(DocumentModelBridge.class);
    XDOM content = new XDOM(Collections.<Block>emptyList());
    Mockito.when(mockDocument.getXDOM()).thenReturn(content);
    // The reference of the current document musts be set as the value of the BASE meta data.
    DocumentReference currentDocRef = new DocumentReference("wiki", "Space", "Page");
    DocumentAccessBridge mockDocumentAccessBridge = mocker.getInstance(DocumentAccessBridge.class);
    Mockito.when(mockDocumentAccessBridge.getCurrentDocumentReference()).thenReturn(currentDocRef);
    EntityReferenceSerializer<String> serializer = mocker.getInstance(EntityReferenceSerializer.TYPE_STRING);
    Mockito.when(serializer.serialize(currentDocRef)).thenReturn("foo");
    // We can't verify the meta data after the display method is called because we want to make sure the BASE meta
    // data is correctly set before XDOM transformations are executed, not after.
    TransformationManager mockTransformationManager = mocker.getInstance(TransformationManager.class);
    Mockito.doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            XDOM xdom = (XDOM) invocation.getArguments()[0];
            // We have to assert the meta data before the transformations are executed not at the end!
            Assert.assertEquals("foo", xdom.getMetaData().getMetaData(MetaData.BASE));
            return null;
        }
    }).when(mockTransformationManager).performTransformations(Mockito.any(XDOM.class), Mockito.any(TransformationContext.class));
    // Execute the display.
    Assert.assertSame(content, mocker.getComponentUnderTest().display(mockDocument, new DocumentDisplayerParameters()));
    // Make sure the transformations are executed exactly once, and on the right content.
    Mockito.verify(mockTransformationManager, Mockito.times(1)).performTransformations(Mockito.same(content), Mockito.any(TransformationContext.class));
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) TransformationContext(org.xwiki.rendering.transformation.TransformationContext) Execution(org.xwiki.context.Execution) ExecutionContext(org.xwiki.context.ExecutionContext) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) InvocationOnMock(org.mockito.invocation.InvocationOnMock) TransformationManager(org.xwiki.rendering.transformation.TransformationManager) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 63 with ExecutionContext

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

the class SUExecutor method push.

/**
 * Setup the context so that following code is executed with provided user rights.
 * <p>
 * {@link #pop()} should be called to restore the context to its previous state.
 *
 * @param user the user to check rights on
 * @see #pop()
 */
public void push(DocumentReference user) {
    // Modify the context for that following code is executed with the right of wiki macro author
    SUExecutorContext sucontext = before(user);
    // Put it in an hidden context property to restore it later
    ExecutionContext econtext = this.execution.getContext();
    // Use a stack in case a wiki macro calls another wiki macro
    Stack<SUExecutorContext> backup = (Stack<SUExecutorContext>) econtext.getProperty(SUExecutor.class.getName());
    if (backup == null) {
        backup = new Stack<SUExecutorContext>();
        econtext.setProperty(SUExecutor.class.getName(), backup);
    }
    backup.push(sucontext);
}
Also used : ExecutionContext(org.xwiki.context.ExecutionContext) Stack(java.util.Stack)

Example 64 with ExecutionContext

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

the class SUExecutor method pop.

/**
 * Restore the context to it's previous state as defined by the provided {@link SUExecutorContext}.
 */
public void pop() {
    // Get the su context to restore
    ExecutionContext econtext = this.execution.getContext();
    // Use a stack in case a wiki macro calls another wiki macro
    Stack<SUExecutorContext> backup = (Stack<SUExecutorContext>) econtext.getProperty(SUExecutor.class.getName());
    if (backup != null && !backup.isEmpty()) {
        // Restore the context execution rights
        after(backup.pop());
    } else {
        this.logger.error("Can't find any backed up execution right information in the execution context");
    }
}
Also used : ExecutionContext(org.xwiki.context.ExecutionContext) Stack(java.util.Stack)

Example 65 with ExecutionContext

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

the class ExecutionContextCopier method copy.

@Override
public ExecutionContext copy(ExecutionContext originalExecutionContext) throws CloneFailedException {
    try {
        ExecutionContext clonedExecutionContext = this.executionContextManager.clone(originalExecutionContext);
        // XWikiContext
        // The above clone just creates and initializes an empty XWiki Context, so it needs special handling.
        XWikiContext xwikiContext = (XWikiContext) originalExecutionContext.getProperty(XWikiContext.EXECUTIONCONTEXT_KEY);
        XWikiContext clonedXWikiContext = xwikiContextCloner.copy(xwikiContext);
        clonedExecutionContext.setProperty(XWikiContext.EXECUTIONCONTEXT_KEY, clonedXWikiContext);
        // VelocityContext
        // Reset the VelocityContext from the EC by removing it and calling the Velocity ECInitializer which is
        // normally called by the execution of the ECInitializers by ECManager.clone(). This ensures a clean new
        // VC is created. It'll get filled when VelocityContextManager.getVelocityContext() is called by whatever
        // code need the VC.
        clonedExecutionContext.removeProperty(VelocityExecutionContextInitializer.VELOCITY_CONTEXT_ID);
        this.velocityExecutionContextInitializer.initialize(clonedExecutionContext);
        return clonedExecutionContext;
    } catch (Exception e) {
        throw new CloneFailedException(String.format("Failed to clone [%s]", originalExecutionContext), e);
    }
}
Also used : ExecutionContext(org.xwiki.context.ExecutionContext) XWikiContext(com.xpn.xwiki.XWikiContext) CloneFailedException(org.apache.commons.lang3.exception.CloneFailedException) CloneFailedException(org.apache.commons.lang3.exception.CloneFailedException)

Aggregations

ExecutionContext (org.xwiki.context.ExecutionContext)114 Execution (org.xwiki.context.Execution)62 XWikiContext (com.xpn.xwiki.XWikiContext)47 Before (org.junit.Before)31 Test (org.junit.Test)25 XWiki (com.xpn.xwiki.XWiki)19 DocumentReference (org.xwiki.model.reference.DocumentReference)19 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)15 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)14 ComponentManager (org.xwiki.component.manager.ComponentManager)9 ExecutionContextException (org.xwiki.context.ExecutionContextException)9 WikiDescriptorManager (org.xwiki.wiki.descriptor.WikiDescriptorManager)9 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)8 ExecutionContextManager (org.xwiki.context.ExecutionContextManager)8 List (java.util.List)6 Map (java.util.Map)6 Properties (java.util.Properties)6 Session (javax.mail.Session)6 MimeMessage (javax.mail.internet.MimeMessage)6 XWikiException (com.xpn.xwiki.XWikiException)5