FileDocCategorySizeDatePackage
SocketServerTestCase.javaAPI DocApache log4j 1.2.1514268Sat Aug 25 00:09:34 BST 2007org.apache.log4j.net

SocketServerTestCase

public class SocketServerTestCase extends TestCase
author
Ceki Gülcü

Fields Summary
static String
TEMP
static String
FILTERED
static String
PAT1
static String
PAT2
static String
PAT3
static String
PAT4
static String
PAT5
static String
PAT6
static String
PAT7
static String
PAT8
static String
EXCEPTION1
static String
EXCEPTION2
static String
EXCEPTION3
static String
EXCEPTION4
static String
EXCEPTION5
static Logger
logger
public static final int
PORT
static Logger
rootLogger
SocketAppender
socketAppender
Constructors Summary
public SocketServerTestCase(String name)


     
    super(name);
  
Methods Summary
static voidcommon(java.lang.String dc, java.lang.String key, java.lang.Object o)

    String oldThreadName = Thread.currentThread().getName();
    Thread.currentThread().setName("main");

    int i = -1; 
    NDC.push(dc); 
    MDC.put(key, o);
    Logger root = Logger.getRootLogger();

    logger.setLevel(Level.DEBUG);
    rootLogger.setLevel(Level.DEBUG);
    
    logger.log(XLevel.TRACE, "Message " + ++i);

    logger.setLevel(Level.TRACE);
    rootLogger.setLevel(Level.TRACE);
    
    logger.trace("Message " + ++i);
    root.trace("Message " + ++i);
    logger.debug("Message " + ++i);
    root.debug("Message " + ++i);
    logger.info("Message " + ++i);
    logger.warn("Message " + ++i);
    logger.log(XLevel.LETHAL, "Message " + ++i); //5
    
    Exception e = new Exception("Just testing");
    logger.debug("Message " + ++i, e);
    root.error("Message " + ++i, e);
    NDC.pop();
    MDC.remove(key);

    Thread.currentThread().setName(oldThreadName);
  
public voiddelay(int secs)

    try {Thread.currentThread().sleep(secs*1000);} catch(Exception e) {}
  
public voidsetUp()

    System.out.println("Setting up test case.");
  
public static junit.framework.Testsuite()

    TestSuite suite = new TestSuite();
    suite.addTest(new SocketServerTestCase("test1"));
    suite.addTest(new SocketServerTestCase("test2"));
    suite.addTest(new SocketServerTestCase("test3"));
    suite.addTest(new SocketServerTestCase("test4"));
    suite.addTest(new SocketServerTestCase("test5"));
    suite.addTest(new SocketServerTestCase("test6"));
    suite.addTest(new SocketServerTestCase("test7"));
    suite.addTest(new SocketServerTestCase("test8"));
    return suite;
  
public voidtearDown()

    System.out.println("Tearing down test case.");
    socketAppender = null;
    rootLogger.removeAllAppenders();
  
public voidtest1()
The pattern on the server side: %5p %x [%t] %c %m%n We are testing NDC functionality across the wire.

    socketAppender = new SocketAppender("localhost", PORT);
    rootLogger.addAppender(socketAppender);
    common("T1", "key1", "MDC-TEST1");
    delay(1);
    ControlFilter cf = new ControlFilter(new String[]{PAT1, EXCEPTION1, 
						       EXCEPTION2, EXCEPTION3, EXCEPTION4, EXCEPTION5});
    
    Transformer.transform(
      TEMP, FILTERED,
      new Filter[] { cf, new LineNumberFilter(), 
          new JunitTestRunnerFilter(),
          new SunReflectFilter() });

    assertTrue(Compare.compare(FILTERED, "witness/socketServer.1"));
  
