Search in sources :

Example 46 with Execution

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

the class DocumentTitleDisplayerTest method configure.

@Before
public void configure() throws Exception {
    // The execution context is expected to have the "xwikicontext" property set.
    Execution mockExecution = this.mocker.getInstance(Execution.class);
    ExecutionContext executionContext = new ExecutionContext();
    executionContext.setProperty("xwikicontext", new HashMap<String, Object>());
    Mockito.when(mockExecution.getContext()).thenReturn(executionContext);
}
Also used : Execution(org.xwiki.context.Execution) ExecutionContext(org.xwiki.context.ExecutionContext) Before(org.junit.Before)

Example 47 with Execution

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

the class DisplayMacroTest method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    // Put a fake XWiki context on the execution context.
    Execution execution = getComponentManager().getInstance(Execution.class);
    execution.getContext().setProperty("xwikicontext", new HashMap<Object, Object>());
}
Also used : Execution(org.xwiki.context.Execution)

Example 48 with Execution

use of org.xwiki.context.Execution 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 49 with Execution

use of org.xwiki.context.Execution 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 50 with Execution

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

the class AuthenticatingIntegrationTest method sendTextMail.

@Test
public void sendTextMail() throws Exception {
    // Set the EC
    Execution execution = this.componentManager.getInstance(Execution.class);
    ExecutionContext executionContext = new ExecutionContext();
    XWikiContext xContext = new XWikiContext();
    xContext.setWikiId("wiki");
    executionContext.setProperty(XWikiContext.EXECUTIONCONTEXT_KEY, xContext);
    when(execution.getContext()).thenReturn(executionContext);
    Copier<ExecutionContext> executionContextCloner = this.componentManager.getInstance(new DefaultParameterizedType(null, Copier.class, ExecutionContext.class));
    // Just return the same execution context
    when(executionContextCloner.copy(executionContext)).thenReturn(executionContext);
    // Step 1: Create a JavaMail Session
    Properties properties = this.configuration.getAllProperties();
    assertEquals("true", properties.getProperty(DefaultMailSenderConfiguration.JAVAMAIL_SMTP_AUTH));
    Session session = Session.getInstance(properties, new XWikiAuthenticator(this.configuration));
    // Step 2: Create the Message to send
    MimeMessage message = new MimeMessage(session);
    message.setSubject("subject");
    message.setRecipient(RecipientType.TO, new InternetAddress("john@doe.com"));
    // Step 3: Add the Message Body
    Multipart multipart = new MimeMultipart("mixed");
    // Add text in the body
    multipart.addBodyPart(this.defaultBodyPartFactory.create("some text here", Collections.<String, Object>singletonMap("mimetype", "text/plain")));
    message.setContent(multipart);
    // Step 4: Send the mail
    this.sender.sendAsynchronously(Arrays.asList(message), session, null);
    // Verify that the mail has been received (wait maximum 30 seconds).
    this.mail.waitForIncomingEmail(30000L, 1);
    MimeMessage[] messages = this.mail.getReceivedMessages();
    assertEquals(1, messages.length);
    assertEquals("subject", messages[0].getHeader("Subject", null));
    assertEquals("john@doe.com", messages[0].getHeader("To", null));
    assertEquals(1, ((MimeMultipart) messages[0].getContent()).getCount());
    BodyPart textBodyPart = ((MimeMultipart) messages[0].getContent()).getBodyPart(0);
    assertEquals("text/plain", textBodyPart.getHeader("Content-Type")[0]);
    assertEquals("some text here", textBodyPart.getContent());
}
Also used : BodyPart(javax.mail.BodyPart) InternetAddress(javax.mail.internet.InternetAddress) MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) XWikiAuthenticator(org.xwiki.mail.XWikiAuthenticator) XWikiContext(com.xpn.xwiki.XWikiContext) Properties(java.util.Properties) Execution(org.xwiki.context.Execution) ExecutionContext(org.xwiki.context.ExecutionContext) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) Copier(org.xwiki.mail.internal.thread.context.Copier) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) Session(javax.mail.Session) Test(org.junit.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest)

Aggregations

Execution (org.xwiki.context.Execution)82 ExecutionContext (org.xwiki.context.ExecutionContext)58 Before (org.junit.Before)36 XWikiContext (com.xpn.xwiki.XWikiContext)30 Test (org.junit.Test)19 DocumentReference (org.xwiki.model.reference.DocumentReference)17 XWiki (com.xpn.xwiki.XWiki)13 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)13 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)11 WikiDescriptorManager (org.xwiki.wiki.descriptor.WikiDescriptorManager)10 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)8 ComponentManager (org.xwiki.component.manager.ComponentManager)8 HashMap (java.util.HashMap)7 Expectations (org.jmock.Expectations)7 VelocityContext (org.apache.velocity.VelocityContext)5 ExecutionContextManager (org.xwiki.context.ExecutionContextManager)5 BaseObject (com.xpn.xwiki.objects.BaseObject)4 DefaultComponentDescriptor (org.xwiki.component.descriptor.DefaultComponentDescriptor)4 XDOM (org.xwiki.rendering.block.XDOM)4 AuthorizationManager (org.xwiki.security.authorization.AuthorizationManager)4