Search in sources :

Example 1 with ExecutionContextManager

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;
}
Also used : Execution(org.xwiki.context.Execution) ExecutionContext(org.xwiki.context.ExecutionContext) ExecutionContextManager(org.xwiki.context.ExecutionContextManager)

Example 2 with ExecutionContextManager

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);
}
Also used : Execution(org.xwiki.context.Execution) ExecutionContextManager(org.xwiki.context.ExecutionContextManager) ArrayList(java.util.ArrayList) WikiProvisioningJobRequest(org.xwiki.wiki.provisioning.WikiProvisioningJobRequest) DocumentReference(org.xwiki.model.reference.DocumentReference) WikiProvisioningJob(org.xwiki.wiki.provisioning.WikiProvisioningJob) Test(org.junit.Test)

Example 3 with ExecutionContextManager

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());
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) ExecutionContextManager(org.xwiki.context.ExecutionContextManager) XWikiContext(com.xpn.xwiki.XWikiContext) EntityResourceReference(org.xwiki.resource.entity.EntityResourceReference) XWiki(com.xpn.xwiki.XWiki) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Date(java.util.Date) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ExecutionContext(org.xwiki.context.ExecutionContext) ByteArrayInputStream(java.io.ByteArrayInputStream) VelocityManager(org.xwiki.velocity.VelocityManager) ResourceReferenceManager(org.xwiki.resource.ResourceReferenceManager) XWikiPluginManager(com.xpn.xwiki.plugin.XWikiPluginManager) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 4 with ExecutionContextManager

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();
    }
}
Also used : JobDataMap(org.quartz.JobDataMap) Execution(org.xwiki.context.Execution) JobExecutionContext(org.quartz.JobExecutionContext) ExecutionContext(org.xwiki.context.ExecutionContext) JobExecutionException(org.quartz.JobExecutionException) ExecutionContextManager(org.xwiki.context.ExecutionContextManager) ExecutionContextException(org.xwiki.context.ExecutionContextException)

Example 5 with ExecutionContextManager

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);
}
Also used : XWikiServletRequestStub(com.xpn.xwiki.web.XWikiServletRequestStub) XWikiServletResponseStub(com.xpn.xwiki.web.XWikiServletResponseStub) Query(org.xwiki.query.Query) ExecutionContextManager(org.xwiki.context.ExecutionContextManager) Before(org.junit.Before)

Aggregations

ExecutionContextManager (org.xwiki.context.ExecutionContextManager)11 ExecutionContext (org.xwiki.context.ExecutionContext)8 Execution (org.xwiki.context.Execution)6 XWikiContext (com.xpn.xwiki.XWikiContext)4 DocumentReference (org.xwiki.model.reference.DocumentReference)4 ExecutionContextException (org.xwiki.context.ExecutionContextException)3 XWiki (com.xpn.xwiki.XWiki)2 XWikiException (com.xpn.xwiki.XWikiException)2 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)2 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)2 Before (org.junit.Before)2 Test (org.junit.Test)2 EmbeddableComponentManager (org.xwiki.component.embed.EmbeddableComponentManager)2 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)2 CoreConfiguration (com.xpn.xwiki.CoreConfiguration)1 XWikiCfgConfigurationSource (com.xpn.xwiki.internal.XWikiCfgConfigurationSource)1 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)1