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 OrRule.
33   */
34  public class OrRuleTest extends TestCase {
35  
36      /***
37       * Create new test.
38       *
39       * @param testName test name.
40       */
41      public OrRuleTest(final String testName) {
42          super(testName);
43      }
44  
45      /***
46       * OrRule.getRule(Stack) throws exception if only one rule provided.
47       */
48      public void test1() {
49          Stack stack = new Stack();
50          stack.push(LevelEqualsRule.getRule("INFO"));
51          try {
52              OrRule.getRule(stack);
53              fail("Should have thrown IllegalArgumentException");
54          } catch (IllegalArgumentException ex) {
55          }
56      }
57  
58      /***
59       * OrRule.getRule(Stack) throws exception if non-rules are provided.
60       */
61      public void test2() {
62          Stack stack = new Stack();
63          stack.push("Hello");
64          stack.push("World");
65          try {
66              OrRule.getRule(stack);
67              fail("Should have thrown IllegalArgumentException");
68          } catch (IllegalArgumentException ex) {
69          }
70      }
71  
72      /***
73       * Test Or of Level and Time.
74       */
75      public void test3() {
76          Stack stack = new Stack();
77          stack.push(LevelEqualsRule.getRule("INFO"));
78          stack.push(TimestampInequalityRule.getRule(">=", "2008/05/21 00:44:45"));
79          Rule rule = OrRule.getRule(stack);
80          assertEquals(0, stack.size());
81          Calendar cal = new GregorianCalendar(2008, 04, 21, 00, 45, 44);
82          LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
83                  Logger.getRootLogger(), cal.getTimeInMillis(), Level.INFO,
84                  "Hello, World", null);
85          assertTrue(rule.evaluate(event, null));
86      }
87  
88      /***
89       * Test Or of Level and Time when Level does not match.
90       */
91      public void test4() {
92          Stack stack = new Stack();
93          stack.push(LevelEqualsRule.getRule("INFO"));
94          stack.push(TimestampInequalityRule.getRule(">=", "2008/05/21 00:44:45"));
95          Rule rule = OrRule.getRule(stack);
96          assertEquals(0, stack.size());
97          Calendar cal = new GregorianCalendar(2008, 04, 21, 00, 45, 44);
98          LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
99                  Logger.getRootLogger(), cal.getTimeInMillis(), Level.WARN,
100                 "Hello, World", null);
101         assertTrue(rule.evaluate(event, null));
102     }
103 
104     /***
105      * Test Or of Level and Time when Time does not match.
106      */
107     public void test5() {
108         Stack stack = new Stack();
109         stack.push(LevelEqualsRule.getRule("INFO"));
110         stack.push(TimestampInequalityRule.getRule(">=", "2009/05/21 00:44:45"));
111         Rule rule = OrRule.getRule(stack);
112         assertEquals(0, stack.size());
113         Calendar cal = new GregorianCalendar(2008, 04, 21, 00, 45, 44);
114         LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
115                 Logger.getRootLogger(), cal.getTimeInMillis(), Level.INFO,
116                 "Hello, World", null);
117         assertTrue(rule.evaluate(event, null));
118     }
119 
120 
121     /***
122      * Test Or of Level and Time when Time and Level do not match.
123      */
124     public void test6() {
125         Stack stack = new Stack();
126         stack.push(LevelEqualsRule.getRule("INFO"));
127         stack.push(TimestampInequalityRule.getRule(">=", "2009/05/21 00:44:45"));
128         Rule rule = OrRule.getRule(stack);
129         assertEquals(0, stack.size());
130         Calendar cal = new GregorianCalendar(2008, 04, 21, 00, 45, 44);
131         LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
132                 Logger.getRootLogger(), cal.getTimeInMillis(), Level.WARN,
133                 "Hello, World", null);
134         assertFalse(rule.evaluate(event, null));
135     }
136 
137     /***
138      * Test deserialized Or.
139      */
140     public void test7() throws IOException, ClassNotFoundException {
141         Stack stack = new Stack();
142         stack.push(LevelEqualsRule.getRule("INFO"));
143         stack.push(TimestampInequalityRule.getRule(">=", "2008/05/21 00:44:45"));
144         Rule rule = (Rule) SerializationTestHelper.serializeClone(OrRule.getRule(stack));
145         assertEquals(0, stack.size());
146         Calendar cal = new GregorianCalendar(2008, 04, 21, 00, 45, 44);
147         LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
148                 Logger.getRootLogger(), cal.getTimeInMillis(), Level.INFO,
149                 "Hello, World", null);
150         assertTrue(rule.evaluate(event, null));
151     }
152 
153 
154     /***
155      * Test deserialized Or when neither rule match.
156      */
157     public void test8() throws IOException, ClassNotFoundException {
158         Stack stack = new Stack();
159         stack.push(LevelEqualsRule.getRule("INFO"));
160         stack.push(TimestampInequalityRule.getRule(">=", "2009/05/21 00:44:45"));
161         Rule rule = (Rule) SerializationTestHelper.serializeClone(OrRule.getRule(stack));
162         assertEquals(0, stack.size());
163         Calendar cal = new GregorianCalendar(2008, 04, 21, 00, 45, 44);
164         LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
165                 Logger.getRootLogger(), cal.getTimeInMillis(), Level.WARN,
166                 "Hello, World", null);
167         assertFalse(rule.evaluate(event, null));
168     }
169 
170 }