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  
22  import org.apache.log4j.FileAppender;
23  import org.apache.log4j.Level;
24  import org.apache.log4j.Logger;
25  import org.apache.log4j.MDC;
26  import org.apache.log4j.helpers.MDCKeySetExtractor;
27  import org.apache.log4j.spi.LoggingEvent;
28  import org.apache.log4j.util.Compare;
29  import org.apache.log4j.util.Filter;
30  import org.apache.log4j.util.JunitTestRunnerFilter;
31  import org.apache.log4j.util.LineNumberFilter;
32  import org.apache.log4j.util.SunReflectFilter;
33  import org.apache.log4j.util.Transformer;
34  import org.apache.log4j.util.XMLLineAttributeFilter;
35  import org.apache.log4j.util.XMLTimestampFilter;
36  import org.apache.log4j.util.XMLDateFilter;
37  
38  import java.util.Set;
39  import java.util.Iterator;
40  import java.util.Hashtable;
41  
42  
43  public class XSLTLayoutTestCase extends TestCase {
44    static String TEMP = "temp";
45    static String FILTERED = "filtered";
46    Logger root;
47    Logger logger;
48  
49    public XSLTLayoutTestCase(final String name) {
50      super(name);
51    }
52  
53    public void setUp() {
54      root = Logger.getRootLogger();
55      logger = Logger.getLogger(XSLTLayoutTestCase.class);
56    }
57  
58    public void tearDown() {
59      root.getLoggerRepository().resetConfiguration();
60    }
61  
62    public void testBasic() throws Exception {
63      XSLTLayout xmlLayout = new XSLTLayout();
64      root.addAppender(new FileAppender(xmlLayout, TEMP, false));
65      common();
66      Transformer.transform(
67        TEMP, FILTERED,
68        new Filter[] {
69          new LineNumberFilter(),
70          new JunitTestRunnerFilter(),
71          new XMLTimestampFilter(), 
72          new SunReflectFilter(),
73          new XMLDateFilter()
74        });
75      assertTrue(Compare.compare(XSLTLayoutTestCase.class,
76              FILTERED, "witness/xml/xsltLayout.1"));
77    }
78  
79    public void testLocationInfo() throws Exception {
80      XSLTLayout xmlLayout = new XSLTLayout();
81      xmlLayout.setLocationInfo(true);
82      root.addAppender(new FileAppender(xmlLayout, TEMP, false));
83      common();
84      Transformer.transform(
85        TEMP, FILTERED,
86        new Filter[] {
87          new LineNumberFilter(), 
88          new JunitTestRunnerFilter(),
89          new XMLTimestampFilter(),
90          new XMLLineAttributeFilter(),
91          new SunReflectFilter(),
92          new XMLDateFilter()
93        });
94      assertTrue(Compare.compare(XSLTLayoutTestCase.class,
95              FILTERED, "witness/xml/xsltLayout.2"));
96    }
97  
98    public void testCDATA() throws Exception {
99      XSLTLayout xmlLayout = new XSLTLayout();
100     xmlLayout.setLocationInfo(true);
101     root.addAppender(new FileAppender(xmlLayout, TEMP, false));
102 
103     logger.debug("Message with embedded <![CDATA[<hello>hi</hello>]]>.");
104 
105     Transformer.transform(
106       TEMP, FILTERED,
107       new Filter[] {
108         new LineNumberFilter(), 
109         new JunitTestRunnerFilter(),
110         new XMLTimestampFilter(),
111         new XMLLineAttributeFilter(),
112         new SunReflectFilter(),
113         new XMLDateFilter()
114       });
115     assertTrue(Compare.compare(XSLTLayoutTestCase.class,
116             FILTERED, "witness/xml/xsltLayout.3"));
117   }
118 
119   public void testNull() throws Exception {
120     XSLTLayout xmlLayout = new XSLTLayout();
121     root.addAppender(new FileAppender(xmlLayout, TEMP, false));
122     logger.debug("hi");
123     logger.debug(null);
124 
125     Exception e = new Exception((String) null);
126     logger.debug("hi", e);
127     Transformer.transform(
128       TEMP, FILTERED,
129       new Filter[] { new LineNumberFilter(), 
130           new JunitTestRunnerFilter(),
131           new SunReflectFilter(),
132           new XMLTimestampFilter(),
133           new XMLDateFilter()});
134     assertTrue(Compare.compare(XSLTLayoutTestCase.class,
135             FILTERED, "witness/xml/xsltLayout.null"));
136   }
137 
138   /***
139    * Tests the format of the MDC portion of the layout to ensure
140    * the KVP's we put in turn up in the output file.
141    * @throws Exception
142    */
143   public void testMDC() throws Exception {
144     XSLTLayout xmlLayout = new XSLTLayout();
145     root.addAppender(new FileAppender(xmlLayout, TEMP, false));
146 
147     clearMDC();
148     MDC.put("key1", "val1");
149     MDC.put("key2", "val2");
150 
151     logger.debug("Hello");
152     Transformer.transform(
153       TEMP, FILTERED,
154       new Filter[] { new LineNumberFilter(), 
155           new JunitTestRunnerFilter(),
156           new XMLTimestampFilter(),
157           new XMLDateFilter()});
158     assertTrue(Compare.compare(XSLTLayoutTestCase.class,
159             FILTERED, "witness/xml/xsltLayout.mdc.1"));
160   }
161 
162   public void testMDCEscaped() throws Exception {
163     XSLTLayout xmlLayout = new XSLTLayout();
164     root.addAppender(new FileAppender(xmlLayout, TEMP, false));
165 
166     clearMDC();
167     MDC.put("blahAttribute", "<blah value=\"blah\">");
168     MDC.put("<blahKey value=\"blah\"/>", "blahValue");
169 
170     logger.debug("Hello");
171     Transformer.transform(
172       TEMP, FILTERED,
173       new Filter[] { new LineNumberFilter(), 
174           new JunitTestRunnerFilter(),
175           new XMLTimestampFilter(),
176           new XMLDateFilter()});
177     assertTrue(Compare.compare(XSLTLayoutTestCase.class,
178             FILTERED, "witness/xml/xsltLayout.mdc.2"));
179   }
180 
181   void common() {
182     int i = -1;
183 
184     X x = new X();
185 
186     logger.debug("Message " + ++i);
187     root.debug("Message " + i);
188 
189     logger.info("Message " + ++i);
190     root.info("Message " + i);
191 
192     logger.warn("Message " + ++i);
193     root.warn("Message " + i);
194 
195     logger.error("Message " + ++i);
196     root.error("Message " + i);
197 
198     logger.log(Level.FATAL, "Message " + ++i);
199     root.log(Level.FATAL, "Message " + i);
200 
201     Exception e = new Exception("Just testing");
202     logger.debug("Message " + ++i, e);
203     root.debug("Message " + i, e);
204 
205     logger.error("Message " + ++i, e);
206     root.error("Message " + i, e);
207   }
208 
209     private static void clearMDC() throws Exception {
210         Hashtable context = MDC.getContext();
211         if (context != null) {
212             context.clear();
213         }
214     }
215 
216 
217   private static final class X {
218     Logger logger = Logger.getLogger(X.class);
219 
220     public X() {
221       logger.info("in X() constructor");
222     }
223   }
224 }