    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;
    19
    20  import junit.framework.TestCase;
    21  import org.apache.log4j.util.AbsoluteDateAndTimeFilter;
    22  import org.apache.log4j.util.AbsoluteTimeFilter;
    23  import org.apache.log4j.util.Compare;
    24  import org.apache.log4j.util.ControlFilter;
    25  import org.apache.log4j.util.Filter;
    26  import org.apache.log4j.util.ISO8601Filter;
    27  import org.apache.log4j.util.JunitTestRunnerFilter;
    28  import org.apache.log4j.util.LineNumberFilter;
    29  import org.apache.log4j.util.RelativeTimeFilter;
    30  import org.apache.log4j.util.SunReflectFilter;
    31  import org.apache.log4j.util.Transformer;
    32
    33  public class PatternLayoutTestCase extends TestCase {
    34
    35    static String TEMP = "output/temp";
    36    static String FILTERED = "output/filtered";
    37
    38    Logger root;
    39    Logger logger;
    40
    41    static String EXCEPTION1 = "java.lang.Exception: Just testing";
    42    static String EXCEPTION2 = "\\s*at .*\\(.*\\)";
    43    static String EXCEPTION3 = "\\s*at .*\\(Native Method\\)";
    44    static String EXCEPTION4 = "\\s*at .*\\(.*Compiled Code\\)";
    45    static String EXCEPTION5 = "\\s*at .*\\(.*libgcj.*\\)";
    46
    47    static String PAT0 = "\\[main]\\ (TRACE|DEBUG|INFO |WARN |ERROR|FATAL) .* - Message \\d{1,2}";
    48    static String PAT1 = Filter.ISO8601_PAT + " " + PAT0;
    49    static String PAT2 = Filter.ABSOLUTE_DATE_AND_TIME_PAT+ " " + PAT0;
    50    static String PAT3 = Filter.ABSOLUTE_TIME_PAT+ " " + PAT0;
    51    static String PAT4 = Filter.RELATIVE_TIME_PAT+ " " + PAT0;
    52
    53    static String PAT5 = "\\[main]\\ (TRACE|DEBUG|INFO |WARN |ERROR|FATAL) .* : Message \\d{1,2}";
    54    static String PAT6 = "\\[main]\\ (TRACE|DEBUG|INFO |WARN |ERROR|FATAL) org.apache.log4j.PatternLayoutTestCase.common\\(PatternLayoutTestCase.java(:\\d{1,4})?\\): Message \\d{1,2}";
    55
    56    static String PAT11a = "^(TRACE|DEBUG|INFO |WARN |ERROR|FATAL) \\[main]\\ log4j.PatternLayoutTestCase: Message \\d{1,2}";
    57    static String PAT11b = "^(TRACE|DEBUG|INFO |WARN |ERROR|FATAL) \\[main]\\ root: Message \\d{1,2}";
    58
    59    static String PAT12 = "^\\[main]\\ (TRACE|DEBUG|INFO |WARN |ERROR|FATAL) "+
    60      "org.apache.log4j.PatternLayoutTestCase.common\\(PatternLayoutTestCase.java:\\d{3}\\): "+
    61      "Message \\d{1,2}";
    62
    63    static String PAT13 = "^\\[main]\\ (TRACE|DEBUG|INFO |WARN |ERROR|FATAL) "+
    64      "apache.log4j.PatternLayoutTestCase.common\\(PatternLayoutTestCase.java:\\d{3}\\): "+
    65      "Message \\d{1,2}";
    66
    67    static String PAT14 = "^(TRACE|DEBUG| INFO| WARN|ERROR|FATAL)\\ \\d{1,2}\\ *- Message \\d{1,2}";
    68
    69    public PatternLayoutTestCase(String name) {
    70      super(name);
    71    }
    72
    73    public void setUp() {
    74      root = Logger.getRootLogger();
    75      logger = Logger.getLogger(PatternLayoutTestCase.class);
    76    }
    77
    78    public void tearDown() {
    79      root.getLoggerRepository().resetConfiguration();
    80    }
    81
    82    public void test1() throws Exception {
    83      PropertyConfigurator.configure("input/patternLayout1.properties");
    84      common();
    85      Transformer.transform(
    86        TEMP, FILTERED,
    87        new Filter[] {
    88          new LineNumberFilter(), new SunReflectFilter(),
    89          new JunitTestRunnerFilter()
    90        });
    91      assertTrue(Compare.compare(FILTERED, "witness/patternLayout.1"));
    92    }
    93
    94    public void test2() throws Exception {
    95      PropertyConfigurator.configure("input/patternLayout2.properties");
    96      common();
    97      ControlFilter cf1 = new ControlFilter(new String[]{PAT1, EXCEPTION1,
    98  						       EXCEPTION2, EXCEPTION3, EXCEPTION4, EXCEPTION5});
    99      Transformer.transform(
    100       TEMP, FILTERED,
    101       new Filter[] {
    102         cf1, new LineNumberFilter(), new ISO8601Filter(),
    103         new SunReflectFilter(), new JunitTestRunnerFilter()
    104       });
    105     assertTrue(Compare.compare(FILTERED, "witness/patternLayout.2"));
    106   }
    107
    108   public void test3() throws Exception {
    109     PropertyConfigurator.configure("input/patternLayout3.properties");
    110     common();
    111     ControlFilter cf1 = new ControlFilter(new String[]{PAT1, EXCEPTION1,
    112 						       EXCEPTION2, EXCEPTION3, EXCEPTION4, EXCEPTION5});
    113     Transformer.transform(
    114       TEMP, FILTERED,
    115       new Filter[] {
    116         cf1, new LineNumberFilter(), new ISO8601Filter(),
    117         new SunReflectFilter(), new JunitTestRunnerFilter()
    118       });
    119     assertTrue(Compare.compare(FILTERED, "witness/patternLayout.3"));
    120   }
    121
    122   // Output format:
    123   // 06 avr. 2002 18:30:58,937 [main] DEBUG rnLayoutTestCase - Message 0
    124   public void test4() throws Exception {
    125     PropertyConfigurator.configure("input/patternLayout4.properties");
    126     common();
    127     ControlFilter cf1 = new ControlFilter(new String[]{PAT2, EXCEPTION1,
    128 						       EXCEPTION2, EXCEPTION3, EXCEPTION4, EXCEPTION5});
    129     Transformer.transform(
    130       TEMP, FILTERED,
    131       new Filter[] {
    132         cf1, new LineNumberFilter(), new AbsoluteDateAndTimeFilter(),
    133         new SunReflectFilter(), new JunitTestRunnerFilter()
    134       });
    135     assertTrue(Compare.compare(FILTERED, "witness/patternLayout.4"));
    136   }
    137
    138   public void test5() throws Exception {
    139     PropertyConfigurator.configure("input/patternLayout5.properties");
    140     common();
    141     ControlFilter cf1 = new ControlFilter(new String[]{PAT2, EXCEPTION1,
    142 						       EXCEPTION2, EXCEPTION3, EXCEPTION4, EXCEPTION5});
    143     Transformer.transform(
    144       TEMP, FILTERED,
    145       new Filter[] {
    146         cf1, new LineNumberFilter(), new AbsoluteDateAndTimeFilter(),
    147         new SunReflectFilter(), new JunitTestRunnerFilter()
    148       });
    149     assertTrue(Compare.compare(FILTERED, "witness/patternLayout.5"));
    150   }
    151
    152   // 18:54:19,201 [main] DEBUG rnLayoutTestCase - Message 0
    153   public void test6() throws Exception {
    154     PropertyConfigurator.configure("input/patternLayout6.properties");
    155     common();
    156     ControlFilter cf1 = new ControlFilter(new String[]{PAT3, EXCEPTION1,
    157 						       EXCEPTION2, EXCEPTION3, EXCEPTION4, EXCEPTION5});
    158     Transformer.transform(
    159       TEMP, FILTERED,
    160       new Filter[] {
    161         cf1, new LineNumberFilter(), new AbsoluteTimeFilter(),
    162         new SunReflectFilter(), new JunitTestRunnerFilter()
    163       });
    164     assertTrue(Compare.compare(FILTERED, "witness/patternLayout.6"));
    165   }
    166
    167
    168   public void test7() throws Exception {
    169     PropertyConfigurator.configure("input/patternLayout7.properties");
    170     common();
    171     ControlFilter cf1 = new ControlFilter(new String[]{PAT3, EXCEPTION1,
    172 						       EXCEPTION2, EXCEPTION3, EXCEPTION4, EXCEPTION5});
    173     Transformer.transform(
    174       TEMP, FILTERED,
    175       new Filter[] {
    176         cf1, new LineNumberFilter(), new AbsoluteTimeFilter(),
    177         new SunReflectFilter(), new JunitTestRunnerFilter()
    178       });
    179     assertTrue(Compare.compare(FILTERED, "witness/patternLayout.7"));
    180   }
    181
    182   public void test8() throws Exception {
    183     PropertyConfigurator.configure("input/patternLayout8.properties");
    184     common();
    185     ControlFilter cf1 = new ControlFilter(new String[]{PAT4, EXCEPTION1,
    186 						       EXCEPTION2, EXCEPTION3, EXCEPTION4, EXCEPTION5});
    187     Transformer.transform(
    188       TEMP, FILTERED,
    189       new Filter[] {
    190         cf1, new LineNumberFilter(), new RelativeTimeFilter(),
    191         new SunReflectFilter(), new JunitTestRunnerFilter()
    192       });
    193     assertTrue(Compare.compare(FILTERED, "witness/patternLayout.8"));
    194   }
    195
    196   public void test9() throws Exception {
    197     PropertyConfigurator.configure("input/patternLayout9.properties");
    198     common();
    199     ControlFilter cf1 = new ControlFilter(new String[]{PAT5, EXCEPTION1,
    200 						       EXCEPTION2, EXCEPTION3, EXCEPTION4, EXCEPTION5});
    201     Transformer.transform(
    202       TEMP, FILTERED,
    203       new Filter[] {
    204         cf1, new LineNumberFilter(), new SunReflectFilter(),
    205         new JunitTestRunnerFilter()
    206       });
    207     assertTrue(Compare.compare(FILTERED, "witness/patternLayout.9"));
    208   }
    209
    210   public void test10() throws Exception {
    211     PropertyConfigurator.configure("input/patternLayout10.properties");
    212     common();
    213     ControlFilter cf1 = new ControlFilter(new String[]{PAT6, EXCEPTION1,
    214 						       EXCEPTION2, EXCEPTION3, EXCEPTION4, EXCEPTION5});
    215     Transformer.transform(
    216       TEMP, FILTERED,
    217       new Filter[] {
    218         cf1, new LineNumberFilter(), new SunReflectFilter(),
    219         new JunitTestRunnerFilter()
    220       });
    221     assertTrue(Compare.compare(FILTERED, "witness/patternLayout.10"));
    222   }
    223
    224   public void test11() throws Exception {
    225     PropertyConfigurator.configure("input/patternLayout11.properties");
    226     common();
    227     ControlFilter cf1 = new ControlFilter(new String[]{PAT11a, PAT11b, EXCEPTION1,
    228 						       EXCEPTION2, EXCEPTION3, EXCEPTION4, EXCEPTION5});
    229     Transformer.transform(
    230       TEMP, FILTERED,
    231       new Filter[] {
    232         cf1, new LineNumberFilter(), new SunReflectFilter(),
    233         new JunitTestRunnerFilter()
    234       });
    235     assertTrue(Compare.compare(FILTERED, "witness/patternLayout.11"));
    236   }
    237
    238   public void test12() throws Exception {
    239     PropertyConfigurator.configure("input/patternLayout12.properties");
    240     common();
    241     ControlFilter cf1 = new ControlFilter(new String[]{PAT12, EXCEPTION1,
    242 						       EXCEPTION2, EXCEPTION3, EXCEPTION4, EXCEPTION5});
    243     Transformer.transform(
    244       TEMP, FILTERED,
    245       new Filter[] {
    246         cf1, new LineNumberFilter(), new SunReflectFilter(),
    247         new JunitTestRunnerFilter()
    248       });
    249     assertTrue(Compare.compare(FILTERED, "witness/patternLayout.12"));
    250   }
    251
    252   public void test13() throws Exception {
    253     PropertyConfigurator.configure("input/patternLayout13.properties");
    254     common();
    255     ControlFilter cf1 = new ControlFilter(new String[]{PAT13, EXCEPTION1,
    256 						       EXCEPTION2, EXCEPTION3, EXCEPTION4, EXCEPTION5});
    257     Transformer.transform(
    258       TEMP, FILTERED,
    259       new Filter[] {
    260         cf1, new LineNumberFilter(), new SunReflectFilter(),
    261         new JunitTestRunnerFilter()
    262       });
    263     assertTrue(Compare.compare(FILTERED, "witness/patternLayout.13"));
    264   }
    265
    266   public void test14() throws Exception {
    267     PropertyConfigurator.configure("input/patternLayout14.properties");
    268     common();
    269     ControlFilter cf1 = new ControlFilter(new String[]{PAT14, EXCEPTION1,
    270 						       EXCEPTION2, EXCEPTION3, EXCEPTION4, EXCEPTION5});
    271     Transformer.transform(
    272       TEMP, FILTERED,
    273       new Filter[] {
    274         cf1, new LineNumberFilter(), new SunReflectFilter(),
    275         new JunitTestRunnerFilter()
    276       });
    277     assertTrue(Compare.compare(FILTERED, "witness/patternLayout.14"));
    278   }
    279
    280     public void testMDC1() throws Exception {
    281       PropertyConfigurator.configure("input/patternLayout.mdc.1.properties");
    282       MDC.put("key1", "va11");
    283       MDC.put("key2", "va12");
    284       logger.debug("Hello World");
    285       MDC.remove("key1");
    286       MDC.remove("key2");
    287
    288       assertTrue(Compare.compare(TEMP, "witness/patternLayout.mdc.1"));
    289     }
    290
    291     public void testMDCClear() throws Exception {
    292       PropertyConfigurator.configure("input/patternLayout.mdc.1.properties");
    293       MDC.put("key1", "va11");
    294       MDC.put("key2", "va12");
    295       logger.debug("Hello World");
    296       MDC.clear();
    297       logger.debug("Hello World");
    298
    299       assertTrue(Compare.compare(TEMP, "witness/patternLayout.mdc.clear"));
    300     }
    301
    302
    303
    304   void common() {
    305     String oldThreadName = Thread.currentThread().getName();
    306     Thread.currentThread().setName("main");
    307
    308     int i = -1;
    309
    310     logger.trace("Message " + ++i);
    311     root.trace("Message " + i);
    312
    313     logger.debug("Message " + ++i);
    314     root.debug("Message " + i);
    315
    316     logger.info ("Message " + ++i);
    317     root.info("Message " + i);
    318
    319     logger.warn ("Message " + ++i);
    320     root.warn("Message " + i);
    321
    322     logger.error("Message " + ++i);
    323     root.error("Message " + i);
    324
    325     logger.log(Level.FATAL, "Message " + ++i);
    326     root.log(Level.FATAL, "Message " + i);
    327
    328     Exception e = new Exception("Just testing");
    329     logger.trace("Message " + ++i, e);
    330     logger.debug("Message " + ++i, e);
    331     logger.info("Message " + ++i, e);
    332     logger.warn("Message " + ++i , e);
    333     logger.error("Message " + ++i, e);
    334     logger.log(Level.FATAL, "Message " + ++i, e);
    335
    336     Thread.currentThread().setName(oldThreadName);
    337   }
    338
    339
    340 }