1 27 package org.htmlparser.tests.visitorsTests; 28 29 import org.htmlparser.Node; 30 import org.htmlparser.Text; 31 import org.htmlparser.tags.TableColumn; 32 import org.htmlparser.tags.TableRow; 33 import org.htmlparser.tags.TableTag; 34 import org.htmlparser.tests.ParserTestCase; 35 import org.htmlparser.util.NodeList; 36 import org.htmlparser.visitors.HtmlPage; 37 38 public class HtmlPageTest extends ParserTestCase { 39 40 static 41 { 42 System.setProperty ("org.htmlparser.tests.visitorsTests.HtmlPageTest", "HtmlPageTest"); 43 } 44 45 private static final String SIMPLE_PAGE = 46 "<html>" + 47 "<head>" + 48 "<title>Welcome to the HTMLParser website</title>" + 49 "</head>" + 50 "<body>" + 51 "Welcome to HTMLParser" + 52 "</body>" + 53 "</html>"; 54 55 private static final String guts = 56 "Welcome to HTMLParser" + 57 "<table>" + 58 "<tr>" + 59 "<td>cell 1</td>" + 60 "<td>cell 2</td>" + 61 "</tr>" + 62 "</table>"; 63 64 private static final String PAGE_WITH_TABLE = 65 "<html>" + 66 "<head>" + 67 "<title>Welcome to the HTMLParser website</title>" + 68 "</head>" + 69 "<body>" + 70 guts + 71 "</body>" + 72 "</html>"; 73 74 public HtmlPageTest(String name) { 75 super(name); 76 } 77 78 public void testCreateSimplePage() throws Exception { 79 createParser( 80 SIMPLE_PAGE 81 ); 82 HtmlPage page = new HtmlPage(parser); 83 parser.visitAllNodesWith(page); 84 assertStringEquals( 85 "title", 86 "Welcome to the HTMLParser website", 87 page.getTitle() 88 ); 89 NodeList bodyNodes = page.getBody(); 90 assertEquals("number of nodes in body",1,bodyNodes.size()); 91 Node node = bodyNodes.elementAt(0); 92 assertTrue("expected stringNode but was "+node.getClass().getName(), 93 node instanceof Text 94 ); 95 assertStringEquals( 96 "body contents", 97 "Welcome to HTMLParser", 98 page.getBody().asString() 99 ); 100 } 101 102 public void testCreatePageWithTables() throws Exception { 103 createParser( 104 PAGE_WITH_TABLE 105 ); 106 HtmlPage page = new HtmlPage(parser); 107 parser.visitAllNodesWith(page); 108 NodeList bodyNodes = page.getBody(); 109 assertEquals("number of nodes in body",2,bodyNodes.size()); 110 assertXmlEquals("body html", guts, bodyNodes.toHtml()); 111 TableTag tables [] = page.getTables(); 112 assertEquals("number of tables",1,tables.length); 113 assertEquals("number of rows",1,tables[0].getRowCount()); 114 TableRow row = tables[0].getRow(0); 115 assertEquals("number of columns",2,row.getColumnCount()); 116 TableColumn [] col = row.getColumns(); 117 assertEquals("column contents","cell 1",col[0].toPlainTextString()); 118 assertEquals("column contents","cell 2",col[1].toPlainTextString()); 119 } 120 } 121 | Popular Tags |