1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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 /***
35 * JUnit test suite for TextUtils
36 *
37 * @author gojomo
38 * @version $ Id$
39 */
40 public class TextUtilsTest extends TestCase {
41 /***
42 * Create a new TextUtilsTest object
43 *
44 * @param testName
45 * the name of the test
46 */
47 public TextUtilsTest(final String testName) {
48 super(testName);
49 }
50
51 /***
52 * run all the tests for TextUtilsTest
53 *
54 * @param argv
55 * the command line arguments
56 */
57 public static void main(String argv[]) {
58 junit.textui.TestRunner.run(suite());
59 }
60
61 /***
62 * return the suite of tests for MemQueueTest
63 *
64 * @return the suite of test
65 */
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
79 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 final String hexencoded = "aaa
aaa";
114 cs = TextUtils.unescapeHtml(hexencoded);
115 assertEquals(cs, "aaa\naaa");
116 final String zeroPos = "&aaa";
117 cs = TextUtils.unescapeHtml(zeroPos);
118 assertEquals(cs, "&aaa");
119 }
120
121 public void testUnescapeHtmlWithDanglingAmpersand() {
122 final String mixedEncodedAmpersand1 = "aaa&aaa&aaa";
123 CharSequence cs = TextUtils.unescapeHtml(mixedEncodedAmpersand1);
124 assertEquals(cs,"aaa&aaa&aaa");
125 final String mixedEncodedAmpersand2 = "aaa&aaa&aaa&aaa";
126 cs = TextUtils.unescapeHtml(mixedEncodedAmpersand2);
127 assertEquals(cs,"aaa&aaa&aaa&aaa");
128 }
129 }
130