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 TimestampEqualsRule.
32 */
33 public class TimestampEqualsRuleTest extends TestCase {
34
35 /***
36 * Create new test.
37 *
38 * @param testName test name.
39 */
40 public TimestampEqualsRuleTest(final String testName) {
41 super(testName);
42 }
43
44 /***
45 * Tests evaluate when timestamps are equal.
46 */
47 public void test1() {
48 TimestampEqualsRule rule = (TimestampEqualsRule) TimestampEqualsRule.getRule("2008/05/21 00:45:44");
49 Calendar cal = new GregorianCalendar(2008,04,21,00,45,44);
50 LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
51 Logger.getRootLogger(), cal.getTimeInMillis(), Level.INFO,
52 "Hello, World", null);
53 assertTrue(rule.evaluate(event, null));
54 }
55
56 /***
57 * Tests evaluate when levels are not equal.
58 */
59 public void test2() {
60 TimestampEqualsRule rule = (TimestampEqualsRule) TimestampEqualsRule.getRule("2008/05/21 00:45:44");
61 Calendar cal = new GregorianCalendar(2008,04,21,00,46,44);
62 LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
63 Logger.getRootLogger(), cal.getTimeInMillis(), Level.WARN,
64 "Hello, World", null);
65 assertFalse(rule.evaluate(event, null));
66 }
67
68 /***
69 * Tests evaluate of a deserialized clone when timestamps are equal.
70 */
71 public void test3() throws IOException, ClassNotFoundException {
72 TimestampEqualsRule rule = (TimestampEqualsRule)
73 SerializationTestHelper.serializeClone(TimestampEqualsRule.getRule("2008/05/21 00:45:44"));
74 Calendar cal = new GregorianCalendar(2008,04,21,00,45,44);
75 LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
76 Logger.getRootLogger(), cal.getTimeInMillis(), Level.INFO,
77 "Hello, World", null);
78 assertTrue(rule.evaluate(event, null));
79 }
80
81 /***
82 * Tests evaluate of a deserialized clone when timestamps are not equal.
83 */
84 public void test4() throws IOException, ClassNotFoundException {
85 TimestampEqualsRule rule = (TimestampEqualsRule)
86 SerializationTestHelper.serializeClone(TimestampEqualsRule.getRule("2008/05/21 00:45:44"));
87 Calendar cal = new GregorianCalendar(2008,04,21,00,46,44);
88 LoggingEvent event = new LoggingEvent("org.apache.log4j.Logger",
89 Logger.getRootLogger(), cal.getTimeInMillis(), Level.INFO,
90 "Hello, World", null);
91 assertFalse(rule.evaluate(event, null));
92 }
93
94 /***
95 * Tests constructor will badly formed time specification.
96 */
97 public void test5() {
98 try {
99 TimestampEqualsRule.getRule("2008/May/21");
100 fail("IllegalArgumentException expected to be thrown");
101 } catch(IllegalArgumentException ex) {
102 }
103 }
104
105 }