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  package org.apache.log4j.rewrite;
    18
    19  import junit.framework.*;
    20  import org.apache.log4j.*;
    21  import org.apache.log4j.util.Compare;
    22  import org.apache.log4j.xml.*;
    23
    24  import java.io.InputStream;
    25  import java.util.Map;
    26  import java.util.TreeMap;
    27  import java.util.Hashtable;
    28  import javax.xml.parsers.*;
    29  import org.w3c.dom.*;
    30
    31  public class RewriteAppenderTest extends TestCase {
    32      public RewriteAppenderTest(final String name) {
    33          super(name);
    34      }
    35
    36      public void setUp() {
    37          LogManager.getLoggerRepository().resetConfiguration();
    38          Hashtable context = MDC.getContext();
    39          if (context != null) {
    40              context.clear();
    41          }
    42      }
    43
    44      public void tearDown() {
    45          LogManager.getLoggerRepository().shutdown();
    46      }
    47
    48      public void configure(final String resourceName) throws Exception {
    49          InputStream is = RewriteAppenderTest.class.getResourceAsStream(resourceName);
    50          DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    51          factory.setNamespaceAware(false);
    52          DocumentBuilder builder = factory.newDocumentBuilder();
    53          Document doc = builder.parse(is);
    54          DOMConfigurator.configure(doc.getDocumentElement());
    55      }
    56
    57
    58      public void testMapPolicy() throws Exception {
    59          configure("map.xml");
    60          Logger logger = Logger.getLogger(RewriteAppenderTest.class);
    61          logger.info("Message 0");
    62          MDC.put("p1", "Hola");
    63
    64          Map<String, String> msg = new TreeMap<>();
    65          msg.put("p1", "Hello");
    66          msg.put("p2", "World");
    67          msg.put("x1", "Mundo");
    68          logger.info(msg);
    69          msg.put("message", "Message 1");
    70          logger.info(msg);
    71          assertTrue(Compare.compare(RewriteAppenderTest.class, "temp", "map.log"));
    72      }
    73
    74      private static class BaseBean {
    75          private final Object p2;
    76          private final Object x1;
    77
    78          public BaseBean(final Object p2,
    79                          final Object x1) {
    80               this.p2 = p2;
    81               this.x1 = x1;
    82          }
    83
    84          public Object getP2() {
    85              return p2;
    86          }
    87
    88          public Object getX1() {
    89              return x1;
    90          }
    91
    92          public String toString() {
    93              return "I am bean.";
    94          }
    95      }
    96
    97      private static class MessageBean extends BaseBean {
    98          private final Object msg;
    99
    100         public MessageBean(final Object msg,
    101                            final Object p2,
    102                            final Object x1) {
    103             super(p2, x1);
    104             this.msg = msg;
    105         }
    106
    107         public Object getMessage() {
    108             return msg;
    109         }
    110     }
    111
    112     public void testReflectionPolicy() throws Exception {
    113         configure("reflection.xml");
    114         Logger logger = Logger.getLogger(RewriteAppenderTest.class);
    115         logger.info("Message 0");
    116         logger.info(new BaseBean("Hello", "World" ));
    117         MDC.put("p1", "Hola");
    118         MDC.put("p2", "p2");
    119         logger.info(new MessageBean("Welcome to The Hub", "Hello", "World" ));
    120         assertTrue(Compare.compare(RewriteAppenderTest.class, "temp", "reflection.log"));
    121     }
    122
    123     public void testPropertyPolicy() throws Exception {
    124         configure("property.xml");
    125         Logger logger = Logger.getLogger(RewriteAppenderTest.class);
    126         logger.info("Message 0");
    127         MDC.put("p1", "Hola");
    128         logger.info("Message 1");
    129         assertTrue(Compare.compare(RewriteAppenderTest.class, "temp", "property.log"));
    130     }
    131 }