Repository: james-project Updated Branches: refs/heads/master 7dbbdee6d -> 1ce23080e
MAILET-104: Ensure RemoveMailAttributes has a good coverage and meet new standards Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/665a11a7 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/665a11a7 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/665a11a7 Branch: refs/heads/master Commit: 665a11a7d3209563a1fed645617baa7d6624ba5a Parents: 2fda869 Author: Quynh Nguyen <[email protected]> Authored: Mon Oct 3 17:20:24 2016 +0700 Committer: Quynh Nguyen <[email protected]> Committed: Thu Oct 20 13:38:52 2016 +0700 ---------------------------------------------------------------------- .../transport/mailets/RemoveMailAttribute.java | 62 +++++----- .../mailets/RemoveMailAttributeTest.java | 115 ++++++++++++------- 2 files changed, 102 insertions(+), 75 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/665a11a7/mailet/standard/src/main/java/org/apache/james/transport/mailets/RemoveMailAttribute.java ---------------------------------------------------------------------- diff --git a/mailet/standard/src/main/java/org/apache/james/transport/mailets/RemoveMailAttribute.java b/mailet/standard/src/main/java/org/apache/james/transport/mailets/RemoveMailAttribute.java index 97e4d4d..aaf3193 100644 --- a/mailet/standard/src/main/java/org/apache/james/transport/mailets/RemoveMailAttribute.java +++ b/mailet/standard/src/main/java/org/apache/james/transport/mailets/RemoveMailAttribute.java @@ -21,12 +21,14 @@ package org.apache.james.transport.mailets; +import com.google.common.base.Preconditions; +import com.google.common.base.Splitter; +import com.google.common.base.Strings; +import com.google.common.collect.ImmutableList; import org.apache.mailet.base.GenericMailet; import org.apache.mailet.Mail; import org.apache.mailet.MailetException; -import java.util.ArrayList; -import java.util.StringTokenizer; import javax.mail.MessagingException; /** @@ -44,50 +46,38 @@ import javax.mail.MessagingException; * @since 2.2.0 */ public class RemoveMailAttribute extends GenericMailet { - - private final ArrayList<String> attributesToRemove = new ArrayList<String>(); - - /** - * Return a string describing this mailet. - * - * @return a string describing this mailet - */ + + private static final char ATTRIBUTE_SEPARATOR_CHAR = ','; + protected static final String MAILET_NAME_PARAMETER = "name"; + + private ImmutableList<String> attributesToRemove; + public String getMailetInfo() { return "Remove Mail Attribute Mailet"; } - /** - * Initialize the mailet - * - * @throws MailetException if the processor parameter is missing - */ - public void init() throws MailetException - { - String name = getInitParameter("name"); + @Override + public void init() throws MailetException { + String name = getInitParameter(MAILET_NAME_PARAMETER); - if (name != null) { - StringTokenizer st = new StringTokenizer(name, ",") ; - while (st.hasMoreTokens()) { - String attribute_name = st.nextToken().trim() ; - attributesToRemove.add(attribute_name); - } - } else { + if (Strings.isNullOrEmpty(name)) { throw new MailetException("Please configure at least one attribute to remove"); } + + attributesToRemove = getAttributes(name); } - /** - * Remove the configured attributes - * - * @param mail the mail to process - * - * @throws MessagingException in all cases - */ + private ImmutableList<String> getAttributes(String name) { + return ImmutableList.copyOf(Splitter.on(ATTRIBUTE_SEPARATOR_CHAR) + .trimResults() + .split(name)); + } + + @Override public void service(Mail mail) throws MessagingException { - for (String anAttributesToRemove : attributesToRemove) { - mail.removeAttribute(anAttributesToRemove); + Preconditions.checkNotNull(mail); + for (String attributeToRemove : attributesToRemove) { + mail.removeAttribute(attributeToRemove); } } - - } http://git-wip-us.apache.org/repos/asf/james-project/blob/665a11a7/mailet/standard/src/test/java/org/apache/james/transport/mailets/RemoveMailAttributeTest.java ---------------------------------------------------------------------- diff --git a/mailet/standard/src/test/java/org/apache/james/transport/mailets/RemoveMailAttributeTest.java b/mailet/standard/src/test/java/org/apache/james/transport/mailets/RemoveMailAttributeTest.java index 4c73843..e88036c 100644 --- a/mailet/standard/src/test/java/org/apache/james/transport/mailets/RemoveMailAttributeTest.java +++ b/mailet/standard/src/test/java/org/apache/james/transport/mailets/RemoveMailAttributeTest.java @@ -19,69 +19,106 @@ package org.apache.james.transport.mailets; +import static org.assertj.core.api.Assertions.assertThat; import javax.mail.MessagingException; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; import org.apache.mailet.Mail; import org.apache.mailet.Mailet; +import org.apache.mailet.MailetException; import org.apache.mailet.base.test.FakeMail; import org.apache.mailet.base.test.FakeMailContext; import org.apache.mailet.base.test.FakeMailetConfig; -import org.junit.Assert; +import org.junit.Before; import org.junit.Test; -public class RemoveMailAttributeTest { - - public static final String MAIL_ATTRIBUTE_NAME1 = "org.apache.james.test.junit"; +import java.io.Serializable; - public static final String MAIL_ATTRIBUTE_NAME2 = "org.apache.james.test.junit2"; +public class RemoveMailAttributeTest { - private Mail createMail() throws MessagingException { - return FakeMail.builder() - .attribute(MAIL_ATTRIBUTE_NAME1, "true") - .attribute(MAIL_ATTRIBUTE_NAME2, "true") - .build(); + private static final String ATTRIBUTE_1 = "attribute1"; + private static final String ATTRIBUTE_2 = "attribute2"; + private static final String ATTRIBUTE_3 = "attribute3"; + private static final String VALUE_1 = "value1"; + private static final String VALUE_2 = "value2"; + private static final String VALUE_3 = "value3"; + private static final String ATTRIBUTE1_ATTRIBUTE2 = "attribute1, attribute2"; + private Mailet removeMailet; + private FakeMailetConfig mailetConfig; + + @Before + public void setup() throws Exception { + removeMailet = new RemoveMailAttribute(); + mailetConfig = new FakeMailetConfig("Test", FakeMailContext.defaultContext()); } - private Mailet setupMailet(String attribute) throws MessagingException { - Mailet mailet = new RemoveMailAttribute(); - FakeMailetConfig mci = new FakeMailetConfig("Test", - FakeMailContext.defaultContext()); - if (attribute != null) { - mci.setProperty("name", attribute); - } + @Test + public void getMailetInfoShouldReturnCorrectInformation() throws Exception { + assertThat(removeMailet.getMailetInfo()).isEqualTo("Remove Mail Attribute Mailet"); + } - mailet.init(mci); - return mailet; + @Test(expected = MailetException.class) + public void initShouldThrowExceptionIfMailetConfigDoesNotContainAttribute() throws MessagingException { + removeMailet.init(mailetConfig); } + @Test(expected = NullPointerException.class) + public void serviceShouldThrowExceptionWithMailNull() throws MessagingException { + removeMailet.service(null); + } @Test - public void testRemoveMailAttribute() throws MessagingException { - Mail m = createMail(); - Mailet mailet = setupMailet(MAIL_ATTRIBUTE_NAME1); - - // check if the mail has a attribute - Assert.assertNotNull("Attribute exists", m.getAttribute(MAIL_ATTRIBUTE_NAME1)); - Assert.assertNotNull("Attribute exists", m.getAttribute(MAIL_ATTRIBUTE_NAME2)); + public void serviceShouldDoNothingWhenMailHasEmptyAttribute() throws MessagingException { + mailetConfig.setProperty(RemoveMailAttribute.MAILET_NAME_PARAMETER, ATTRIBUTE1_ATTRIBUTE2); + removeMailet.init(mailetConfig); - mailet.service(m); + Mail mail = FakeMail.builder().build(); + removeMailet.service(mail); - // Check if the attribute was removed - Assert.assertNull("Attribute exists", m.getAttribute(MAIL_ATTRIBUTE_NAME1)); - Assert.assertNotNull("Attribute deleted", m.getAttribute(MAIL_ATTRIBUTE_NAME2)); + assertThat(mail.getAttributeNames()).isEmpty(); } + @Test + public void serviceShouldDoNothingWhenMailDoNotMatchAttribute() throws MessagingException { + mailetConfig.setProperty(RemoveMailAttribute.MAILET_NAME_PARAMETER, ATTRIBUTE1_ATTRIBUTE2); + removeMailet.init(mailetConfig); + + Mail mail = FakeMail.builder() + .attribute(ATTRIBUTE_3, VALUE_3) + .build(); + removeMailet.service(mail); + + assertThat(mail.getAttributeNames()).containsExactly(ATTRIBUTE_3); + } @Test - public void testInvalidConfig() throws MessagingException { - boolean exception = false; - try { - setupMailet(null); - } catch (MessagingException e) { - exception = true; - } - - Assert.assertTrue("invalid Config", exception); + public void serviceShouldRemoveSpecifiedAttribute() throws MessagingException { + mailetConfig.setProperty(RemoveMailAttribute.MAILET_NAME_PARAMETER, ATTRIBUTE_1); + removeMailet.init(mailetConfig); + + Mail mail = FakeMail.builder() + .attribute(ATTRIBUTE_1, VALUE_1) + .attribute(ATTRIBUTE_2, VALUE_2) + .attribute(ATTRIBUTE_3, VALUE_3) + .build(); + removeMailet.service(mail); + + assertThat(mail.getAttributeNames()).containsOnly(ATTRIBUTE_2, ATTRIBUTE_3); } + @Test + public void serviceShouldRemoveSpecifiedAttributes() throws MessagingException { + mailetConfig.setProperty(RemoveMailAttribute.MAILET_NAME_PARAMETER, ATTRIBUTE1_ATTRIBUTE2); + removeMailet.init(mailetConfig); + + Mail mail = FakeMail.builder() + .attribute(ATTRIBUTE_1, VALUE_1) + .attribute(ATTRIBUTE_2, VALUE_2) + .attribute(ATTRIBUTE_3, VALUE_3) + .build(); + removeMailet.service(mail); + + assertThat(mail.getAttributeNames()).containsExactly(ATTRIBUTE_3); + } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
