Search in sources :

Example 21 with ComponentManager

use of org.xwiki.component.manager.ComponentManager in project xwiki-platform by xwiki.

the class SerializedFilesMimeMessageFactoryTest method createMessageWhenNoExecution.

@Test
public void createMessageWhenNoExecution() throws Exception {
    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("batchId", null);
        fail("Should have thrown an exception");
    } catch (MessagingException expected) {
        assertEquals("Failed to find an Environment Component", expected.getMessage());
    }
}
Also used : MessagingException(javax.mail.MessagingException) ComponentManager(org.xwiki.component.manager.ComponentManager) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) Provider(javax.inject.Provider) Test(org.junit.Test)

Example 22 with ComponentManager

use of org.xwiki.component.manager.ComponentManager in project xwiki-platform by xwiki.

the class SerializedFilesMimeMessageIteratorTest method createMessageWhenFileNoLongerExists.

/**
 * Error that can happen if the file has been locally deleted between the time the time the user executes a
 * resend and the time the Mail Sender Thread reaches that file for processing (i.e. deserializing it).
 */
@Test
public void createMessageWhenFileNoLongerExists() throws Exception {
    Environment environment = mock(Environment.class);
    when(environment.getPermanentDirectory()).thenReturn(new File(TEMPORARY_DIRECTORY));
    ComponentManager componentManager = mock(ComponentManager.class);
    when(componentManager.getInstance(eq(Environment.class))).thenReturn(environment);
    // Create a serialized file before the iterator is initialized
    String mailID = "<1128820400.0.1419205781342.JavaMail.contact@xwiki.org>";
    createSerializedMessage(mailID);
    SerializedFilesMimeMessageIterator iterator = new SerializedFilesMimeMessageIterator(this.batchId, Collections.<String, Object>emptyMap(), componentManager);
    // Remove the file before next() is called to generate the error
    File messageFile = new File(this.batchDirectory, URLEncoder.encode(mailID, "UTF-8"));
    messageFile.delete();
    MimeMessage message = iterator.next();
    // Verify that:
    // 1) the returned message is null since there was an error
    // 2) that the log contains the error
    assertNull(message);
    assertEquals("Failed to create Mime Message", this.logRule.getMessage(0));
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) ComponentManager(org.xwiki.component.manager.ComponentManager) Environment(org.xwiki.environment.Environment) File(java.io.File) Test(org.junit.Test)

Example 23 with ComponentManager

use of org.xwiki.component.manager.ComponentManager in project xwiki-platform by xwiki.

the class SerializedFilesMimeMessageIteratorTest method createMessage.

@Test
public void createMessage() throws Exception {
    String mailID1 = "<1128820400.0.1419205781342.JavaMail.contact@xwiki.org>";
    String mailID2 = "<1128820400.1.1419205781342.JavaMail.contact@xwiki.org>";
    String mailID3 = "<1128820400.2.1419205781342.JavaMail.contact@xwiki.org>";
    createSerializedMessage(mailID1);
    createSerializedMessage(mailID2);
    createSerializedMessage(mailID3);
    Map<String, Object> parameters = new HashMap<>();
    parameters.put("parameters", Collections.EMPTY_MAP);
    Environment environment = mock(Environment.class);
    when(environment.getPermanentDirectory()).thenReturn(new File(TEMPORARY_DIRECTORY));
    ComponentManager componentManager = mock(ComponentManager.class);
    when(componentManager.getInstance(eq(Environment.class))).thenReturn(environment);
    SerializedFilesMimeMessageIterator iterator = new SerializedFilesMimeMessageIterator(this.batchId, parameters, componentManager);
    ArrayList<String> listID = new ArrayList<>();
    listID.add(mailID1);
    listID.add(mailID2);
    listID.add(mailID3);
    assertTrue(iterator.hasNext());
    MimeMessage message1 = iterator.next();
    assertTrue(listID.contains(message1.getMessageID()));
    listID.remove(message1.getMessageID());
    assertTrue(iterator.hasNext());
    MimeMessage message2 = iterator.next();
    assertTrue(listID.contains(message2.getMessageID()));
    listID.remove(message1.getMessageID());
    assertTrue(iterator.hasNext());
    MimeMessage message3 = iterator.next();
    assertTrue(listID.contains(message2.getMessageID()));
    listID.remove(message3.getMessageID());
    assertFalse(iterator.hasNext());
}
Also used : HashMap(java.util.HashMap) MimeMessage(javax.mail.internet.MimeMessage) ComponentManager(org.xwiki.component.manager.ComponentManager) ArrayList(java.util.ArrayList) Environment(org.xwiki.environment.Environment) File(java.io.File) Test(org.junit.Test)

