Repository: logging-log4j2 Updated Branches: refs/heads/master 41d126107 -> 2ea183ee7
LOG4J2-2007 unit test Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/2ea183ee Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/2ea183ee Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/2ea183ee Branch: refs/heads/master Commit: 2ea183ee7ab530005e5e395deeac003a432b5cc7 Parents: 41d1261 Author: Mikael Ståldal <mik...@staldal.nu> Authored: Thu Nov 30 21:53:38 2017 +0100 Committer: Mikael Ståldal <mik...@staldal.nu> Committed: Sun Dec 3 12:48:55 2017 +0100 ---------------------------------------------------------------------- .../core/appender/SmtpAppenderAsyncTest.java | 98 ++++++++++++++++++++ .../test/resources/SmtpAppenderAsyncTest.xml | 42 +++++++++ 2 files changed, 140 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/2ea183ee/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/SmtpAppenderAsyncTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/SmtpAppenderAsyncTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/SmtpAppenderAsyncTest.java new file mode 100644 index 0000000..91503d3 --- /dev/null +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/SmtpAppenderAsyncTest.java @@ -0,0 +1,98 @@ +/* + * 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.logging.log4j.core.appender; + +import org.apache.logging.dumbster.smtp.SimpleSmtpServer; +import org.apache.logging.dumbster.smtp.SmtpMessage; +import org.apache.logging.log4j.ThreadContext; +import org.apache.logging.log4j.core.Logger; +import org.apache.logging.log4j.junit.LoggerContextRule; +import org.apache.logging.log4j.test.AvailablePortFinder; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; + +import java.util.Iterator; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +public class SmtpAppenderAsyncTest { + + private static int PORT; + + private SimpleSmtpServer smtpServer; + + @BeforeClass + public static void setupClass() { + PORT = AvailablePortFinder.getNextAvailable(); + System.setProperty("smtp.port", String.valueOf(PORT)); + } + + @Before + public void setup() { + smtpServer = SimpleSmtpServer.start(PORT); + } + + @Rule + public LoggerContextRule ctx = new LoggerContextRule("SmtpAppenderAsyncTest.xml"); + + @Test + public void testSync() { + testSmtpAppender(ctx.getLogger("sync")); + } + + @Test + public void testAsync() { + testSmtpAppender(ctx.getLogger("async")); + } + + private void testSmtpAppender(Logger logger) { + ThreadContext.put("MDC1", "mdc1"); + logger.error("the message"); + ctx.getLoggerContext().stop(); + smtpServer.stop(); + + assertEquals(1, smtpServer.getReceivedEmailSize()); + final Iterator<SmtpMessage> messages = smtpServer.getReceivedEmail(); + final SmtpMessage email = messages.next(); + + assertEquals("t...@example.com", email.getHeaderValue("To")); + assertEquals("f...@example.com", email.getHeaderValue("From")); + assertEquals("[mdc1]", email.getHeaderValue("Subject")); + + String body = email.getBody(); + if (!body.contains("Body:[mdc1]")) { + fail(body); + } + } + + @After + public void teardown() { + if (smtpServer != null) { + smtpServer.stop(); + } + } + + @AfterClass + public static void teardownClass() { + System.clearProperty("smtp.port"); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/2ea183ee/log4j-core/src/test/resources/SmtpAppenderAsyncTest.xml ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/resources/SmtpAppenderAsyncTest.xml b/log4j-core/src/test/resources/SmtpAppenderAsyncTest.xml new file mode 100644 index 0000000..1939ddd --- /dev/null +++ b/log4j-core/src/test/resources/SmtpAppenderAsyncTest.xml @@ -0,0 +1,42 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ~ 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. + --> +<Configuration name="SmtpAppenderAsyncTest" status="WARN"> + <Appenders> + <SMTP name="mail-sync" to="t...@example.com" from="f...@example.com" smtpHost="localhost" + smtpPort="${sys:smtp.port}" ignoreExceptions="false" subject="[%X{MDC1}]"> + <PatternLayout pattern="Body:[%X{MDC1}]" /> + </SMTP> + <Async name="mail-async"> + <AppenderRef ref="mail-sync"/> + </Async> + <Console name="Console" target="SYSTEM_OUT"> + <PatternLayout pattern="%d [%t] %-5level: %msg%n%throwable" /> + </Console> + </Appenders> + <Loggers> + <Root level="FATAL"> + <AppenderRef ref="Console"/> + </Root> + <Logger name="sync" level="INFO"> + <AppenderRef ref="mail-sync"/> + </Logger> + <Logger name="async" level="INFO"> + <AppenderRef ref="mail-async"/> + </Logger> + </Loggers> +</Configuration>