MAILET-99 Cover LogMessage 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/38ba938d Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/38ba938d Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/38ba938d Branch: refs/heads/master Commit: 38ba938dd43457ea87c9744122083e0c7eed909a Parents: a24824e Author: Antoine Duprat <[email protected]> Authored: Thu Aug 25 14:46:08 2016 +0200 Committer: Antoine Duprat <[email protected]> Committed: Mon Aug 29 14:54:33 2016 +0200 ---------------------------------------------------------------------- mailet/base/pom.xml | 23 +- .../mailet/base/test/FakeMailContext.java | 37 +++- mailet/pom.xml | 6 + mailet/standard/pom.xml | 5 + .../james/transport/mailets/LogMessage.java | 27 +-- .../james/transport/mailets/LogMessageTest.java | 214 +++++++++++++++++++ 6 files changed, 283 insertions(+), 29 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/38ba938d/mailet/base/pom.xml ---------------------------------------------------------------------- diff --git a/mailet/base/pom.xml b/mailet/base/pom.xml index 7e6b978..38bf3da 100644 --- a/mailet/base/pom.xml +++ b/mailet/base/pom.xml @@ -41,16 +41,26 @@ <dependencies> <dependency> - <groupId>javax.mail</groupId> - <artifactId>mail</artifactId> + <groupId>org.apache.james</groupId> + <artifactId>apache-mailet-api</artifactId> + </dependency> + <dependency> + <groupId>com.google.guava</groupId> + <artifactId>guava</artifactId> + <scope>test</scope> </dependency> <dependency> <groupId>javax.activation</groupId> <artifactId>activation</artifactId> </dependency> <dependency> - <groupId>org.apache.james</groupId> - <artifactId>apache-mailet-api</artifactId> + <groupId>javax.mail</groupId> + <artifactId>mail</artifactId> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> </dependency> <dependency> <groupId>org.apache.james</groupId> @@ -70,6 +80,11 @@ <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-api</artifactId> + <scope>test</scope> + </dependency> </dependencies> <build> http://git-wip-us.apache.org/repos/asf/james-project/blob/38ba938d/mailet/base/src/test/java/org/apache/mailet/base/test/FakeMailContext.java ---------------------------------------------------------------------- diff --git a/mailet/base/src/test/java/org/apache/mailet/base/test/FakeMailContext.java b/mailet/base/src/test/java/org/apache/mailet/base/test/FakeMailContext.java index 7c9dc5f..516dc62 100644 --- a/mailet/base/src/test/java/org/apache/mailet/base/test/FakeMailContext.java +++ b/mailet/base/src/test/java/org/apache/mailet/base/test/FakeMailContext.java @@ -35,9 +35,11 @@ import org.apache.mailet.LookupException; import org.apache.mailet.Mail; import org.apache.mailet.MailAddress; import org.apache.mailet.MailetContext; +import org.slf4j.Logger; import com.google.common.base.MoreObjects; import com.google.common.base.Objects; +import com.google.common.base.Optional; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Lists; @@ -55,8 +57,15 @@ public class FakeMailContext implements MailetContext { public static class Builder { + private Logger logger; + + public Builder logger(Logger logger) { + this.logger = logger; + return this; + } + public FakeMailContext build() { - return new FakeMailContext(); + return new FakeMailContext(Optional.fromNullable(logger)); } } @@ -120,10 +129,12 @@ public class FakeMailContext implements MailetContext { private final HashMap<String, Object> attributes; private final List<SentMail> sentMails; + private final Optional<Logger> logger; - private FakeMailContext() { + private FakeMailContext(Optional<Logger> logger) { attributes = new HashMap<String, Object>(); sentMails = new ArrayList<SentMail>(); + this.logger = logger; } public void bounce(Mail mail, String message) throws MessagingException { @@ -244,12 +255,28 @@ public class FakeMailContext implements MailetContext { } public void log(LogLevel level, String message) { - System.out.println("[" + level + "]" + message); + if (logger.isPresent()) { + switch (level) { + case INFO: + logger.get().info(message); + break; + case WARN: + logger.get().warn(message); + break; + case ERROR: + logger.get().error(message); + break; + default: + logger.get().debug(message); + } + } else { + System.out.println("[" + level + "]" + message); + } } public void log(LogLevel level, String message, Throwable t) { - System.out.println("[" + level + "]" + message); - t.printStackTrace(System.out); + log(level, message); + log(level, t.getMessage()); } public List<String> dnsLookup(String name, RecordType type) throws LookupException { http://git-wip-us.apache.org/repos/asf/james-project/blob/38ba938d/mailet/pom.xml ---------------------------------------------------------------------- diff --git a/mailet/pom.xml b/mailet/pom.xml index 8639392..f1ac89e 100644 --- a/mailet/pom.xml +++ b/mailet/pom.xml @@ -55,6 +55,7 @@ <maven-reporting-api.version>3.0</maven-reporting-api.version> <qdox.version>1.12.1</qdox.version> <assertj-1.version>1.7.1</assertj-1.version> + <slf4j.version>1.7.7</slf4j.version> </properties> @@ -161,6 +162,11 @@ <artifactId>mockito-core</artifactId> <version>1.9.5</version> </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-api</artifactId> + <version>${slf4j.version}</version> + </dependency> </dependencies> </dependencyManagement> http://git-wip-us.apache.org/repos/asf/james-project/blob/38ba938d/mailet/standard/pom.xml ---------------------------------------------------------------------- diff --git a/mailet/standard/pom.xml b/mailet/standard/pom.xml index 5b9f776..815727d 100644 --- a/mailet/standard/pom.xml +++ b/mailet/standard/pom.xml @@ -87,6 +87,11 @@ <scope>test</scope> <version>1.9.5</version> </dependency> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-api</artifactId> + <scope>test</scope> + </dependency> </dependencies> <build> http://git-wip-us.apache.org/repos/asf/james-project/blob/38ba938d/mailet/standard/src/main/java/org/apache/james/transport/mailets/LogMessage.java ---------------------------------------------------------------------- diff --git a/mailet/standard/src/main/java/org/apache/james/transport/mailets/LogMessage.java b/mailet/standard/src/main/java/org/apache/james/transport/mailets/LogMessage.java index c6f6914..ba3a35d 100644 --- a/mailet/standard/src/main/java/org/apache/james/transport/mailets/LogMessage.java +++ b/mailet/standard/src/main/java/org/apache/james/transport/mailets/LogMessage.java @@ -21,15 +21,14 @@ package org.apache.james.transport.mailets; +import java.io.InputStream; import java.util.Enumeration; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; -import org.apache.mailet.base.GenericMailet; import org.apache.mailet.Mail; - -import java.io.InputStream; +import org.apache.mailet.base.GenericMailet; /** * Logs Message Headers and/or Body. @@ -50,9 +49,6 @@ public class LogMessage extends GenericMailet { private int bodyMax = 0; private String comment = null; - /** - * Initialize the mailet, loading configuration information. - */ public void init() { try { passThrough = (getInitParameter("passThrough") == null) ? true : Boolean.valueOf(getInitParameter("passThrough")); @@ -65,11 +61,11 @@ public class LogMessage extends GenericMailet { } } - /** - * Log a particular message - * - * @param mail the mail to process - */ + @Override + public String getMailetInfo() { + return "LogHeaders Mailet"; + } + public void service(Mail mail) { log("Logging mail " + mail.getName()); if (comment != null) log(comment); @@ -113,13 +109,4 @@ public class LogMessage extends GenericMailet { } return headBuffer.toString(); } - - /** - * Return a string describing this mailet. - * - * @return a string describing this mailet - */ - public String getMailetInfo() { - return "LogHeaders Mailet"; - } } http://git-wip-us.apache.org/repos/asf/james-project/blob/38ba938d/mailet/standard/src/test/java/org/apache/james/transport/mailets/LogMessageTest.java ---------------------------------------------------------------------- diff --git a/mailet/standard/src/test/java/org/apache/james/transport/mailets/LogMessageTest.java b/mailet/standard/src/test/java/org/apache/james/transport/mailets/LogMessageTest.java new file mode 100644 index 0000000..df3cad8 --- /dev/null +++ b/mailet/standard/src/test/java/org/apache/james/transport/mailets/LogMessageTest.java @@ -0,0 +1,214 @@ +/**************************************************************** + * 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; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.when; + +import java.io.ByteArrayInputStream; +import java.util.Properties; + +import javax.mail.MessagingException; +import javax.mail.Session; +import javax.mail.internet.AddressException; +import javax.mail.internet.MimeMessage; + +import org.apache.mailet.Mail; +import org.apache.mailet.MailAddress; +import org.apache.mailet.base.test.FakeMail; +import org.apache.mailet.base.test.FakeMailContext; +import org.apache.mailet.base.test.FakeMailetConfig; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.slf4j.Logger; + +import com.google.common.base.Charsets; +import com.google.common.collect.Lists; + +public class LogMessageTest { + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + private LogMessage mailet; + private FakeMailetConfig mailetConfig; + private Logger logger; + + @Before + public void setup() { + logger = mock(Logger.class); + FakeMailContext mailContext = FakeMailContext.builder() + .logger(logger) + .build(); + mailetConfig = new FakeMailetConfig("LogContext", mailContext); + mailet = new LogMessage(); + } + + @Test + public void getMailetInfoShouldReturnValue() { + assertThat(mailet.getMailetInfo()).isEqualTo("LogHeaders Mailet"); + } + + @Test + public void initShouldIgnoreExceptions() throws Exception { + mailetConfig.setProperty("maxBody", "comment"); + mailet.init(mailetConfig); + } + + @Test + public void serviceShouldFailWhenMailHasNoStream() throws Exception { + mailet.init(mailetConfig); + expectedException.expect(NegativeArraySizeException.class); + + MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties())); + message.setSubject("subject"); + message.setText("This is a fake mail"); + mailet.service(new FakeMail(message)); + } + + @Test + public void serviceShouldLog() throws Exception { + mailet.init(mailetConfig); + + mailet.service(createMail()); + + verify(logger).info("Logging mail name"); + verify(logger).info("\n" + + "Subject: subject\n" + + "Content-Type: text/plain\n"); + verify(logger).info("This is a fake mail"); + verifyNoMoreInteractions(logger); + } + + @Test + public void serviceShouldLogWhenExceptionOccured() throws Exception { + mailet.init(mailetConfig); + + Mail mail = mock(Mail.class); + when(mail.getName()) + .thenReturn("name"); + MessagingException messagingException = new MessagingException("exception message"); + when(mail.getMessage()) + .thenThrow(messagingException); + + mailet.service(mail); + + verify(logger).info("Logging mail name"); + verify(logger).error("Error logging message."); + verify(logger).error("exception message"); + verifyNoMoreInteractions(logger); + } + + @Test + public void serviceShouldSetTheMailStateWhenPassThroughIsFalse() throws Exception { + mailetConfig.setProperty("passThrough", "false"); + mailet.init(mailetConfig); + + FakeMail mail = createMail(); + mailet.service(mail); + + assertThat(mail.getState()).isEqualTo(Mail.GHOST); + } + + @Test + public void serviceShouldNotChangeTheMailStateWhenPassThroughIsTrue() throws Exception { + mailetConfig.setProperty("passThrough", "true"); + mailet.init(mailetConfig); + + FakeMail mail = createMail(); + String initialState = mail.getState(); + mailet.service(mail); + + assertThat(mail.getState()).isEqualTo(initialState); + } + + @Test + public void serviceShouldNotLogHeadersWhenFalse() throws Exception { + mailetConfig.setProperty("headers", "false"); + mailet.init(mailetConfig); + + mailet.service(createMail()); + + verify(logger).info("Logging mail name"); + verify(logger).info("This is a fake mail"); + verifyNoMoreInteractions(logger); + } + + @Test + public void serviceShouldNotLogBodyWhenFalse() throws Exception { + mailetConfig.setProperty("body", "false"); + mailet.init(mailetConfig); + + mailet.service(createMail()); + + verify(logger).info("Logging mail name"); + verify(logger).info("\n" + + "Subject: subject\n" + + "Content-Type: text/plain\n"); + verifyNoMoreInteractions(logger); + } + + @Test + public void serviceShouldNotLogFullBodyWhenBodyMaxIsSet() throws Exception { + mailetConfig.setProperty("maxBody", "2"); + mailet.init(mailetConfig); + + mailet.service(createMail()); + + verify(logger).info("Logging mail name"); + verify(logger).info("\n" + + "Subject: subject\n" + + "Content-Type: text/plain\n"); + verify(logger).info("Th"); + verifyNoMoreInteractions(logger); + } + + @Test + public void serviceShouldLogAdditionalCommentWhenCommentIsSet() throws Exception { + mailetConfig.setProperty("comment", "comment"); + mailet.init(mailetConfig); + + mailet.service(createMail()); + + verify(logger).info("Logging mail name"); + verify(logger).info("comment"); + verify(logger).info("\n" + + "Subject: subject\n" + + "Content-Type: text/plain\n"); + verify(logger).info("This is a fake mail"); + verifyNoMoreInteractions(logger); + } + + private FakeMail createMail() throws MessagingException, AddressException { + MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties()), + new ByteArrayInputStream("Subject: subject\r\nContent-Type: text/plain\r\n\r\nThis is a fake mail".getBytes(Charsets.UTF_8))); + FakeMail mail = new FakeMail(message); + mail.setName("name"); + mail.setState(Mail.DEFAULT); + mail.setRecipients(Lists.newArrayList(new MailAddress("[email protected]"))); + mail.setSender(new MailAddress("[email protected]")); + return mail; + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
