KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > messenger > plugin > ContentFilterTest


1 package org.jivesoftware.messenger.plugin;
2
3 import java.util.regex.PatternSyntaxException JavaDoc;
4
5 import junit.framework.TestCase;
6
7 import org.xmpp.packet.Message;
8
9 /**
10  * Basic unit tests for ContentFilter.
11  *
12  * @author chayes
13  */

14 public class ContentFilterTest extends TestCase
15 {
16   private ContentFilter filter;
17
18   public static void main(String JavaDoc[] args)
19   {
20     junit.textui.TestRunner.run(ContentFilterTest.class);
21   }
22
23   /*
24    * @see TestCase#setUp()
25    */

26   protected void setUp() throws Exception JavaDoc
27   {
28     filter = new ContentFilter();
29   }
30
31   /*
32    * @see TestCase#tearDown()
33    */

34   protected void tearDown() throws Exception JavaDoc
35   {
36     super.tearDown();
37   }
38
39   public void testSetInvalidPatterns()
40   {
41     try
42     {
43       filter.setPatterns("$*[");
44     }
45     catch (PatternSyntaxException JavaDoc e)
46     {
47       System.out.println(e.getMessage());
48       System.out.println(e.getPattern());
49     }
50   }
51   public void testSetContentMask()
52   {
53     assertFalse(filter.isMaskingContent());
54     filter.setMask("dummy");
55     assertTrue(filter.isMaskingContent());
56     filter.clearMask();
57     assertFalse(filter.isMaskingContent());
58   }
59
60   public void testFilterWithEmptyMessage()
61   {
62     Message message = new Message();
63     boolean matched = filter.filter(message);
64
65     // no matches should be found
66
assertFalse(matched);
67
68     // message should not have changed
69
assertEquals(new Message().toXML(), message.toXML());
70   }
71
72   public void testFilterMessageSubject()
73   {
74     // filter on the word fox
75
filter.setPatterns("fox");
76
77     // test message
78
Message message = new Message();
79     message.setSubject("the quick brown fox jumped over the lazy dog");
80     boolean matched = filter.filter(message);
81
82     // matches should be found
83
assertTrue(matched);
84
85     // content has not changed as there is no content mask
86
assertEquals("the quick brown fox jumped over the lazy dog", message
87         .getSubject());
88     assertNull(message.getBody());
89   }
90
91   public void testFilterMessageSubjectWithMask()
92   {
93
94     // filter on the word fox
95
filter.setPatterns("fox");
96     
97     //set a content mask
98
filter.setMask("**");
99
100     // test message
101
Message message = new Message();
102     message.setSubject("the quick brown fox jumped over the lazy dog");
103     boolean matched = filter.filter(message);
104
105     // matches should be found
106
assertTrue(matched);
107
108     // content has changed
109
assertEquals("the quick brown ** jumped over the lazy dog", message
110         .getSubject());
111     assertNull(message.getBody());
112
113   }
114
115   public void testFilterMessageBody()
116   {
117
118     // filter on the word fox
119
filter.setPatterns("fox");
120
121     // test message
122
Message message = new Message();
123     message.setBody("the quick brown fox jumped over the lazy dog");
124     boolean matched = filter.filter(message);
125
126     // matches should be found
127
assertTrue(matched);
128
129     // content has not changed as there is no content mask
130
assertEquals("the quick brown fox jumped over the lazy dog", message
131         .getBody());
132     assertNull(message.getSubject());
133   }
134
135   public void testFilterMessageBodyWithMask()
136   {
137
138     // filter on the word "fox" and "dog"
139
filter.setPatterns("fox,dog");
140     filter.setMask("**");
141
142     // test message
143
Message message = new Message();
144     message.setBody("the quick brown fox jumped over the lazy dog");
145     boolean matched = filter.filter(message);
146
147     // matches should not be found
148
assertTrue(matched);
149
150     // content has changed
151
assertEquals("the quick brown ** jumped over the lazy **", message
152         .getBody());
153     assertNull(message.getSubject());
154
155   }
156 }
Popular Tags