1 package org.jivesoftware.messenger.plugin; 2 3 import java.util.regex.PatternSyntaxException ; 4 5 import junit.framework.TestCase; 6 7 import org.xmpp.packet.Message; 8 9 14 public class ContentFilterTest extends TestCase 15 { 16 private ContentFilter filter; 17 18 public static void main(String [] args) 19 { 20 junit.textui.TestRunner.run(ContentFilterTest.class); 21 } 22 23 26 protected void setUp() throws Exception 27 { 28 filter = new ContentFilter(); 29 } 30 31 34 protected void tearDown() throws Exception 35 { 36 super.tearDown(); 37 } 38 39 public void testSetInvalidPatterns() 40 { 41 try 42 { 43 filter.setPatterns("$*["); 44 } 45 catch (PatternSyntaxException 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 assertFalse(matched); 67 68 assertEquals(new Message().toXML(), message.toXML()); 70 } 71 72 public void testFilterMessageSubject() 73 { 74 filter.setPatterns("fox"); 76 77 Message message = new Message(); 79 message.setSubject("the quick brown fox jumped over the lazy dog"); 80 boolean matched = filter.filter(message); 81 82 assertTrue(matched); 84 85 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.setPatterns("fox"); 96 97 filter.setMask("**"); 99 100 Message message = new Message(); 102 message.setSubject("the quick brown fox jumped over the lazy dog"); 103 boolean matched = filter.filter(message); 104 105 assertTrue(matched); 107 108 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.setPatterns("fox"); 120 121 Message message = new Message(); 123 message.setBody("the quick brown fox jumped over the lazy dog"); 124 boolean matched = filter.filter(message); 125 126 assertTrue(matched); 128 129 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.setPatterns("fox,dog"); 140 filter.setMask("**"); 141 142 Message message = new Message(); 144 message.setBody("the quick brown fox jumped over the lazy dog"); 145 boolean matched = filter.filter(message); 146 147 assertTrue(matched); 149 150 assertEquals("the quick brown ** jumped over the lazy **", message 152 .getBody()); 153 assertNull(message.getSubject()); 154 155 } 156 } | Popular Tags |