logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mwom...@apache.org
Subject cvs commit: jakarta-log4j-sandbox/tests/witness filters.LevelMatchFilter.test1.txt filters.LevelMatchFilter.test2.txt filters.LevelMatchFilter.test3.txt filters.LevelMatchFilter.test4.txt
Date Tue, 04 Feb 2003 06:57:32 GMT
mwomack     2003/02/03 22:57:32

  Added:       tests/src/java/org/apache/log4j/filter
                        LevelMatchFilterTestCase.java
               tests/witness filters.LevelMatchFilter.test1.txt
                        filters.LevelMatchFilter.test2.txt
                        filters.LevelMatchFilter.test3.txt
                        filters.LevelMatchFilter.test4.txt
  Log:
  Moved over filter related test from core log4j cvs.
  
  Revision  Changes    Path
  1.1                  jakarta-log4j-sandbox/tests/src/java/org/apache/log4j/filter/LevelMatchFilterTestCase.java
  
  Index: LevelMatchFilterTestCase.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software
   * License version 1.1, a copy of which has been included with this
   * distribution in the LICENSE.txt file.  */
  
  package org.apache.log4j.filter;
  
  import java.io.File;
  
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  import junit.framework.Test;
  
  import org.apache.log4j.*;
  import org.apache.log4j.spi.Filter;
  import org.apache.log4j.xml.DOMConfigurator;
  import org.apache.log4j.filter.LevelMatchFilter;
  import org.apache.log4j.varia.DenyAllFilter;
  
  import org.apache.log4j.util.Compare;
  
  /**
     Test case for varia/LevelMatchFilter.java.  Test both the accept
     and deny cases.  Testing the accept requires the use of the
     DenyAllFilter to prevent non-matched messages from still getting
     logged to the appender.
   */
  public class LevelMatchFilterTestCase extends TestCase {
    
    static String TEST1_FILE    = "output/filters.LevelMatchFilter.test1.txt";
    static String TEST1_WITNESS = "witness/filters.LevelMatchFilter.test1.txt";
    static String TEST2_FILE    = "output/filters.LevelMatchFilter.test2.txt";
    static String TEST2_WITNESS = "witness/filters.LevelMatchFilter.test2.txt";
    static String TEST3_FILE    = "output/filters.LevelMatchFilter.test3.txt";
    static String TEST3_WITNESS = "witness/filters.LevelMatchFilter.test3.txt";
    static String TEST4_FILE    = "output/filters.LevelMatchFilter.test4.txt";
    static String TEST4_WITNESS = "witness/filters.LevelMatchFilter.test4.txt";
    static String TEST4_CONFIG  = "input/xml//filters.LevelMatchFilter.test4.";
  
    public LevelMatchFilterTestCase(String name) {
      super(name);
    }
  
    Logger root;
    
    public void setUp() {
      root = Logger.getRootLogger();
      root.removeAllAppenders();
      
      // delete the output file if they happen to exist
      File file = new File(TEST1_FILE);
      file.delete();
      file = new File(TEST2_FILE);
      file.delete();
      file = new File(TEST3_FILE);
      file.delete();
      file = new File(TEST4_FILE);
      file.delete();
    }
  
    public void tearDown() {  
      root.getLoggerRepository().resetConfiguration();  
    }
  
    /**
      Test #1: Simple accept/deny configuration set programatically. */
    public void test1() throws Exception {
      
      // set up appender
      Layout layout = new SimpleLayout();
      Appender appender = new FileAppender(layout, TEST1_FILE, false);
      
      // create LevelMatchFilter
      LevelMatchFilter matchFilter = new LevelMatchFilter();
      matchFilter.setMatchReturnValue("accept");
      matchFilter.setNoMatchReturnValue("deny");
      
       // attach match filter to appender
      appender.addFilter(matchFilter);
             
      // set appender on root and set level to debug
      root.addAppender(appender);
      root.setLevel(Level.DEBUG);
      
      Level[] levelArray = new Level[] {Level.DEBUG, Level.INFO, Level.WARN, 
  				      Level.ERROR, Level.FATAL};
      for (int x = 0; x < levelArray.length; x++) {
        // set the level to match
        matchFilter.setLevelToMatch(levelArray[x].toString());
        common("pass " + x + "; filter set to accept only " 
  	     + levelArray[x].toString() + " msgs");
      }
      
      assertTrue(Compare.compare(TEST1_FILE, TEST1_WITNESS));
    }
  
    /**
      Test #2: Simple deny/accept configuration set programatically. */
    public void test2() throws Exception {
      
      // set up appender
      Layout layout = new SimpleLayout();
      Appender appender = new FileAppender(layout, TEST2_FILE, false);
      
      // create LevelMatchFilter, set to deny matches
      LevelMatchFilter matchFilter = new LevelMatchFilter();
      matchFilter.setMatchReturnValue("deny");
      matchFilter.setNoMatchReturnValue("accept");
   
       // attach match filter to appender
      appender.addFilter(matchFilter);
             
      // set appender on root and set level to debug
      root.addAppender(appender);
      root.setLevel(Level.DEBUG);
      
      Level[] levelArray = new Level[] {Level.DEBUG, Level.INFO, Level.WARN,
  				      Level.ERROR, Level.FATAL};
      for (int x = 0; x < levelArray.length; x++) {
        // set the level to match
        matchFilter.setLevelToMatch(levelArray[x].toString());
        common("pass " + x + "; filter set to deny only " + levelArray[x].toString()
                + " msgs");
      }
      
      assertTrue(Compare.compare(TEST2_FILE, TEST2_WITNESS));
    }
  
    /**
      Test #3: Simple chain policy configuration set programatically. */
    public void test3() throws Exception {
      // set up appender
      Layout layout = new SimpleLayout();
      Appender appender = new FileAppender(layout, TEST3_FILE, false);
      
      // create LevelMatchFilter, set chain policy to accept/neutral
      LevelMatchFilter match1Filter = new LevelMatchFilter();
      match1Filter.setChainPolicy("AcceptOnMatch");
      
      LevelMatchFilter match2Filter = new LevelMatchFilter();
      match2Filter.setChainPolicy("AcceptOnMatch");
   
       // attach match filter to appender
      appender.addFilter(match1Filter);
      appender.addFilter(match2Filter);
  
      // attach DenyAllFilter to end of filter chain to deny neutral
      // (non matching) messages
      appender.addFilter(new DenyAllFilter());
             
      // set appender on root and set level to debug
      root.addAppender(appender);
      root.setLevel(Level.DEBUG);
      
      Level[] levelArray = new Level[] {Level.DEBUG, Level.INFO, Level.WARN,
  				      Level.ERROR, Level.FATAL};
      for (int x = 0, passCount = 0; x < levelArray.length; x++) {
        // set the level to match
        match1Filter.setLevelToMatch(levelArray[x].toString());
        for (int y = levelArray.length-1; y >= 0; y--,passCount++) {
          match2Filter.setLevelToMatch(levelArray[y].toString());
          common("pass " + passCount + "; filter chain set to accept " 
          + levelArray[x].toString() + " or " + levelArray[y].toString() 
          + " msgs");
        }
      }
      
      assertTrue(Compare.compare(TEST3_FILE, TEST3_WITNESS));
    }
  
    /**
      Test #4: Same test at Test #1, but set via configuration file. */
    public void test4() throws Exception {
      Level[] levelArray = new Level[] {Level.DEBUG, Level.INFO, Level.WARN, 
  				      Level.ERROR, Level.FATAL};
  
      for (int x = 0; x < levelArray.length; x++) {
        DOMConfigurator.configure(TEST4_CONFIG + x + ".xml");
        
        common("pass " + x + "; filter should accept only " 
  	     + levelArray[x].toString() + " msgs");
        
        tearDown();
      }
      
      assertTrue(Compare.compare(TEST4_FILE, TEST4_WITNESS));
    }
    
    void common(String msg) {
      Logger logger = Logger.getLogger("test");
      logger.debug(msg);
      logger.info(msg);
      logger.warn(msg);
      logger.error(msg);
      logger.fatal(msg);
    }
    
    public static Test suite() {
      TestSuite suite = new TestSuite();
      suite.addTest(new LevelMatchFilterTestCase("test1"));
      suite.addTest(new LevelMatchFilterTestCase("test2"));
      suite.addTest(new LevelMatchFilterTestCase("test3"));
      suite.addTest(new LevelMatchFilterTestCase("test4"));
      return suite;
    }
  
  }
  
  
  
  1.1                  jakarta-log4j-sandbox/tests/witness/filters.LevelMatchFilter.test1.txt
  
  Index: filters.LevelMatchFilter.test1.txt
  ===================================================================
  DEBUG - pass 0; filter set to accept only DEBUG msgs
  INFO - pass 1; filter set to accept only INFO msgs
  WARN - pass 2; filter set to accept only WARN msgs
  ERROR - pass 3; filter set to accept only ERROR msgs
  FATAL - pass 4; filter set to accept only FATAL msgs
  
  
  
  1.1                  jakarta-log4j-sandbox/tests/witness/filters.LevelMatchFilter.test2.txt
  
  Index: filters.LevelMatchFilter.test2.txt
  ===================================================================
  INFO - pass 0; filter set to deny only DEBUG msgs
  WARN - pass 0; filter set to deny only DEBUG msgs
  ERROR - pass 0; filter set to deny only DEBUG msgs
  FATAL - pass 0; filter set to deny only DEBUG msgs
  DEBUG - pass 1; filter set to deny only INFO msgs
  WARN - pass 1; filter set to deny only INFO msgs
  ERROR - pass 1; filter set to deny only INFO msgs
  FATAL - pass 1; filter set to deny only INFO msgs
  DEBUG - pass 2; filter set to deny only WARN msgs
  INFO - pass 2; filter set to deny only WARN msgs
  ERROR - pass 2; filter set to deny only WARN msgs
  FATAL - pass 2; filter set to deny only WARN msgs
  DEBUG - pass 3; filter set to deny only ERROR msgs
  INFO - pass 3; filter set to deny only ERROR msgs
  WARN - pass 3; filter set to deny only ERROR msgs
  FATAL - pass 3; filter set to deny only ERROR msgs
  DEBUG - pass 4; filter set to deny only FATAL msgs
  INFO - pass 4; filter set to deny only FATAL msgs
  WARN - pass 4; filter set to deny only FATAL msgs
  ERROR - pass 4; filter set to deny only FATAL msgs
  
  
  
  1.1                  jakarta-log4j-sandbox/tests/witness/filters.LevelMatchFilter.test3.txt
  
  Index: filters.LevelMatchFilter.test3.txt
  ===================================================================
  DEBUG - pass 0; filter chain set to accept DEBUG or FATAL msgs
  FATAL - pass 0; filter chain set to accept DEBUG or FATAL msgs
  DEBUG - pass 1; filter chain set to accept DEBUG or ERROR msgs
  ERROR - pass 1; filter chain set to accept DEBUG or ERROR msgs
  DEBUG - pass 2; filter chain set to accept DEBUG or WARN msgs
  WARN - pass 2; filter chain set to accept DEBUG or WARN msgs
  DEBUG - pass 3; filter chain set to accept DEBUG or INFO msgs
  INFO - pass 3; filter chain set to accept DEBUG or INFO msgs
  DEBUG - pass 4; filter chain set to accept DEBUG or DEBUG msgs
  INFO - pass 5; filter chain set to accept INFO or FATAL msgs
  FATAL - pass 5; filter chain set to accept INFO or FATAL msgs
  INFO - pass 6; filter chain set to accept INFO or ERROR msgs
  ERROR - pass 6; filter chain set to accept INFO or ERROR msgs
  INFO - pass 7; filter chain set to accept INFO or WARN msgs
  WARN - pass 7; filter chain set to accept INFO or WARN msgs
  INFO - pass 8; filter chain set to accept INFO or INFO msgs
  DEBUG - pass 9; filter chain set to accept INFO or DEBUG msgs
  INFO - pass 9; filter chain set to accept INFO or DEBUG msgs
  WARN - pass 10; filter chain set to accept WARN or FATAL msgs
  FATAL - pass 10; filter chain set to accept WARN or FATAL msgs
  WARN - pass 11; filter chain set to accept WARN or ERROR msgs
  ERROR - pass 11; filter chain set to accept WARN or ERROR msgs
  WARN - pass 12; filter chain set to accept WARN or WARN msgs
  INFO - pass 13; filter chain set to accept WARN or INFO msgs
  WARN - pass 13; filter chain set to accept WARN or INFO msgs
  DEBUG - pass 14; filter chain set to accept WARN or DEBUG msgs
  WARN - pass 14; filter chain set to accept WARN or DEBUG msgs
  ERROR - pass 15; filter chain set to accept ERROR or FATAL msgs
  FATAL - pass 15; filter chain set to accept ERROR or FATAL msgs
  ERROR - pass 16; filter chain set to accept ERROR or ERROR msgs
  WARN - pass 17; filter chain set to accept ERROR or WARN msgs
  ERROR - pass 17; filter chain set to accept ERROR or WARN msgs
  INFO - pass 18; filter chain set to accept ERROR or INFO msgs
  ERROR - pass 18; filter chain set to accept ERROR or INFO msgs
  DEBUG - pass 19; filter chain set to accept ERROR or DEBUG msgs
  ERROR - pass 19; filter chain set to accept ERROR or DEBUG msgs
  FATAL - pass 20; filter chain set to accept FATAL or FATAL msgs
  ERROR - pass 21; filter chain set to accept FATAL or ERROR msgs
  FATAL - pass 21; filter chain set to accept FATAL or ERROR msgs
  WARN - pass 22; filter chain set to accept FATAL or WARN msgs
  FATAL - pass 22; filter chain set to accept FATAL or WARN msgs
  INFO - pass 23; filter chain set to accept FATAL or INFO msgs
  FATAL - pass 23; filter chain set to accept FATAL or INFO msgs
  DEBUG - pass 24; filter chain set to accept FATAL or DEBUG msgs
  FATAL - pass 24; filter chain set to accept FATAL or DEBUG msgs
  
  
  
  1.1                  jakarta-log4j-sandbox/tests/witness/filters.LevelMatchFilter.test4.txt
  
  Index: filters.LevelMatchFilter.test4.txt
  ===================================================================
  DEBUG - pass 0; filter should accept only DEBUG msgs
  INFO - pass 1; filter should accept only INFO msgs
  WARN - pass 2; filter should accept only WARN msgs
  ERROR - pass 3; filter should accept only ERROR msgs
  FATAL - pass 4; filter should accept only FATAL msgs
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: log4j-dev-help@jakarta.apache.org


Mime
View raw message