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 ExistsRule.
31   */
32  public class ExistsRuleTest extends TestCase {
33  
34      /***
35       * Create new test.
36       *
37       * @param testName test name.
38       */
39      public ExistsRuleTest(final String testName) {
40          super(testName);
41      }
42  
43      /***
44       * getRule() with no entry on stack should throw IllegalArgumentException.
45       */
46      public void test1() {
47          Stack stack = new Stack();
48          try {
49              ExistsRule.getRule(stack);
50              fail("Should have thrown IllegalArgumentException");
51          } catch (IllegalArgumentException ex) {
52          }
53      }
54  
55      /***
56       * getRule() with bad field name should throw IllegalArgumentException.
57       */
58      public void test2() {
59          Stack stack = new Stack();
60          stack.push("Hello");
61          try {
62              ExistsRule.getRule(stack);
63              fail("Should have thrown IllegalArgumentException");
64          } catch (IllegalArgumentException ex) {
65          }
66      }
67  
68      /***
69       * getRule with "msg".
70       */
71      public void test3() {
72          Stack stack = new Stack();
73          stack.push("msg");
74          Rule rule = ExistsRule.getRule(stack);
75          assertEquals(0, stack.size());
76          LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
77                  Logger.getRootLogger(), System.currentTimeMillis(), Level.INFO,
78                  "Hello, World", null);
79          assertTrue(rule.evaluate(event, null));
80      }
81  
82      /***
83       * getRule with "msg".
84       */
85      public void test4() {
86          Stack stack = new Stack();
87          stack.push("msg");
88          Rule rule = ExistsRule.getRule(stack);
89          assertEquals(0, stack.size());
90          LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
91                  Logger.getRootLogger(), System.currentTimeMillis(), Level.INFO,
92                  "", null);
93          assertFalse(rule.evaluate(event, null));
94      }
95  
96      /***
97       * getRule with "msg".
98       */
99      public void test5() throws IOException, ClassNotFoundException {
100         Stack stack = new Stack();
101         stack.push("msg");
102         Rule rule = (Rule) SerializationTestHelper.serializeClone(ExistsRule.getRule(stack));
103         assertEquals(0, stack.size());
104         LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
105                 Logger.getRootLogger(), System.currentTimeMillis(), Level.INFO,
106                 "Hello, World", null);
107         assertTrue(rule.evaluate(event, null));
108     }
109 
110 }