[LOGBack-dev] svn commit: r490 - logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net

noreply.seb at qos.ch noreply.seb at qos.ch
Tue Aug 29 16:46:49 CEST 2006


Author: seb
Date: Tue Aug 29 16:46:48 2006
New Revision: 490

Added:
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/SMTPAppenderTestApp.java
Modified:
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/SMTPAppenderTest.java
Log:
on going work: testing SMTPAppender

Modified: 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/SMTPAppenderTest.java	(original)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/SMTPAppenderTest.java	Tue Aug 29 16:46:48 2006
@@ -1,40 +1,89 @@
 package ch.qos.logback.classic.net;
 
-import org.slf4j.LoggerFactory;
+import javax.mail.Address;
+import javax.mail.MessagingException;
 
-import ch.qos.logback.classic.Logger;
+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;
-import ch.qos.logback.core.util.StatusPrinter;
 
-public class SMTPAppenderTest {
+public class SMTPAppenderTest extends TestCase {
 
-	public static void main(String[] args) {
+	SMTPAppender appender;
 
-		Logger logger = (Logger) LoggerFactory.getLogger(SocketAppenderTestApp.class);
-		LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
-		SMTPAppender appender = new SMTPAppender();
+	public void 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 at qos.ch");
-		
+		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");
+	}
+	
+	public void testStart() {
+		try {
+		Address[] addressArray = appender.msg.getFrom();
+		Address address = addressArray[0];
+		assertEquals("user at host.dom", address.toString());
 		
-		StatusPrinter.print(lc.getStatusManager());
+		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());
+	}
+	
+	//TODO test defaultEvaluator == test triggering event
+	public void tearDown() {
+		appender = null;
+	}
 	private static Layout buildLayout(LoggerContext lc) {
 		PatternLayout layout = new PatternLayout();
 		layout.setContext(lc);
@@ -44,5 +93,4 @@
 		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	Tue Aug 29 16:46:48 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