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 EqualsRule.
33   */
34  public class EqualsRuleTest extends TestCase {
35  
36      /***
37       * Create new test.
38       *
39       * @param testName test name.
40       */
41      public EqualsRuleTest(final String testName) {
42          super(testName);
43      }
44  
45      /***
46       * getRule() with only one entry on stack should throw IllegalArgumentException.
47       */
48      public void test1() {
49          Stack stack = new Stack();
50          stack.push("Hello");
51          try {
52              EqualsRule.getRule(stack);
53              fail("Should have thrown IllegalArgumentException");
54          } catch (IllegalArgumentException ex) {
55          }
56      }
57  
58      /***
59       * getRule() with bad field name should throw IllegalArgumentException.
60       */
61      public void test2() {
62          Stack stack = new Stack();
63          stack.push("Hello");
64          stack.push("World");
65          try {
66              EqualsRule.getRule(stack);
67              fail("Should have thrown IllegalArgumentException");
68          } catch (IllegalArgumentException ex) {
69          }
70      }
71  
72      /***
73       * getRule with "level" and "info" should return a LevelEqualsRule.
74       */
75      public void test3() {
76          Stack stack = new Stack();
77          stack.push("level");
78          stack.push("info");
79          Rule rule = EqualsRule.getRule(stack);
80          assertEquals(0, stack.size());
81          assertTrue(rule instanceof LevelEqualsRule);
82          LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
83                  Logger.getRootLogger(), System.currentTimeMillis(), Level.INFO,
84                  "Hello, World", null);
85          assertTrue(rule.evaluate(event, null));
86      }
87  
88      /***
89       * getRule with "timestamp" and time should return a TimestampEqualsRule.
90       */
91      public void test4() {
92          Stack stack = new Stack();
93          stack.push("timestamp");
94          stack.push("2008/05/21 00:45:44");
95          Rule rule = EqualsRule.getRule(stack);
96          assertEquals(0, stack.size());
97          assertTrue(rule instanceof TimestampEqualsRule);
98          Calendar cal = new GregorianCalendar(2008, 04, 21, 00, 45, 44);
99          LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
100                 Logger.getRootLogger(), cal.getTimeInMillis(), Level.INFO,
101                 "Hello, World", null);
102         assertTrue(rule.evaluate(event, null));
103     }
104 
105     /***
106      * getRule with "msg" should return an EqualsRule.
107      */
108     public void test5() {
109         Stack stack = new Stack();
110         stack.push("msg");
111         stack.push("Hello, World");
112         Rule rule = EqualsRule.getRule(stack);
113         assertEquals(0, stack.size());
114         LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
115                 Logger.getRootLogger(), System.currentTimeMillis(), Level.INFO,
116                 "Hello, World", null);
117         assertTrue(rule.evaluate(event, null));
118     }
119 
120     /***
121      * getRule with "msg" should return an EqualsRule.
122      */
123     public void test6() {
124         Stack stack = new Stack();
125         stack.push("msg");
126         stack.push("Bonjour, Monde");
127         Rule rule = EqualsRule.getRule(stack);
128         assertEquals(0, stack.size());
129         LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
130                 Logger.getRootLogger(), System.currentTimeMillis(), Level.INFO,
131                 "Hello, World", null);
132         assertFalse(rule.evaluate(event, null));
133     }
134 
135     /***
136      * Check EqualsRule serialization.
137      */
138     public void test7() throws IOException, ClassNotFoundException {
139         Stack stack = new Stack();
140         stack.push("msg");
141         stack.push("Hello, World");
142         Rule rule = (Rule) SerializationTestHelper.serializeClone(EqualsRule.getRule(stack));
143         assertEquals(0, stack.size());
144         LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
145                 Logger.getRootLogger(), System.currentTimeMillis(), Level.INFO,
146                 "Hello, World", null);
147         assertTrue(rule.evaluate(event, null));
148     }
149 
150 }