Search in sources :

Example 31 with ExecutionContext

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

the class MessageStreamScriptServiceTest method configure.

@Before
public void configure() throws Exception {
    this.streamService = this.mocker.getComponentUnderTest();
    Execution execution = this.mocker.getInstance(Execution.class);
    this.executionContext = new ExecutionContext();
    when(execution.getContext()).thenReturn(this.executionContext);
}
Also used : Execution(org.xwiki.context.Execution) ExecutionContext(org.xwiki.context.ExecutionContext) Before(org.junit.Before)

Example 32 with ExecutionContext

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

the class DefaultModelContext method getCurrentEntityReference.

@Override
public EntityReference getCurrentEntityReference() {
    WikiReference result = null;
    // TODO: This is bridge to the old XWiki Context since we currently don't store the current entity in the
    // new Execution Context yet. Remove when we do so.
    ExecutionContext econtext = this.execution.getContext();
    if (econtext != null) {
        Map<String, Object> xcontext = (Map<String, Object>) econtext.getProperty(XCONTEXT_KEY);
        if (xcontext != null) {
            String wikiName = (String) xcontext.get(WIKINAME_KEY);
            if (wikiName != null) {
                result = new WikiReference(wikiName);
            }
        }
    }
    return result;
}
Also used : ExecutionContext(org.xwiki.context.ExecutionContext) WikiReference(org.xwiki.model.reference.WikiReference) Map(java.util.Map)

Example 33 with ExecutionContext

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

the class UsersAndGroupsMimeMessageIteratorTest method setUpBaseMocks.

@Before
public void setUpBaseMocks() {
    this.execution = mock(Execution.class);
    this.xwikiContext = mock(XWikiContext.class);
    ExecutionContext executionContext = new ExecutionContext();
    executionContext.setProperty(XWikiContext.EXECUTIONCONTEXT_KEY, this.xwikiContext);
    when(this.execution.getContext()).thenReturn(executionContext);
    this.xwiki = mock(XWiki.class);
    when(this.xwikiContext.getWiki()).thenReturn(this.xwiki);
    this.factory = mock(MimeMessageFactory.class);
    this.resolver = mock(DocumentReferenceResolver.class);
}
Also used : DocumentReferenceResolver(org.xwiki.model.reference.DocumentReferenceResolver) Execution(org.xwiki.context.Execution) ExecutionContext(org.xwiki.context.ExecutionContext) MimeMessageFactory(org.xwiki.mail.MimeMessageFactory) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) Before(org.junit.Before)

Example 34 with ExecutionContext

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

the class PrepareMailQueueItemTest method verifyToString.

@Test
public void verifyToString() throws Exception {
    Session session = Session.getDefaultInstance(new Properties());
    MimeMessage message = new MimeMessage(session);
    String batchId = UUID.randomUUID().toString();
    ExecutionContext context = new ExecutionContext();
    XWikiContext xContext = new XWikiContext();
    xContext.setWikiId("wiki");
    context.setProperty(XWikiContext.EXECUTIONCONTEXT_KEY, xContext);
    PrepareMailQueueItem item = new PrepareMailQueueItem(Arrays.asList(message), session, null, batchId, context);
    assertEquals("batchId = [" + batchId + "], context = [[xwikicontext] = [{originalWiki=wiki, wiki=wiki}]]", item.toString());
}
Also used : ExecutionContext(org.xwiki.context.ExecutionContext) MimeMessage(javax.mail.internet.MimeMessage) XWikiContext(com.xpn.xwiki.XWikiContext) Properties(java.util.Properties) Session(javax.mail.Session) Test(org.junit.Test)

Example 35 with ExecutionContext

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

the class PrepareMailRunnableTest method prepareMailWhenContentStoreFails.

@Test
public void prepareMailWhenContentStoreFails() throws Exception {
    Properties properties = new Properties();
    Session session = Session.getDefaultInstance(properties);
    MimeMessage message1 = new MimeMessage(session);
    message1.setText("Content1");
    MimeMessage message2 = new MimeMessage(session);
    message2.setText("Content2");
    String batchId1 = UUID.randomUUID().toString();
    String batchId2 = UUID.randomUUID().toString();
    ExecutionContext context1 = new ExecutionContext();
    XWikiContext xContext1 = new XWikiContext();
    xContext1.setWikiId("wiki1");
    context1.setProperty(XWikiContext.EXECUTIONCONTEXT_KEY, xContext1);
    ExecutionContext context2 = new ExecutionContext();
    XWikiContext xContext2 = new XWikiContext();
    xContext2.setWikiId("wiki2");
    context2.setProperty(XWikiContext.EXECUTIONCONTEXT_KEY, xContext2);
    MemoryMailListener listener1 = this.mocker.getInstance(MailListener.class, "memory");
    PrepareMailQueueItem item1 = new PrepareMailQueueItem(Arrays.asList(message1), session, listener1, batchId1, context1);
    MemoryMailListener listener2 = this.mocker.getInstance(MailListener.class, "memory");
    PrepareMailQueueItem item2 = new PrepareMailQueueItem(Arrays.asList(message2), session, listener2, batchId2, context2);
    MailQueueManager mailQueueManager = this.mocker.getInstance(new DefaultParameterizedType(null, MailQueueManager.class, PrepareMailQueueItem.class));
    // Make the content store save fail
    MailContentStore contentStore = this.mocker.getInstance(MailContentStore.class, "filesystem");
    doThrow(new MailStoreException("error")).when(contentStore).save(any(String.class), any(ExtendedMimeMessage.class));
    // Prepare 2 mails. Both will fail but we want to verify that the second one is processed even though the first
    // one failed.
    mailQueueManager.addToQueue(item1);
    mailQueueManager.addToQueue(item2);
    MailRunnable runnable = this.mocker.getComponentUnderTest();
    Thread thread = new Thread(runnable);
    thread.start();
    // Wait for the mails to have been processed.
    try {
        listener1.getMailStatusResult().waitTillProcessed(10000L);
        listener2.getMailStatusResult().waitTillProcessed(10000L);
    } finally {
        runnable.stopProcessing();
        thread.interrupt();
        thread.join();
    }
    MailStatusResult result1 = listener1.getMailStatusResult();
    MailStatusResult result2 = listener2.getMailStatusResult();
    // Despite the errors, both process should be ended with known total number of mails
    assertTrue(result1.isProcessed());
    assertTrue(result2.isProcessed());
    // This is the real test: we verify that there's been an error while sending each email.
    MailStatus status1 = result1.getByState(MailState.PREPARE_ERROR).next();
    assertEquals("MailStoreException: error", status1.getErrorSummary());
    MailStatus status2 = result2.getByState(MailState.PREPARE_ERROR).next();
    assertEquals("MailStoreException: error", status2.getErrorSummary());
}
Also used : ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) MailStoreException(org.xwiki.mail.MailStoreException) MailContentStore(org.xwiki.mail.MailContentStore) XWikiContext(com.xpn.xwiki.XWikiContext) Properties(java.util.Properties) MemoryMailListener(org.xwiki.mail.internal.MemoryMailListener) ExecutionContext(org.xwiki.context.ExecutionContext) MimeMessage(javax.mail.internet.MimeMessage) ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) MailStatusResult(org.xwiki.mail.MailStatusResult) UpdateableMailStatusResult(org.xwiki.mail.internal.UpdateableMailStatusResult) MailStatus(org.xwiki.mail.MailStatus) Session(javax.mail.Session) 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