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  package org.apache.log4j.filter;
18  
19  import junit.framework.TestCase;
20  import org.apache.log4j.Level;
21  import org.apache.log4j.Logger;
22  import org.apache.log4j.spi.Filter;
23  import org.apache.log4j.spi.LoggingEvent;
24  
25  
26  /***
27   * Unit tests for LevelMatchFilter.
28   */
29  public class LevelMatchFilterTest extends TestCase {
30  
31      /***
32       * Create new test instance.
33       *
34       * @param name test name.
35       */
36      public LevelMatchFilterTest(final String name) {
37          super(name);
38      }
39  
40      /***
41       * Check that LevelMatchFilter.decide() returns Filter.ACCEPT when level matches.
42       */
43      public void test1() {
44          LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
45                  Logger.getLogger(LevelMatchFilterTest.class),
46                  System.currentTimeMillis(), Level.INFO, "Hello, World", null);
47          LevelMatchFilter filter = new LevelMatchFilter();
48          filter.setLevelToMatch("info");
49          filter.activateOptions();
50          assertEquals(Filter.ACCEPT, filter.decide(event));
51      }
52  
53      /***
54       * Check that LevelMatchFilter.decide() returns Filter.DENY
55       *    when level matches and acceptOnMatch = false.
56       */
57      public void test2() {
58          LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
59                  Logger.getLogger(LevelMatchFilterTest.class),
60                  System.currentTimeMillis(), Level.INFO, "Hello, World", null);
61          LevelMatchFilter filter = new LevelMatchFilter();
62          filter.setLevelToMatch("info");
63          filter.setAcceptOnMatch(false);
64          filter.activateOptions();
65          assertEquals(Filter.DENY, filter.decide(event));
66      }
67  
68      /***
69       * Check that LevelMatchFilter.decide() returns Filter.NEUTRAL
70       *    when levelToMatch is unspecified.
71       */
72      public void test3() {
73          LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
74                  Logger.getLogger(LevelMatchFilterTest.class),
75                  System.currentTimeMillis(), Level.INFO, "Hello, World", null);
76          LevelMatchFilter filter = new LevelMatchFilter();
77          filter.activateOptions();
78          assertEquals(Filter.NEUTRAL, filter.decide(event));
79      }
80  
81      /***
82       * Check that LevelMatchFilter.decide() returns Filter.NEUTRAL
83       *    when event level is higher than level to match.
84       */
85      public void test4() {
86          LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
87                  Logger.getLogger(LevelMatchFilterTest.class),
88                  System.currentTimeMillis(), Level.INFO, "Hello, World", null);
89          LevelMatchFilter filter = new LevelMatchFilter();
90          filter.setLevelToMatch("debug");
91          filter.activateOptions();
92          assertEquals(Filter.NEUTRAL, filter.decide(event));
93      }
94  
95      /***
96       * Check that LevelMatchFilter.decide() returns Filter.NEUTRAL
97       *    when event level is lower than level to match.
98       */
99      public void test5() {
100         LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
101                 Logger.getLogger(LevelMatchFilterTest.class),
102                 System.currentTimeMillis(), Level.INFO, "Hello, World", null);
103         LevelMatchFilter filter = new LevelMatchFilter();
104         filter.setLevelToMatch("warn");
105         filter.activateOptions();
106         assertEquals(Filter.NEUTRAL, filter.decide(event));
107     }
108 }
109