[LOGBack-dev] svn commit: r584 - logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net
noreply.seb at qos.ch
noreply.seb at qos.ch
Thu Sep 14 12:07:30 CEST 2006
Author: seb
Date: Thu Sep 14 12:07:30 2006
New Revision: 584
Modified:
logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/SocketAppenderTest.java
Log:
- Back to a version with MockServer
Modified: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/SocketAppenderTest.java
==============================================================================
--- logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/SocketAppenderTest.java (original)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/SocketAppenderTest.java Thu Sep 14 12:07:30 2006
@@ -1,10 +1,5 @@
package ch.qos.logback.classic.net;
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
import java.util.Map;
import junit.framework.TestCase;
@@ -18,25 +13,8 @@
public class SocketAppenderTest extends TestCase {
- ByteArrayOutputStream bos;
- ObjectInputStream inputStream;
- SocketAppender appender;
- Logger logger;
-
- LoggerContext lc;
-
- public void setUp() throws Exception {
- lc = new LoggerContext();
- lc.setName("test");
- lc.setProperty("testKey", "testValue");
- appender = new SocketAppender();
- appender.setPort(123);
- appender.setContext(lc);
- appender.setRemoteHost("localhost");
- appender.start();
- logger = lc.getLogger(LoggerContext.ROOT_NAME);
- logger.addAppender(appender);
- }
+ private LoggerContext lc;
+ private MockSocketServer mockSocketServer;
public void testStartFailNoRemoteHost() {
lc = new LoggerContext();
@@ -47,40 +25,38 @@
assertEquals(1, lc.getStatusManager().getCount());
}
- public void testRecieveMessage() throws InterruptedException, IOException,
- ClassNotFoundException {
-
- //create the byte output stream
- bos = new ByteArrayOutputStream() ;
- appender.oos = new ObjectOutputStream(bos);
-
- LoggingEvent event = createLoggingEvent();
- appender.append(event);
-
- //create the input stream based on the ouput stream
- ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
- inputStream = new ObjectInputStream(bis);
-
- LoggingEvent remoteEvent = (LoggingEvent) inputStream.readObject();
+ public void testRecieveMessage() throws InterruptedException {
+ startServer(1);
+ configureClient();
+
+ Logger logger = lc.getLogger(LoggerContext.ROOT_NAME);
+ logger.debug("test msg");
+
+ // Wait max 2 seconds for mock server to finish. However, it should
+ // finish much sooner than that.
+ mockSocketServer.join(2000);
+ assertTrue(mockSocketServer.finished);
+ assertEquals(1, mockSocketServer.loggingEventList.size());
- assertEquals("test message", remoteEvent.getMessage());
+ LoggingEvent remoteEvent = mockSocketServer.loggingEventList.get(0);
+ assertEquals("test msg", remoteEvent.getMessage());
assertEquals(Level.DEBUG, remoteEvent.getLevel());
}
- public void testRecieveWithContext() throws InterruptedException, IOException, ClassNotFoundException {
+ public void testRecieveWithContext() throws InterruptedException {
+ startServer(1);
+ configureClient();
+
+ Logger logger = lc.getLogger(LoggerContext.ROOT_NAME);
+ logger.debug("test msg");
+
+ // Wait max 2 seconds for mock server to finish. However, it should
+ // finish much sooner than that.
+ mockSocketServer.join(2000);
+ assertTrue(mockSocketServer.finished);
+ assertEquals(1, mockSocketServer.loggingEventList.size());
- //create the byte output stream
- bos = new ByteArrayOutputStream() ;
- appender.oos = new ObjectOutputStream(bos);
-
- LoggingEvent event = createLoggingEvent();
- appender.append(event);
-
- //create the input stream based on the ouput stream
- ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
- inputStream = new ObjectInputStream(bis);
-
- LoggingEvent remoteEvent = (LoggingEvent) inputStream.readObject();
+ LoggingEvent remoteEvent = mockSocketServer.loggingEventList.get(0);
LoggerRemoteView loggerRemoteView = remoteEvent.getLoggerRemoteView();
assertNotNull(loggerRemoteView);
@@ -94,53 +70,69 @@
assertEquals("testValue", props.get("testKey"));
}
- public void testMessageWithMDC() throws InterruptedException, IOException, ClassNotFoundException {
- //create the byte output stream
- bos = new ByteArrayOutputStream() ;
- appender.oos = new ObjectOutputStream(bos);
+ public void testMessageWithMDC() throws InterruptedException {
+ startServer(1);
+ configureClient();
+
+ Logger logger = lc.getLogger(LoggerContext.ROOT_NAME);
MDC.put("key", "testValue");
- LoggingEvent event = createLoggingEvent();
- appender.append(event);
+ logger.debug("test msg");
+
+ // Wait max 2 seconds for mock server to finish. However, it should
+ // finish much sooner than that.
+ mockSocketServer.join(2000);
+ assertTrue(mockSocketServer.finished);
+ assertEquals(1, mockSocketServer.loggingEventList.size());
- //create the input stream based on the ouput stream
- ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
- inputStream = new ObjectInputStream(bis);
-
- LoggingEvent remoteEvent = (LoggingEvent) inputStream.readObject();
-
+ LoggingEvent remoteEvent = mockSocketServer.loggingEventList.get(0);
Map<String, String> MDCPropertyMap = remoteEvent.getMDCPropertyMap();
assertEquals("testValue", MDCPropertyMap.get("key"));
}
- public void testMessageWithUpdatedMDC() throws InterruptedException, IOException, ClassNotFoundException {
- //create the byte output stream
- bos = new ByteArrayOutputStream() ;
- appender.oos = new ObjectOutputStream(bos);
+ public void testMessageWithUpdatedMDC() throws InterruptedException {
+ startServer(2);
+ configureClient();
+
+ Logger logger = lc.getLogger(LoggerContext.ROOT_NAME);
MDC.put("key", "testValue");
- LoggingEvent event = createLoggingEvent();
- appender.append(event);
+ logger.debug("test msg");
MDC.put("key", "updatedTestValue");
- event = createLoggingEvent();
- appender.append(event);
+ logger.debug("test msg 2");
- //create the input stream based on the ouput stream
- ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
- inputStream = new ObjectInputStream(bis);
-
- @SuppressWarnings("unused")
- LoggingEvent remoteEvent1 = (LoggingEvent) inputStream.readObject();
- LoggingEvent remoteEvent2 = (LoggingEvent) inputStream.readObject();
-
- Map<String, String> MDCPropertyMap = remoteEvent2.getMDCPropertyMap();
+ // Wait max 2 seconds for mock server to finish. However, it should
+ // finish much sooner than that.
+ mockSocketServer.join(2000);
+ assertTrue(mockSocketServer.finished);
+ assertEquals(2, mockSocketServer.loggingEventList.size());
+
+ // We observe the second logging event. It should provide us with
+ // the updated MDC property.
+ LoggingEvent remoteEvent = mockSocketServer.loggingEventList.get(1);
+ Map<String, String> MDCPropertyMap = remoteEvent.getMDCPropertyMap();
assertEquals("updatedTestValue", MDCPropertyMap.get("key"));
}
- private LoggingEvent createLoggingEvent() {
- LoggingEvent le = new LoggingEvent(this.getClass().getName(), logger,
- Level.DEBUG, "test message", null, null);
- return le;
+ private void startServer(int expectedEventNumber) throws InterruptedException {
+ mockSocketServer = new MockSocketServer(expectedEventNumber);
+ mockSocketServer.start();
+ // give MockSocketServer head start
+ Thread.sleep(100);
+ }
+
+ private void configureClient() {
+ lc = new LoggerContext();
+ lc.setName("test");
+ lc.setProperty("testKey", "testValue");
+ Logger root = lc.getLogger(LoggerContext.ROOT_NAME);
+ SocketAppender socketAppender = new SocketAppender();
+ socketAppender.setContext(lc);
+ socketAppender.setName("socket");
+ socketAppender.setPort(4560);
+ socketAppender.setRemoteHost("localhost");
+ root.addAppender(socketAppender);
+ socketAppender.start();
}
}
More information about the logback-dev
mailing list