1 27 package org.htmlparser.tests.tagTests; 28 29 import org.htmlparser.Node; 30 import org.htmlparser.Parser; 31 import org.htmlparser.tags.Html; 32 import org.htmlparser.tags.TableColumn; 33 import org.htmlparser.tags.TableRow; 34 import org.htmlparser.tags.TableTag; 35 import org.htmlparser.tests.ParserTestCase; 36 import org.htmlparser.util.NodeIterator; 37 import org.htmlparser.util.ParserException; 38 39 public class TableTagTest extends ParserTestCase 40 { 41 static 42 { 43 System.setProperty ("org.htmlparser.tests.tagTests.TableTagTest", "TableTagTest"); 44 } 45 46 47 public TableTagTest (String name) 48 { 49 super(name); 50 } 51 52 private String createHtmlWithTable() { 53 return 54 "<table width=\"100.0%\" align=\"Center\" cellpadding=\"5.0\" cellspacing=\"0.0\" border=\"0.0\">"+ 55 " <tr>" + 56 " <td border=\"0.0\" valign=\"Top\" colspan=\"5\">" + 57 " <img SRC=\"file:/c:/data/dev/eclipse_workspace/ShoppingServerTests/resources/pictures/fishbowl.jpg\" width=\"446.0\" height=\"335.0\" />" + 58 " </td>" + 59 " <td border=\"0.0\" align=\"Justify\" valign=\"Top\" colspan=\"7\">" + 60 " <span>The best way to improve your refactoring skills is to practice cleaning up poorly-designed code. And we've got just the thing: code we custom-designed to reek of over 90% of the code smells identified in the refactoring literature. This poorly designed code functions correctly, which you can verify by running a full suite of tests against it. Your challenge is to identify the smells in this code, determining which refactoring(s) can help you clean up the smells and implement the refactorings to arrive at a well-designed new version of the code that continues to pass its unit tests. This exercise takes place using our popular class fishbowl. There is a lot to learn from this challenge, so we recommend that you spend as much time on it as possible.
<br /></span>" + 61 " </td>" + 62 " </tr>" + 63 "</table>"; 64 } 65 66 public void testScan() throws ParserException 67 { 68 createParser(createHtmlWithTable()); 69 parseAndAssertNodeCount(1); 70 assertTrue(node[0] instanceof TableTag); 71 TableTag tableTag = (TableTag)node[0]; 72 assertEquals("rows",1,tableTag.getRowCount()); 73 TableRow row = tableTag.getRow(0); 74 assertEquals("columns in row 1",2,row.getColumnCount()); 75 assertEquals("table width","100.0%",tableTag.getAttribute("WIDTH")); 76 } 77 78 public void testErroneousTables() throws ParserException { 79 createParser( 80 "<HTML>\n"+ 81 "<table border>\n"+ 82 "<tr>\n"+ 83 "<td>Head1</td>\n"+ 84 "<td>Val1</td>\n"+ 85 "</tr>\n"+ 86 "<tr>\n"+ 87 "<td>Head2</td>\n"+ 88 "<td>Val2</td>\n"+ 89 "</tr>\n"+ 90 "<tr>\n"+ 91 "<td>\n"+ 92 "<table border>\n"+ 93 "<tr>\n"+ 94 "<td>table2 Head1</td>\n"+ 95 "<td>table2 Val1</td>\n"+ 96 "</tr>\n"+ 97 "</table>\n"+ 98 "</td>\n"+ 99 "</tr>\n"+ 100 "</BODY>\n"+ 101 "</HTML>" 102 ); 103 parseAndAssertNodeCount(1); 104 assertType("only tag should be a HTML tag", Html.class,node[0]); 105 Html html = (Html)node[0]; 106 assertEquals("html tag should have 4 children", 4, html.getChildCount ()); 107 assertType("second tag",TableTag.class,html.getChild (1)); 108 TableTag table = (TableTag)html.getChild (1); 109 assertEquals("rows",3,table.getRowCount()); 110 TableRow tr = table.getRow(2); 111 assertEquals("columns",1,tr.getColumnCount()); 112 TableColumn td = tr.getColumns()[0]; 113 Node node = td.childAt(1); 114 assertType("node",TableTag.class,node); 115 TableTag table2 = (TableTag)node; 116 assertEquals("second table row count",1,table2.getRowCount()); 117 tr = table2.getRow(0); 118 assertEquals("second table col count",2,tr.getColumnCount()); 119 } 120 121 126 public void testRecursionDepth () throws ParserException 127 { 128 Parser parser; 129 String url = "http://htmlparser.sourceforge.net/test/badtable2.html"; 130 131 parser = new Parser (url); 132 for (NodeIterator e = parser.elements();e.hasMoreNodes();) 133 e.nextNode(); 134 assertTrue ("Crash", true); 137 } 138 139 142 public void testUnClosed1 () throws ParserException 143 { 144 createParser ("<TABLE><TR><TR></TR></TABLE>"); 145 parseAndAssertNodeCount (1); 146 String s = node[0].toHtml (); 147 assertEquals ("Unclosed","<TABLE><TR></TR><TR></TR></TABLE>",s); 148 } 149 150 153 public void testUnClosed2 () throws ParserException 154 { 155 createParser ("<TABLE><TR><TD><TD></TD></TR></TABLE>"); 156 parseAndAssertNodeCount (1); 157 String s = node[0].toHtml (); 158 assertEquals ("Unclosed","<TABLE><TR><TD></TD><TD></TD></TR></TABLE>",s); 159 } 160 161 164 public void testUnClosed3 () throws ParserException 165 { 166 createParser ("<TABLE><TR><TD>blah blah</TD><TR><TD>blah blah</TD></TR></TABLE>"); 167 parseAndAssertNodeCount (1); 168 String s = node[0].toHtml (); 169 assertEquals ("Unclosed","<TABLE><TR><TD>blah blah</TD></TR><TR><TD>blah blah</TD></TR></TABLE>",s); 170 } 171 172 176 public void testOverFlow () throws ParserException 177 { 178 parser = 179 new Parser( 180 "http://www.sec.gov/Archives/edgar/data/30554/000089322002000287/w57038e10-k.htm" 181 ); 182 for (NodeIterator e = parser.elements(); e.hasMoreNodes(); ) 183 e.nextNode(); 184 } 185 } 186
| Popular Tags
|