JAMES-2363 Make clear MessageAlteringUtils is generating a new message
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/4559beb1 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/4559beb1 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/4559beb1 Branch: refs/heads/master Commit: 4559beb102baabc63d00576965139092391033a4 Parents: 02f126a Author: benwa <[email protected]> Authored: Thu Mar 29 11:02:42 2018 +0700 Committer: benwa <[email protected]> Committed: Thu Mar 29 16:43:29 2018 +0700 ---------------------------------------------------------------------- .../redirect/MailMessageAlteringUtils.java | 254 ------------------- .../mailets/redirect/MessageAlteringUtils.java | 248 ++++++++++++++++++ .../mailets/redirect/ProcessRedirectNotify.java | 12 +- .../redirect/MailMessageAlteringUtilsTest.java | 113 --------- .../redirect/MessageAlteringUtilsTest.java | 99 ++++++++ 5 files changed, 351 insertions(+), 375 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/4559beb1/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/redirect/MailMessageAlteringUtils.java ---------------------------------------------------------------------- diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/redirect/MailMessageAlteringUtils.java b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/redirect/MailMessageAlteringUtils.java deleted file mode 100644 index c998e2c..0000000 --- a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/redirect/MailMessageAlteringUtils.java +++ /dev/null @@ -1,254 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ -package org.apache.james.transport.mailets.redirect; - -import java.io.ByteArrayOutputStream; -import java.util.Enumeration; - -import javax.mail.BodyPart; -import javax.mail.MessagingException; -import javax.mail.internet.MimeBodyPart; -import javax.mail.internet.MimeMessage; -import javax.mail.internet.MimeMultipart; - -import org.apache.james.server.core.MimeMessageUtil; -import org.apache.james.transport.mailets.utils.MimeMessageUtils; -import org.apache.mailet.Mail; -import org.apache.mailet.base.RFC2822Headers; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.google.common.annotations.VisibleForTesting; -import com.google.common.base.Preconditions; - -public class MailMessageAlteringUtils { - private static final Logger LOGGER = LoggerFactory.getLogger(MailMessageAlteringUtils.class); - private static final char LINE_BREAK = '\n'; - - public static Builder from(RedirectNotify mailet) { - return new Builder(mailet); - } - - public static class Builder { - - private RedirectNotify mailet; - private Mail originalMail; - private Mail newMail; - - private Builder(RedirectNotify mailet) { - this.mailet = mailet; - } - - public Builder originalMail(Mail originalMail) { - this.originalMail = originalMail; - return this; - } - - public Builder newMail(Mail newMail) { - this.newMail = newMail; - return this; - } - - public void alterNewMessage() throws MessagingException { - build().alterNewMessage(); - } - - @VisibleForTesting MailMessageAlteringUtils build() { - Preconditions.checkNotNull(mailet, "'mailet' is mandatory"); - Preconditions.checkNotNull(originalMail, "'originalMail' is mandatory"); - Preconditions.checkNotNull(newMail, "'newMail' is mandatory"); - return new MailMessageAlteringUtils(mailet, originalMail, newMail); - } - } - - private final RedirectNotify mailet; - private final Mail originalMail; - private final Mail newMail; - - private MailMessageAlteringUtils(RedirectNotify mailet, Mail originalMail, Mail newMail) { - this.mailet = mailet; - this.originalMail = originalMail; - this.newMail = newMail; - } - - /** - * Builds the message of the newMail in case it has to be altered. - */ - private void alterNewMessage() throws MessagingException { - - MimeMessage originalMessage = originalMail.getMessage(); - MimeMessage newMessage = newMail.getMessage(); - - // Copy the relevant headers - copyRelevantHeaders(originalMessage, newMessage); - - String head = new MimeMessageUtils(originalMessage).getMessageHeaders(); - try { - // Create the message body - MimeMultipart multipart = new MimeMultipart("mixed"); - - // Create the message - MimeMultipart mpContent = new MimeMultipart("alternative"); - mpContent.addBodyPart(getBodyPart(originalMail, originalMessage, head)); - - MimeBodyPart contentPartRoot = new MimeBodyPart(); - contentPartRoot.setContent(mpContent); - - multipart.addBodyPart(contentPartRoot); - - if (mailet.getInitParameters().isDebug()) { - LOGGER.debug("attachmentType:{}", mailet.getInitParameters().getAttachmentType()); - } - if (!mailet.getInitParameters().getAttachmentType().equals(TypeCode.NONE)) { - multipart.addBodyPart(getAttachmentPart(originalMessage, head)); - } - - if (mailet.getInitParameters().isAttachError() && originalMail.getErrorMessage() != null) { - multipart.addBodyPart(getErrorPart(originalMail)); - } - newMail.getMessage().setContent(multipart); - newMail.getMessage().setHeader(RFC2822Headers.CONTENT_TYPE, multipart.getContentType()); - - } catch (Exception ioe) { - throw new MessagingException("Unable to create multipart body", ioe); - } - } - - private BodyPart getBodyPart(Mail originalMail, MimeMessage originalMessage, String head) throws MessagingException, Exception { - MimeBodyPart part = new MimeBodyPart(); - part.setText(getText(originalMail, originalMessage, head)); - part.setDisposition(javax.mail.Part.INLINE); - return part; - } - - private MimeBodyPart getAttachmentPart(MimeMessage originalMessage, String head) throws MessagingException, Exception { - MimeBodyPart attachmentPart = new MimeBodyPart(); - switch (mailet.getInitParameters().getAttachmentType()) { - case HEADS: - attachmentPart.setText(head); - break; - case BODY: - try { - attachmentPart.setText(getMessageBody(originalMessage)); - } catch (Exception e) { - attachmentPart.setText("body unavailable"); - } - break; - case ALL: - attachmentPart.setText(head + "\r\nMessage:\r\n" + getMessageBody(originalMessage)); - break; - case MESSAGE: - attachmentPart.setContent(originalMessage, "message/rfc822"); - break; - case NONE: - break; - case UNALTERED: - break; - } - attachmentPart.setFileName(getFileName(originalMessage.getSubject())); - attachmentPart.setDisposition(javax.mail.Part.ATTACHMENT); - return attachmentPart; - } - - @VisibleForTesting String getFileName(String subject) { - if (subject != null && !subject.trim().isEmpty()) { - return subject.trim(); - } - return "No Subject"; - } - - private MimeBodyPart getErrorPart(Mail originalMail) throws MessagingException { - MimeBodyPart errorPart = new MimeBodyPart(); - errorPart.setContent(originalMail.getErrorMessage(), "text/plain"); - errorPart.setHeader(RFC2822Headers.CONTENT_TYPE, "text/plain"); - errorPart.setFileName("Reasons"); - errorPart.setDisposition(javax.mail.Part.ATTACHMENT); - return errorPart; - } - - private String getText(Mail originalMail, MimeMessage originalMessage, String head) throws MessagingException { - StringBuilder builder = new StringBuilder(); - - String messageText = mailet.getMessage(originalMail); - if (messageText != null) { - builder.append(messageText) - .append(LINE_BREAK); - } - - if (mailet.getInitParameters().isDebug()) { - LOGGER.debug("inline:{}", mailet.getInitParameters().getInLineType()); - } - switch (mailet.getInitParameters().getInLineType()) { - case ALL: - builder.append(headText(head)); - builder.append(bodyText(originalMessage)); - break; - case HEADS: - builder.append(headText(head)); - break; - case BODY: - builder.append(bodyText(originalMessage)); - break; - case NONE: - break; - case MESSAGE: - break; - case UNALTERED: - break; - } - return builder.toString(); - } - - private String headText(String head) { - return "Message Headers:" + LINE_BREAK + head + LINE_BREAK; - } - - private String bodyText(MimeMessage originalMessage) { - StringBuilder builder = new StringBuilder(); - builder.append("Message:") - .append(LINE_BREAK); - try { - builder.append(getMessageBody(originalMessage)) - .append(LINE_BREAK); - } catch (Exception e) { - builder.append("body unavailable") - .append(LINE_BREAK); - } - return builder.toString(); - } - - /** - * Utility method for obtaining a string representation of a Message's body - */ - private String getMessageBody(MimeMessage message) throws Exception { - ByteArrayOutputStream bodyOs = new ByteArrayOutputStream(); - MimeMessageUtil.writeMessageBodyTo(message, bodyOs); - return bodyOs.toString(); - } - - private void copyRelevantHeaders(MimeMessage originalMessage, MimeMessage newMessage) throws MessagingException { - Enumeration<String> headerEnum = originalMessage.getMatchingHeaderLines( - new String[] { RFC2822Headers.DATE, RFC2822Headers.FROM, RFC2822Headers.REPLY_TO, RFC2822Headers.TO, - RFC2822Headers.SUBJECT, RFC2822Headers.RETURN_PATH }); - while (headerEnum.hasMoreElements()) { - newMessage.addHeaderLine(headerEnum.nextElement()); - } - } - -} http://git-wip-us.apache.org/repos/asf/james-project/blob/4559beb1/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/redirect/MessageAlteringUtils.java ---------------------------------------------------------------------- diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/redirect/MessageAlteringUtils.java b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/redirect/MessageAlteringUtils.java new file mode 100644 index 0000000..21f5b08 --- /dev/null +++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/redirect/MessageAlteringUtils.java @@ -0,0 +1,248 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ +package org.apache.james.transport.mailets.redirect; + +import java.io.ByteArrayOutputStream; +import java.util.Enumeration; + +import javax.mail.BodyPart; +import javax.mail.Header; +import javax.mail.MessagingException; +import javax.mail.Session; +import javax.mail.internet.MimeBodyPart; +import javax.mail.internet.MimeMessage; +import javax.mail.internet.MimeMultipart; + +import org.apache.james.server.core.MimeMessageUtil; +import org.apache.james.transport.mailets.utils.MimeMessageUtils; +import org.apache.mailet.Mail; +import org.apache.mailet.base.RFC2822Headers; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Preconditions; + +public class MessageAlteringUtils { + private static final Logger LOGGER = LoggerFactory.getLogger(MessageAlteringUtils.class); + private static final char LINE_BREAK = '\n'; + + public static Builder from(RedirectNotify mailet) { + return new Builder(mailet); + } + + public static class Builder { + + private RedirectNotify mailet; + private Mail originalMail; + + private Builder(RedirectNotify mailet) { + this.mailet = mailet; + } + + public Builder originalMail(Mail originalMail) { + this.originalMail = originalMail; + return this; + } + + + public MimeMessage alteredMessage() throws MessagingException { + return build().alteredMessage(); + } + + @VisibleForTesting MessageAlteringUtils build() { + Preconditions.checkNotNull(mailet, "'mailet' is mandatory"); + Preconditions.checkNotNull(originalMail, "'originalMail' is mandatory"); + return new MessageAlteringUtils(mailet, originalMail); + } + } + + private final RedirectNotify mailet; + private final Mail originalMail; + + private MessageAlteringUtils(RedirectNotify mailet, Mail originalMail) { + this.mailet = mailet; + this.originalMail = originalMail; + } + + /** + * Builds the message of the newMail in case it has to be altered. + */ + private MimeMessage alteredMessage() throws MessagingException { + MimeMessage originalMessage = originalMail.getMessage(); + MimeMessage newMessage = new MimeMessage(Session.getDefaultInstance(System.getProperties(), null)); + + // Copy the relevant headers + copyRelevantHeaders(originalMessage, newMessage); + + String head = new MimeMessageUtils(originalMessage).getMessageHeaders(); + try { + // Create the message body + MimeMultipart multipart = new MimeMultipart("mixed"); + + // Create the message + MimeMultipart mpContent = new MimeMultipart("alternative"); + mpContent.addBodyPart(getBodyPart(originalMail, originalMessage, head)); + + MimeBodyPart contentPartRoot = new MimeBodyPart(); + contentPartRoot.setContent(mpContent); + + multipart.addBodyPart(contentPartRoot); + + if (mailet.getInitParameters().isDebug()) { + LOGGER.debug("attachmentType:{}", mailet.getInitParameters().getAttachmentType()); + } + if (!mailet.getInitParameters().getAttachmentType().equals(TypeCode.NONE)) { + multipart.addBodyPart(getAttachmentPart(originalMessage, head)); + } + + if (mailet.getInitParameters().isAttachError() && originalMail.getErrorMessage() != null) { + multipart.addBodyPart(getErrorPart(originalMail)); + } + newMessage.setContent(multipart); + newMessage.setHeader(RFC2822Headers.CONTENT_TYPE, multipart.getContentType()); + return newMessage; + } catch (Exception ioe) { + throw new MessagingException("Unable to create multipart body", ioe); + } + } + + private BodyPart getBodyPart(Mail originalMail, MimeMessage originalMessage, String head) throws MessagingException, Exception { + MimeBodyPart part = new MimeBodyPart(); + part.setText(getText(originalMail, originalMessage, head)); + part.setDisposition(javax.mail.Part.INLINE); + return part; + } + + private MimeBodyPart getAttachmentPart(MimeMessage originalMessage, String head) throws MessagingException, Exception { + MimeBodyPart attachmentPart = new MimeBodyPart(); + switch (mailet.getInitParameters().getAttachmentType()) { + case HEADS: + attachmentPart.setText(head); + break; + case BODY: + try { + attachmentPart.setText(getMessageBody(originalMessage)); + } catch (Exception e) { + attachmentPart.setText("body unavailable"); + } + break; + case ALL: + attachmentPart.setText(head + "\r\nMessage:\r\n" + getMessageBody(originalMessage)); + break; + case MESSAGE: + attachmentPart.setContent(originalMessage, "message/rfc822"); + break; + case NONE: + break; + case UNALTERED: + break; + } + attachmentPart.setFileName(getFileName(originalMessage.getSubject())); + attachmentPart.setDisposition(javax.mail.Part.ATTACHMENT); + return attachmentPart; + } + + @VisibleForTesting String getFileName(String subject) { + if (subject != null && !subject.trim().isEmpty()) { + return subject.trim(); + } + return "No Subject"; + } + + private MimeBodyPart getErrorPart(Mail originalMail) throws MessagingException { + MimeBodyPart errorPart = new MimeBodyPart(); + errorPart.setContent(originalMail.getErrorMessage(), "text/plain"); + errorPart.setHeader(RFC2822Headers.CONTENT_TYPE, "text/plain"); + errorPart.setFileName("Reasons"); + errorPart.setDisposition(javax.mail.Part.ATTACHMENT); + return errorPart; + } + + private String getText(Mail originalMail, MimeMessage originalMessage, String head) throws MessagingException { + StringBuilder builder = new StringBuilder(); + + String messageText = mailet.getMessage(originalMail); + if (messageText != null) { + builder.append(messageText) + .append(LINE_BREAK); + } + + if (mailet.getInitParameters().isDebug()) { + LOGGER.debug("inline:{}", mailet.getInitParameters().getInLineType()); + } + switch (mailet.getInitParameters().getInLineType()) { + case ALL: + builder.append(headText(head)); + builder.append(bodyText(originalMessage)); + break; + case HEADS: + builder.append(headText(head)); + break; + case BODY: + builder.append(bodyText(originalMessage)); + break; + case NONE: + break; + case MESSAGE: + break; + case UNALTERED: + break; + } + return builder.toString(); + } + + private String headText(String head) { + return "Message Headers:" + LINE_BREAK + head + LINE_BREAK; + } + + private String bodyText(MimeMessage originalMessage) { + StringBuilder builder = new StringBuilder(); + builder.append("Message:") + .append(LINE_BREAK); + try { + builder.append(getMessageBody(originalMessage)) + .append(LINE_BREAK); + } catch (Exception e) { + builder.append("body unavailable") + .append(LINE_BREAK); + } + return builder.toString(); + } + + /** + * Utility method for obtaining a string representation of a Message's body + */ + private String getMessageBody(MimeMessage message) throws Exception { + ByteArrayOutputStream bodyOs = new ByteArrayOutputStream(); + MimeMessageUtil.writeMessageBodyTo(message, bodyOs); + return bodyOs.toString(); + } + + private void copyRelevantHeaders(MimeMessage originalMessage, MimeMessage newMessage) throws MessagingException { + Enumeration<Header> headerEnum = originalMessage.getMatchingHeaders( + new String[] { RFC2822Headers.DATE, RFC2822Headers.FROM, RFC2822Headers.REPLY_TO, RFC2822Headers.TO, + RFC2822Headers.SUBJECT, RFC2822Headers.RETURN_PATH }); + while (headerEnum.hasMoreElements()) { + Header header = headerEnum.nextElement(); + newMessage.addHeader(header.getName(), header.getValue()); + } + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/4559beb1/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/redirect/ProcessRedirectNotify.java ---------------------------------------------------------------------- diff --git a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/redirect/ProcessRedirectNotify.java b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/redirect/ProcessRedirectNotify.java index fc2fcc2..7aa25a7 100644 --- a/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/redirect/ProcessRedirectNotify.java +++ b/server/mailet/mailets/src/main/java/org/apache/james/transport/mailets/redirect/ProcessRedirectNotify.java @@ -19,7 +19,6 @@ package org.apache.james.transport.mailets.redirect; import javax.mail.MessagingException; -import javax.mail.Session; import javax.mail.internet.MimeMessage; import org.apache.james.server.core.MailImpl; @@ -116,13 +115,10 @@ public class ProcessRedirectNotify { if (isDebug) { LOGGER.debug("Alter message"); } - newMail.setMessage(new MimeMessage(Session.getDefaultInstance(System.getProperties(), null))); - - // handle the new message if altered - MailMessageAlteringUtils.from(mailet) - .originalMail(originalMail) - .newMail(newMail) - .alterNewMessage(); + newMail.setMessage( + MessageAlteringUtils.from(mailet) + .originalMail(originalMail) + .alteredMessage()); } private void createUnalteredMessage(Mail originalMail, MailImpl newMail) throws MessagingException { http://git-wip-us.apache.org/repos/asf/james-project/blob/4559beb1/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/redirect/MailMessageAlteringUtilsTest.java ---------------------------------------------------------------------- diff --git a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/redirect/MailMessageAlteringUtilsTest.java b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/redirect/MailMessageAlteringUtilsTest.java deleted file mode 100644 index 70a7768..0000000 --- a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/redirect/MailMessageAlteringUtilsTest.java +++ /dev/null @@ -1,113 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ -package org.apache.james.transport.mailets.redirect; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.mock; - -import org.apache.mailet.Mail; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; - -public class MailMessageAlteringUtilsTest { - - @Rule - public ExpectedException expectedException = ExpectedException.none(); - - @Test - public void buildShouldThrowWhenMailetIsNull() { - expectedException.expect(NullPointerException.class); - expectedException.expectMessage("'mailet' is mandatory"); - MailMessageAlteringUtils.from(null).build(); - } - - @Test - public void buildShouldThrowWhenOriginalMailIsNull() { - expectedException.expect(NullPointerException.class); - expectedException.expectMessage("'originalMail' is mandatory"); - MailMessageAlteringUtils.from(mock(RedirectNotify.class)) - .build(); - } - - @Test - public void buildShouldThrowWhenNewMailIsNull() { - expectedException.expect(NullPointerException.class); - expectedException.expectMessage("'newMail' is mandatory"); - MailMessageAlteringUtils.from(mock(RedirectNotify.class)) - .originalMail(mock(Mail.class)) - .build(); - } - - @Test - public void buildShouldWorkWhenEverythingProvided() { - MailMessageAlteringUtils.from(mock(RedirectNotify.class)) - .originalMail(mock(Mail.class)) - .newMail(mock(Mail.class)) - .build(); - } - - @Test - public void getFileNameShouldReturnNoSubjectWhenSubjectIsNull() { - MailMessageAlteringUtils alteredMailUtils = MailMessageAlteringUtils.from(mock(RedirectNotify.class)) - .originalMail(mock(Mail.class)) - .newMail(mock(Mail.class)) - .build(); - - String fileName = alteredMailUtils.getFileName(null); - - assertThat(fileName).isEqualTo("No Subject"); - } - - @Test - public void getFileNameShouldReturnNoSubjectWhenSubjectContainsOnlySpaces() { - MailMessageAlteringUtils alteredMailUtils = MailMessageAlteringUtils.from(mock(RedirectNotify.class)) - .originalMail(mock(Mail.class)) - .newMail(mock(Mail.class)) - .build(); - - String fileName = alteredMailUtils.getFileName(" "); - - assertThat(fileName).isEqualTo("No Subject"); - } - - @Test - public void getFileNameShouldReturnSubjectWhenSubjectIsGiven() { - MailMessageAlteringUtils alteredMailUtils = MailMessageAlteringUtils.from(mock(RedirectNotify.class)) - .originalMail(mock(Mail.class)) - .newMail(mock(Mail.class)) - .build(); - - String fileName = alteredMailUtils.getFileName("my Subject"); - - assertThat(fileName).isEqualTo("my Subject"); - } - - @Test - public void getFileNameShouldReturnTrimmedSubjectWhenSubjectStartsWithSpaces() { - MailMessageAlteringUtils alteredMailUtils = MailMessageAlteringUtils.from(mock(RedirectNotify.class)) - .originalMail(mock(Mail.class)) - .newMail(mock(Mail.class)) - .build(); - - String fileName = alteredMailUtils.getFileName(" my Subject"); - - assertThat(fileName).isEqualTo("my Subject"); - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/4559beb1/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/redirect/MessageAlteringUtilsTest.java ---------------------------------------------------------------------- diff --git a/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/redirect/MessageAlteringUtilsTest.java b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/redirect/MessageAlteringUtilsTest.java new file mode 100644 index 0000000..248f31a --- /dev/null +++ b/server/mailet/mailets/src/test/java/org/apache/james/transport/mailets/redirect/MessageAlteringUtilsTest.java @@ -0,0 +1,99 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ +package org.apache.james.transport.mailets.redirect; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; + +import org.apache.mailet.Mail; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +public class MessageAlteringUtilsTest { + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + @Test + public void buildShouldThrowWhenMailetIsNull() { + expectedException.expect(NullPointerException.class); + expectedException.expectMessage("'mailet' is mandatory"); + MessageAlteringUtils.from(null).build(); + } + + @Test + public void buildShouldThrowWhenOriginalMailIsNull() { + expectedException.expect(NullPointerException.class); + expectedException.expectMessage("'originalMail' is mandatory"); + MessageAlteringUtils.from(mock(RedirectNotify.class)) + .build(); + } + + @Test + public void buildShouldWorkWhenEverythingProvided() { + MessageAlteringUtils.from(mock(RedirectNotify.class)) + .originalMail(mock(Mail.class)) + .build(); + } + + @Test + public void getFileNameShouldReturnNoSubjectWhenSubjectIsNull() { + MessageAlteringUtils alteredMailUtils = MessageAlteringUtils.from(mock(RedirectNotify.class)) + .originalMail(mock(Mail.class)) + .build(); + + String fileName = alteredMailUtils.getFileName(null); + + assertThat(fileName).isEqualTo("No Subject"); + } + + @Test + public void getFileNameShouldReturnNoSubjectWhenSubjectContainsOnlySpaces() { + MessageAlteringUtils alteredMailUtils = MessageAlteringUtils.from(mock(RedirectNotify.class)) + .originalMail(mock(Mail.class)) + .build(); + + String fileName = alteredMailUtils.getFileName(" "); + + assertThat(fileName).isEqualTo("No Subject"); + } + + @Test + public void getFileNameShouldReturnSubjectWhenSubjectIsGiven() { + MessageAlteringUtils alteredMailUtils = MessageAlteringUtils.from(mock(RedirectNotify.class)) + .originalMail(mock(Mail.class)) + .build(); + + String fileName = alteredMailUtils.getFileName("my Subject"); + + assertThat(fileName).isEqualTo("my Subject"); + } + + @Test + public void getFileNameShouldReturnTrimmedSubjectWhenSubjectStartsWithSpaces() { + MessageAlteringUtils alteredMailUtils = MessageAlteringUtils.from(mock(RedirectNotify.class)) + .originalMail(mock(Mail.class)) + .build(); + + String fileName = alteredMailUtils.getFileName(" my Subject"); + + assertThat(fileName).isEqualTo("my Subject"); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
