Search in sources :

Example 1 with SmtpMessage

use of org.apache.logging.dumbster.smtp.SmtpMessage in project logging-log4j2 by apache.

the class SmtpAppenderTest method testDelivery.

@Test
public void testDelivery() {
    final String subjectKey = getClass().getName();
    final String subjectValue = "SubjectValue1";
    ThreadContext.put(subjectKey, subjectValue);
    final SmtpAppender appender = SmtpAppender.createAppender(null, "Test", "to@example.com", "cc@example.com", "bcc@example.com", "from@example.com", "replyTo@example.com", "Subject Pattern %X{" + subjectKey + "}", null, HOST, PORT, null, null, "false", "3", null, null, "true");
    appender.start();
    final LoggerContext context = LoggerContext.getContext();
    final Logger root = context.getLogger("SMTPAppenderTest");
    root.addAppender(appender);
    root.setAdditive(false);
    root.setLevel(Level.DEBUG);
    final SimpleSmtpServer server = SimpleSmtpServer.start(PORTNUM);
    root.debug("Debug message #1");
    root.debug("Debug message #2");
    root.debug("Debug message #3");
    root.debug("Debug message #4");
    root.error("Error with exception", new RuntimeException("Exception message"));
    root.error("Error message #2");
    server.stop();
    assertTrue(server.getReceivedEmailSize() == 2);
    final Iterator<SmtpMessage> messages = server.getReceivedEmail();
    final SmtpMessage email = messages.next();
    assertEquals("to@example.com", email.getHeaderValue("To"));
    assertEquals("cc@example.com", email.getHeaderValue("Cc"));
    // assertEquals("bcc@example.com", email.getHeaderValue("Bcc")); // BCC
    // can't be tested with Dumpster 1.6
    assertEquals("from@example.com", email.getHeaderValue("From"));
    assertEquals("replyTo@example.com", email.getHeaderValue("Reply-To"));
    final String headerValue = email.getHeaderValue("Subject");
    assertEquals(headerValue, "Subject Pattern " + subjectValue);
    final String body = email.getBody();
    assertFalse(body.contains("Debug message #1"));
    assertTrue(body.contains("Debug message #2"));
    assertTrue(body.contains("Debug message #3"));
    assertTrue(body.contains("Debug message #4"));
    assertTrue(body.contains("Error with exception"));
    assertTrue(body.contains("RuntimeException"));
    assertTrue(body.contains("Exception message"));
    assertFalse(body.contains("Error message #2"));
    final SmtpMessage email2 = messages.next();
    final String body2 = email2.getBody();
    assertFalse(body2.contains("Debug message #4"));
    assertFalse(body2.contains("Error with exception"));
    assertTrue(body2.contains("Error message #2"));
}
Also used : SmtpMessage(org.apache.logging.dumbster.smtp.SmtpMessage) SimpleSmtpServer(org.apache.logging.dumbster.smtp.SimpleSmtpServer) Logger(org.apache.logging.log4j.core.Logger) LoggerContext(org.apache.logging.log4j.core.LoggerContext) Test(org.junit.Test)

Aggregations

SimpleSmtpServer (org.apache.logging.dumbster.smtp.SimpleSmtpServer)1 SmtpMessage (org.apache.logging.dumbster.smtp.SmtpMessage)1 Logger (org.apache.logging.log4j.core.Logger)1 LoggerContext (org.apache.logging.log4j.core.LoggerContext)1 Test (org.junit.Test)1