1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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 }