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  
30  /***
31   * Test for TimestampInequalityRule.
32   */
33  public class TimestampInequalityRuleTest extends TestCase {
34  
35      /***
36       * Create new test.
37       *
38       * @param testName test name.
39       */
40      public TimestampInequalityRuleTest(final String testName) {
41          super(testName);
42      }
43  
44      /***
45       * Test construction when timestamp is unrecognized.
46       */
47      public void test1() {
48          try {
49              TimestampInequalityRule.getRule(">", "now");
50              fail("Expected IllegalArgumentException");
51          } catch(IllegalArgumentException ex) {
52          }
53      }
54  
55      /***
56       * Tests construction when operator is unrecognized.
57       */
58      public void test2() {
59          //
60          //   unlike LevelInequalityRule, does not throw exception.  Resulting rule never satisified.
61          //
62          TimestampInequalityRule.getRule("~", "2008/05/21 00:45:46");
63      }
64  
65      /***
66       * Tests evaluate of a deserialized clone when rule is satisified.
67       */
68      public void test3() throws IOException, ClassNotFoundException {
69          Rule rule = (Rule)
70                  SerializationTestHelper.serializeClone(TimestampInequalityRule.getRule(">=", "2008/05/21 00:44:45"));
71          Calendar cal = new GregorianCalendar(2008,04,21,00,45,44);
72          LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
73                  Logger.getRootLogger(), cal.getTimeInMillis(), Level.INFO,
74                  "Hello, World", null);
75          assertTrue(rule.evaluate(event, null));
76      }
77  
78      /***
79       * Tests evaluate of a deserialized clone when rule is not satisfied.
80       */
81      public void test4() throws IOException, ClassNotFoundException {
82          Rule rule = (Rule)
83                  SerializationTestHelper.serializeClone(TimestampInequalityRule.getRule("<", "2008/05/21 00:44:44"));
84          Calendar cal = new GregorianCalendar(2008,04,21,00,45,44);
85          LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
86                  Logger.getRootLogger(), cal.getTimeInMillis(), Level.WARN,
87                  "Hello, World", null);
88          assertFalse(rule.evaluate(event, null));
89      }
90  
91  }