Example 24 with ComponentManager

use of org.xwiki.component.manager.ComponentManager in project xwiki-platform by xwiki.

the class GroupMimeMessageFactoryTest method createMessageWhenNotExistingMimeMessageFactory.

@Test
public void createMessageWhenNotExistingMimeMessageFactory() throws Exception {
    DocumentReference groupReference = new DocumentReference("wiki", "space", "page");
    Map<String, Object> parameters = new HashMap<>();
    parameters.put("hint", "factoryHint");
    parameters.put("source", "factorySource");
    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(groupReference, 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) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) DocumentReference(org.xwiki.model.reference.DocumentReference) Provider(javax.inject.Provider) Test(org.junit.Test)

Example 25 with ComponentManager

use of org.xwiki.component.manager.ComponentManager in project xwiki-platform by xwiki.

the class GroupMimeMessageIteratorTest method createMessage.

@Test
public void createMessage() throws Exception {
    DocumentReference groupReference = new DocumentReference("xwiki", "XWiki", "Marketing");
    DocumentReference userReference1 = new DocumentReference("xwiki", "XWiki", "JohnDoe");
    DocumentReference userReference2 = new DocumentReference("xwiki", "XWiki", "JaneDoe");
    DocumentReference userReference3 = new DocumentReference("xwiki", "XWiki", "JonnieDoe");
    Session session = Session.getInstance(new Properties());
    MimeMessageFactory<MimeMessage> factory = new MimeMessageFactory<MimeMessage>() {

        @Override
        public MimeMessage createMessage(Object source, Map parameters) throws MessagingException {
            return new ExtendedMimeMessage();
        }
    };
    Map<String, Object> parameters = new HashMap<>();
    parameters.put("parameters", Collections.EMPTY_MAP);
    parameters.put("session", session);
    DocumentAccessBridge accessBridge = mock(DocumentAccessBridge.class);
    when(accessBridge.getProperty(eq(groupReference), any(), eq(0), eq("member"))).thenReturn("XWiki.JohnDoe");
    when(accessBridge.getProperty(eq(groupReference), any(), eq(1), eq("member"))).thenReturn("XWiki.JaneDoe");
    when(accessBridge.getProperty(eq(groupReference), any(), eq(2), eq("member"))).thenReturn("XWiki.JonnieDoe");
    when(accessBridge.getProperty(eq(userReference1), any(), eq("email"))).thenReturn("john@doe.com");
    when(accessBridge.getProperty(eq(userReference2), any(), eq("email"))).thenReturn("jane@doe.com");
    when(accessBridge.getProperty(eq(userReference3), any(), eq("email"))).thenReturn("jannie@doe.com");
    Execution execution = mock(Execution.class);
    ExecutionContext executionContext = mock(ExecutionContext.class);
    when(execution.getContext()).thenReturn(executionContext);
    XWikiContext xwikiContext = mock(XWikiContext.class);
    when(executionContext.getProperty("xwikicontext")).thenReturn(xwikiContext);
    XWiki xwiki = mock(XWiki.class);
    when(xwikiContext.getWiki()).thenReturn(xwiki);
    XWikiDocument document = mock(XWikiDocument.class);
    when(xwiki.getDocument(eq(groupReference), eq(xwikiContext))).thenReturn(document);
    BaseObject object = mock(BaseObject.class);
    when(document.getXObjects(any(EntityReference.class))).thenReturn(Arrays.asList(object, object, object));
    DocumentReferenceResolver<String> resolver = (DocumentReferenceResolver<String>) mock(DocumentReferenceResolver.class);
    when(resolver.resolve("XWiki.JohnDoe")).thenReturn(userReference1);
    when(resolver.resolve("XWiki.JaneDoe")).thenReturn(userReference2);
    when(resolver.resolve("XWiki.JonnieDoe")).thenReturn(userReference3);
    ComponentManager componentManager = mock(ComponentManager.class);
    when(componentManager.getInstance(eq(DocumentAccessBridge.class))).thenReturn(accessBridge);
    when(componentManager.getInstance(eq(Execution.class))).thenReturn(execution);
    when(componentManager.getInstance(eq(DocumentReferenceResolver.TYPE_STRING), eq("current"))).thenReturn(resolver);
    GroupMimeMessageIterator iterator = new GroupMimeMessageIterator(groupReference, factory, parameters, componentManager);
    assertTrue(iterator.hasNext());
    MimeMessage message1 = iterator.next();
    assertArrayEquals(message1.getRecipients(Message.RecipientType.TO), InternetAddress.parse("john@doe.com"));
    assertTrue(iterator.hasNext());
    MimeMessage message2 = iterator.next();
    assertArrayEquals(message2.getRecipients(Message.RecipientType.TO), InternetAddress.parse("jane@doe.com"));
    assertTrue(iterator.hasNext());
    MimeMessage message3 = iterator.next();
    assertArrayEquals(message3.getRecipients(Message.RecipientType.TO), InternetAddress.parse("jannie@doe.com"));
    assertFalse(iterator.hasNext());
}
Also used : ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) DocumentReferenceResolver(org.xwiki.model.reference.DocumentReferenceResolver) HashMap(java.util.HashMap) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) Properties(java.util.Properties) BaseObject(com.xpn.xwiki.objects.BaseObject) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) Execution(org.xwiki.context.Execution) ExecutionContext(org.xwiki.context.ExecutionContext) MimeMessage(javax.mail.internet.MimeMessage) ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) MimeMessageFactory(org.xwiki.mail.MimeMessageFactory) EntityReference(org.xwiki.model.reference.EntityReference) ComponentManager(org.xwiki.component.manager.ComponentManager) BaseObject(com.xpn.xwiki.objects.BaseObject) HashMap(java.util.HashMap) Map(java.util.Map) DocumentReference(org.xwiki.model.reference.DocumentReference) Session(javax.mail.Session) Test(org.junit.Test)

Aggregations

ComponentManager (org.xwiki.component.manager.ComponentManager)76 Test (org.junit.Test)34 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)23 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)18 DocumentReference (org.xwiki.model.reference.DocumentReference)17 Provider (javax.inject.Provider)14 NamespacedComponentManager (org.xwiki.component.manager.NamespacedComponentManager)12 Before (org.junit.Before)11 DefaultComponentDescriptor (org.xwiki.component.descriptor.DefaultComponentDescriptor)11 XWikiContext (com.xpn.xwiki.XWikiContext)9 Expectations (org.jmock.Expectations)9 WikiReference (org.xwiki.model.reference.WikiReference)9 HashMap (java.util.HashMap)7 Execution (org.xwiki.context.Execution)7 SpaceReference (org.xwiki.model.reference.SpaceReference)7 XWiki (com.xpn.xwiki.XWiki)6 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)6 ArrayList (java.util.ArrayList)6 ExecutionContext (org.xwiki.context.ExecutionContext)6 MimeMessage (javax.mail.internet.MimeMessage)5