View Javadoc

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