Search in sources :

Example 71 with DefaultParameterizedType

use of org.xwiki.component.util.DefaultParameterizedType in project xwiki-platform by xwiki.

the class UsersAndGroupsMimeMessageFactoryTest method createMessageWhenNotExistingMimeMessageFactory.

@Test
public void createMessageWhenNotExistingMimeMessageFactory() throws Exception {
    Session session = Session.getInstance(new Properties());
    Map<String, Object> parameters = new HashMap<>();
    parameters.put("hint", "factoryHint");
    parameters.put("source", "factoryHint");
    Provider<ComponentManager> componentManagerProvider = this.mocker.registerMockComponent(new DefaultParameterizedType(null, Provider.class, ComponentManager.class), "context");
    when(componentManagerProvider.get()).thenReturn(this.mocker);
    try {
        this.mocker.getComponentUnderTest().createMessage(Collections.<String, Object>emptyMap(), parameters);
        fail("Should have thrown an exception");
    } catch (MessagingException expected) {
        assertEquals("Failed to find a [MimeMessageFactory<MimeMessage>] for hint [factoryHint]", expected.getMessage());
    }
}
Also used : HashMap(java.util.HashMap) MessagingException(javax.mail.MessagingException) ComponentManager(org.xwiki.component.manager.ComponentManager) Properties(java.util.Properties) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) Session(javax.mail.Session) Provider(javax.inject.Provider) Test(org.junit.Test)

Example 72 with DefaultParameterizedType

use of org.xwiki.component.util.DefaultParameterizedType in project xwiki-platform by xwiki.

the class UsersAndGroupsMimeMessageFactoryTest method createMessage.

@Test
public void createMessage() throws Exception {
    DocumentReference userReference = new DocumentReference("userwiki", "userspace", "userpage");
    Map<String, Object> source = Collections.<String, Object>singletonMap("users", Collections.singletonList(userReference));
    Map<String, Object> parameters = new HashMap<>();
    parameters.put("hint", "template");
    parameters.put("source", new DocumentReference("templatewiki", "templatespace", "templatepage"));
    Provider<ComponentManager> componentManagerProvider = this.mocker.registerMockComponent(new DefaultParameterizedType(null, Provider.class, ComponentManager.class), "context");
    when(componentManagerProvider.get()).thenReturn(this.mocker);
    this.mocker.registerMockComponent(new DefaultParameterizedType(null, MimeMessageFactory.class, MimeMessage.class), "template");
    // Setup XWikiContext since this is required internally by the iterator constructor
    Execution execution = this.mocker.registerMockComponent(Execution.class);
    XWikiContext xwikiContext = mock(XWikiContext.class);
    ExecutionContext executionContext = new ExecutionContext();
    executionContext.setProperty(XWikiContext.EXECUTIONCONTEXT_KEY, xwikiContext);
    when(execution.getContext()).thenReturn(executionContext);
    Iterator<MimeMessage> iterator = this.mocker.getComponentUnderTest().createMessage(source, parameters);
    assertNotNull(iterator);
}
Also used : HashMap(java.util.HashMap) XWikiContext(com.xpn.xwiki.XWikiContext) Provider(javax.inject.Provider) Execution(org.xwiki.context.Execution) ExecutionContext(org.xwiki.context.ExecutionContext) MimeMessage(javax.mail.internet.MimeMessage) MimeMessageFactory(org.xwiki.mail.MimeMessageFactory) ComponentManager(org.xwiki.component.manager.ComponentManager) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 73 with DefaultParameterizedType

use of org.xwiki.component.util.DefaultParameterizedType in project xwiki-platform by xwiki.

the class PrepareMailRunnableTest method prepareMailWhenIteratorFails.

