use of org.xwiki.context.ExecutionContextManager in project xwiki-platform by xwiki.
the class AbstractXWikiRunnable method initExecutionContext.
/**
* Initialize execution context for the current thread.
*
* @return the new execution context
* @throws ExecutionContextException error when try to initialize execution context
*/
protected ExecutionContext initExecutionContext() throws ExecutionContextException {
// Keep a reference to the Execution component to avoid a lookup in #cleanupExecutionContext() in case this
// thread is stopped after the Component Manager disposes its components.
this.execution = Utils.getComponent(Execution.class);
ExecutionContextManager ecim = Utils.getComponent(ExecutionContextManager.class);
ExecutionContext context = new ExecutionContext();
declareProperties(context);
ecim.initialize(context);
context.setProperties(this.properties);
return context;
}
use of org.xwiki.context.ExecutionContextManager in project xwiki-platform by xwiki.
the class DefaultWikiProvisioningJobExecutorTest method createAndExecuteJob.
@Test
public void createAndExecuteJob() throws Exception {
// Mocks
WikiProvisioningJob provisioningJob = mock(WikiProvisioningJob.class);
mocker.registerComponent(Job.class, "wikiprovisioning.test", provisioningJob);
ExecutionContextManager executionContextManager = mock(ExecutionContextManager.class);
mocker.registerComponent(ExecutionContextManager.class, executionContextManager);
Execution execution = mock(Execution.class);
mocker.registerComponent(Execution.class, execution);
DocumentReference user = new DocumentReference("xwiki", "XWiki", "User");
when(xcontext.getUserReference()).thenReturn(user);
// Execute
WikiProvisioningJob job = mocker.getComponentUnderTest().createAndExecuteJob("wikiid", "wikiprovisioning.test", "templateid");
// Verify
// Id of the job.
List<String> jobId = new ArrayList<String>();
jobId.add("wiki");
jobId.add("provisioning");
jobId.add("wikiprovisioning.test");
jobId.add("wikiid");
verify(provisioningJob).initialize(eq(new WikiProvisioningJobRequest(jobId, "wikiid", "templateid", user)));
Thread.sleep(100);
verify(provisioningJob).run();
// getJobs also works
assertEquals(mocker.getComponentUnderTest().getJob(jobId), job);
}
use of org.xwiki.context.ExecutionContextManager in project xwiki-platform by xwiki.
the class MockitoDownloadActionTest method renderWhenZipExplorerPluginURL.
@Test
public void renderWhenZipExplorerPluginURL() throws Exception {
DownloadAction action = new DownloadAction();
XWikiContext xcontext = this.oldcore.getXWikiContext();
// Set the Request URL
XWikiServletRequestStub request = new XWikiServletRequestStub();
request.setRequestURI("http://localhost:8080/xwiki/bin/download/space/page/file.ext/some/path");
xcontext.setRequest(request);
// Set the current doc and current wiki
XWikiDocument document = mock(XWikiDocument.class);
when(document.getAttachment("path")).thenReturn(null);
xcontext.setDoc(document);
xcontext.setWikiId("wiki");
xcontext.setAction("download");
// Set the Response
XWikiResponse response = mock(XWikiResponse.class);
StubServletOutputStream ssos = new StubServletOutputStream();
when(response.getOutputStream()).thenReturn(ssos);
xcontext.setResponse(response);
// Set the Resource Reference Manager used to parse the URL and extract the attachment name
ResourceReferenceManager rrm = this.oldcore.getMocker().registerMockComponent(ResourceReferenceManager.class);
when(rrm.getResourceReference()).thenReturn(new EntityResourceReference(new AttachmentReference("path", new DocumentReference("wiki", Arrays.asList("space", "page", "file.ext"), "some")), EntityResourceAction.VIEW));
// Setup the returned attachment
XWikiAttachment attachment = mock(XWikiAttachment.class);
when(attachment.getContentSize(xcontext)).thenReturn(100);
Date now = new Date();
when(attachment.getDate()).thenReturn(now);
when(attachment.getFilename()).thenReturn("file.ext");
when(attachment.getContentInputStream(xcontext)).thenReturn(new ByteArrayInputStream("test".getBytes()));
when(attachment.getMimeType(xcontext)).thenReturn("mimetype");
when(attachment.clone()).thenReturn(attachment);
// Configure an existing doc in the store
XWiki xwiki = this.oldcore.getSpyXWiki();
XWikiDocument backwardCompatDocument = new XWikiDocument(new DocumentReference("wiki", "space", "page"));
backwardCompatDocument.addAttachment(attachment);
xwiki.saveDocument(backwardCompatDocument, xcontext);
// Make sure the user has permission to access the doc
doReturn(true).when(xwiki).checkAccess(eq("download"), any(XWikiDocument.class), any(XWikiContext.class));
// Setup ExecutionContextManager & VelocityManager using in the context backup
ExecutionContextManager ecm = this.oldcore.getMocker().registerMockComponent(ExecutionContextManager.class);
ExecutionContext ec = this.oldcore.getExecutionContext();
when(ecm.clone(ec)).thenReturn(ec);
VelocityManager vm = this.oldcore.getMocker().registerMockComponent(VelocityManager.class);
// Set the Plugin Manager
XWikiPluginManager pluginManager = mock(XWikiPluginManager.class);
when(pluginManager.downloadAttachment(attachment, xcontext)).thenReturn(attachment);
doReturn(pluginManager).when(this.oldcore.getSpyXWiki()).getPluginManager();
assertNull(action.render(xcontext));
// This is the test, we verify what is set in the response
verify(response).setContentType("mimetype");
verify(response).setHeader("Accept-Ranges", "bytes");
verify(response).addHeader("Content-disposition", "attachment; filename*=utf-8''file.ext");
verify(response).setDateHeader("Last-Modified", now.getTime());
verify(response).setContentLength(100);
assertEquals("test", ssos.baos.toString());
}
use of org.xwiki.context.ExecutionContextManager in project xwiki-platform by xwiki.
the class AbstractJob method execute.
@Override
public final void execute(JobExecutionContext jobContext) throws JobExecutionException {
JobDataMap data = jobContext.getJobDetail().getJobDataMap();
// The XWiki context was saved in the Job execution data map. Get it as we'll retrieve
// the script to execute from it.
this.xcontext = (XWikiContext) data.get("context");
// Clone the XWikiContex to have a new one for each run
this.xcontext = this.xcontext.clone();
// Init execution context
Execution execution;
try {
ExecutionContextManager ecim = Utils.getComponent(ExecutionContextManager.class);
execution = Utils.getComponent(Execution.class);
ExecutionContext context = new ExecutionContext();
// Bridge with old XWiki Context, required for old code
this.xcontext.declareInExecutionContext(context);
ecim.initialize(context);
} catch (ExecutionContextException e) {
throw new JobExecutionException("Fail to initialize execution context", e);
}
try {
// Execute the job
executeJob(jobContext);
} finally {
// We must ensure we clean the ThreadLocal variables located in the Execution
// component as otherwise we will have a potential memory leak.
execution.removeContext();
}
}
use of org.xwiki.context.ExecutionContextManager in project xwiki-platform by xwiki.
the class PageTest method setUpForPageTest.
/**
* Configures the various Components and their mocks with default values for page tests.
*
* @throws Exception in case of errors
*/
@Before
public void setUpForPageTest() throws Exception {
// Configure mocks from OldcoreRule
context = oldcore.getXWikiContext();
xwiki = oldcore.getSpyXWiki();
// We need this one because some component in its init creates a query...
when(oldcore.getQueryManager().createQuery(any(String.class), any(String.class))).thenReturn(mock(Query.class));
// Set up a fake Request
// Configure request so that $!request.outputSyntax" == 'plain
// Need to be executed before ecm.initialize() so that XWikiScriptContextInitializer will initialize the
// script context properly
request = new XWikiServletRequestStub();
request.setScheme("http");
context.setRequest(request);
response = new XWikiServletResponseStub();
context.setResponse(response);
ExecutionContextManager ecm = mocker.getInstance(ExecutionContextManager.class);
ecm.initialize(oldcore.getExecutionContext());
// Let the user have view access to all pages
when(oldcore.getMockRightService().hasAccessLevel(eq("view"), eq("XWiki.XWikiGuest"), any(), eq(context))).thenReturn(true);
// Set up URL Factory
URLFactorySetup.setUp(xwiki, context);
// Set up Localization
LocalizationSetup.setUp(mocker);
// Set up Skin Extensions
SkinExtensionSetup.setUp(xwiki, context);
}
Aggregations