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.customLogger;
19  
20  import org.apache.log4j.xml.DOMConfigurator;
21  import org.apache.log4j.Logger;
22  import org.apache.log4j.Level;
23  
24  import org.apache.log4j.util.*;
25  
26  import junit.framework.TestCase;
27  import junit.framework.TestSuite;
28  import junit.framework.Test;
29  
30  /***
31     Tests handling of custom loggers.
32     
33     @author Ceki Gülcü
34  */
35  public class XLoggerTestCase extends TestCase {
36  
37    static String FILTERED = "output/filtered";
38    static XLogger logger = (XLogger) XLogger.getLogger(XLoggerTestCase.class);
39  
40    public XLoggerTestCase(String name){
41      super(name);
42    }
43  
44    public void tearDown() {
45      logger.getLoggerRepository().resetConfiguration();
46    }
47  
48    public void test1()  throws Exception  { common(1); }
49    public void test2()  throws Exception  { common(2); }
50  
51    void common(int number) throws Exception {
52      DOMConfigurator.configure("input/xml/customLogger"+number+".xml");
53  
54      int i = -1;
55      Logger root = Logger.getRootLogger();
56  
57      logger.trace("Message " + ++i);
58      logger.debug("Message " + ++i);
59      logger.warn ("Message " + ++i);
60      logger.error("Message " + ++i);
61      logger.fatal("Message " + ++i);
62      Exception e = new Exception("Just testing");
63      logger.debug("Message " + ++i, e);
64  
65      Transformer.transform(
66        "output/temp", FILTERED,
67        new Filter[] {
68          new LineNumberFilter(), new SunReflectFilter(),
69          new JunitTestRunnerFilter()
70        });
71      assertTrue(Compare.compare(FILTERED, "witness/customLogger."+number));
72  
73    }
74  
75    public static Test suite() {
76      TestSuite suite = new TestSuite();
77      suite.addTest(new XLoggerTestCase("test1"));
78      suite.addTest(new XLoggerTestCase("test2"));
79      return suite;
80    }
81  }