    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.helpers.AbsoluteTimeDateFormat;
    25  import org.apache.log4j.util.*;
    26
    27  /**
    28     A superficial but general test of log4j.
    29   */
    30  public class MinimumTestCase extends TestCase {
    31
    32    static String FILTERED = "output/filtered";
    33
    34    static String EXCEPTION1 = "java.lang.Exception: Just testing";
    35    static String EXCEPTION2 = "\\s*at .*\\(.*\\)";
    36    static String EXCEPTION3 = "\\s*at .*\\(Native Method\\)";
    37    static String EXCEPTION4 = "\\s*at .*\\(.*Compiled Code\\)";
    38    static String EXCEPTION5 = "\\s*at .*\\(.*libgcj.*\\)";
    39
    40    //18 fevr. 2002 20:02:41,551 [main] FATAL ERR - Message 0
    41
    42    static String TTCC_PAT = Filter.ABSOLUTE_DATE_AND_TIME_PAT+
    43                " \\[main]\\ (TRACE|DEBUG|INFO|WARN|ERROR|FATAL) .* - Message \\d{1,2}";
    44
    45    static String TTCC2_PAT = Filter.ABSOLUTE_DATE_AND_TIME_PAT+
    46                " \\[main]\\ (TRACE|DEBUG|INFO|WARN|ERROR|FATAL) .* - Messages should bear numbers 0 through 29\\.";
    47
    48    //18 fvr. 2002 19:49:53,456
    49
    50    Logger root;
    51    Logger logger;
    52
    53    public MinimumTestCase(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 simple() throws Exception {
    67
    68      Layout layout = new SimpleLayout();
    69      Appender appender = new FileAppender(layout, "output/simple", false);
    70      root.addAppender(appender);
    71      common();
    72
    73      Transformer.transform(
    74        "output/simple", FILTERED,
    75        new Filter[] { new LineNumberFilter(),
    76                       new SunReflectFilter(),
    77                       new JunitTestRunnerFilter() });
    78      assertTrue(Compare.compare(FILTERED, "witness/simple"));
    79    }
    80
    81    public void ttcc() throws Exception {
    82
    83      Layout layout = new TTCCLayout(AbsoluteTimeDateFormat.DATE_AND_TIME_DATE_FORMAT);
    84      Appender appender = new FileAppender(layout, "output/ttcc", false);
    85      root.addAppender(appender);
    86
    87      String oldName = Thread.currentThread().getName();
    88      Thread.currentThread().setName("main");
    89      common();
    90      Thread.currentThread().setName(oldName);
    91
    92      ControlFilter cf1 = new ControlFilter(new String[]{TTCC_PAT,
    93         TTCC2_PAT, EXCEPTION1, EXCEPTION2,
    94         EXCEPTION3, EXCEPTION4, EXCEPTION5 });
    95
    96      Transformer.transform(
    97        "output/ttcc", FILTERED,
    98        new Filter[] {
    99          cf1, new LineNumberFilter(),
    100         new AbsoluteDateAndTimeFilter(),
    101         new SunReflectFilter(), new JunitTestRunnerFilter()
    102       });
    103
    104     assertTrue(Compare.compare(FILTERED, "witness/ttcc"));
    105   }
    106
    107
    108   void common() {
    109
    110     int i = 0;
    111
    112     // In the lines below, the category names are chosen as an aid in
    113     // remembering their level values. In general, the category names
    114     // have no bearing to level values.
    115
    116     Logger ERR = Logger.getLogger("ERR");
    117     ERR.setLevel(Level.ERROR);
    118     Logger INF = Logger.getLogger("INF");
    119     INF.setLevel(Level.INFO);
    120     Logger INF_ERR = Logger.getLogger("INF.ERR");
    121     INF_ERR.setLevel(Level.ERROR);
    122     Logger DEB = Logger.getLogger("DEB");
    123     DEB.setLevel(Level.DEBUG);
    124     Logger TRC = Logger.getLogger("TRC");
    125     TRC.setLevel(Level.TRACE);
    126
    127     // Note: categories with undefined level
    128     Logger INF_UNDEF = Logger.getLogger("INF.UNDEF");
    129     Logger INF_ERR_UNDEF = Logger.getLogger("INF.ERR.UNDEF");
    130     Logger UNDEF = Logger.getLogger("UNDEF");
    131
    132
    133     // These should all log.----------------------------
    134     ERR.log(Level.FATAL, "Message " + i); i++;  //0
    135     ERR.error( "Message " + i); i++;
    136
    137     INF.log(Level.FATAL, "Message " + i); i++; // 2
    138     INF.error( "Message " + i); i++;
    139     INF.warn ( "Message " + i); i++;
    140     INF.info ( "Message " + i); i++;
    141
    142     INF_UNDEF.log(Level.FATAL, "Message " + i); i++;  //6
    143     INF_UNDEF.error( "Message " + i); i++;
    144     INF_UNDEF.warn ( "Message " + i); i++;
    145     INF_UNDEF.info ( "Message " + i); i++;
    146
    147     INF_ERR.log(Level.FATAL, "Message " + i); i++;  // 10
    148     INF_ERR.error( "Message " + i); i++;
    149
    150     INF_ERR_UNDEF.log(Level.FATAL, "Message " + i); i++;
    151     INF_ERR_UNDEF.error( "Message " + i); i++;
    152
    153     DEB.log(Level.FATAL, "Message " + i); i++;  //14
    154     DEB.error( "Message " + i); i++;
    155     DEB.warn ( "Message " + i); i++;
    156     DEB.info ( "Message " + i); i++;
    157     DEB.debug( "Message " + i); i++;
    158
    159     TRC.log(Level.FATAL, "Message " + i); i++;  //19
    160     TRC.error( "Message " + i); i++;
    161     TRC.warn ( "Message " + i); i++;
    162     TRC.info ( "Message " + i); i++;
    163     TRC.debug( "Message " + i); i++;
    164     TRC.trace( "Message " + i); i++;
    165
    166     // defaultLevel=DEBUG
    167     UNDEF.log(Level.FATAL, "Message " + i); i++;  // 25
    168     UNDEF.error("Message " + i); i++;
    169     UNDEF.warn ("Message " + i); i++;
    170     UNDEF.info ("Message " + i); i++;
    171     UNDEF.debug("Message " + i, new Exception("Just testing."));
    172     int printCount = i;
    173     i++;
    174
    175     // -------------------------------------------------
    176     // The following should not log
    177     ERR.warn("Message " + i);  i++;
    178     ERR.info("Message " + i);  i++;
    179     ERR.debug("Message " + i);  i++;
    180
    181     INF.debug("Message " + i);  i++;
    182     INF_UNDEF.debug("Message " + i); i++;
    183
    184
    185     INF_ERR.warn("Message " + i);  i++;
    186     INF_ERR.info("Message " + i);  i++;
    187     INF_ERR.debug("Message " + i); i++;
    188     INF_ERR_UNDEF.warn("Message " + i);  i++;
    189     INF_ERR_UNDEF.info("Message " + i);  i++;
    190     INF_ERR_UNDEF.debug("Message " + i); i++;
    191
    192     UNDEF.trace("Message " + i, new Exception("Just testing.")); i++;
    193     // -------------------------------------------------
    194
    195     INF.info("Messages should bear numbers 0 through "+printCount+".");
    196   }
    197
    198   public static Test suite() {
    199     TestSuite suite = new TestSuite();
    200     suite.addTest(new MinimumTestCase("simple"));
    201     suite.addTest(new MinimumTestCase("ttcc"));
    202     return suite;
    203   }
    204
    205 }