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 NotEqualsRule.
33   */
34  public class NotEqualsRuleTest extends TestCase {
35  
36      /***
37       * Create new test.
38       *
39       * @param testName test name.
40       */
41      public NotEqualsRuleTest(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              NotEqualsRule.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              NotEqualsRule.getRule(stack);
67              fail("Should have thrown IllegalArgumentException");
68          } catch (IllegalArgumentException ex) {
69          }
70      }
71  
72      /***
73       * getRule with "level" and "info".
74       */
75      public void test3() {
76          Stack stack = new Stack();
77          stack.push("level");
78          stack.push("info");
79          Rule rule = NotEqualsRule.getRule(stack);
80          assertEquals(0, stack.size());
81          LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
82                  Logger.getRootLogger(), System.currentTimeMillis(), Level.WARN,
83                  "Hello, World", null);
84          assertTrue(rule.evaluate(event, null));
85      }
86  
87      /***
88       * getRule with "timestamp" and time.
89       */
90      public void test4() {
91          Stack stack = new Stack();
92          stack.push("timestamp");
93          stack.push("2008/05/21 00:45:44");
94          Rule rule = NotEqualsRule.getRule(stack);
95          assertEquals(0, stack.size());
96          Calendar cal = new GregorianCalendar(2009, 04, 21, 00, 45, 44);
97          LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
98                  Logger.getRootLogger(), cal.getTimeInMillis(), Level.INFO,
99                  "Hello, World", null);
100         assertTrue(rule.evaluate(event, null));
101     }
102 
103     /***
104      * getRule with "msg".
105      */
106     public void test5() {
107         Stack stack = new Stack();
108         stack.push("msg");
109         stack.push("Hello, World");
110         Rule rule = NotEqualsRule.getRule(stack);
111         assertEquals(0, stack.size());
112         LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
113                 Logger.getRootLogger(), System.currentTimeMillis(), Level.INFO,
114                 "Hello, World", null);
115         assertFalse(rule.evaluate(event, null));
116     }
117 
118     /***
119      * getRule with "msg".
120      */
121     public void test6() {
122         Stack stack = new Stack();
123         stack.push("msg");
124         stack.push("Bonjour, Monde");
125         Rule rule = NotEqualsRule.getRule(stack);
126         assertEquals(0, stack.size());
127         LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
128                 Logger.getRootLogger(), System.currentTimeMillis(), Level.INFO,
129                 "Hello, World", null);
130         assertTrue(rule.evaluate(event, null));
131     }
132 
133     /***
134      * Check NotEqualsRule serialization.
135      */
136     public void test7() throws IOException, ClassNotFoundException {
137         Stack stack = new Stack();
138         stack.push("msg");
139         stack.push("Hello, World");
140         Rule rule = (Rule) SerializationTestHelper.serializeClone(NotEqualsRule.getRule(stack));
141         assertEquals(0, stack.size());
142         LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
143                 Logger.getRootLogger(), System.currentTimeMillis(), Level.INFO,
144                 "Hello, World", null);
145         assertFalse(rule.evaluate(event, null));
146     }
147 
148 }