[logback-dev] svn commit: r1630 - in logback/trunk/logback-classic: src/main/java/ch/qos/logback/classic/net src/test/java/ch/qos/logback/classic/net src/test/java/ch/qos/logback/classic/net/mock stylesheets

noreply.ceki at qos.ch noreply.ceki at qos.ch
Tue Mar 4 21:01:53 CET 2008


Author: ceki
Date: Tue Mar  4 21:01:52 2008
New Revision: 1630

Removed:
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/mock/MockSocketServer.java
   logback/trunk/logback-classic/stylesheets/
Modified:
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/net/SimpleSocketServer.java
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/SocketAppenderTest.java

Log:
- MockSocketServer is no longer needed for tests as SimpleSocketServer does the job
- removing unused stylesheets directory

Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/net/SimpleSocketServer.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/net/SimpleSocketServer.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/net/SimpleSocketServer.java	Tue Mar  4 21:01:52 2008
@@ -9,6 +9,7 @@
  */
 package ch.qos.logback.classic.net;
 
+import java.io.IOException;
 import java.net.ServerSocket;
 import java.net.Socket;
 
@@ -23,17 +24,13 @@
  * A simple {@link SocketNode} based server.
  * 
  * <pre>
- *     &lt;b&gt;Usage:&lt;/b&gt; java ch.qos.logback.classic.net.SimpleSocketServer port configFile
- *    
- *     where
- * <em>
- * port
- * </em>
- *     is a part number where the server listens and
- * <em>
- * configFile
- * </em>
- *     is an xml configuration file fed to {@link JoranConfigurator}.
+ *      &lt;b&gt;Usage:&lt;/b&gt; java ch.qos.logback.classic.net.SimpleSocketServer port configFile
+ * </pre>
+ * 
+ * where <em>port</em> is a port number where the server listens and
+ * <em>configFile</em> is an xml configuration file fed to
+ * {@link JoranConfigurator}.
+ * 
  * </pre>
  * 
  * @author Ceki G&uuml;lc&uuml;
@@ -41,36 +38,69 @@
  * 
  * @since 0.8.4
  */
