use of org.xwiki.environment.Environment in project xwiki-platform by xwiki.
the class FileSystemMailContentStoreTest method saveMessageThrowsMailStoreExceptionWhenError.
@Test
public void saveMessageThrowsMailStoreExceptionWhenError() throws Exception {
Environment environment = this.mocker.getInstance(Environment.class);
when(environment.getPermanentDirectory()).thenReturn(new File(TEMPORARY_DIRECTORY));
String batchId = UUID.randomUUID().toString();
String messageId = "ar1vm0Wca42E/dDn3dsH8ogs3/s=";
ExtendedMimeMessage message = mock(ExtendedMimeMessage.class);
when(message.getUniqueMessageId()).thenReturn(messageId);
this.thrown.expect(MailStoreException.class);
this.thrown.expectMessage("Failed to save message (id [" + messageId + "], batch id [" + batchId + "]) into file");
when(message.getContent()).thenReturn("Lorem ipsum dolor sit amet, consectetur adipiscing elit");
doThrow(new IOException()).when(message).writeTo(any(OutputStream.class));
this.mocker.getComponentUnderTest().save(batchId, message);
}
use of org.xwiki.environment.Environment in project xwiki-platform by xwiki.
the class FileSystemMailContentStoreTest method registerMockComponents.
@BeforeComponent
public void registerMockComponents() throws Exception {
Environment environment = this.mocker.registerMockComponent(Environment.class);
when(environment.getPermanentDirectory()).thenReturn(new File(TEMPORARY_DIRECTORY));
}
use of org.xwiki.environment.Environment in project xwiki-platform by xwiki.
the class AttachmentMimeBodyPartFactoryTest method createAttachmentBodyPart.
@Test
public void createAttachmentBodyPart() throws Exception {
Environment environment = this.mocker.getInstance(Environment.class);
when(environment.getTemporaryDirectory()).thenReturn(new File(TEMPORARY_DIRECTORY));
Attachment attachment = mock(Attachment.class);
when(attachment.getContent()).thenReturn("Lorem Ipsum".getBytes());
when(attachment.getFilename()).thenReturn("image.png");
when(attachment.getMimeType()).thenReturn("image/png");
MimeBodyPart part = this.mocker.getComponentUnderTest().create(attachment, Collections.<String, Object>emptyMap());
assertEquals("<image.png>", part.getContentID());
// JavaMail adds some extra params to the content-type header
// (e.g. image/png; name=image.png) , we just verify the content type that we passed.
assertTrue(part.getContentType().startsWith("image/png"));
// We verify that the Content-Disposition has the correct file namr
assertTrue(part.getFileName().matches("image\\.png"));
assertEquals("Lorem Ipsum", IOUtils.toString(part.getDataHandler().getInputStream()));
}
use of org.xwiki.environment.Environment in project xwiki-platform by xwiki.
the class AttachmentMimeBodyPartFactoryTest method createAttachmentBodyPartWithHeader.
@Test
public void createAttachmentBodyPartWithHeader() throws Exception {
Environment environment = this.mocker.getInstance(Environment.class);
when(environment.getTemporaryDirectory()).thenReturn(new File(TEMPORARY_DIRECTORY));
Attachment attachment = mock(Attachment.class);
when(attachment.getContent()).thenReturn("Lorem Ipsum".getBytes());
when(attachment.getFilename()).thenReturn("image.png");
when(attachment.getMimeType()).thenReturn("image/png");
Map<String, Object> parameters = Collections.singletonMap("headers", (Object) Collections.singletonMap("Content-Transfer-Encoding", "quoted-printable"));
MimeBodyPart part = this.mocker.getComponentUnderTest().create(attachment, parameters);
assertEquals("<image.png>", part.getContentID());
// JavaMail adds some extra params to the content-type header
// (e.g. image/png; name=image.png) , we just verify the content type that we passed.
assertTrue(part.getContentType().startsWith("image/png"));
// We verify that the Content-Disposition has the correct file namr
assertTrue(part.getFileName().matches("image\\.png"));
assertArrayEquals(new String[] { "quoted-printable" }, part.getHeader("Content-Transfer-Encoding"));
assertEquals("Lorem Ipsum", IOUtils.toString(part.getDataHandler().getInputStream()));
}
use of org.xwiki.environment.Environment in project xwiki-platform by xwiki.
the class AttachmentMimeBodyPartFactoryTest method createAttachmentBodyPartWhenWriteError.
@Test
public void createAttachmentBodyPartWhenWriteError() throws Exception {
Environment environment = this.mocker.getInstance(Environment.class);
when(environment.getTemporaryDirectory()).thenReturn(new File(TEMPORARY_DIRECTORY));
Attachment attachment = mock(Attachment.class);
when(attachment.getFilename()).thenReturn("image.png");
when(attachment.getContent()).thenThrow(new RuntimeException("error"));
try {
this.mocker.getComponentUnderTest().create(attachment, Collections.<String, Object>emptyMap());
fail("Should have thrown an exception here!");
} catch (MessagingException expected) {
assertEquals("Failed to save attachment [image.png] to the file system", expected.getMessage());
}
}
Aggregations