MAILET-130 cover SetMailAttribute with tests

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

Branch: refs/heads/master
Commit: 9b1dd0bae24d2ec4b660d46bfd14de5d3aeabbbf
Parents: 0ef78d6
Author: Matthieu Baechler <matthieu.baech...@linagora.com>
Authored: Mon Aug 29 17:20:52 2016 +0200
Committer: Matthieu Baechler <matthieu.baech...@linagora.com>
Committed: Tue Sep 20 09:12:10 2016 +0200

----------------------------------------------------------------------
 .../transport/mailets/SetMailAttributeTest.java | 79 ++++++++++++++------
 1 file changed, 58 insertions(+), 21 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/9b1dd0ba/mailet/standard/src/test/java/org/apache/james/transport/mailets/SetMailAttributeTest.java
----------------------------------------------------------------------
diff --git 
a/mailet/standard/src/test/java/org/apache/james/transport/mailets/SetMailAttributeTest.java
 
b/mailet/standard/src/test/java/org/apache/james/transport/mailets/SetMailAttributeTest.java
index ae59925..009d3cd 100644
--- 
a/mailet/standard/src/test/java/org/apache/james/transport/mailets/SetMailAttributeTest.java
+++ 
b/mailet/standard/src/test/java/org/apache/james/transport/mailets/SetMailAttributeTest.java
@@ -20,8 +20,7 @@
 
 package org.apache.james.transport.mailets;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNull;
+import static org.assertj.core.api.Assertions.assertThat;
 
 import javax.mail.MessagingException;
 
@@ -31,38 +30,76 @@ import org.apache.mailet.base.test.FakeMailContext;
 import org.apache.mailet.base.test.FakeMailetConfig;
 import org.apache.mailet.base.test.MailUtil;
 import org.junit.Before;
+import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.ExpectedException;
 
 public class SetMailAttributeTest {
 
+    @Rule public ExpectedException expectedException = 
ExpectedException.none();
+    
     private Mailet mailet;
 
-    private final String ATTRIBUTE_NAME1 = "org.apache.james.junit1";
-
-    private final String ATTRIBUTE_NAME2 = "org.apache.james.junit2";
-
-    private Mail mockedMail;
-
     @Before
     public void setupMailet() throws MessagingException {
-        mockedMail = MailUtil.createMockMail2Recipients(null);
         mailet = new SetMailAttribute();
-        FakeMailetConfig mci = new FakeMailetConfig("Test",
-                FakeMailContext.defaultContext());
-        mci.setProperty(ATTRIBUTE_NAME1, "true");
-        mci.setProperty(ATTRIBUTE_NAME2, "true");
+    }
+
+    @Test
+    public void shouldAddConfiguredAttributes() throws MessagingException {
+        FakeMailetConfig mailetConfig = new FakeMailetConfig("Test", 
FakeMailContext.defaultContext());
+        mailetConfig.setProperty("org.apache.james.junit1", "true");
+        mailetConfig.setProperty("org.apache.james.junit2", "happy");
+
+        mailet.init(mailetConfig);
+
+        Mail mail = MailUtil.createMockMail2Recipients(null);
+        
+        mailet.service(mail);
 
-        mailet.init(mci);
+        
assertThat(mail.getAttribute("org.apache.james.junit1")).isEqualTo("true");
+        
assertThat(mail.getAttribute("org.apache.james.junit2")).isEqualTo("happy");
     }
+    
+    @Test
+    public void shouldAddNothingWhenNoConfiguredAttribute() throws 
MessagingException {
+        FakeMailetConfig mailetConfig = new FakeMailetConfig("Test", 
FakeMailContext.defaultContext());
+     
+        mailet.init(mailetConfig);
+
+        Mail mail = MailUtil.createMockMail2Recipients(null);
+        
+        mailet.service(mail);
 
-    // test if the Header was add
+        assertThat(mail.getAttributeNames()).isEmpty();
+    }
+    
     @Test
-    public void testMailAttributeAdded() throws MessagingException {
-        assertNull(mockedMail.getAttribute(ATTRIBUTE_NAME1));
-        assertNull(mockedMail.getAttribute(ATTRIBUTE_NAME2));
-        mailet.service(mockedMail);
+    public void shouldOverwriteAttributeWhenAttributeAlreadyPresent() throws 
MessagingException {
+        FakeMailetConfig mailetConfig = new FakeMailetConfig("Test", 
FakeMailContext.defaultContext());
+        mailetConfig.setProperty("org.apache.james.junit1", "bar");
+        
+        mailet.init(mailetConfig);
+        
+        Mail mail = MailUtil.createMockMail2Recipients(null);
+        mail.setAttribute("org.apache.james.junit1", "foo");
+        
+        mailet.service(mail);
 
-        assertEquals("true", mockedMail.getAttribute(ATTRIBUTE_NAME1));
-        assertEquals("true", mockedMail.getAttribute(ATTRIBUTE_NAME2));
+        
assertThat(mail.getAttribute("org.apache.james.junit1")).isEqualTo("bar");
+    }
+    
+    @Test
+    public void shouldThrowWhenNullProperty() throws MessagingException {
+        FakeMailetConfig mailetConfig = new FakeMailetConfig("Test", 
FakeMailContext.defaultContext());
+        expectedException.expect(NullPointerException.class);
+        mailetConfig.setProperty(null, "bar");
+    }
+    
+    @Test
+    public void shouldThrowWhenNullValue() throws MessagingException {
+        FakeMailetConfig mailetConfig = new FakeMailetConfig("Test", 
FakeMailContext.defaultContext());
+        expectedException.expect(NullPointerException.class);
+        mailetConfig.setProperty("org.apache.james.junit1", null);
     }
 }


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

Reply via email to