public voidtest2()
The pattern on the server side: %5p %x [%t] %C (%F:%L) %m%n We are testing NDC across the wire. Localization is turned off by default so it is not tested here even if the conversion pattern uses localization.

    socketAppender = new SocketAppender("localhost", PORT);
    rootLogger.addAppender(socketAppender);

    common("T2", "key2", "MDC-TEST2");
    delay(1);
    ControlFilter cf = new ControlFilter(new String[]{PAT2, EXCEPTION1, 
						       EXCEPTION2, EXCEPTION3, EXCEPTION4, EXCEPTION5});
    
    Transformer.transform(
      TEMP, FILTERED,
      new Filter[] { cf, new LineNumberFilter(), 
          new JunitTestRunnerFilter(),
          new SunReflectFilter() });

    assertTrue(Compare.compare(FILTERED, "witness/socketServer.2"));
  
public voidtest3()
The pattern on the server side: %5p %x [%t] %C (%F:%L) %m%n meaning that we are testing NDC and locatization functionality across the wire.

    socketAppender = new SocketAppender("localhost", PORT);
    socketAppender.setLocationInfo(true);
    rootLogger.addAppender(socketAppender);

    common("T3", "key3", "MDC-TEST3");
    delay(1);
    ControlFilter cf = new ControlFilter(new String[]{PAT3, EXCEPTION1, 
						       EXCEPTION2, EXCEPTION3, EXCEPTION4, EXCEPTION5});
    
    Transformer.transform(
      TEMP, FILTERED,
      new Filter[] { cf, new LineNumberFilter(), 
          new JunitTestRunnerFilter(),
          new SunReflectFilter() });

    assertTrue(Compare.compare(FILTERED, "witness/socketServer.3"));
  
public voidtest4()
The pattern on the server side: %5p %x %X{key1}%X{key4} [%t] %c{1} - %m%n meaning that we are testing NDC, MDC and localization functionality across the wire.

    socketAppender = new SocketAppender("localhost", PORT);
    socketAppender.setLocationInfo(true);
    rootLogger.addAppender(socketAppender);

    NDC.push("some");
    common("T4", "key4", "MDC-TEST4");
    NDC.pop();
    delay(1);
    //
    //  These tests check MDC operation which
    //    requires JDK 1.2 or later
    if(!System.getProperty("java.version").startsWith("1.1.")) {
    
        ControlFilter cf = new ControlFilter(new String[]{PAT4, EXCEPTION1, 
						           EXCEPTION2, EXCEPTION3, EXCEPTION4, EXCEPTION5});
        Transformer.transform(
          TEMP, FILTERED,
          new Filter[] { cf, new LineNumberFilter(), 
              new JunitTestRunnerFilter(),
              new SunReflectFilter() });

        assertTrue(Compare.compare(FILTERED, "witness/socketServer.4"));
    }
  
public voidtest5()
The pattern on the server side: %5p %x %X{key1}%X{key5} [%t] %c{1} - %m%n The test case uses wraps an AsyncAppender around the SocketAppender. This tests was written specifically for bug report #9155. Prior to the bug fix the output on the server did not contain the MDC-TEST5 string because the MDC clone operation (in getMDCCopy method) operation is performed twice, once from the main thread which is correct, and a second time from the AsyncAppender's dispatch thread which is incrorrect.

    socketAppender = new SocketAppender("localhost", PORT);
    socketAppender.setLocationInfo(true);
    AsyncAppender asyncAppender = new AsyncAppender();
    asyncAppender.setLocationInfo(true);
    asyncAppender.addAppender(socketAppender);
    rootLogger.addAppender(asyncAppender);

    NDC.push("some5");
    common("T5", "key5", "MDC-TEST5");
    NDC.pop();
    delay(2);
    //
    //  These tests check MDC operation which
    //    requires JDK 1.2 or later
    if(!System.getProperty("java.version").startsWith("1.1.")) {
        ControlFilter cf = new ControlFilter(new String[]{PAT5, EXCEPTION1, 
						           EXCEPTION2, EXCEPTION3, EXCEPTION4, EXCEPTION5});
    
        Transformer.transform(
          TEMP, FILTERED,
          new Filter[] { cf, new LineNumberFilter(), 
              new JunitTestRunnerFilter(),
              new SunReflectFilter() });

        assertTrue(Compare.compare(FILTERED, "witness/socketServer.5"));
    }
  
