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