1 25 26 package org.archive.util; 27 28 import java.util.regex.Matcher ; 29 30 import junit.framework.Test; 31 import junit.framework.TestCase; 32 import junit.framework.TestSuite; 33 34 40 public class TextUtilsTest extends TestCase { 41 47 public TextUtilsTest(final String testName) { 48 super(testName); 49 } 50 51 57 public static void main(String argv[]) { 58 junit.textui.TestRunner.run(suite()); 59 } 60 61 66 public static Test suite() { 67 return new TestSuite(TextUtilsTest.class); 68 } 69 70 public void testMatcherRecycling() { 71 String pattern = "f.*"; 72 Matcher m1 = TextUtils.getMatcher(pattern,"foo"); 73 assertTrue("matcher against 'foo' problem", m1.matches()); 74 TextUtils.recycleMatcher(m1); 75 Matcher m2 = TextUtils.getMatcher(pattern,""); 76 assertFalse("matcher against '' problem", m2.matches()); 77 assertTrue("matcher not recycled",m1==m2); 78 Matcher m3 = TextUtils.getMatcher(pattern,"fuggedaboutit"); 80 assertTrue("matcher against 'fuggedaboutit' problem",m3.matches()); 81 assertFalse("matcher was recycled",m3==m2); 82 } 83 84 public void testGetFirstWord() { 85 final String firstWord = "one"; 86 String tmpStr = TextUtils.getFirstWord(firstWord + " two three"); 87 assertTrue("Failed to get first word 1 " + tmpStr, 88 tmpStr.equals(firstWord)); 89 tmpStr = TextUtils.getFirstWord(firstWord); 90 assertTrue("Failed to get first word 2 " + tmpStr, 91 tmpStr.equals(firstWord)); 92 } 93 94 public void testUnescapeHtml() { 95 final String abc = "abc"; 96 CharSequence cs = TextUtils.unescapeHtml("abc"); 97 assertEquals(cs, abc); 98 final String backwards = "aaa;lt&aaa"; 99 cs = TextUtils.unescapeHtml(backwards); 100 assertEquals(cs, backwards); 101 final String ampersand = "aaa&aaa"; 102 cs = TextUtils.unescapeHtml(ampersand); 103 assertEquals(cs, ampersand); 104 final String encodedAmpersand = "aaa&aaa"; 105 cs = TextUtils.unescapeHtml(encodedAmpersand); 106 assertEquals(cs, ampersand); 107 final String encodedQuote = "aaa'aaa"; 108 cs = TextUtils.unescapeHtml(encodedQuote); 109 assertEquals(cs, "aaa'aaa"); 110 final String entityQuote = "aaa"aaa"; 111 cs = TextUtils.unescapeHtml(entityQuote); 112 assertEquals(cs, "aaa\"aaa"); 113 } 114 } 115 116 | Popular Tags |