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 LevelInequalityRule.
31   */
32  public class LevelInequalityRuleTest extends TestCase {
33  
34      /***
35       * Create new test.
36       *
37       * @param testName test name.
38       */
39      public LevelInequalityRuleTest(final String testName) {
40          super(testName);
41      }
42  
43      /***
44       * Test construction when level is unrecognized.
45       */
46      public void test1() {
47          try {
48              LevelInequalityRule.getRule(">", "emergency");
49              fail("Expected IllegalArgumentException");
50          } catch(IllegalArgumentException ex) {
51          }
52      }
53  
54      /***
55       * Tests construction when operator is unrecognized.
56       */
57      public void test2() {
58          Rule rule = LevelInequalityRule.getRule("~", "iNfO");
59          assertNull(rule);
60      }
61  
62      /***
63       * Tests evaluate of a deserialized clone when level satisfies rule.
64       */
65      public void test3() throws IOException, ClassNotFoundException {
66          Rule rule = (Rule)
67                  SerializationTestHelper.serializeClone(LevelInequalityRule.getRule(">=", "info"));
68          LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
69                  Logger.getRootLogger(), System.currentTimeMillis(), Level.INFO,
70                  "Hello, World", null);
71          assertTrue(rule.evaluate(event, null));
72      }
73  
74      /***
75       * Tests evaluate of a deserialized clone when level does not satisfy rule.
76       */
77      public void test4() throws IOException, ClassNotFoundException {
78          Rule rule = (Rule)
79                  SerializationTestHelper.serializeClone(LevelInequalityRule.getRule("<", "info"));
80          LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
81                  Logger.getRootLogger(), System.currentTimeMillis(), Level.INFO,
82                  "Hello, World", null);
83          assertFalse(rule.evaluate(event, null));
84      }
85  
86      /***
87       * Tests evaluate when levels are JDK 1.4 levels and satisified.
88       */
89      public void test5() {
90          Rule rule = (Rule) LevelInequalityRule.getRule(">=", "finer");
91          LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
92                  Logger.getRootLogger(), System.currentTimeMillis(), UtilLoggingLevel.FINER,
93                  "Hello, World", null);
94          assertTrue(rule.evaluate(event, null));
95      }
96  
97      /***
98       * Tests evaluate when levels are JDK 1.4 levels and not equal.
99       */
100     public void test6() {
101         Rule rule = (Rule) LevelInequalityRule.getRule("<", "finer");
102         LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
103                 Logger.getRootLogger(), System.currentTimeMillis(), UtilLoggingLevel.FINER,
104                 "Hello, World", null);
105         assertFalse(rule.evaluate(event, null));
106     }
107 
108     /***
109      * Tests evaluate of a deserialized clone when levels are JDK 1.4 levels and satisified.
110      */
111     public void test7() throws IOException, ClassNotFoundException {
112         Rule rule = (Rule)
113                 SerializationTestHelper.serializeClone(LevelInequalityRule.getRule(">=", "finer"));
114         LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
115                 Logger.getRootLogger(), System.currentTimeMillis(), UtilLoggingLevel.FINER,
116                 "Hello, World", null);
117         assertTrue(rule.evaluate(event, null));
118     }
119 
120     /***
121      * Tests evaluate of a deserialized clone when levels are JDK 1.4 levels and not satified.
122      */
123     public void test8() throws IOException, ClassNotFoundException {
124         Rule rule = (Rule)
125                 SerializationTestHelper.serializeClone(LevelInequalityRule.getRule("<", "finer"));
126         LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
127                 Logger.getRootLogger(), System.currentTimeMillis(), UtilLoggingLevel.FINER,
128                 "Hello, World", null);
129         assertFalse(rule.evaluate(event, null));
130     }
131 
132 }