1 19 20 33 package org.htmlparser.tests.scannersTests; 34 35 import org.htmlparser.Node; 36 import org.htmlparser.Parser; 37 import org.htmlparser.scanners.TableScanner; 38 import org.htmlparser.tags.TableColumn; 39 import org.htmlparser.tags.TableRow; 40 import org.htmlparser.tags.TableTag; 41 import org.htmlparser.tests.ParserTestCase; 42 import org.htmlparser.util.NodeIterator; 43 import org.htmlparser.util.ParserException; 44 45 public class TableScannerTest extends ParserTestCase 46 { 47 48 public TableScannerTest(String name) 49 { 50 super(name); 51 } 52 53 private String createHtmlWithTable() 54 { 55 return "<table width=\"100.0%\" align=\"Center\" cellpadding=\"5.0\" cellspacing=\"0.0\" border=\"0.0\">" 56 + " <tr>" 57 + " <td border=\"0.0\" valign=\"Top\" colspan=\"5\">" 58 + " <img SRC=\"file:/c:/data/dev/eclipse_workspace/ShoppingServerTests/resources/pictures/fishbowl.jpg\" width=\"446.0\" height=\"335.0\" />" 59 + " </td>" 60 + " <td border=\"0.0\" align=\"Justify\" valign=\"Top\" colspan=\"7\">" 61 + " <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>" 62 + " </td>" 63 + " </tr>" 64 + "</table>"; 65 } 66 67 public void testScan() throws Exception 68 { 69 createParser(createHtmlWithTable()); 70 parser.addScanner(new TableScanner(parser)); 71 parseAndAssertNodeCount(1); 72 assertTrue(node[0] instanceof TableTag); 73 TableTag tableTag = (TableTag) node[0]; 74 assertEquals("rows", 1, tableTag.getRowCount()); 75 TableRow row = tableTag.getRow(0); 76 assertEquals("columns in row 1", 2, row.getColumnCount()); 77 assertEquals("table width", "100.0%", tableTag.getAttribute("WIDTH")); 78 } 79 80 public void testErroneousTables() throws ParserException 81 { 82 createParser( 83 "<HTML>\n" 84 + "<table border>\n" 85 + "<tr>\n" 86 + "<td>Head1</td>\n" 87 + "<td>Val1</td>\n" 88 + "</tr>\n" 89 + "<tr>\n" 90 + "<td>Head2</td>\n" 91 + "<td>Val2</td>\n" 92 + "</tr>\n" 93 + "<tr>\n" 94 + "<td>\n" 95 + "<table border>\n" 96 + "<tr>\n" 97 + "<td>table2 Head1</td>\n" 98 + "<td>table2 Val1</td>\n" 99 + "</tr>\n" 100 + "</table>\n" 101 + "</td>\n" 102 + "</tr>\n" 103 + "</BODY>\n" 104 + "</HTML>"); 105 parser.registerScanners(); 106 parseAndAssertNodeCount(4); 107 assertType("second tag", TableTag.class, node[1]); 108 TableTag table = (TableTag) node[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(0); 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 if (1.4 <= Parser.getVersionNumber()) 133 { 134 parser.registerScanners(); 135 for (NodeIterator e = parser.elements(); e.hasMoreNodes();) 136 e.nextNode(); 137 assertTrue("Crash", true); 140 } 141 } 142 143 146 public void testUnClosed1() throws ParserException 147 { 148 createParser("<TABLE><TR><TR></TR></TABLE>"); 149 parser.registerScanners(); 150 parseAndAssertNodeCount(1); 151 String s = node[0].toHtml(); 152 assertEquals("Unclosed", "<TABLE><TR></TR><TR></TR></TABLE>", s); 153 } 154 155 158 public void testUnClosed2() throws ParserException 159 { 160 createParser("<TABLE><TR><TD><TD></TD></TR></TABLE>"); 161 parser.registerScanners(); 162 parseAndAssertNodeCount(1); 163 String s = node[0].toHtml(); 164 assertEquals( 165 "Unclosed", 166 "<TABLE><TR><TD></TD><TD></TD></TR></TABLE>", 167 s); 168 } 169 170 173 public void testUnClosed3() throws ParserException 174 { 175 createParser("<TABLE><TR><TD>blah blah</TD><TR><TD>blah blah</TD></TR></TABLE>"); 176 parser.registerScanners(); 177 parseAndAssertNodeCount(1); 178 String s = node[0].toHtml(); 179 assertEquals( 180 "Unclosed", 181 "<TABLE><TR><TD>blah blah</TD></TR><TR><TD>blah blah</TD></TR></TABLE>", 182 s); 183 } 184 } 185 | Popular Tags |