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.spi.LoggingEvent;
24  import org.apache.log4j.util.SerializationTestHelper;
25  
26  import java.io.IOException;
27  import java.util.Calendar;
28  import java.util.GregorianCalendar;
29  import java.util.Stack;
30  
31  /***
32   * Test for NotRule.
33   */
34  public class NotRuleTest extends TestCase {
35  
36      /***
37       * Create new test.
38       *
39       * @param testName test name.
40       */
41      public NotRuleTest(final String testName) {
42          super(testName);
43      }
44  
45      /***
46       * NotRule.getRule(Stack) throws exception if only one rule provided.
47       */
48      public void test1() {
49          Stack stack = new Stack();
50          try {
51              NotRule.getRule(stack);
52              fail("Should have thrown IllegalArgumentException");
53          } catch (IllegalArgumentException ex) {
54          }
55      }
56  
57      /***
58       * NotRule.getRule(Stack) throws exception if non-rules are provided.
59       */
60      public void test2() {
61          Stack stack = new Stack();
62          stack.push("Hello");
63          try {
64              NotRule.getRule(stack);
65              fail("Should have thrown IllegalArgumentException");
66          } catch (IllegalArgumentException ex) {
67          }
68      }
69  
70      /***
71       * Test Not of LevelEqualsRule.
72       */
73      public void test3() {
74          Stack stack = new Stack();
75          stack.push(LevelEqualsRule.getRule("INFO"));
76          Rule rule = NotRule.getRule(stack);
77          assertEquals(0, stack.size());
78          Calendar cal = new GregorianCalendar(2008, 04, 21, 00, 45, 44);
79          LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
80                  Logger.getRootLogger(), cal.getTimeInMillis(), Level.INFO,
81                  "Hello, World", null);
82          assertFalse(rule.evaluate(event, null));
83      }
84  
85      /***
86       * Test Not of Level when Level does not match.
87       */
88      public void test4() {
89          Stack stack = new Stack();
90          stack.push(LevelEqualsRule.getRule("INFO"));
91          Rule rule = NotRule.getRule(stack);
92          assertEquals(0, stack.size());
93          Calendar cal = new GregorianCalendar(2008, 04, 21, 00, 45, 44);
94          LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
95                  Logger.getRootLogger(), cal.getTimeInMillis(), Level.WARN,
96                  "Hello, World", null);
97          assertTrue(rule.evaluate(event, null));
98      }
99  
100 
101     /***
102      * Test deserialized Not.
103      */
104     public void test5() throws IOException, ClassNotFoundException {
105         Stack stack = new Stack();
106         stack.push(LevelEqualsRule.getRule("INFO"));
107         Rule rule = (Rule) SerializationTestHelper.serializeClone(NotRule.getRule(stack));
108         assertEquals(0, stack.size());
109         Calendar cal = new GregorianCalendar(2008, 04, 21, 00, 45, 44);
110         LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
111                 Logger.getRootLogger(), cal.getTimeInMillis(), Level.INFO,
112                 "Hello, World", null);
113         assertFalse(rule.evaluate(event, null));
114     }
115 
116 
117     /***
118      * Test deserialized Not.
119      */
120     public void test6() throws IOException, ClassNotFoundException {
121         Stack stack = new Stack();
122         stack.push(LevelEqualsRule.getRule("INFO"));
123         Rule rule = (Rule) SerializationTestHelper.serializeClone(NotRule.getRule(stack));
124         assertEquals(0, stack.size());
125         Calendar cal = new GregorianCalendar(2008, 04, 21, 00, 45, 44);
126         LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
127                 Logger.getRootLogger(), cal.getTimeInMillis(), Level.WARN,
128                 "Hello, World", null);
129         assertTrue(rule.evaluate(event, null));
130     }
131 
132 }