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.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 }