Search in sources :

Example 21 with ExecutionContext

use of org.xwiki.context.ExecutionContext 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 22 with ExecutionContext

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

the class XWikiSetupCleanupFilter method getReleasableComponents.

/**
 * @param componentManager the component manager
 * @return the list of JAX-RS resources that are implemented as components with per-lookup policy and that have been
 *         instantiated during this request
 */
private List<XWikiRestComponent> getReleasableComponents(ComponentManager componentManager) {
    try {
        ExecutionContext executionContext = componentManager.<Execution>getInstance(Execution.class).getContext();
        @SuppressWarnings("unchecked") List<XWikiRestComponent> releasableComponents = (List<XWikiRestComponent>) executionContext.getProperty(Constants.RELEASABLE_COMPONENT_REFERENCES);
        return releasableComponents != null ? releasableComponents : Collections.<XWikiRestComponent>emptyList();
    } catch (Exception e) {
        getLogger().log(Level.WARNING, "Failed to retrieve the list of releasable components.", e);
        return Collections.emptyList();
    }
}
Also used : ExecutionContext(org.xwiki.context.ExecutionContext) Execution(org.xwiki.context.Execution) XWikiRestComponent(org.xwiki.rest.XWikiRestComponent) List(java.util.List) ComponentLifecycleException(org.xwiki.component.manager.ComponentLifecycleException)

Example 23 with ExecutionContext

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

the class WikiUIExtensionParametersTest method getParametersFromDifferentRequests.

@Test
public void getParametersFromDifferentRequests() throws Exception {
    when(modelContext.getCurrentEntityReference()).thenReturn(new WikiReference("wiki1"));
    when(velocityEngine.evaluate(any(VelocityContext.class), any(StringWriter.class), eq("id:key"), eq("value"))).thenReturn(true);
    WikiUIExtensionParameters parameters = new WikiUIExtensionParameters("id", "key=value", componentManager);
    ExecutionContext ec1 = mock(ExecutionContext.class, "ec1");
    ExecutionContext ec2 = mock(ExecutionContext.class, "ec2");
    when(execution.getContext()).thenReturn(ec1).thenReturn(ec2);
    // It should fail silently
    Assert.assertEquals("", parameters.get().get("key"));
    Assert.assertEquals("", parameters.get().get("key"));
    // Verify the velocity evaluation has been done for both wikis.
    verify(velocityEngine, times(2)).evaluate(any(VelocityContext.class), any(StringWriter.class), eq("id:key"), eq("value"));
}
Also used : ExecutionContext(org.xwiki.context.ExecutionContext) StringWriter(java.io.StringWriter) WikiUIExtensionParameters(org.xwiki.uiextension.internal.WikiUIExtensionParameters) VelocityContext(org.apache.velocity.VelocityContext) WikiReference(org.xwiki.model.reference.WikiReference) Test(org.junit.Test)

Example 24 with ExecutionContext

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

the class URLExecutionContextInitializerTest method initializeWhenFormatIdNotInContext.

@Test
public void initializeWhenFormatIdNotInContext() throws Exception {
    ExecutionContext ec = new ExecutionContext();
    URLConfiguration configuration = this.mocker.getInstance(URLConfiguration.class);
    when(configuration.getURLFormatId()).thenReturn("test");
    this.mocker.getComponentUnderTest().initialize(ec);
    assertEquals("test", ec.getProperty("urlscheme"));
}
Also used : ExecutionContext(org.xwiki.context.ExecutionContext) Test(org.junit.Test)

Example 25 with ExecutionContext

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

the class URLExecutionContextInitializerTest method initializeWhenFormatIdAlreadyInContext.

@Test
public void initializeWhenFormatIdAlreadyInContext() throws Exception {
    ExecutionContext ec = new ExecutionContext();
    ec.setProperty("urlscheme", "existing");
    this.mocker.getComponentUnderTest().initialize(ec);
    assertEquals("existing", ec.getProperty("urlscheme"));
}
Also used : ExecutionContext(org.xwiki.context.ExecutionContext) Test(org.junit.Test)

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