FileDocCategorySizeDatePackage
XMLLayoutTestCase.javaAPI DocApache log4j 1.2.157634Sat Aug 25 00:09:34 BST 2007org.apache.log4j.xml

XMLLayoutTestCase

public class XMLLayoutTestCase extends TestCase

Fields Summary
static String
TEMP
static String
FILTERED
Logger
root
Logger
logger
Constructors Summary
public XMLLayoutTestCase(String name)


     
    super(name);
  
Methods Summary
public voidbasic()

    XMLLayout xmlLayout = new XMLLayout();
    root.addAppender(new FileAppender(xmlLayout, TEMP, false));
    common();
    Transformer.transform(
      TEMP, FILTERED,
      new Filter[] {
        new LineNumberFilter(),
        new XMLTimestampFilter(),
        new JunitTestRunnerFilter(),
        new SunReflectFilter()
      });
    assertTrue(Compare.compare(FILTERED, "witness/xmlLayout.1"));
  
voidcommon()

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

    int i = -1;
 
    new X();

    logger.trace("Message " + ++i);
    root.trace("Message " + i);    

    logger.debug("Message " + ++i);
    root.debug("Message " + i);        

    logger.info("Message " + ++i);
    root.info("Message " + i);        

    logger.warn ("Message " + ++i);
    root.warn("Message " + i);        
 
    logger.error("Message " + ++i);
    root.error("Message " + i);
    
    logger.log(Level.FATAL, "Message " + ++i);
    root.log(Level.FATAL, "Message " + i);    
    
    Exception e = new Exception("Just testing");
    logger.debug("Message " + ++i, e);
    root.debug("Message " + i, e);
    
    logger.error("Message " + ++i, e);
    root.error("Message " + i, e);    


    Thread.currentThread().setName(oldThreadName);
  
public voidlocationInfo()

    XMLLayout xmlLayout = new XMLLayout();
    xmlLayout.setLocationInfo(true);
    root.addAppender(new FileAppender(xmlLayout, TEMP, false));
    common();
    Transformer.transform(
      TEMP, FILTERED,
      new Filter[] {
        new LineNumberFilter(),
        new XMLTimestampFilter(), 
        new XMLLineAttributeFilter(),
        new JunitTestRunnerFilter(),
        new SunReflectFilter()
      });
    assertTrue(Compare.compare(FILTERED, "witness/xmlLayout.2"));
  
public voidsetUp()

    root = Logger.getRootLogger();
    root.setLevel(Level.TRACE);
    logger = Logger.getLogger(XMLLayoutTestCase.class);
    logger.setLevel(Level.TRACE);
  
public static junit.framework.Testsuite()

    TestSuite suite = new TestSuite();
    suite.addTest(new XMLLayoutTestCase("basic"));
    suite.addTest(new XMLLayoutTestCase("locationInfo"));
    suite.addTest(new XMLLayoutTestCase("testCDATA"));
    suite.addTest(new XMLLayoutTestCase("testNull"));
    suite.addTest(new XMLLayoutTestCase("testMDC"));
    suite.addTest(new XMLLayoutTestCase("testMDCEscaped"));
    return suite;
  
public voidtearDown()

  
    root.getLoggerRepository().resetConfiguration();
  
public voidtestCDATA()

    XMLLayout xmlLayout = new XMLLayout();
    xmlLayout.setLocationInfo(true);
    root.addAppender(new FileAppender(xmlLayout, TEMP, false));

    String oldThreadName = Thread.currentThread().getName();
    Thread.currentThread().setName("main");
    
    logger.trace("Message with embedded <![CDATA[<hello>hi</hello>]]>.");
    logger.debug("Message with embedded <![CDATA[<hello>hi</hello>]]>.");

    Thread.currentThread().setName(oldThreadName);

    Transformer.transform(
      TEMP, FILTERED,
      new Filter[] {
        new LineNumberFilter(), 
        new XMLTimestampFilter(),
        new XMLLineAttributeFilter(), 
        new SunReflectFilter(),
        new JunitTestRunnerFilter()

      });
    Transformer.transform(TEMP, FILTERED, new Filter[] {new LineNumberFilter(),
    						  new XMLTimestampFilter(),
    						  new XMLLineAttributeFilter()});
    assertTrue(Compare.compare(FILTERED, "witness/xmlLayout.3"));
  
public voidtestMDC()
Tests the format of the MDC portion of the layout to ensure the key-value pairs we put in turn up in the output file.

throws
Exception

      XMLLayout xmlLayout = new XMLLayout();
      xmlLayout.setProperties(true);
      root.addAppender(new FileAppender(xmlLayout, TEMP, false));

      Hashtable context = MDC.getContext();
      if (context != null) {
          context.clear();
      }
      MDC.put("key1", "val1");
      MDC.put("key2", "val2");

      logger.debug("Hello");
      Transformer.transform(
        TEMP, FILTERED,
        new Filter[] { new LineNumberFilter(),
            new JunitTestRunnerFilter(),
            new XMLTimestampFilter()});
      assertTrue(Compare.compare(FILTERED, "witness/xmlLayout.mdc.1"));
    
public voidtestMDCEscaped()

      XMLLayout xmlLayout = new XMLLayout();
      xmlLayout.setProperties(true);
      root.addAppender(new FileAppender(xmlLayout, TEMP, false));

      Hashtable context = MDC.getContext();
      if (context != null) {
          context.clear();
      }
      MDC.put("blahAttribute", "<blah value='blah'>");
      MDC.put("<blahKey value='blah'/>", "blahValue");

      logger.debug("Hello");
      Transformer.transform(
        TEMP, FILTERED,
        new Filter[] { new LineNumberFilter(),
            new JunitTestRunnerFilter(),
            new XMLTimestampFilter() });
      assertTrue(Compare.compare(FILTERED, "witness/xmlLayout.mdc.2"));
    
public voidtestNull()

    XMLLayout xmlLayout = new XMLLayout();
    root.addAppender(new FileAppender(xmlLayout, TEMP, false));

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

    logger.debug("hi");
    logger.debug(null);
    Exception e = new Exception((String) null);
    logger.debug("hi", e);

    Thread.currentThread().setName(oldThreadName);

    Transformer.transform(
      TEMP, FILTERED,
      new Filter[] { new LineNumberFilter(),
          new XMLTimestampFilter(),  
          new JunitTestRunnerFilter(),
          new SunReflectFilter()});
    assertTrue(Compare.compare(FILTERED, "witness/xmlLayout.null"));