LevelMatchFilterTestCase xref

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17
    18  package org.apache.log4j.varia;
    19
    20  import junit.framework.TestCase;
    21  import junit.framework.TestSuite;
    22  import junit.framework.Test;
    23
    24  import org.apache.log4j.Logger;
    25  import org.apache.log4j.Level;
    26  import org.apache.log4j.Appender;
    27  import org.apache.log4j.FileAppender;
    28  import org.apache.log4j.Layout;
    29  import org.apache.log4j.SimpleLayout;
    30  import org.apache.log4j.varia.LevelMatchFilter;
    31  import org.apache.log4j.varia.DenyAllFilter;
    32
    33  import org.apache.log4j.util.Transformer;
    34  import org.apache.log4j.util.Compare;
    35  import org.apache.log4j.util.LineNumberFilter;
    36
    37  /**
    38     Test case for varia/LevelMatchFilter.java.
    39   */
    40  public class LevelMatchFilterTestCase extends TestCase {
    41
    42    static String ACCEPT_FILE     = "output/LevelMatchFilter_accept";
    43    static String ACCEPT_FILTERED = "output/LevelMatchFilter_accept_filtered";
    44    static String ACCEPT_WITNESS  = "witness/LevelMatchFilter_accept";
    45
    46    static String DENY_FILE       = "output/LevelMatchFilter_deny";
    47    static String DENY_FILTERED   = "output/LevelMatchFilter_deny_filtered";
    48    static String DENY_WITNESS    = "witness/LevelMatchFilter_deny";
    49
    50    Logger root;
    51    Logger logger;
    52
    53    public LevelMatchFilterTestCase(String name) {
    54      super(name);
    55    }
    56
    57    public void setUp() {
    58      root = Logger.getRootLogger();
    59      root.removeAllAppenders();
    60    }
    61
    62    public void tearDown() {
    63      root.getLoggerRepository().resetConfiguration();
    64    }
    65
    66    public void accept() throws Exception {
    67
    68      // set up appender
    69      Layout layout = new SimpleLayout();
    70      Appender appender = new FileAppender(layout, ACCEPT_FILE, false);
    71
    72      // create LevelMatchFilter
    73      LevelMatchFilter matchFilter = new LevelMatchFilter();
    74
    75       // attach match filter to appender
    76      appender.addFilter(matchFilter);
    77
    78      // attach DenyAllFilter to end of filter chain to deny neutral
    79      // (non matching) messages
    80      appender.addFilter(new DenyAllFilter());
    81
    82      // set appender on root and set level to debug
    83      root.addAppender(appender);
    84      root.setLevel(Level.TRACE);
    85
    86      Level[] levelArray = new Level[] {Level.TRACE, Level.DEBUG, Level.INFO, Level.WARN,
    87  				      Level.ERROR, Level.FATAL};
    88      for (int x = 0; x < levelArray.length; x++) {
    89        // set the level to match
    90        matchFilter.setLevelToMatch(levelArray[x].toString());
    91        common("pass " + x + "; filter set to accept only "
    92  	     + levelArray[x].toString() + " msgs");
    93      }
    94
    95      Transformer.transform(ACCEPT_FILE, ACCEPT_FILTERED, new LineNumberFilter());
    96      assertTrue(Compare.compare(ACCEPT_FILTERED, ACCEPT_WITNESS));
    97    }
    98
    99    public void deny() throws Exception {
    100
    101     // set up appender
    102     Layout layout = new SimpleLayout();
    103     Appender appender = new FileAppender(layout, DENY_FILE, false);
    104
    105     // create LevelMatchFilter, set to deny matches
    106     LevelMatchFilter matchFilter = new LevelMatchFilter();
    107     matchFilter.setAcceptOnMatch(false);
    108
    109      // attach match filter to appender
    110     appender.addFilter(matchFilter);
    111
    112     // set appender on root and set level to debug
    113     root.addAppender(appender);
    114     root.setLevel(Level.TRACE);
    115
    116     Level[] levelArray = new Level[] {Level.TRACE, Level.DEBUG, Level.INFO, Level.WARN,
    117 				      Level.ERROR, Level.FATAL};
    118     for (int x = 0; x < levelArray.length; x++) {
    119       // set the level to match
    120       matchFilter.setLevelToMatch(levelArray[x].toString());
    121       common("pass " + x + "; filter set to deny only " + levelArray[x].toString()
    122               + " msgs");
    123     }
    124
    125     Transformer.transform(DENY_FILE, DENY_FILTERED, new LineNumberFilter());
    126     assertTrue(Compare.compare(DENY_FILTERED, DENY_WITNESS));
    127   }
    128
    129
    130   void common(String msg) {
    131     Logger logger = Logger.getLogger("test");
    132     logger.trace(msg);
    133     logger.debug(msg);
    134     logger.info(msg);
    135     logger.warn(msg);
    136     logger.error(msg);
    137     logger.fatal(msg);
    138   }
    139
    140   public static Test suite() {
    141     TestSuite suite = new TestSuite();
    142     suite.addTest(new LevelMatchFilterTestCase("accept"));
    143     suite.addTest(new LevelMatchFilterTestCase("deny"));
    144     return suite;
    145   }
    146
    147 }