FileDocCategorySizeDatePackage
PatternParserTestCase.javaAPI DocApache log4j 1.2.154217Sat Aug 25 00:09:32 BST 2007org.apache.log4j.helpers

PatternParserTestCase

public class PatternParserTestCase extends TestCase
Test case for helpers/PatternParser.java. Tests the various conversion patterns supported by PatternParser. This test class tests PatternParser via the PatternLayout class which uses it.

Fields Summary
static String
OUTPUT_FILE
static String
WITNESS_FILE
static String
msgPattern
Logger
root
Logger
logger
Constructors Summary
public PatternParserTestCase(String name)


     
    super(name);
  
Methods Summary
public voidmdcPattern()
Test case for MDC conversion pattern.

    
    String mdcMsgPattern1 = "%m : %X%n";
    String mdcMsgPattern2 = "%m : %X{key1}%n";
    String mdcMsgPattern3 = "%m : %X{key2}%n";
    String mdcMsgPattern4 = "%m : %X{key3}%n";
    String mdcMsgPattern5 = "%m : %X{key1},%X{key2},%X{key3}%n";
    
    // set up appender
    PatternLayout layout = new PatternLayout(msgPattern);
    Appender appender = new FileAppender(layout, OUTPUT_FILE+"_mdc", false);
            
    // set appender on root and set level to debug
    root.addAppender(appender);
    root.setLevel(Level.DEBUG);
    
    // output starting message
    root.debug("starting mdc pattern test");
 
    layout.setConversionPattern(mdcMsgPattern1);
    root.debug("empty mdc, no key specified in pattern");
    
    layout.setConversionPattern(mdcMsgPattern2);
    root.debug("empty mdc, key1 in pattern");
    
    layout.setConversionPattern(mdcMsgPattern3);
    root.debug("empty mdc, key2 in pattern");
    
    layout.setConversionPattern(mdcMsgPattern4);
    root.debug("empty mdc, key3 in pattern");
    
    layout.setConversionPattern(mdcMsgPattern5);
    root.debug("empty mdc, key1, key2, and key3 in pattern");

    MDC.put("key1", "value1");
    MDC.put("key2", "value2");

    layout.setConversionPattern(mdcMsgPattern1);
    root.debug("filled mdc, no key specified in pattern");
    
    layout.setConversionPattern(mdcMsgPattern2);
    root.debug("filled mdc, key1 in pattern");
    
    layout.setConversionPattern(mdcMsgPattern3);
    root.debug("filled mdc, key2 in pattern");
    
    layout.setConversionPattern(mdcMsgPattern4);
    root.debug("filled mdc, key3 in pattern");
    
    layout.setConversionPattern(mdcMsgPattern5);
    root.debug("filled mdc, key1, key2, and key3 in pattern");

    MDC.remove("key1");
    MDC.remove("key2");

    layout.setConversionPattern(msgPattern);
    root.debug("finished mdc pattern test");

    assertTrue(Compare.compare(OUTPUT_FILE+"_mdc", WITNESS_FILE+"_mdc"));
  
public voidsetUp()

    root = Logger.getRootLogger();
    root.removeAllAppenders();
  
public static junit.framework.Testsuite()

    TestSuite suite = new TestSuite();
    //
    //   MDC requires JDK 1.2+
    //
    if (!System.getProperty("java.version").startsWith("1.1.")) {
       suite.addTest(new PatternParserTestCase("mdcPattern"));
    }
    return suite;
  
public voidtearDown()

  
    root.getLoggerRepository().resetConfiguration();