james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rouaz...@apache.org
Subject [2/4] james-project git commit: MAILET-106 Refactor RemoveMimeHeader
Date Tue, 30 Aug 2016 07:59:58 GMT
MAILET-106 Refactor RemoveMimeHeader


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/b384e4bd
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/b384e4bd
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/b384e4bd

Branch: refs/heads/master
Commit: b384e4bdbe0b2445702b4fcc84229334f010def2
Parents: 93d543c
Author: Antoine Duprat <aduprat@linagora.com>
Authored: Mon Aug 29 09:17:36 2016 +0200
Committer: Antoine Duprat <aduprat@linagora.com>
Committed: Mon Aug 29 16:17:54 2016 +0200

----------------------------------------------------------------------
 .../transport/mailets/RemoveMimeHeader.java     |  53 ++-----
 .../transport/mailets/RemoveMimeHeaderTest.java | 150 ++++++++++++-------
 2 files changed, 109 insertions(+), 94 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/b384e4bd/mailet/standard/src/main/java/org/apache/james/transport/mailets/RemoveMimeHeader.java
----------------------------------------------------------------------
diff --git a/mailet/standard/src/main/java/org/apache/james/transport/mailets/RemoveMimeHeader.java
b/mailet/standard/src/main/java/org/apache/james/transport/mailets/RemoveMimeHeader.java
index 4b34a2e..43857a6 100644
--- a/mailet/standard/src/main/java/org/apache/james/transport/mailets/RemoveMimeHeader.java
+++ b/mailet/standard/src/main/java/org/apache/james/transport/mailets/RemoveMimeHeader.java
@@ -21,15 +21,17 @@
 
 package org.apache.james.transport.mailets;
 
-import java.util.ArrayList;
-import java.util.StringTokenizer;
+import java.util.List;
 
 import javax.mail.MessagingException;
 import javax.mail.internet.MimeMessage;
 
-import org.apache.mailet.base.GenericMailet;
 import org.apache.mailet.Mail;
 import org.apache.mailet.MailetException;
