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