-public class SimpleSocketServer {
+public class SimpleSocketServer extends Thread {
 
-  static Logger logger = LoggerFactory.getLogger(SimpleSocketServer.class);
+  Logger logger = LoggerFactory.getLogger(SimpleSocketServer.class);
 
-  static int port;
+  private final int port;
+  private final LoggerContext lc;
+  private boolean closed = false;
+  private ServerSocket serverSocket;
 
   public static void main(String argv[]) throws Exception {
+    int port = -1;
     if (argv.length == 2) {
-      init(argv[0], argv[1]);
+      port = parsePortNumber(argv[0]);
     } else {
       usage("Wrong number of arguments.");
     }
 
-    runServer();
+    String configFile = argv[1];
+    LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
+    configureLC(lc, configFile);
+
+    SimpleSocketServer sss = new SimpleSocketServer(lc, port);
+    sss.start();
   }
 
-  static void runServer() {
+  public SimpleSocketServer(LoggerContext lc, int port) {
+    this.lc = lc;
+    this.port = port;
+  }
+
+  public void run() {
     try {
       logger.info("Listening on port " + port);
-      ServerSocket serverSocket = new ServerSocket(port);
-      while (true) {
+      serverSocket = new ServerSocket(port);
+      while (!closed) {
         logger.info("Waiting to accept a new client.");
         Socket socket = serverSocket.accept();
         logger.info("Connected to client at " + socket.getInetAddress());
         logger.info("Starting new socket node.");
-        LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
         new Thread(new SocketNode(socket, lc)).start();
       }
     } catch (Exception e) {
-      e.printStackTrace();
+      if(closed) {
+        logger.info("Exception in run method for a closed server. This is expected");
+      } else {
+        logger.error("Failure in run method", e);
+      }
+    }
+  }
+
+  
+  public boolean isClosed() {
+    return closed;
+  }
+
+  public void close() {
+    closed = true;
+    if (serverSocket != null) {
+      try {
+        serverSocket.close();
+      } catch (IOException e) {
+        logger.error("Failed to close serverSocket", e);
+      }
     }
   }
 
@@ -81,21 +111,22 @@
     System.exit(1);
   }
 
-  static void init(String portStr, String configFile) throws JoranException {
+  static int parsePortNumber(String portStr) {
     try {
-      port = Integer.parseInt(portStr);
+      return Integer.parseInt(portStr);
     } catch (java.lang.NumberFormatException e) {
       e.printStackTrace();
       usage("Could not interpret port number [" + portStr + "].");
+      // we won't get here
+      return -1;
     }
+  }
 
-    if (configFile.endsWith(".xml")) {
-      LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
-      JoranConfigurator configurator = new JoranConfigurator();
-      lc.shutdownAndReset();
-      configurator.setContext(lc);
-      configurator.doConfigure(configFile);
-      //StatusPrinter.print(lc);
-    }
+  static public void configureLC(LoggerContext lc, String configFile)
+      throws JoranException {
+    JoranConfigurator configurator = new JoranConfigurator();
+    lc.shutdownAndReset();
+    configurator.setContext(lc);
+    configurator.doConfigure(configFile);
   }
 }

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	Tue Mar  4 21:01:52 2008
@@ -20,18 +20,23 @@
 import ch.qos.logback.classic.Level;
 import ch.qos.logback.classic.Logger;
 import ch.qos.logback.classic.LoggerContext;
-import ch.qos.logback.classic.net.mock.MockSocketServer;
 import ch.qos.logback.classic.spi.LoggerContextRemoteView;
 import ch.qos.logback.classic.spi.LoggerRemoteView;
 import ch.qos.logback.classic.spi.LoggingEvent;
+import ch.qos.logback.core.read.ListAppender;
 
 public class SocketAppenderTest extends TestCase {
 
-  private LoggerContext lc;
-  private MockSocketServer mockSocketServer;
-
+  static final String LIST_APPENDER_NAME = "la";
+  int port = 4560;
+  LoggerContext lc = new LoggerContext();
+  LoggerContext serverLC = new LoggerContext();
+  ListAppender<LoggingEvent> la = new ListAppender<LoggingEvent>();
+  
+  //private MockSocketServer mockSocketServer;
+  private SimpleSocketServer simpleSocketServer;
+  
   public void testStartFailNoRemoteHost() {
-    lc = new LoggerContext();
     SocketAppender appender = new SocketAppender();
     appender.setContext(lc);
     appender.setPort(123);
@@ -45,14 +50,17 @@
 
     Logger logger = lc.getLogger(LoggerContext.ROOT_NAME);
     logger.debug("test msg");
-
+    
+    Thread.sleep(100);
+    
     // Wait max 2 seconds for mock server to finish. However, it should
     // finish much sooner than that.
-    mockSocketServer.join(2000);
-    assertTrue(mockSocketServer.isFinished());
-    assertEquals(1, mockSocketServer.getEventsList().size());
+    simpleSocketServer.close();
+    simpleSocketServer.join(2000);
+    assertTrue(simpleSocketServer.isClosed());
+    assertEquals(1, la.list.size());
 
-    LoggingEvent remoteEvent = mockSocketServer.getEventsList().get(0);
+    LoggingEvent remoteEvent = la.list.get(0);
     assertEquals("test msg", remoteEvent.getMessage());
     assertEquals(Level.DEBUG, remoteEvent.getLevel());
   }
@@ -66,11 +74,13 @@
 
     // Wait max 2 seconds for mock server to finish. However, it should
     // finish much sooner than that.
-    mockSocketServer.join(2000);
-    assertTrue(mockSocketServer.isFinished());
-    assertEquals(1, mockSocketServer.getEventsList().size());
+    Thread.sleep(100);
+    simpleSocketServer.close();
+    simpleSocketServer.join(2000);
+    assertTrue(simpleSocketServer.isClosed());
+    assertEquals(1, la.list.size());
 
-    LoggingEvent remoteEvent = mockSocketServer.getEventsList().get(0);
+    LoggingEvent remoteEvent = la.list.get(0);
 
     LoggerRemoteView loggerRemoteView = remoteEvent.getLoggerRemoteView();
     assertNotNull(loggerRemoteView);
@@ -95,11 +105,14 @@
 
     // Wait max 2 seconds for mock server to finish. However, it should
     // finish much sooner than that.
-    mockSocketServer.join(2000);
-    assertTrue(mockSocketServer.isFinished());
-    assertEquals(1, mockSocketServer.getEventsList().size());
+    Thread.sleep(100);
+    simpleSocketServer.close();
+    simpleSocketServer.join(2000);
+    assertTrue(simpleSocketServer.isClosed());
+    ListAppender<LoggingEvent> la = getListAppender();
+    assertEquals(1, la.list.size());
 
-    LoggingEvent remoteEvent = mockSocketServer.getEventsList().get(0);
+    LoggingEvent remoteEvent = la.list.get(0);
     Map<String, String> MDCPropertyMap = remoteEvent.getMDCPropertyMap();
     assertEquals("testValue", MDCPropertyMap.get("key"));
   }
@@ -113,13 +126,16 @@
     Marker marker = MarkerFactory.getMarker("testMarker");
     logger.debug(marker, "test msg");
 
+    Thread.sleep(100);
+    
     // Wait max 2 seconds for mock server to finish. However, it should
     // finish much sooner than that.
-    mockSocketServer.join(2000);
-    assertTrue(mockSocketServer.isFinished());
-    assertEquals(1, mockSocketServer.getEventsList().size());
+    simpleSocketServer.close();
+    simpleSocketServer.join(2000);
+    assertTrue(simpleSocketServer.isClosed());
+    assertEquals(1, la.list.size());
 
-    LoggingEvent remoteEvent = mockSocketServer.getEventsList().get(0);
+    LoggingEvent remoteEvent = la.list.get(0);
     assertEquals("testMarker", remoteEvent.getMarker().getName());
   }
 
@@ -135,26 +151,44 @@
     MDC.put("key", "updatedTestValue");
     logger.debug("test msg 2");
 
+    Thread.sleep(100);
+    
     // Wait max 2 seconds for mock server to finish. However, it should
     // finish much sooner than that.
-    mockSocketServer.join(2000);
-    assertTrue(mockSocketServer.isFinished());
-    assertEquals(2, mockSocketServer.getEventsList().size());
+    simpleSocketServer.close();
+    simpleSocketServer.join(2000);
+    assertTrue(simpleSocketServer.isClosed());
+    ListAppender<LoggingEvent> la = getListAppender();
+    
+    assertEquals(2, la.list.size());
 
     // We observe the second logging event. It should provide us with
     // the updated MDC property.
-    LoggingEvent remoteEvent = mockSocketServer.getEventsList().get(1);
+    LoggingEvent remoteEvent = la.list.get(1);
     Map<String, String> MDCPropertyMap = remoteEvent.getMDCPropertyMap();
     assertEquals("updatedTestValue", MDCPropertyMap.get("key"));
   }
 
   private void startServer(int expectedNumberOfEvents) throws InterruptedException {
-    mockSocketServer = new MockSocketServer(expectedNumberOfEvents);
-    mockSocketServer.start();
+    Logger root = serverLC.getLogger("root");
+    la.setName(LIST_APPENDER_NAME);
+    la.setContext(serverLC);
+    la.start();
+    root.addAppender(la);
+    simpleSocketServer = new SimpleSocketServer(serverLC, port);
+    simpleSocketServer.start();
+    //mockSocketServer = new MockSocketServer(expectedNumberOfEvents);
+    //mockSocketServer.start();
     // give MockSocketServer head start
     Thread.sleep(100);
   }
 
+  
+  ListAppender<LoggingEvent> getListAppender() {
+    Logger root = serverLC.getLogger("root");
+    return (ListAppender<LoggingEvent>) root.getAppender(LIST_APPENDER_NAME);
+  }
+  
   private void configureClient() {
     lc = new LoggerContext();
     lc.setName("test");
@@ -163,7 +197,7 @@
     SocketAppender socketAppender = new SocketAppender();
     socketAppender.setContext(lc);
     socketAppender.setName("socket");
-    socketAppender.setPort(4560);
+    socketAppender.setPort(port);
     socketAppender.setRemoteHost("localhost");
     root.addAppender(socketAppender);
     socketAppender.start();



More information about the logback-dev mailing list