001: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
002: // Released under the terms of the GNU General Public License version 2 or later.
003: package fit;
004:
005: import junit.framework.TestCase;
006: import fit.exception.FitParseException;
007:
008: public class ParseTest extends TestCase {
009: public void testParsing() throws Exception {
010: Parse p = new Parse("leader<Table foo=2>body</table>trailer",
011: new String[] { "table" });
012: assertEquals("leader", p.leader);
013: assertEquals("<Table foo=2>", p.tag);
014: assertEquals("body", p.body);
015: assertEquals("trailer", p.trailer);
016: }
017:
018: public void testRecursing() throws Exception {
019: Parse p = new Parse(
020: "leader<table><TR><Td>body</tD></TR></table>trailer");
021: assertEquals(null, p.body);
022: assertEquals(null, p.parts.body);
023: assertEquals("body", p.parts.parts.body);
024: }
025:
026: public void testIterating() throws Exception {
027: Parse p = new Parse(
028: "leader<table><tr><td>one</td><td>two</td><td>three</td></tr></table>trailer");
029: assertEquals("one", p.parts.parts.body);
030: assertEquals("two", p.parts.parts.more.body);
031: assertEquals("three", p.parts.parts.more.more.body);
032: }
033:
034: public void testIndexing() throws Exception {
035: Parse p = new Parse(
036: "leader<table><tr><td>one</td><td>two</td><td>three</td></tr><tr><td>four</td></tr></table>trailer");
037: assertEquals("one", p.at(0, 0, 0).body);
038: assertEquals("two", p.at(0, 0, 1).body);
039: assertEquals("three", p.at(0, 0, 2).body);
040: assertEquals("three", p.at(0, 0, 3).body);
041: assertEquals("three", p.at(0, 0, 4).body);
042: assertEquals("four", p.at(0, 1, 0).body);
043: assertEquals("four", p.at(0, 1, 1).body);
044: assertEquals("four", p.at(0, 2, 0).body);
045: assertEquals(1, p.size());
046: assertEquals(2, p.parts.size());
047: assertEquals(3, p.parts.parts.size());
048: assertEquals("one", p.leaf().body);
049: assertEquals("four", p.parts.last().leaf().body);
050: }
051:
052: public void testParseException() {
053: try {
054: new Parse(
055: "leader<table><tr><th>one</th><th>two</th><th>three</th></tr><tr><td>four</td></tr></table>trailer");
056: } catch (FitParseException e) {
057: assertEquals(17, e.getErrorOffset());
058: assertEquals("Can't find tag: td", e.getMessage());
059: return;
060: }
061: fail("exptected exception not thrown");
062: }
063:
064: public void testText() throws Exception {
065: String tags[] = { "td" };
066: Parse p = new Parse("<td>a<b</td>", tags);
067: assertEquals("a<b", p.body);
068: assertEquals("a<b", p.text());
069: p = new Parse("<td>\ta>b & b>c && </td>",
070: tags);
071: assertEquals("a>b & b>c &&", p.text());
072: p = new Parse("<td>\ta>b & b>c & </td>",
073: tags);
074: assertEquals("a>b & b>c &", p.text());
075: p = new Parse(
076: "<TD><P><FONT FACE=\"Arial\" SIZE=2>GroupTestFixture</FONT></TD>",
077: tags);
078: assertEquals("GroupTestFixture", p.text());
079: }
080:
081: public void testUnescape() {
082: assertEquals("a<b", Parse.unescape("a<b"));
083: assertEquals("a>b & b>c &&", Parse
084: .unescape("a>b & b>c &&"));
085: assertEquals("&&", Parse.unescape("&amp;&amp;"));
086: assertEquals("a>b & b>c &&", Parse
087: .unescape("a>b & b>c &&"));
088: }
089:
090: public void testUnformat() {
091: assertEquals("ab", Parse.unformat("<font size=+1>a</font>b"));
092: assertEquals("ab", Parse.unformat("a<font size=+1>b</font>"));
093: assertEquals("a<b", Parse.unformat("a<b"));
094: }
095:
096: public void testFindNestedEnd() throws FitParseException {
097: assertEquals(0, Parse.findMatchingEndTag("</t>", 0, "t", 0));
098: assertEquals(7, Parse.findMatchingEndTag("<t></t></t>", 0, "t",
099: 0));
100: assertEquals(14, Parse.findMatchingEndTag("<t></t><t></t></t>",
101: 0, "t", 0));
102: }
103:
104: public void testNestedTables() throws Exception {
105: String nestedTable = "<table><tr><td>embedded</td></tr></table>";
106: Parse p = new Parse(
107: "<table><tr><td>"
108: + nestedTable
109: + "</td></tr>"
110: + "<tr><td>two</td></tr><tr><td>three</td></tr></table>trailer");
111: Parse sub = p.at(0, 0, 0).parts;
112: assertEquals(1, p.size());
113: assertEquals(3, p.parts.size());
114:
115: assertEquals(1, sub.at(0, 0, 0).size());
116: assertEquals("embedded", sub.at(0, 0, 0).body);
117: assertEquals(1, sub.size());
118: assertEquals(1, sub.parts.size());
119: assertEquals(1, sub.parts.parts.size());
120:
121: assertEquals("two", p.at(0, 1, 0).body);
122: assertEquals("three", p.at(0, 2, 0).body);
123: assertEquals(1, p.at(0, 1, 0).size());
124: assertEquals(1, p.at(0, 2, 0).size());
125: }
126:
127: public void testNestedTables2() throws Exception {
128: String nestedTable = "<table><tr><td>embedded</td></tr></table>";
129: String nestedTable2 = "<table><tr><td>" + nestedTable
130: + "</td></tr><tr><td>two</td></tr></table>";
131: Parse p = new Parse("<table><tr><td>one</td></tr><tr><td>"
132: + nestedTable2 + "</td></tr>"
133: + "<tr><td>three</td></tr></table>trailer");
134:
135: assertEquals(1, p.size());
136: assertEquals(3, p.parts.size());
137:
138: assertEquals("one", p.at(0, 0, 0).body);
139: assertEquals("three", p.at(0, 2, 0).body);
140: assertEquals(1, p.at(0, 0, 0).size());
141: assertEquals(1, p.at(0, 2, 0).size());
142:
143: Parse sub = p.at(0, 1, 0).parts;
144: assertEquals(2, sub.parts.size());
145: assertEquals(1, sub.at(0, 0, 0).size());
146: Parse subSub = sub.at(0, 0, 0).parts;
147:
148: assertEquals("embedded", subSub.at(0, 0, 0).body);
149: assertEquals(1, subSub.at(0, 0, 0).size());
150:
151: assertEquals("two", sub.at(0, 1, 0).body);
152: assertEquals(1, sub.at(0, 1, 0).size());
153: }
154:
155: }
|