public voidtest6()
The pattern on the server side: %5p %x %X{hostID}${key6} [%t] %c{1} - %m%n This test checks whether client-side MDC overrides the server side. It uses an AsyncAppender encapsulating a SocketAppender

    socketAppender = new SocketAppender("localhost", PORT);
    socketAppender.setLocationInfo(true);
    AsyncAppender asyncAppender = new AsyncAppender();
    asyncAppender.setLocationInfo(true);
    asyncAppender.addAppender(socketAppender);
    rootLogger.addAppender(asyncAppender);

    NDC.push("some6");
    MDC.put("hostID", "client-test6");
    common("T6", "key6", "MDC-TEST6");
    NDC.pop();
    MDC.remove("hostID");
    delay(2);
    //
    //  These tests check MDC operation which
    //    requires JDK 1.2 or later
    if(!System.getProperty("java.version").startsWith("1.1.")) {
        ControlFilter cf = new ControlFilter(new String[]{PAT6, EXCEPTION1, 
						           EXCEPTION2, EXCEPTION3, EXCEPTION4, EXCEPTION5});
    
        Transformer.transform(
          TEMP, FILTERED,
          new Filter[] { cf, new LineNumberFilter(), 
              new JunitTestRunnerFilter(),
              new SunReflectFilter() });

        assertTrue(Compare.compare(FILTERED, "witness/socketServer.6"));
    }
  
public voidtest7()
The pattern on the server side: %5p %x %X{hostID}${key7} [%t] %c{1} - %m%n This test checks whether client-side MDC overrides the server side.

    socketAppender = new SocketAppender("localhost", PORT);
    socketAppender.setLocationInfo(true);
    rootLogger.addAppender(socketAppender);

    NDC.push("some7");
    MDC.put("hostID", "client-test7");
    common("T7", "key7", "MDC-TEST7");
    NDC.pop();
    MDC.remove("hostID"); 
    delay(2);
    //
    //  These tests check MDC operation which
    //    requires JDK 1.2 or later
    if(!System.getProperty("java.version").startsWith("1.1.")) {
        ControlFilter cf = new ControlFilter(new String[]{PAT7, EXCEPTION1, 
						           EXCEPTION2, EXCEPTION3, EXCEPTION4, EXCEPTION5});
    
        Transformer.transform(
          TEMP, FILTERED,
          new Filter[] { cf, new LineNumberFilter(), 
              new JunitTestRunnerFilter(),
              new SunReflectFilter() });
        assertTrue(Compare.compare(FILTERED, "witness/socketServer.7"));
    }
  
public voidtest8()
The pattern on the server side: %5p %x %X{hostID}${key7} [%t] %c{1} - %m%n This test checks whether server side MDC works.

    socketAppender = new SocketAppender("localhost", PORT);
    socketAppender.setLocationInfo(true);
    rootLogger.addAppender(socketAppender);

    NDC.push("some8");
    common("T8", "key8", "MDC-TEST8");
    NDC.pop();
    delay(2);
    //
    //  These tests check MDC operation which
    //    requires JDK 1.2 or later
    if(!System.getProperty("java.version").startsWith("1.1.")) {
        ControlFilter cf = new ControlFilter(new String[]{PAT8, EXCEPTION1, 
						           EXCEPTION2, EXCEPTION3, EXCEPTION4, EXCEPTION5});
    
        Transformer.transform(
          TEMP, FILTERED,
          new Filter[] { cf, new LineNumberFilter(), 
              new JunitTestRunnerFilter(),
              new SunReflectFilter() });
        assertTrue(Compare.compare(FILTERED, "witness/socketServer.8"));
    }