[LOGBack-dev] svn commit: r504 - logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net
noreply.seb at qos.ch
noreply.seb at qos.ch
Thu Aug 31 15:25:27 CEST 2006
Author: seb
Date: Thu Aug 31 15:25:27 2006
New Revision: 504
Added:
logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/SMTPAppenderTest.java
logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/SMTPAppenderTestApp.java
Log:
tests are back
Added: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/SMTPAppenderTest.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/SMTPAppenderTest.java Thu Aug 31 15:25:27 2006
@@ -0,0 +1,113 @@
+package ch.qos.logback.classic.net;
+
+import javax.mail.Address;
+import javax.mail.MessagingException;
+
+import junit.framework.TestCase;
+import ch.qos.logback.classic.Level;
+import ch.qos.logback.classic.LoggerContext;
+import ch.qos.logback.classic.PatternLayout;
+import ch.qos.logback.classic.spi.LoggingEvent;
+import ch.qos.logback.core.Layout;
+
+public class SMTPAppenderTest extends TestCase {
+
+ SMTPAppender appender;
+
+ public void setUp() throws Exception {
+ super.setUp();
+ LoggerContext lc = new LoggerContext();
+ appender = new SMTPAppender();
+ appender.setContext(lc);
+ appender.setName("smtp");
+ appender.setFrom("user at host.dom");
+ appender.setLayout(buildLayout(lc));
+ appender.setSMTPHost("mail.qos.ch");
+ appender.setSubject("logging report");
+ appender.setTo("sebastien.nospam at qos.ch");
+ appender.start();
+ }
+
+ public void tearDown() throws Exception {
+ super.tearDown();
+ appender = null;
+ }
+
+ public void testStart() {
+ try {
+ Address[] addressArray = appender.msg.getFrom();
+ Address address = addressArray[0];
+ assertEquals("user at host.dom", address.toString());
+
+ addressArray = null;
+ address = null;
+
+ addressArray = appender.msg.getAllRecipients();
+ address = addressArray[0];
+ assertEquals("sebastien.nospam at qos.ch", address.toString());
+
+ assertEquals("logging report", appender.msg.getSubject());
+
+ assertTrue(appender.isStarted());
+
+ } catch (MessagingException ex) {
+ fail("Unexpected exception.");
+ }
+ }
+
+ public void testAppendNonTriggeringEvent() {
+ LoggingEvent event = new LoggingEvent();
+ event.setThreadName("thead name");
+ event.setLevel(Level.DEBUG);
+ appender.append(event);
+ assertEquals(1, appender.cb.length());
+ }
+
+ public void testEntryConditionsCheck() {
+ appender.checkEntryConditions();
+ assertEquals(0, appender.getContext().getStatusManager().getCount());
+ }
+
+ public void testEntryConditionsCheckNoMessage() {
+ appender.msg = null;
+ appender.checkEntryConditions();
+ assertEquals(1, appender.getContext().getStatusManager().getCount());
+ }
+
+ public void testEntryConditionsCheckNoEvaluator() {
+ appender.evaluator = null;
+ appender.checkEntryConditions();
+ assertEquals(1, appender.getContext().getStatusManager().getCount());
+ }
+
+ public void testEntryConditionsCheckNoLayout() {
+ appender.setLayout(null);
+ appender.checkEntryConditions();
+ assertEquals(1, appender.getContext().getStatusManager().getCount());
+ }
+
+
+ public void testDefaultEvaluatorNoTrigger() {
+ DefaultEvaluator evaluator = new DefaultEvaluator();
+ LoggingEvent le = new LoggingEvent();
+ le.setLevel(Level.DEBUG);
+ assertFalse(evaluator.isTriggeringEvent(null,le));
+ }
+
+ public void testDefaultEvaluatorTrigger() {
+ DefaultEvaluator evaluator = new DefaultEvaluator();
+ LoggingEvent le = new LoggingEvent();
+ le.setLevel(Level.ERROR);
+ assertTrue(evaluator.isTriggeringEvent(null,le));
+ }
+
+ private static Layout buildLayout(LoggerContext lc) {
+ PatternLayout layout = new PatternLayout();
+ layout.setContext(lc);
+ layout.setHeader("Some header\n");
+ layout.setPattern("%-4relative [%thread] %-5level %class - %msg%n");
+ layout.setFooter("Some footer");
+ layout.start();
+ return layout;
+ }
+}
Added: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/SMTPAppenderTestApp.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/SMTPAppenderTestApp.java Thu Aug 31 15:25:27 2006
@@ -0,0 +1,48 @@
+package ch.qos.logback.classic.net;
+
+import org.slf4j.LoggerFactory;
+
+import ch.qos.logback.classic.Logger;
+import ch.qos.logback.classic.LoggerContext;
+import ch.qos.logback.classic.PatternLayout;
+import ch.qos.logback.core.Layout;
+import ch.qos.logback.core.util.StatusPrinter;
+
+public class SMTPAppenderTestApp {
+
+ public static void main(String[] args) {
+
+ Logger logger = (Logger) LoggerFactory.getLogger(SocketAppenderTestApp.class);
+ LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
+ SMTPAppender appender = new SMTPAppender();
+ appender.setContext(lc);
+ appender.setName("smtp");
+ appender.setFrom("user at host.dom");
+ appender.setLayout(buildLayout(lc));
+ appender.setSMTPHost("mail.qos.ch");
+ appender.setSubject("logging report");
+ appender.setTo("sebastien.nospam at qos.ch");
+
+ appender.start();
+
+ logger.addAppender(appender);
+
+ for (int i = 0; i <= 10; i++) {
+ logger.debug("** Hello world. n=" + i);
+ }
+ logger.error("Triggering request");
+
+ StatusPrinter.print(lc.getStatusManager());
+ }
+
+ private static Layout buildLayout(LoggerContext lc) {
+ PatternLayout layout = new PatternLayout();
+ layout.setContext(lc);
+ layout.setHeader("Some header\n");
+ layout.setPattern("%-4relative [%thread] %-5level %class - %msg%n");
+ layout.setFooter("Some footer");
+ layout.start();
+ return layout;
+ }
+
+}
More information about the logback-dev
mailing list