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.varia;
19  
20  import junit.framework.TestCase;
21  import junit.framework.TestSuite;
22  import junit.framework.Test;
23  
24  import org.apache.log4j.Logger;
25  import org.apache.log4j.Level;
26  import org.apache.log4j.xml.DOMConfigurator;
27  import org.apache.log4j.util.Filter;
28  import org.apache.log4j.util.LineNumberFilter;
29  import org.apache.log4j.util.ControlFilter;
30  import org.apache.log4j.util.ISO8601Filter;
31  import org.apache.log4j.util.Transformer;
32  import org.apache.log4j.util.Compare;
33  
34  public class ErrorHandlerTestCase extends TestCase {
35  
36    static String TEMP_A1 = "output/temp.A1";
37    static String TEMP_A2 = "output/temp.A2";
38    static String FILTERED_A1 = "output/filtered.A1";
39    static String FILTERED_A2 = "output/filtered.A2";
40  
41  
42    static String EXCEPTION1 = "java.lang.Exception: Just testing";
43    static String EXCEPTION2 = "//s*at .*//(.*://d{1,4}//)";
44    static String EXCEPTION3 = "//s*at .*//(Native Method//)";
45  
46    static String TEST1_1A_PAT = 
47                         "(DEBUG|INFO |WARN |ERROR|FATAL) //w*//.//w* - Message //d";
48  
49    static String TEST1_1B_PAT = "(DEBUG|INFO |WARN |ERROR|FATAL) root - Message //d";
50  
51    static String TEST1_2_PAT = "^//d{4}-//d{2}-//d{2} //d{2}://d{2}://d{2},//d{3} "+
52                          "//[main]// (DEBUG|INFO|WARN|ERROR|FATAL) .* - Message //d";
53  
54  
55  
56    Logger root; 
57    Logger logger;
58  
59    public ErrorHandlerTestCase(String name) {
60      super(name);
61    }
62  
63    public void setUp() {
64      root = Logger.getRootLogger();
65      logger = Logger.getLogger("test");
66    }
67  
68    public void tearDown() {  
69      root.getLoggerRepository().resetConfiguration();
70    }
71  
72    public void test1() throws Exception {
73      DOMConfigurator.configure("input/xml/fallback1.xml");
74      common();
75  
76      ControlFilter cf1 = new ControlFilter(new String[]{TEST1_1A_PAT, TEST1_1B_PAT, 
77  					       EXCEPTION1, EXCEPTION2, EXCEPTION3});
78  
79      ControlFilter cf2 = new ControlFilter(new String[]{TEST1_2_PAT, 
80  					       EXCEPTION1, EXCEPTION2, EXCEPTION3});
81  
82      Transformer.transform(TEMP_A1, FILTERED_A1, new Filter[] {cf1, 
83  							new LineNumberFilter()});
84  
85      Transformer.transform(TEMP_A2, FILTERED_A2, new Filter[] {cf2,
86                                        new LineNumberFilter(), new ISO8601Filter()});
87  
88      assertTrue(Compare.compare(FILTERED_A1, "witness/dom.A1.1"));
89      assertTrue(Compare.compare(FILTERED_A2, "witness/dom.A2.1"));
90    }
91  
92    void common() {
93      int i = -1;
94   
95      logger.debug("Message " + ++i);
96      root.debug("Message " + i);        
97  
98      logger.info ("Message " + ++i);
99      root.info("Message " + i);        
100 
101     logger.warn ("Message " + ++i);
102     root.warn("Message " + i);        
103 
104     logger.error("Message " + ++i);
105     root.error("Message " + i);
106     
107     logger.log(Level.FATAL, "Message " + ++i);
108     root.log(Level.FATAL, "Message " + i);    
109     
110     Exception e = new Exception("Just testing");
111     logger.debug("Message " + ++i, e);
112     root.debug("Message " + i, e);
113     
114     logger.error("Message " + ++i, e);
115     root.error("Message " + i, e);    
116 
117   }
118 
119   public static Test suite() {
120     TestSuite suite = new TestSuite();
121     suite.addTest(new ErrorHandlerTestCase("test1"));
122     return suite;
123   }
124 
125 }