Search in sources :

Example 1 with Environment

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);
}
Also used : ExtendedMimeMessage(org.xwiki.mail.ExtendedMimeMessage) OutputStream(java.io.OutputStream) Environment(org.xwiki.environment.Environment) IOException(java.io.IOException) File(java.io.File) Test(org.junit.Test)

Example 2 with Environment

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));
}
Also used : Environment(org.xwiki.environment.Environment) File(java.io.File) BeforeComponent(org.xwiki.test.annotation.BeforeComponent)

Example 3 with Environment

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()));
}
Also used : Environment(org.xwiki.environment.Environment) Attachment(com.xpn.xwiki.api.Attachment) MimeBodyPart(javax.mail.internet.MimeBodyPart) File(java.io.File) Test(org.junit.Test)

Example 4 with Environment

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()));
}
Also used : Environment(org.xwiki.environment.Environment) Attachment(com.xpn.xwiki.api.Attachment) MimeBodyPart(javax.mail.internet.MimeBodyPart) File(java.io.File) Test(org.junit.Test)

Example 5 with Environment

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());
    }
}
Also used : MessagingException(javax.mail.MessagingException) Environment(org.xwiki.environment.Environment) Attachment(com.xpn.xwiki.api.Attachment) File(java.io.File) Test(org.junit.Test)

Aggregations

Environment (org.xwiki.environment.Environment)21 File (java.io.File)15 Test (org.junit.Test)10 ServletEnvironment (org.xwiki.environment.internal.ServletEnvironment)6 Before (org.junit.Before)5 ComponentManager (org.xwiki.component.manager.ComponentManager)5 ServletContext (javax.servlet.ServletContext)4 Attachment (com.xpn.xwiki.api.Attachment)3 CoreConfiguration (com.xpn.xwiki.CoreConfiguration)2 XWikiContext (com.xpn.xwiki.XWikiContext)2 XWikiStubContextProvider (com.xpn.xwiki.util.XWikiStubContextProvider)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 MimeBodyPart (javax.mail.internet.MimeBodyPart)2 MimeMessage (javax.mail.internet.MimeMessage)2 Expectations (org.jmock.Expectations)2 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)2 WikiReference (org.xwiki.model.reference.WikiReference)2 XWiki (com.xpn.xwiki.XWiki)1