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());
}
}
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));
}
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());
}
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());
}
}
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());
}
Aggregations