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.rule;
18  
19  
20  import junit.framework.TestCase;
21  import org.apache.log4j.Level;
22  import org.apache.log4j.Logger;
23  import org.apache.log4j.helpers.UtilLoggingLevel;
24  import org.apache.log4j.spi.LoggingEvent;
25  import org.apache.log4j.util.SerializationTestHelper;
26  
27  import java.io.IOException;
28  
29  /***
30   * Test for LevelEqualsRule.
31   */
32  public class LevelEqualsRuleTest extends TestCase {
33  
34      /***
35       * Create new test.
36       *
37       * @param testName test name.
38       */
39      public LevelEqualsRuleTest(final String testName) {
40          super(testName);
41      }
42  
43      /***
44       * Tests evaluate when levels are equal.
45       */
46      public void test1() {
47          LevelEqualsRule rule = (LevelEqualsRule) LevelEqualsRule.getRule("info");
48          LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
49                  Logger.getRootLogger(), System.currentTimeMillis(), Level.INFO,
50                  "Hello, World", null);
51          assertTrue(rule.evaluate(event, null));
52      }
53  
54      /***
55       * Tests evaluate when levels are not equal.
56       */
57      public void test2() {
58          LevelEqualsRule rule = (LevelEqualsRule) LevelEqualsRule.getRule("info");
59          LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
60                  Logger.getRootLogger(), System.currentTimeMillis(), Level.WARN,
61                  "Hello, World", null);
62          assertFalse(rule.evaluate(event, null));
63      }
64  
65      /***
66       * Tests evaluate of a deserialized clone when levels are equal.
67       */
68      public void test3() throws IOException, ClassNotFoundException {
69          LevelEqualsRule rule = (LevelEqualsRule)
70                  SerializationTestHelper.serializeClone(LevelEqualsRule.getRule("info"));
71          LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
72                  Logger.getRootLogger(), System.currentTimeMillis(), Level.INFO,
73                  "Hello, World", null);
74          assertTrue(rule.evaluate(event, null));
75      }
76  
77      /***
78       * Tests evaluate of a deserialized clone when levels are not equal.
79       */
80      public void test4() throws IOException, ClassNotFoundException {
81          LevelEqualsRule rule = (LevelEqualsRule)
82                  SerializationTestHelper.serializeClone(LevelEqualsRule.getRule("info"));
83          LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
84                  Logger.getRootLogger(), System.currentTimeMillis(), Level.WARN,
85                  "Hello, World", null);
86          assertFalse(rule.evaluate(event, null));
87      }
88  
89      /***
90       * Tests evaluate when levels are JDK 1.4 levels and equal.
91       */
92      public void test5() {
93          LevelEqualsRule rule = (LevelEqualsRule) LevelEqualsRule.getRule("finer");
94          LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
95                  Logger.getRootLogger(), System.currentTimeMillis(), UtilLoggingLevel.FINER,
96                  "Hello, World", null);
97          assertTrue(rule.evaluate(event, null));
98      }
99  
100     /***
101      * Tests evaluate when levels are JDK 1.4 levels and not equal.
102      */
103     public void test6() {
104         LevelEqualsRule rule = (LevelEqualsRule) LevelEqualsRule.getRule("finer");
105         LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
106                 Logger.getRootLogger(), System.currentTimeMillis(), UtilLoggingLevel.FINE,
107                 "Hello, World", null);
108         assertFalse(rule.evaluate(event, null));
109     }
110 
111     /***
112      * Tests evaluate of a deserialized clone when levels are JDK 1.4 levels and equal.
113      */
114     public void test7() throws IOException, ClassNotFoundException {
115         LevelEqualsRule rule = (LevelEqualsRule)
116                 SerializationTestHelper.serializeClone(LevelEqualsRule.getRule("finer"));
117         LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
118                 Logger.getRootLogger(), System.currentTimeMillis(), UtilLoggingLevel.FINER,
119                 "Hello, World", null);
120         assertTrue(rule.evaluate(event, null));
121     }
122 
123     /***
124      * Tests evaluate of a deserialized clone when levels are JDK 1.4 levels and not equal.
125      */
126     public void test8() throws IOException, ClassNotFoundException {
127         LevelEqualsRule rule = (LevelEqualsRule)
128                 SerializationTestHelper.serializeClone(LevelEqualsRule.getRule("finer"));
129         LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
130                 Logger.getRootLogger(), System.currentTimeMillis(), UtilLoggingLevel.FINE,
131                 "Hello, World", null);
132         assertFalse(rule.evaluate(event, null));
133     }
134 
135 }