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 org.apache.log4j.Appender;
22  import org.apache.log4j.Level;
23  import org.apache.log4j.Logger;
24  import org.apache.log4j.spi.ErrorHandler;
25  import org.apache.log4j.util.Filter;
26  import org.apache.log4j.util.Transformer;
27  import org.apache.log4j.util.Compare;
28  import org.apache.log4j.util.JunitTestRunnerFilter;
29  import org.apache.log4j.util.SunReflectFilter;
30  import org.apache.log4j.util.LineNumberFilter;
31  import org.apache.log4j.util.ControlFilter;
32  import org.apache.log4j.xml.DOMConfigurator;
33  import org.apache.log4j.PropertyConfigurator;
34  
35  public class ErrorHandlerTestCase extends TestCase {
36  
37    static String TEMP = "output/temp";
38    static String FILTERED = "output/filtered";
39  
40  
41    static String EXCEPTION1 = "java.lang.Exception: Just testing";
42    static String EXCEPTION2 = "\\s*at .*\\(.*\\)";
43    static String EXCEPTION3 = "\\s*at .*\\(Native Method\\)";
44  
45    static String TEST1_PAT =
46                         "FALLBACK - (root|test) - Message \\d";
47  
48  
49    Logger root; 
50    Logger logger;
51  
52    public ErrorHandlerTestCase(String name) {
53      super(name);
54    }
55  
56    public void setUp() {
57      root = Logger.getRootLogger();
58      logger = Logger.getLogger("test");
59    }
60  
61    public void tearDown() {  
62      root.getLoggerRepository().resetConfiguration();
63    }
64  
65    public void test1() throws Exception {
66      DOMConfigurator.configure("input/xml/fallback1.xml");
67      Appender primary = root.getAppender("PRIMARY");
68      ErrorHandler eh = primary.getErrorHandler();
69      assertNotNull(eh);
70  
71      common();
72  
73      ControlFilter cf = new ControlFilter(new String[]{TEST1_PAT,
74  					       EXCEPTION1, EXCEPTION2, EXCEPTION3});
75  
76      Transformer.transform(TEMP, FILTERED, new Filter[] {cf,
77                              new LineNumberFilter(),
78                              new JunitTestRunnerFilter(),
79                              new SunReflectFilter()});
80  
81  
82      assertTrue(Compare.compare(FILTERED, "witness/fallback1"));
83    }
84    
85    public void test2() throws Exception {
86      PropertyConfigurator.configure("input/fallback1.properties");
87      Appender primary = root.getAppender("PRIMARY");
88      ErrorHandler eh = primary.getErrorHandler();
89      assertNotNull(eh);
90  
91      common();
92  
93      ControlFilter cf = new ControlFilter(new String[]{TEST1_PAT,
94  					       EXCEPTION1, EXCEPTION2, EXCEPTION3});
95  
96      Transformer.transform(TEMP, FILTERED, new Filter[] {cf,
97                              new LineNumberFilter(),
98                              new JunitTestRunnerFilter(),
99                              new SunReflectFilter()});
100 
101 
102     assertTrue(Compare.compare(FILTERED, "witness/fallback1"));
103   }
104 
105   void common() {
106     int i = -1;
107  
108     logger.debug("Message " + ++i);
109     root.debug("Message " + i);        
110 
111     logger.info ("Message " + ++i);
112     root.info("Message " + i);        
113 
114     logger.warn ("Message " + ++i);
115     root.warn("Message " + i);        
116 
117     logger.error("Message " + ++i);
118     root.error("Message " + i);
119     
120     logger.log(Level.FATAL, "Message " + ++i);
121     root.log(Level.FATAL, "Message " + i);    
122     
123     Exception e = new Exception("Just testing");
124     logger.debug("Message " + ++i, e);
125     root.debug("Message " + i, e);
126     
127     logger.error("Message " + ++i, e);
128     root.error("Message " + i, e);    
129 
130   }
131 
132 }