@Test
public void prepareMailWhenIteratorFails() throws Exception {
    Properties properties = new Properties();
    Session session = Session.getDefaultInstance(properties);
    final 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(new Iterable<MimeMessage>() {

        @Override
        public Iterator<MimeMessage> iterator() {
            return new Iterator<MimeMessage>() {

                int index = 0;

                @Override
                public boolean hasNext() {
                    return true;
                }

                @Override
                public MimeMessage next() {
                    if (index++ == 0) {
                        return message1;
                    }
                    throw new RuntimeException("Iterator failure");
                }

                @Override
                public void remove() {
                }
            };
        }
    }, session, listener1, batchId1, context1);
    MemoryMailListener listener2 = this.mocker.getInstance(MailListener.class, "memory");
    PrepareMailQueueItem item2 = new PrepareMailQueueItem(Arrays.asList(message2), session, listener2, batchId2, context2);
    MailQueueManager prepareMailQueueManager = this.mocker.getInstance(new DefaultParameterizedType(null, MailQueueManager.class, PrepareMailQueueItem.class));
    MailQueueManager sendMailQueueManager = this.mocker.getInstance(new DefaultParameterizedType(null, MailQueueManager.class, SendMailQueueItem.class));
    MailContentStore contentStore = this.mocker.getInstance(MailContentStore.class, "filesystem");
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            Object[] args = invocationOnMock.getArguments();
            MimeMessage message = (MimeMessage) args[1];
            message.saveChanges();
            return null;
        }
    }).when(contentStore).save(any(String.class), any(ExtendedMimeMessage.class));
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            Object[] args = invocationOnMock.getArguments();
            SendMailQueueItem item = (SendMailQueueItem) args[0];
            ((UpdateableMailStatusResult) item.getListener().getMailStatusResult()).incrementCurrentSize();
            return null;
        }
    }).when(sendMailQueueManager).addToQueue(any(SendMailQueueItem.class));
    // Prepare 2 mails. Both will fail but we want to verify that the second one is processed even though the first
    // one failed.
    prepareMailQueueManager.addToQueue(item1);
    prepareMailQueueManager.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.
    assertNotNull(listener1.getMailStatusResult().getByState(MailState.PREPARE_SUCCESS).next());
    assertNotNull(listener2.getMailStatusResult().getByState(MailState.PREPARE_SUCCESS).next());
    assertFalse(listener1.getMailStatusResult().getByState(MailState.PREPARE_ERROR).hasNext());
    assertFalse(listener2.getMailStatusResult().getByState(MailState.PREPARE_ERROR).hasNext());
    assertEquals("Failure during preparation phase of thread [" + batchId1 + "]", logRule.getMessage(0));
}
Also used : ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) Properties(java.util.Properties) MemoryMailListener(org.xwiki.mail.internal.MemoryMailListener) MimeMessage(javax.mail.internet.MimeMessage) ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) Iterator(java.util.Iterator) MailContentStore(org.xwiki.mail.MailContentStore) XWikiContext(com.xpn.xwiki.XWikiContext) ExecutionContext(org.xwiki.context.ExecutionContext) InvocationOnMock(org.mockito.invocation.InvocationOnMock) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) MailStatusResult(org.xwiki.mail.MailStatusResult) UpdateableMailStatusResult(org.xwiki.mail.internal.UpdateableMailStatusResult) Session(javax.mail.Session) Test(org.junit.Test)

Example 74 with DefaultParameterizedType

use of org.xwiki.component.util.DefaultParameterizedType in project xwiki-platform by xwiki.

the class XWikiContextCopierTest method setup.