+import org.apache.mailet.base.GenericMailet;
+
+import com.google.common.base.Splitter;
+import com.google.common.collect.ImmutableList;
 
 /**
  * Remove mime headers
@@ -46,55 +48,32 @@ import org.apache.mailet.MailetException;
  */
 public class RemoveMimeHeader extends GenericMailet {
     
-    /**
-     * Arraylist which holds the headers which should be removed
-     */
-    ArrayList<String> headers = new ArrayList<String>();
+    private List<String> headers;
 
-    /*
-     * (non-Javadoc)
-     * @see org.apache.mailet.base.GenericMailet#init()
-     */
+    @Override
     public void init() throws MailetException {
         String header = getInitParameter("name");
-        if (header != null) {
-            StringTokenizer st = new StringTokenizer(header, ",");
-            while (st.hasMoreTokens()) {
-                headers.add(st.nextToken());
-            }
-        } else {
-            throw new MailetException(
-                    "Invalid config. Please specify atleast one name");
+        if (header == null) {
+            throw new MailetException("Invalid config. Please specify atleast one name");
         }
+        headers = ImmutableList.copyOf(Splitter.on(",").split(header));
+    }
+
+    @Override
+    public String getMailetInfo() {
+        return "RemoveMimeHeader Mailet";
     }
-    
 
-    /*
-     * (non-Javadoc)
-     * @see org.apache.mailet.base.GenericMailet#service(org.apache.mailet.Mail)
-     */
+    @Override
     public void service(Mail mail) {
         try {
             MimeMessage  message = mail.getMessage();
-        
-            // loop through the headers
             for (String header : headers) {
                 message.removeHeader(header);
             }
-            
             message.saveChanges();
-
         } catch (MessagingException e) {
-            // just log the exception
             log("Unable to remove headers: " + e.getMessage());
         }
     }
-    
-    /*
-     * (non-Javadoc)
-     * @see org.apache.mailet.base.GenericMailet#getMailetInfo()
-     */
-    public String getMailetInfo() {
-        return "RemoveMimeHeader Mailet";
-    }
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/b384e4bd/mailet/standard/src/test/java/org/apache/james/transport/mailets/RemoveMimeHeaderTest.java
----------------------------------------------------------------------
diff --git a/mailet/standard/src/test/java/org/apache/james/transport/mailets/RemoveMimeHeaderTest.java
b/mailet/standard/src/test/java/org/apache/james/transport/mailets/RemoveMimeHeaderTest.java
index 33685cf..1b56e85 100644
--- a/mailet/standard/src/test/java/org/apache/james/transport/mailets/RemoveMimeHeaderTest.java
+++ b/mailet/standard/src/test/java/org/apache/james/transport/mailets/RemoveMimeHeaderTest.java
@@ -20,6 +20,10 @@
 
 package org.apache.james.transport.mailets;
 
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
 import javax.mail.MessagingException;
 import javax.mail.internet.MimeMessage;
 
@@ -29,97 +33,129 @@ import org.apache.mailet.base.test.FakeMail;
 import org.apache.mailet.base.test.FakeMailContext;
 import org.apache.mailet.base.test.FakeMailetConfig;
 import org.apache.mailet.base.test.MailUtil;
-import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.ExpectedException;
 
 public class RemoveMimeHeaderTest {
 
-    private final static String HEADER1 = "header1";
-
-    private final static String HEADER2 = "header2";
+    private static final String HEADER1 = "header1";
+    private static final String HEADER2 = "header2";
 
-    private GenericMailet setupMockedMailet(String name1, String name2) throws MessagingException
{
-        GenericMailet mailet = new RemoveMimeHeader();
-        FakeMailetConfig mci = new FakeMailetConfig("Test", FakeMailContext.defaultContext());
-        if (name1 != null) mci.setProperty("name", name1);
-        if (name2 != null) mci.setProperty("name", name2);
+    @Rule
+    public ExpectedException expectedException = ExpectedException.none();
 
-        mailet.init(mci);
-        return mailet;
-    }
+    private GenericMailet mailet;
 
-    private MimeMessage getMockedMimeMessage() throws MessagingException {
-        MimeMessage mockedMimeMessage = MailUtil.createMimeMessage();
-        mockedMimeMessage.setHeader(HEADER1, "true");
-        mockedMimeMessage.setHeader(HEADER2, "true");
-        mockedMimeMessage.saveChanges();
-        return mockedMimeMessage;
+    @Before
+    public void setup() {
+        mailet = new RemoveMimeHeader();
     }
 
-    private Mail getMockedMail(MimeMessage message) {
-        Mail m = new FakeMail();
-        m.setMessage(message);
-        return m;
+    @Test
+    public void getMailetInfoShouldReturnValue() {
+        assertThat(mailet.getMailetInfo()).isEqualTo("RemoveMimeHeader Mailet");
     }
 
     @Test
-    public void testOneHeaderRemoved() throws MessagingException {
-        GenericMailet mailet = setupMockedMailet(HEADER1, null);
-        Mail mail = getMockedMail(getMockedMimeMessage());
+    public void serviceShouldRemoveHeaderWhenOneMatching() throws MessagingException {
+        FakeMailetConfig mailetConfig = new FakeMailetConfig("Test", FakeMailContext.defaultContext());
+        mailetConfig.setProperty("name", HEADER1);
+        mailet.init(mailetConfig);
 
-        // Get sure both headers are present
-        Assert.assertNotNull("Header present", mail.getMessage().getHeader(HEADER1));
-        Assert.assertNotNull("Header present", mail.getMessage().getHeader(HEADER2));
+        MimeMessage mimeMessage = MailUtil.createMimeMessage();
+        mimeMessage.setHeader(HEADER1, "true");
+        mimeMessage.setHeader(HEADER2, "true");
+        mimeMessage.saveChanges();
+        Mail mail = createMail(mimeMessage);
 
         mailet.service(mail);
 
-        // The first header should be removed
-        Assert.assertNull("Header removed", mail.getMessage().getHeader(HEADER1));
-        Assert.assertNotNull("Header present", mail.getMessage().getHeader(HEADER2));
+        assertThat(mail.getMessage().getHeader(HEADER1)).isNull();
+        assertThat(mail.getMessage().getHeader(HEADER2)).isNotNull();
     }
 
     @Test
-    public void testTwoHeaderRemoved() throws MessagingException {
-        GenericMailet mailet = setupMockedMailet(HEADER1, HEADER2);
-        Mail mail = getMockedMail(getMockedMimeMessage());
+    public void serviceShouldRemoveHeadersWhenTwoMatching() throws MessagingException {
+        FakeMailetConfig mailetConfig = new FakeMailetConfig("Test", FakeMailContext.defaultContext());
+        mailetConfig.setProperty("name", HEADER1);
+        mailetConfig.setProperty("name", HEADER2);
+        mailet.init(mailetConfig);
+
+        MimeMessage mimeMessage = MailUtil.createMimeMessage();
+        mimeMessage.setHeader(HEADER1, "true");
+        mimeMessage.setHeader(HEADER2, "true");
+        mimeMessage.saveChanges();
+        Mail mail = createMail(mimeMessage);
 
-        // Get sure both headers are present
-        Assert.assertNotNull("Header present", mail.getMessage().getHeader(HEADER1));
-        Assert.assertNotNull("Header present", mail.getMessage().getHeader(HEADER2));
+        mailet.service(mail);
+
+        assertThat(mail.getMessage().getHeader(HEADER1)).isNull();
+        assertThat(mail.getMessage().getHeader(HEADER2)).isNull();
+    }
+
+    @Test
+    public void serviceShouldNotRemoveHeaderWhenNoneMatching() throws MessagingException
{
+        FakeMailetConfig mailetConfig = new FakeMailetConfig("Test", FakeMailContext.defaultContext());
+        mailetConfig.setProperty("name", "other1");
+        mailetConfig.setProperty("name", "other2");
+        mailet.init(mailetConfig);
+
+        MimeMessage mimeMessage = MailUtil.createMimeMessage();
+        mimeMessage.setHeader(HEADER1, "true");
+        mimeMessage.setHeader(HEADER2, "true");
+        mimeMessage.saveChanges();
+        Mail mail = createMail(mimeMessage);
 
         mailet.service(mail);
 
-        // Both header should be removed
-        Assert.assertNull("Header removed", mail.getMessage().getHeader(HEADER1));
-        Assert.assertNull("Header removed", mail.getMessage().getHeader(HEADER2));
+        assertThat(mail.getMessage().getHeader(HEADER1)).isNotNull();
+        assertThat(mail.getMessage().getHeader(HEADER2)).isNotNull();
     }
 
     @Test
-    public void testNoHeaderRemoved() throws MessagingException {
-        GenericMailet mailet = setupMockedMailet("h1", "h2");
-        Mail mail = getMockedMail(getMockedMimeMessage());
+    public void serviceShouldNotRemoveHeaderWhenEmptyConfig() throws MessagingException {
+        FakeMailetConfig mailetConfig = new FakeMailetConfig("Test", FakeMailContext.defaultContext());
+        mailetConfig.setProperty("name", "");
+        mailet.init(mailetConfig);
 
-        // Get sure both headers are present
-        Assert.assertNotNull("Header present", mail.getMessage().getHeader(HEADER1));
-        Assert.assertNotNull("Header present", mail.getMessage().getHeader(HEADER2));
+        MimeMessage mimeMessage = MailUtil.createMimeMessage();
+        mimeMessage.setHeader(HEADER1, "true");
+        mimeMessage.setHeader(HEADER2, "true");
+        mimeMessage.saveChanges();
+        Mail mail = createMail(mimeMessage);
 
         mailet.service(mail);
 
-        // Both header should be removed
-        Assert.assertNotNull("Header present", mail.getMessage().getHeader(HEADER1));
-        Assert.assertNotNull("header present", mail.getMessage().getHeader(HEADER2));
+        assertThat(mail.getMessage().getHeader(HEADER1)).isNotNull();
+        assertThat(mail.getMessage().getHeader(HEADER2)).isNotNull();
     }
 
     @Test
-    public void testInvalidConfig() throws MessagingException {
-        boolean exception = false;
-        try {
-            setupMockedMailet(null, null);
-        } catch (MessagingException e) {
-            exception = true;
-        }
-        Assert.assertTrue("Exception thrown", exception);
+    public void initShouldThrowWhenInvalidConfig() throws MessagingException {
+        expectedException.expect(MessagingException.class);
+        FakeMailetConfig mailetConfig = new FakeMailetConfig("Test", FakeMailContext.defaultContext());
+        mailet.init(mailetConfig);
     }
 
+    @SuppressWarnings("unchecked")
+    @Test
+    public void serviceShouldNotThrowWhenExceptionOccured() throws MessagingException {
+        FakeMailetConfig mailetConfig = new FakeMailetConfig("Test", FakeMailContext.defaultContext());
+        mailetConfig.setProperty("name", "");
+        mailet.init(mailetConfig);
+
+        Mail mail = mock(Mail.class);
+        when(mail.getMessage())
+            .thenThrow(MessagingException.class);
 
+        mailet.service(mail);
+    }
+
+    private Mail createMail(MimeMessage message) {
+        Mail mail = new FakeMail();
+        mail.setMessage(message);
+        return mail;
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message