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.xml;
    19
    20  import junit.framework.TestCase;
    21  import org.apache.log4j.Appender;
    22  import org.apache.log4j.Level;
    23  import org.apache.log4j.Logger;
    24  import org.apache.log4j.VectorAppender;
    25  import org.apache.log4j.spi.ErrorHandler;
    26  import org.apache.log4j.spi.LoggerFactory;
    27  import org.apache.log4j.spi.LoggingEvent;
    28  import org.apache.log4j.util.Compare;
    29  import org.apache.log4j.util.ControlFilter;
    30  import org.apache.log4j.util.Filter;
    31  import org.apache.log4j.util.ISO8601Filter;
    32  import org.apache.log4j.util.JunitTestRunnerFilter;
    33  import org.apache.log4j.util.LineNumberFilter;
    34  import org.apache.log4j.util.SunReflectFilter;
    35  import org.apache.log4j.util.Transformer;
    36
    37  public class DOMTestCase extends TestCase {
    38
    39    static String TEMP_A1 = "output/temp.A1";
    40    static String TEMP_A2 = "output/temp.A2";
    41    static String FILTERED_A1 = "output/filtered.A1";
    42    static String FILTERED_A2 = "output/filtered.A2";
    43
    44
    45    static String EXCEPTION1 = "java.lang.Exception: Just testing";
    46    static String EXCEPTION2 = "//s*at .*//(.*://d{1,4}//)";
    47    static String EXCEPTION3 = "//s*at .*//(Native Method//)";
    48    static String EXCEPTION4 = "//s*at .*//(.*Compiled Code//)";
    49    static String EXCEPTION5 = "//s*at .*//(.*libgcj.*//)";
    50
    51
    52    static String TEST1_1A_PAT =
    53                         "(TRACE|DEBUG|INFO |WARN |ERROR|FATAL) //w*//.//w* - Message //d";
    54
    55    static String TEST1_1B_PAT = "(TRACE|DEBUG|INFO |WARN |ERROR|FATAL) root - Message //d";
    56
    57    static String TEST1_2_PAT = "^//d{4}-//d{2}-//d{2} //d{2}://d{2}://d{2},//d{3} "+
    58                          "//[main]// (TRACE|DEBUG|INFO|WARN|ERROR|FATAL) .* - Message //d";
    59
    60
    61
    62    Logger root;
    63    Logger logger;
    64
    65    public DOMTestCase(String name) {
    66      super(name);
    67    }
    68
    69    public void setUp() {
    70      root = Logger.getRootLogger();
    71      logger = Logger.getLogger(DOMTestCase.class);
    72    }
    73
    74    public void tearDown() {
    75      root.getLoggerRepository().resetConfiguration();
    76    }
    77
    78    public void test1() throws Exception {
    79      DOMConfigurator.configure("input/xml/DOMTestCase1.xml");
    80      common();
    81
    82      ControlFilter cf1 = new ControlFilter(new String[]{TEST1_1A_PAT, TEST1_1B_PAT,
    83  					       EXCEPTION1, EXCEPTION2, EXCEPTION3, EXCEPTION4, EXCEPTION5});
    84
    85      ControlFilter cf2 = new ControlFilter(new String[]{TEST1_2_PAT,
    86  					       EXCEPTION1, EXCEPTION2, EXCEPTION3, EXCEPTION4, EXCEPTION5});
    87
    88      Transformer.transform(
    89        TEMP_A1, FILTERED_A1,
    90        new Filter[] {
    91          cf1, new LineNumberFilter(), new SunReflectFilter(),
    92          new JunitTestRunnerFilter()
    93        });
    94
    95      Transformer.transform(
    96        TEMP_A2, FILTERED_A2,
    97        new Filter[] {
    98          cf2, new LineNumberFilter(), new ISO8601Filter(),
    99          new SunReflectFilter(), new JunitTestRunnerFilter()
    100       });
    101
    102     assertTrue(Compare.compare(FILTERED_A1, "witness/dom.A1.1"));
    103     assertTrue(Compare.compare(FILTERED_A2, "witness/dom.A2.1"));
    104   }
    105
    106   /***
    107    *   Tests processing of external entities in XML file.
    108    */
    109   public void test4() throws Exception {
    110     DOMConfigurator.configure("input/xml/DOMTest4.xml");
    111     common();
    112
    113     ControlFilter cf1 = new ControlFilter(new String[]{TEST1_1A_PAT, TEST1_1B_PAT,
    114 					       EXCEPTION1, EXCEPTION2, EXCEPTION3, EXCEPTION4, EXCEPTION5});
    115
    116     ControlFilter cf2 = new ControlFilter(new String[]{TEST1_2_PAT,
    117 					       EXCEPTION1, EXCEPTION2, EXCEPTION3, EXCEPTION4, EXCEPTION5});
    118
    119     Transformer.transform(
    120       TEMP_A1 + ".4", FILTERED_A1 + ".4",
    121       new Filter[] {
    122         cf1, new LineNumberFilter(), new SunReflectFilter(),
    123         new JunitTestRunnerFilter()
    124       });
    125
    126     Transformer.transform(
    127       TEMP_A2 + ".4", FILTERED_A2 + ".4",
    128       new Filter[] {
    129         cf2, new LineNumberFilter(), new ISO8601Filter(),
    130         new SunReflectFilter(), new JunitTestRunnerFilter()
    131       });
    132
    133     assertTrue(Compare.compare(FILTERED_A1 + ".4", "witness/dom.A1.4"));
    134     assertTrue(Compare.compare(FILTERED_A2 + ".4", "witness/dom.A2.4"));
    135   }
    136
    137   void common() {
    138     String oldThreadName = Thread.currentThread().getName();
    139     Thread.currentThread().setName("main");
    140
    141     int i = -1;
    142
    143     logger.trace("Message " + ++i);
    144     root.trace("Message " + i);
    145
    146     logger.debug("Message " + ++i);
    147     root.debug("Message " + i);
    148
    149     logger.info ("Message " + ++i);
    150     root.info("Message " + i);
    151
    152     logger.warn ("Message " + ++i);
    153     root.warn("Message " + i);
    154
    155     logger.error("Message " + ++i);
    156     root.error("Message " + i);
    157
    158     logger.log(Level.FATAL, "Message " + ++i);
    159     root.log(Level.FATAL, "Message " + i);
    160
    161     Exception e = new Exception("Just testing");
    162     logger.debug("Message " + ++i, e);
    163     root.debug("Message " + i, e);
    164
    165     logger.error("Message " + ++i, e);
    166     root.error("Message " + i, e);
    167
    168     Thread.currentThread().setName(oldThreadName);
    169   }
    170
    171
    172     /***
    173      * CustomLogger implementation for testCategoryFactory1 and 2.
    174      */
    175   private static class CustomLogger extends Logger {
    176         /***
    177          * Creates new instance.
    178          * @param name logger name.
    179          */
    180       public CustomLogger(final String name) {
    181           super(name);
    182       }
    183   }
    184
    185     /***
    186      * Creates new instances of CustomLogger.
    187      */
    188   public static class CustomLoggerFactory implements LoggerFactory {
    189         /***
    190          * Addivity, expected to be set false in configuration file.
    191          */
    192       private boolean additivity;
    193
    194         /***
    195          * Create new instance of factory.
    196          */
    197       public CustomLoggerFactory() {
    198           additivity = true;
    199       }
    200
    201         /***
    202          * Create new logger.
    203          * @param name logger name.
    204          * @return new logger.
    205          */
    206       public Logger makeNewLoggerInstance(final String name) {
    207           Logger logger = new CustomLogger(name);
    208           assertFalse(additivity);
    209           return logger;
    210       }
    211
    212         /***
    213          * Set additivity.
    214          * @param newVal new value of additivity.
    215          */
    216       public void setAdditivity(final boolean newVal) {
    217           additivity = newVal;
    218       }
    219   }
    220
    221     /***
    222      * CustomErrorHandler for testCategoryFactory2.
    223      */
    224   public static class CustomErrorHandler implements ErrorHandler {
    225       public CustomErrorHandler() {}
    226       public void activateOptions() {}
    227       public void setLogger(final Logger logger) {}
    228       public void error(String message, Exception e, int errorCode) {}
    229       public void error(String message) {}
    230       public void error(String message, Exception e, int errorCode, LoggingEvent event) {}
    231       public void setAppender(Appender appender) {}
    232       public void setBackupAppender(Appender appender) {}
    233   }
    234
    235     /***
    236      * Tests that loggers mentioned in logger elements
    237      *    use the specified categoryFactory.  See bug 33708.
    238      */
    239   public void testCategoryFactory1() {
    240       DOMConfigurator.configure("input/xml/categoryfactory1.xml");
    241       //
    242       //   logger explicitly mentioned in configuration,
    243       //         should be a CustomLogger
    244       Logger logger1 = Logger.getLogger("org.apache.log4j.xml.DOMTestCase.testCategoryFactory1.1");
    245       assertTrue(logger1 instanceof CustomLogger);
    246       //
    247       //   logger not explicitly mentioned in configuration,
    248       //         should use default factory
    249       Logger logger2 = Logger.getLogger("org.apache.log4j.xml.DOMTestCase.testCategoryFactory1.2");
    250       assertFalse(logger2 instanceof CustomLogger);
    251   }
    252
    253     /***
    254      * Tests that loggers mentioned in logger-ref elements
    255      *    use the specified categoryFactory.  See bug 33708.
    256      */
    257     public void testCategoryFactory2() {
    258         DOMConfigurator.configure("input/xml/categoryfactory2.xml");
    259         //
    260         //   logger explicitly mentioned in configuration,
    261         //         should be a CustomLogger
    262         Logger logger1 = Logger.getLogger("org.apache.log4j.xml.DOMTestCase.testCategoryFactory2.1");
    263         assertTrue(logger1 instanceof CustomLogger);
    264         //
    265         //   logger not explicitly mentioned in configuration,
    266         //         should use default factory
    267         Logger logger2 = Logger.getLogger("org.apache.log4j.xml.DOMTestCase.testCategoryFactory2.2");
    268         assertFalse(logger2 instanceof CustomLogger);
    269     }
    270
    271     /***
    272      * Tests that loggers mentioned in logger elements
    273      *    use the specified loggerFactory.  See bug 33708.
    274      */
    275   public void testLoggerFactory1() {
    276       DOMConfigurator.configure("input/xml/loggerfactory1.xml");
    277       //
    278       //   logger explicitly mentioned in configuration,
    279       //         should be a CustomLogger
    280       Logger logger1 = Logger.getLogger("org.apache.log4j.xml.DOMTestCase.testLoggerFactory1.1");
    281       assertTrue(logger1 instanceof CustomLogger);
    282       //
    283       //   logger not explicitly mentioned in configuration,
    284       //         should use default factory
    285       Logger logger2 = Logger.getLogger("org.apache.log4j.xml.DOMTestCase.testLoggerFactory1.2");
    286       assertFalse(logger2 instanceof CustomLogger);
    287   }
    288
    289     /***
    290      * Tests that reset="true" on log4j:configuration element resets
    291      * repository before configuration.
    292      * @throws Exception thrown on error.
    293      */
    294   public void testReset() throws Exception {
    295       VectorAppender appender = new VectorAppender();
    296       appender.setName("V1");
    297       Logger.getRootLogger().addAppender(appender);
    298       DOMConfigurator.configure("input/xml/testReset.xml");
    299       assertNull(Logger.getRootLogger().getAppender("V1"));
    300   }
    301
    302
    303     /***
    304      * Test checks that configureAndWatch does initial configuration, see bug 33502.
    305       * @throws Exception if IO error.
    306      */
    307   public void testConfigureAndWatch() throws Exception {
    308     DOMConfigurator.configureAndWatch("input/xml/DOMTestCase1.xml");
    309     assertNotNull(Logger.getRootLogger().getAppender("A1"));
    310   }
    311
    312 }