001: package fit;
002:
003: //Copyright (c) 2002 Cunningham & Cunningham, Inc.
004: //Released under the terms of the GNU General Public License version 2 or later.
005:
006: import junit.framework.TestCase;
007:
008: public class ParseTest extends TestCase {
009:
010: public ParseTest(String name) {
011: super (name);
012: }
013:
014: public void testParsing() throws Exception {
015: Parse p = new Parse("leader<Table foo=2>body</table>trailer",
016: new String[] { "table" });
017: assertEquals("leader", p.leader);
018: assertEquals("<Table foo=2>", p.tag);
019: assertEquals("body", p.body);
020: assertEquals("trailer", p.trailer);
021: }
022:
023: public void testRecursing() throws Exception {
024: Parse p = new Parse(
025: "leader<table><TR><Td>body</tD></TR></table>trailer");
026: assertEquals(null, p.body);
027: assertEquals(null, p.parts.body);
028: assertEquals("body", p.parts.parts.body);
029: }
030:
031: public void testIterating() throws Exception {
032: Parse p = new Parse(
033: "leader<table><tr><td>one</td><td>two</td><td>three</td></tr></table>trailer");
034: assertEquals("one", p.parts.parts.body);
035: assertEquals("two", p.parts.parts.more.body);
036: assertEquals("three", p.parts.parts.more.more.body);
037: }
038:
039: public void testIndexing() throws Exception {
040: Parse p = new Parse(
041: "leader<table><tr><td>one</td><td>two</td><td>three</td></tr><tr><td>four</td></tr></table>trailer");
042: assertEquals("one", p.at(0, 0, 0).body);
043: assertEquals("two", p.at(0, 0, 1).body);
044: assertEquals("three", p.at(0, 0, 2).body);
045: assertEquals("three", p.at(0, 0, 3).body);
046: assertEquals("three", p.at(0, 0, 4).body);
047: assertEquals("four", p.at(0, 1, 0).body);
048: assertEquals("four", p.at(0, 1, 1).body);
049: assertEquals("four", p.at(0, 2, 0).body);
050: assertEquals(1, p.size());
051: assertEquals(2, p.parts.size());
052: assertEquals(3, p.parts.parts.size());
053: assertEquals("one", p.leaf().body);
054: assertEquals("four", p.parts.last().leaf().body);
055: }
056:
057: public void testParseException() {
058: try {
059: Parse p = new Parse(
060: "leader<table><tr><th>one</th><th>two</th><th>three</th></tr><tr><td>four</td></tr></table>trailer");
061: } catch (java.text.ParseException e) {
062: assertEquals(17, e.getErrorOffset());
063: assertEquals("Can't find tag: td", e.getMessage());
064: return;
065: }
066: fail("exptected exception not thrown");
067: }
068:
069: public void testText() throws Exception {
070: String tags[] = { "td" };
071: Parse p = new Parse("<td>a<b</td>", tags);
072: assertEquals("a<b", p.body);
073: assertEquals("a<b", p.text());
074: p = new Parse("<td>\ta>b & b>c &&<</td>",
075: tags);
076: assertEquals("a>b & b>c &&<", p.text());
077: p = new Parse("<td>\ta>b & b>c &<</td>",
078: tags);
079: assertEquals("a>b & b>c &<", p.text());
080: p = new Parse(
081: "<TD><P><FONT FACE=\"Arial\" SIZE=2>GroupTestFixture</FONT></TD>",
082: tags);
083: assertEquals("GroupTestFixture", p.text());
084:
085: assertEquals("", Parse.htmlToText(" "));
086: assertEquals("a b", Parse.htmlToText("a <tag /> b"));
087: assertEquals("a", Parse.htmlToText("a "));
088: assertEquals(" ", Parse.htmlToText("&nbsp;"));
089: assertEquals("1 2", Parse.htmlToText("1 2"));
090: assertEquals("1 2", Parse
091: .htmlToText("1 \u00a0\u00a0\u00a0\u00a02"));
092: assertEquals("a", Parse.htmlToText(" <tag />a"));
093: assertEquals("a\nb", Parse.htmlToText("a<br />b"));
094:
095: assertEquals("ab", Parse.htmlToText("<font size=+1>a</font>b"));
096: assertEquals("ab", Parse.htmlToText("a<font size=+1>b</font>"));
097: assertEquals("a<b", Parse.htmlToText("a<b"));
098:
099: assertEquals("a\nb\nc\nd", Parse
100: .htmlToText("a<br>b<br/>c< br / >d"));
101: assertEquals("a\nb", Parse.htmlToText("a</p><p>b"));
102: assertEquals("a\nb", Parse.htmlToText("a< / p > < p >b"));
103: }
104:
105: public void testUnescape() {
106: assertEquals("a<b", Parse.unescape("a<b"));
107: assertEquals("a>b & b>c &&", Parse
108: .unescape("a>b & b>c &&"));
109: assertEquals("&&", Parse.unescape("&amp;&amp;"));
110: assertEquals("a>b & b>c &&", Parse
111: .unescape("a>b & b>c &&"));
112: assertEquals("\"\"'", Parse.unescape("“”’"));
113: }
114:
115: public void testWhitespaceIsCondensed() {
116: assertEquals("a b", Parse.condenseWhitespace(" a b "));
117: assertEquals("a b", Parse.condenseWhitespace(" a \n\tb "));
118: assertEquals("", Parse.condenseWhitespace(" "));
119: assertEquals("", Parse.condenseWhitespace(" "));
120: assertEquals("", Parse.condenseWhitespace(" "));
121: assertEquals("", Parse.condenseWhitespace(new String(
122: new char[] { (char) 160 })));
123: }
124: }
|