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;
19  
20  import junit.framework.TestCase;
21  import junit.framework.TestSuite;
22  import junit.framework.Test;
23  
24  import org.apache.log4j.*;
25  import org.apache.log4j.helpers.AbsoluteTimeDateFormat;
26  import org.apache.log4j.util.*;
27  
28  /***
29     A superficial but general test of log4j.
30   */
31  public class MinimumTestCase extends TestCase {
32  
33    static String FILTERED = "output/filtered";
34  
35    static String EXCEPTION1 = "java.lang.Exception: Just testing";
36    static String EXCEPTION2 = "//s*at .*//(.*://d{1,4}//)";
37    static String EXCEPTION3 = "//s*at .*//(Native Method//)";
38    static String EXCEPTION4 = "//s*at .*//(.*Compiled Code//)";
39    static String EXCEPTION5 = "//s*at .*//(.*libgcj.*//)";
40  
41    //18 fevr. 2002 20:02:41,551 [main] FATAL ERR - Message 0
42  
43    static String TTCC_PAT = Filter.ABSOLUTE_DATE_AND_TIME_PAT+ 
44                " //[main]// (TRACE|DEBUG|INFO|WARN|ERROR|FATAL) .* - Message //d{1,2}";
45  
46    static String TTCC2_PAT = Filter.ABSOLUTE_DATE_AND_TIME_PAT+ 
47                " //[main]// (TRACE|DEBUG|INFO|WARN|ERROR|FATAL) .* - Messages should bear numbers 0 through 29//.";
48  
49    //18 fvr. 2002 19:49:53,456
50  
51    Logger root; 
52    Logger logger;
53  
54    public MinimumTestCase(String name) {
55      super(name);
56    }
57  
58    public void setUp() {
59      root = Logger.getRootLogger();
60      root.removeAllAppenders();
61    }
62  
63    public void tearDown() {  
64      root.getLoggerRepository().resetConfiguration();
65    }
66  
67    public void simple() throws Exception {
68      
69      Layout layout = new SimpleLayout();
70      Appender appender = new FileAppender(layout, "output/simple", false);
71      root.addAppender(appender);    
72      common();
73  
74      Transformer.transform(
75        "output/simple", FILTERED,
76        new Filter[] { new LineNumberFilter(), 
77                       new SunReflectFilter(), 
78                       new JunitTestRunnerFilter() });
79      assertTrue(Compare.compare(FILTERED, "witness/simple"));
80    }
81  
82    public void ttcc() throws Exception {
83      
84      Layout layout = new TTCCLayout(AbsoluteTimeDateFormat.DATE_AND_TIME_DATE_FORMAT);
85      Appender appender = new FileAppender(layout, "output/ttcc", false);
86      root.addAppender(appender);    
87  
88      String oldName = Thread.currentThread().getName();
89      Thread.currentThread().setName("main");
90      common();
91      Thread.currentThread().setName(oldName);
92  
93      ControlFilter cf1 = new ControlFilter(new String[]{TTCC_PAT, 
94         TTCC2_PAT, EXCEPTION1, EXCEPTION2, 
95         EXCEPTION3, EXCEPTION4, EXCEPTION5 });
96  
97      Transformer.transform(
98        "output/ttcc", FILTERED,
99        new Filter[] {
100         cf1, new LineNumberFilter(), 
101         new AbsoluteDateAndTimeFilter(),
102         new SunReflectFilter(), new JunitTestRunnerFilter()
103       });
104 
105     assertTrue(Compare.compare(FILTERED, "witness/ttcc"));
106   }
107 
108 
109   void common() {
110     
111     int i = 0;
112 
113     // In the lines below, the category names are chosen as an aid in
114     // remembering their level values. In general, the category names
115     // have no bearing to level values.
116     
117     Logger ERR = Logger.getLogger("ERR");
118     ERR.setLevel(Level.ERROR);
119     Logger INF = Logger.getLogger("INF");
120     INF.setLevel(Level.INFO);
121     Logger INF_ERR = Logger.getLogger("INF.ERR");
122     INF_ERR.setLevel(Level.ERROR);
123     Logger DEB = Logger.getLogger("DEB");
124     DEB.setLevel(Level.DEBUG);
125     Logger TRC = Logger.getLogger("TRC");
126     TRC.setLevel(Level.TRACE);
127     
128     // Note: categories with undefined level 
129     Logger INF_UNDEF = Logger.getLogger("INF.UNDEF");
130     Logger INF_ERR_UNDEF = Logger.getLogger("INF.ERR.UNDEF");    
131     Logger UNDEF = Logger.getLogger("UNDEF");   
132 
133 
134     // These should all log.----------------------------
135     ERR.log(Level.FATAL, "Message " + i); i++;  //0
136     ERR.error( "Message " + i); i++;          
137 
138     INF.log(Level.FATAL, "Message " + i); i++; // 2
139     INF.error( "Message " + i); i++;         
140     INF.warn ( "Message " + i); i++; 
141     INF.info ( "Message " + i); i++;
142 
143     INF_UNDEF.log(Level.FATAL, "Message " + i); i++;  //6
144     INF_UNDEF.error( "Message " + i); i++;         
145     INF_UNDEF.warn ( "Message " + i); i++; 
146     INF_UNDEF.info ( "Message " + i); i++; 
147     
148     INF_ERR.log(Level.FATAL, "Message " + i); i++;  // 10
149     INF_ERR.error( "Message " + i); i++;  
150 
151     INF_ERR_UNDEF.log(Level.FATAL, "Message " + i); i++; 
152     INF_ERR_UNDEF.error( "Message " + i); i++;             
153 
154     DEB.log(Level.FATAL, "Message " + i); i++;  //14
155     DEB.error( "Message " + i); i++;         
156     DEB.warn ( "Message " + i); i++; 
157     DEB.info ( "Message " + i); i++; 
158     DEB.debug( "Message " + i); i++;             
159 
160     TRC.log(Level.FATAL, "Message " + i); i++;  //14
161     TRC.error( "Message " + i); i++;         
162     TRC.warn ( "Message " + i); i++; 
163     TRC.info ( "Message " + i); i++; 
164     TRC.debug( "Message " + i); i++; 
165     TRC.trace( "Message " + i); i++; 
166     
167     // defaultLevel=DEBUG
168     UNDEF.log(Level.FATAL, "Message " + i); i++;  // 19
169     UNDEF.error("Message " + i); i++;         
170     UNDEF.warn ("Message " + i); i++; 
171     UNDEF.info ("Message " + i); i++; 
172     UNDEF.debug("Message " + i, new Exception("Just testing."));
173     int printCount = i;
174     i++;
175 
176     // -------------------------------------------------
177     // The following should not log
178     ERR.warn("Message " + i);  i++; 
179     ERR.info("Message " + i);  i++; 
180     ERR.debug("Message " + i);  i++; 
181       
182     INF.debug("Message " + i);  i++; 
183     INF_UNDEF.debug("Message " + i); i++; 
184 
185 
186     INF_ERR.warn("Message " + i);  i++; 
187     INF_ERR.info("Message " + i);  i++; 
188     INF_ERR.debug("Message " + i); i++; 
189     INF_ERR_UNDEF.warn("Message " + i);  i++; 
190     INF_ERR_UNDEF.info("Message " + i);  i++; 
191     INF_ERR_UNDEF.debug("Message " + i); i++; 
192     
193     UNDEF.trace("Message " + i, new Exception("Just testing.")); i++;
194     // -------------------------------------------------
195       
196     INF.info("Messages should bear numbers 0 through "+printCount+".");
197   }
198 
199   public static Test suite() {
200     TestSuite suite = new TestSuite();
201     suite.addTest(new MinimumTestCase("simple"));
202     suite.addTest(new MinimumTestCase("ttcc"));
203     return suite;
204   }
205 
206 }