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