PatternParserTestCase 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.helpers;
    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.PatternLayout;
    29  import org.apache.log4j.MDC;
    30
    31  import org.apache.log4j.util.Compare;
    32
    33  /**
    34     Test case for helpers/PatternParser.java. Tests the various
    35     conversion patterns supported by PatternParser. This test
    36     class tests PatternParser via the PatternLayout class which
    37     uses it.
    38   */
    39  public class PatternParserTestCase extends TestCase {
    40
    41    static String OUTPUT_FILE   = "output/PatternParser";
    42    static String WITNESS_FILE  = "witness/PatternParser";
    43
    44    static String msgPattern = "%m%n";
    45
    46    Logger root;
    47    Logger logger;
    48
    49    public PatternParserTestCase(String name) {
    50      super(name);
    51    }
    52
    53    public void setUp() {
    54      root = Logger.getRootLogger();
    55      root.removeAllAppenders();
    56    }
    57
    58    public void tearDown() {
    59      root.getLoggerRepository().resetConfiguration();
    60    }
    61
    62    /**
    63      Test case for MDC conversion pattern. */
    64    public void mdcPattern() throws Exception {
    65
    66      String mdcMsgPattern1 = "%m : %X%n";
    67      String mdcMsgPattern2 = "%m : %X{key1}%n";
    68      String mdcMsgPattern3 = "%m : %X{key2}%n";
    69      String mdcMsgPattern4 = "%m : %X{key3}%n";
    70      String mdcMsgPattern5 = "%m : %X{key1},%X{key2},%X{key3}%n";
    71
    72      // set up appender
    73      PatternLayout layout = new PatternLayout(msgPattern);
    74      Appender appender = new FileAppender(layout, OUTPUT_FILE+"_mdc", false);
    75
    76      // set appender on root and set level to debug
    77      root.addAppender(appender);
    78      root.setLevel(Level.DEBUG);
    79
    80      // output starting message
    81      root.debug("starting mdc pattern test");
    82
    83      layout.setConversionPattern(mdcMsgPattern1);
    84      root.debug("empty mdc, no key specified in pattern");
    85
    86      layout.setConversionPattern(mdcMsgPattern2);
    87      root.debug("empty mdc, key1 in pattern");
    88
    89      layout.setConversionPattern(mdcMsgPattern3);
    90      root.debug("empty mdc, key2 in pattern");
    91
    92      layout.setConversionPattern(mdcMsgPattern4);
    93      root.debug("empty mdc, key3 in pattern");
    94
    95      layout.setConversionPattern(mdcMsgPattern5);
    96      root.debug("empty mdc, key1, key2, and key3 in pattern");
    97
    98      MDC.put("key1", "value1");
    99      MDC.put("key2", "value2");
    100
    101     layout.setConversionPattern(mdcMsgPattern1);
    102     root.debug("filled mdc, no key specified in pattern");
    103
    104     layout.setConversionPattern(mdcMsgPattern2);
    105     root.debug("filled mdc, key1 in pattern");
    106
    107     layout.setConversionPattern(mdcMsgPattern3);
    108     root.debug("filled mdc, key2 in pattern");
    109
    110     layout.setConversionPattern(mdcMsgPattern4);
    111     root.debug("filled mdc, key3 in pattern");
    112
    113     layout.setConversionPattern(mdcMsgPattern5);
    114     root.debug("filled mdc, key1, key2, and key3 in pattern");
    115
    116     MDC.remove("key1");
    117     MDC.remove("key2");
    118
    119     layout.setConversionPattern(msgPattern);
    120     root.debug("finished mdc pattern test");
    121
    122     assertTrue(Compare.compare(OUTPUT_FILE+"_mdc", WITNESS_FILE+"_mdc"));
    123   }
    124
    125   public static Test suite() {
    126     TestSuite suite = new TestSuite();
    127     //
    128     //   MDC requires JDK 1.2+
    129     //
    130     if (!System.getProperty("java.version").startsWith("1.1.")) {
    131        suite.addTest(new PatternParserTestCase("mdcPattern"));
    132     }
    133     return suite;
    134   }
    135
    136 }