@Before
public void setup() throws Exception {
    Utils.setComponentManager(mocker);
    originalResponse = new XWikiServletResponseStub();
    original = new XWikiContext();
    // Set some values
    original.setWikiId("wiki");
    DocumentReference userReference = new DocumentReference("wiki", "Space", "Page");
    EntityReferenceSerializer<String> serializer = mocker.registerMockComponent(EntityReferenceSerializer.TYPE_STRING, "compactwiki");
    when(serializer.serialize(userReference)).thenReturn("wiki:Space.Page");
    mocker.registerMockComponent(DocumentReferenceResolver.TYPE_STRING, "currentmixed");
    original.setUserReference(userReference);
    // Set the mock request
    this.originalRequest = mock(XWikiRequest.class);
    original.setRequest(this.originalRequest);
    Copier<XWikiRequest> requestCopier = mocker.getInstance(new DefaultParameterizedType(null, Copier.class, XWikiRequest.class));
    when(requestCopier.copy(this.originalRequest)).thenReturn(this.originalRequest);
    // Set the stubbed response
    original.setResponse(originalResponse);
    // XWiki mock
    XWiki xwiki = mock(XWiki.class);
    original.setWiki(xwiki);
    // Store mock
    // Simulate the existence of a hibernate session in context
    original.put(HIBSESSION, "opened session");
    store = mock(XWikiStoreInterface.class);
    // clean up will remove the session in the given context
    doAnswer(new Answer<Void>() {

        public Void answer(InvocationOnMock invocation) {
            XWikiContext context = (XWikiContext) invocation.getArguments()[0];
            context.put(HIBSESSION, null);
            return null;
        }
    }).when(store).cleanUp(any(XWikiContext.class));
    when(xwiki.getStore()).thenReturn(store);
    // URL factory mock
    XWikiURLFactory urlFactory = mock(XWikiURLFactory.class);
    XWikiURLFactoryService urlFactoryService = mock(XWikiURLFactoryService.class);
    when(urlFactoryService.createURLFactory(anyInt(), any(XWikiContext.class))).thenReturn(urlFactory);
    when(xwiki.getURLFactoryService()).thenReturn(urlFactoryService);
}
Also used : XWikiRequest(com.xpn.xwiki.web.XWikiRequest) XWikiURLFactory(com.xpn.xwiki.web.XWikiURLFactory) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) XWikiURLFactoryService(com.xpn.xwiki.web.XWikiURLFactoryService) XWikiStoreInterface(com.xpn.xwiki.store.XWikiStoreInterface) XWikiServletResponseStub(com.xpn.xwiki.web.XWikiServletResponseStub) InvocationOnMock(org.mockito.invocation.InvocationOnMock) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) DocumentReference(org.xwiki.model.reference.DocumentReference) Before(org.junit.Before)

Example 75 with DefaultParameterizedType

use of org.xwiki.component.util.DefaultParameterizedType in project xwiki-platform by xwiki.

the class DefaultExtendedRenderingConfigurationTest method configure.

@Before
public void configure() throws Exception {
    Provider<ComponentManager> contextComponentManagerProvider = this.mocker.registerMockComponent(new DefaultParameterizedType(null, Provider.class, ComponentManager.class), "context");
    when(contextComponentManagerProvider.get()).thenReturn(this.mocker);
}
Also used : ComponentManager(org.xwiki.component.manager.ComponentManager) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) Provider(javax.inject.Provider) Before(org.junit.Before)

Aggregations

DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)104 Test (org.junit.Test)44 Before (org.junit.Before)32 Provider (javax.inject.Provider)27 DocumentReference (org.xwiki.model.reference.DocumentReference)24 XWikiContext (com.xpn.xwiki.XWikiContext)19 ComponentManager (org.xwiki.component.manager.ComponentManager)19 ExecutionContext (org.xwiki.context.ExecutionContext)17 ExtendedURL (org.xwiki.url.ExtendedURL)15 Execution (org.xwiki.context.Execution)14 HashMap (java.util.HashMap)13 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)13 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)12 ResourceReferenceSerializer (org.xwiki.resource.ResourceReferenceSerializer)10 VfsResourceReference (org.xwiki.vfs.VfsResourceReference)10 Properties (java.util.Properties)9 MimeBodyPartFactory (org.xwiki.mail.MimeBodyPartFactory)9 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)8 XWiki (com.xpn.xwiki.XWiki)7 File (java.io.File)7