[Htmlparser-cvs] htmlparser/src/org/htmlparser/tests/tagTests BulletListTagTest.java,NONE,1.1 Bullet
Brought to you by:
derrickoswald
Update of /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tests/tagTests In directory sc8-pr-cvs1:/tmp/cvs-serv16537/tests/tagTests Modified Files: AllTests.java AppletTagTest.java BaseHrefTagTest.java BodyTagTest.java CompositeTagTest.java DoctypeTagTest.java EndTagTest.java FormTagTest.java FrameSetTagTest.java FrameTagTest.java ImageTagTest.java InputTagTest.java JspTagTest.java LinkTagTest.java MetaTagTest.java ObjectCollectionTest.java OptionTagTest.java ScriptTagTest.java SelectTagTest.java StyleTagTest.java TagTest.java TextareaTagTest.java TitleTagTest.java Added Files: BulletListTagTest.java BulletTagTest.java DivTagTest.java HeadTagTest.java HtmlTagTest.java LabelTagTest.java SpanTagTest.java TableTagTest.java Log Message: Remove most of the scanners. The only scanners left are ones that really do something different (script and jsp). Instead of registering a scanner to enable returning a specific tag you now add a tag to the a PrototypicalNodeFactory. All known tags are 'registered' by default in a new Parser which is similar to having called the old 'registerDOMScanners()', so tags are fully nested. This is different behaviour, and specifically, you will need to recurse into returned nodes to get at what you want. I've tried to adjust the applications accordingly, but worked examples are still scarce. If you want to return only some of the derived tags while keeping most as generic tags, there are various constructors and manipulators on the factory. See the javadocs and examples in the tests package. Nearly all the old scanner tests are folded into the tag tests. toString() has been revamped. This means that the default Parser mainline now returns an indented listing of tags, making it easy to see the structure of a page. The downside is the text of the page had to have newlines, tabs etc. turned into escape sequences. But if you were really interested in content you would be using toHtml() or toPlainTextString(). --- NEW FILE: BulletListTagTest.java --- // HTMLParser Library $Name: $ - A java-based parser for HTML // http://sourceforge.org/projects/htmlparser // Copyright (C) 2003 Derrick Oswald // // Revision Control Information // // $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tests/tagTests/BulletListTagTest.java,v $ // $Author: derrickoswald $ // $Date: 2003/12/07 23:41:43 $ // $Revision: 1.1 $ // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // package org.htmlparser.tests.tagTests; import org.htmlparser.Node; import org.htmlparser.tests.ParserTestCase; import org.htmlparser.StringNode; import org.htmlparser.tags.Bullet; import org.htmlparser.tags.BulletList; import org.htmlparser.tags.CompositeTag; import org.htmlparser.util.NodeList; import org.htmlparser.util.ParserException; public class BulletListTagTest extends ParserTestCase { static { System.setProperty ("org.htmlparser.tests.tagTests.BulletListTagTest", "BulletListTagTest"); } public BulletListTagTest (String name) { super(name); } public void testScan() throws ParserException { createParser( "<ul TYPE=DISC>" + "<ul TYPE=\"DISC\"><li>Energy supply\n"+ " (Campbell) <A HREF=\"/hansard/37th3rd/h20307p.htm#1646\">1646</A>\n"+ " (MacPhail) <A HREF=\"/hansard/37th3rd/h20307p.htm#1646\">1646</A>\n"+ "</ul><A NAME=\"calpinecorp\"></A><B>Calpine Corp.</B>\n"+ "<ul TYPE=\"DISC\"><li>Power plant projects\n"+ " (Neufeld) <A HREF=\"/hansard/37th3rd/h20314p.htm#1985\">1985</A>\n"+ "</ul>" + "</ul>" ); parseAndAssertNodeCount(1); NodeList nestedBulletLists = ((CompositeTag)node[0]).searchFor( BulletList.class ); assertEquals( "bullets in first list", 2, nestedBulletLists.size() ); BulletList firstList = (BulletList)nestedBulletLists.elementAt(0); Bullet firstBullet = (Bullet)firstList.childAt(0); Node firstNodeInFirstBullet = firstBullet.childAt(0); assertType( "first child in bullet", StringNode.class, firstNodeInFirstBullet ); assertStringEquals( "expected text", "Energy supply\n" + " (Campbell) ", firstNodeInFirstBullet.toPlainTextString() ); } public void testMissingendtag () throws ParserException { createParser ("<li>item 1<li>item 2"); parseAndAssertNodeCount (2); assertStringEquals ("item 1 not correct", "item 1", ((Bullet)node[0]).childAt (0).toHtml ()); assertStringEquals ("item 2 not correct", "item 2", ((Bullet)node[1]).childAt (0).toHtml ()); } } --- NEW FILE: BulletTagTest.java --- // HTMLParser Library $Name: $ - A java-based parser for HTML // http://sourceforge.org/projects/htmlparser // Copyright (C) 2003 Derrick Oswald // // Revision Control Information // // $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tests/tagTests/BulletTagTest.java,v $ // $Author: derrickoswald $ // $Date: 2003/12/07 23:41:43 $ // $Revision: 1.1 $ // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // package org.htmlparser.tests.tagTests; import org.htmlparser.Node; import org.htmlparser.tests.ParserTestCase; import org.htmlparser.tags.Bullet; import org.htmlparser.util.NodeIterator; import org.htmlparser.util.ParserException; public class BulletTagTest extends ParserTestCase { static { System.setProperty ("org.htmlparser.tests.tagTests.BulletTagTest", "BulletTagTest"); } public BulletTagTest (String name) { super(name); } public void testBulletFound() throws Exception { createParser( "<LI><A HREF=\"collapseHierarchy.html\">Collapse Hierarchy</A>\n"+ "</LI>" ); parseAndAssertNodeCount(1); assertType("should be a bullet",Bullet.class,node[0]); } public void testOutOfMemoryBug() throws ParserException { createParser( "<html>" + "<head>" + "<title>Foo</title>" + "</head>" + "<body>" + " <ul>" + " <li>" + " <a href=\"http://foo.com/c.html\">bibliographies on:" + " <ul>" + " <li>chironomidae</li>" + " </ul>" + " </a>" + " </li>" + " </ul>" + "" + "</body>" + "</html>" ); for (NodeIterator i = parser.elements();i.hasMoreNodes();) i.nextNode(); } public void testNonEndedBullets() throws ParserException { createParser( "<li>forest practices legislation penalties for non-compliance\n"+ " (Kwan) <A HREF=\"/hansard/37th3rd/h21107a.htm#4384\">4384-5</A>\n"+ "<li>passenger rail service\n"+ " (MacPhail) <A HREF=\"/hansard/37th3rd/h21021p.htm#3904\">3904</A>\n"+ "<li>referendum on principles for treaty negotiations\n"+ " (MacPhail) <A HREF=\"/hansard/37th3rd/h20313p.htm#1894\">1894</A>\n"+ "<li>transportation infrastructure projects\n"+ " (MacPhail) <A HREF=\"/hansard/37th3rd/h21022a.htm#3945\">3945-7</A>\n"+ "<li>tuition fee freeze" ); parseAndAssertNodeCount(5); for (int i=0;i<nodeCount;i++) { assertType("node "+i,Bullet.class,node[i]); } } } --- NEW FILE: DivTagTest.java --- // HTMLParser Library $Name: $ - A java-based parser for HTML // http://sourceforge.org/projects/htmlparser // Copyright (C) 2003 Derrick Oswald // // Revision Control Information // // $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tests/tagTests/DivTagTest.java,v $ // $Author: derrickoswald $ // $Date: 2003/12/07 23:41:43 $ // $Revision: 1.1 $ // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // package org.htmlparser.tests.tagTests; import org.htmlparser.PrototypicalNodeFactory; import org.htmlparser.tags.Div; import org.htmlparser.tags.InputTag; import org.htmlparser.tags.TableTag; import org.htmlparser.tags.Tag; import org.htmlparser.tests.ParserTestCase; import org.htmlparser.util.ParserException; public class DivTagTest extends ParserTestCase { static { System.setProperty ("org.htmlparser.tests.tagTests.DivTagTest", "DivTagTest"); } public DivTagTest (String name) { super(name); } public void testScan() throws ParserException { createParser("<table><div align=\"left\">some text</div></table>"); parseAndAssertNodeCount(1); assertType("node should be table",TableTag.class,node[0]); TableTag tableTag = (TableTag)node[0]; Div div = (Div)tableTag.searchFor(Div.class).toNodeArray()[0]; assertEquals("div contents","some text",div.toPlainTextString()); } /** * Test case for bug #735193 Explicit tag type recognition for CompositTags not working. */ public void testInputInDiv() throws ParserException { createParser("<div><INPUT type=\"text\" name=\"X\">Hello</INPUT></div>"); parser.setNodeFactory ( new PrototypicalNodeFactory ( new Tag[] { new Div (), new InputTag (), })); parseAndAssertNodeCount(1); assertType("node should be div",Div.class,node[0]); Div div = (Div)node[0]; assertType("child not input",InputTag.class,div.getChild (0)); } } --- NEW FILE: HeadTagTest.java --- // HTMLParser Library $Name: $ - A java-based parser for HTML // http://sourceforge.org/projects/htmlparser // Copyright (C) 2003 Derrick Oswald // // Revision Control Information // // $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tests/tagTests/HeadTagTest.java,v $ // $Author: derrickoswald $ // $Date: 2003/12/07 23:41:43 $ // $Revision: 1.1 $ // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // package org.htmlparser.tests.tagTests; import org.htmlparser.tags.HeadTag; import org.htmlparser.tags.Html; import org.htmlparser.tests.ParserTestCase; import org.htmlparser.util.ParserException; public class HeadTagTest extends ParserTestCase { static { System.setProperty ("org.htmlparser.tests.tagTests.HeadTagTest", "HeadTagTest"); } public HeadTagTest (String name) { super(name); } public void testSimpleHead() throws ParserException { createParser("<HTML><HEAD></HEAD></HTML>"); parseAndAssertNodeCount(1); assertTrue(node[0] instanceof Html); Html htmlTag = (Html)node[0]; assertTrue(htmlTag.getChild(0) instanceof HeadTag); } public void testSimpleHeadWithoutEndTag() throws ParserException { createParser("<HTML><HEAD></HTML>"); parseAndAssertNodeCount(1); assertTrue(node[0] instanceof Html); Html htmlTag = (Html)node[0]; assertTrue(htmlTag.getChild(0) instanceof HeadTag); HeadTag headTag = (HeadTag)htmlTag.getChild(0); assertEquals("toHtml()","<HEAD></HEAD>",headTag.toHtml()); assertEquals("toHtml()","<HTML><HEAD></HEAD></HTML>",htmlTag.toHtml()); } public void testSimpleHeadWithBody() throws ParserException { createParser("<HTML><HEAD><BODY></HTML>"); parseAndAssertNodeCount(1); assertTrue(node[0] instanceof Html); Html htmlTag = (Html)node[0]; assertTrue(htmlTag.getChild(0) instanceof HeadTag); //assertTrue(htmlTag.getChild(1) instanceof BodyTag); HeadTag headTag = (HeadTag)htmlTag.getChild(0); assertEquals("toHtml()","<HEAD></HEAD>",headTag.toHtml()); assertEquals("toHtml()","<HTML><HEAD></HEAD><BODY></BODY></HTML>",htmlTag.toHtml()); } } --- NEW FILE: HtmlTagTest.java --- // HTMLParser Library $Name: $ - A java-based parser for HTML // http://sourceforge.org/projects/htmlparser // Copyright (C) 2003 Derrick Oswald // // Revision Control Information // // $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tests/tagTests/HtmlTagTest.java,v $ // $Author: derrickoswald $ // $Date: 2003/12/07 23:41:43 $ // $Revision: 1.1 $ // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // package org.htmlparser.tests.tagTests; import org.htmlparser.Node; import org.htmlparser.PrototypicalNodeFactory; import org.htmlparser.filters.NodeClassFilter; import org.htmlparser.tags.Html; import org.htmlparser.tags.Tag; import org.htmlparser.tags.TitleTag; import org.htmlparser.tests.ParserTestCase; import org.htmlparser.util.NodeList; public class HtmlTagTest extends ParserTestCase { static { System.setProperty ("org.htmlparser.tests.tagTests.HtmlTagTest", "HtmlTagTest"); } public HtmlTagTest (String name) { super(name); } public void testScan() throws Exception { createParser( "<html>" + " <head>" + " <title>Some Title</title>" + " </head>" + " <body>" + " Some data" + " </body>" + "</html>"); parser.setNodeFactory ( new PrototypicalNodeFactory ( new Tag[] { new TitleTag (), new Html (), })); parseAndAssertNodeCount(1); assertType("html tag",Html.class,node[0]); Html html = (Html)node[0]; NodeList nodeList = new NodeList(); NodeClassFilter filter = new NodeClassFilter (TitleTag.class); html.collectInto(nodeList, filter); assertEquals("nodelist size",1,nodeList.size()); Node node = nodeList.elementAt(0); assertType("expected title tag",TitleTag.class,node); TitleTag titleTag = (TitleTag)node; assertStringEquals("title","Some Title",titleTag.getTitle()); } } --- NEW FILE: LabelTagTest.java --- // HTMLParser Library $Name: $ - A java-based parser for HTML // http://sourceforge.org/projects/htmlparser // Copyright (C) 2003 Derrick Oswald // // Revision Control Information // // $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tests/tagTests/LabelTagTest.java,v $ // $Author: derrickoswald $ // $Date: 2003/12/07 23:41:43 $ // $Revision: 1.1 $ // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // package org.htmlparser.tests.tagTests; import java.util.Hashtable; import org.htmlparser.tags.LabelTag; import org.htmlparser.tests.ParserTestCase; import org.htmlparser.util.ParserException; public class LabelTagTest extends ParserTestCase { static { System.setProperty ("org.htmlparser.tests.tagTests.LabelTagTest", "LabelTagTest"); } public LabelTagTest (String name) { super(name); } public void testSimpleLabels() throws ParserException { String html = "<label>This is a label tag</label>"; createParser(html); parseAndAssertNodeCount(1); assertTrue(node[0] instanceof LabelTag); // check the title node LabelTag labelTag = (LabelTag) node[0]; assertEquals("Label","This is a label tag",labelTag.getChildrenHTML()); assertEquals("Label","This is a label tag",labelTag.getLabel()); assertStringEquals("Label", html, labelTag.toHtml()); } public void testLabelWithJspTag() throws ParserException { String label = "<label><%=labelValue%></label>"; createParser(label); parseAndAssertNodeCount(1); assertTrue(node[0] instanceof LabelTag); // check the title node LabelTag labelTag = (LabelTag) node[0]; assertStringEquals("Label",label,labelTag.toHtml()); } public void testLabelWithOtherTags() throws ParserException { String html = "<label><span>Span within label</span></label>"; createParser(html); parseAndAssertNodeCount(1); assertTrue(node[0] instanceof LabelTag); // check the title node LabelTag labelTag = (LabelTag) node[0]; assertEquals("Label value","Span within label",labelTag.getLabel()); assertStringEquals("Label", html, labelTag.toHtml()); } public void testLabelWithManyCompositeTags() throws ParserException { String guts = "<span>Jane <b> Doe </b> Smith</span>"; String html = "<label>" + guts + "</label>"; createParser(html); parseAndAssertNodeCount(1); assertTrue(node[0] instanceof LabelTag); LabelTag labelTag = (LabelTag) node[0]; assertEquals("Label value",guts,labelTag.getChildrenHTML()); assertEquals("Label value","Jane Doe Smith",labelTag.getLabel()); assertStringEquals("Label",html,labelTag.toHtml()); } public void testLabelsID() throws ParserException { String html = "<label>John Doe</label>"; createParser(html); parseAndAssertNodeCount(1); assertTrue(node[0] instanceof LabelTag); LabelTag labelTag = (LabelTag) node[0]; assertStringEquals("Label", html, labelTag.toHtml()); Hashtable attr = labelTag.getAttributes(); assertNull("ID",attr.get("id")); } public void testNestedLabels() throws ParserException { String label1 = "<label id=\"attr1\">"; String label2 = "<label>Jane Doe"; createParser(label1 + label2); parseAndAssertNodeCount(2); assertTrue(node[0] instanceof LabelTag); assertTrue(node[1] instanceof LabelTag); LabelTag labelTag = (LabelTag) node[0]; assertStringEquals("Label", label1 + "</label>", labelTag.toHtml()); labelTag = (LabelTag) node[1]; assertStringEquals("Label", label2 + "</label>",labelTag.toHtml()); Hashtable attr = labelTag.getAttributes(); assertNull("ID",attr.get("id")); } public void testNestedLabels2() throws ParserException { String label1 = "<LABEL value=\"Google Search\">Google</LABEL>"; String label2 = "<LABEL value=\"AltaVista Search\">AltaVista"; String label3 = "<LABEL value=\"Lycos Search\"></LABEL>"; String label4 = "<LABEL>Yahoo!</LABEL>"; String label5 = "<LABEL>\nHotmail</LABEL>"; String label6 = "<LABEL value=\"ICQ Messenger\">"; String label7 = "<LABEL>Mailcity\n</LABEL>"; String label8 = "<LABEL>\nIndiatimes\n</LABEL>"; String label9 = "<LABEL>\nRediff\n</LABEL>"; String label10 = "<LABEL>Cricinfo"; String label11 = "<LABEL value=\"Microsoft Passport\">"; String label12 = "<LABEL value=\"AOL\"><SPAN>AOL</SPAN></LABEL>"; String label13 = "<LABEL value=\"Time Warner\">Time <B>Warner <SPAN>AOL </SPAN>Inc.</B>"; String testHTML = label1 + label2 + label3 + label4 + label5 + label6 + label7 + label8 + label9 + label10 + label11 + label12 + label13; createParser(testHTML); parseAndAssertNodeCount(13); LabelTag LabelTag; LabelTag = (LabelTag) node[0]; assertStringEquals("HTML String", label1, LabelTag.toHtml()); LabelTag = (LabelTag) node[1]; assertStringEquals("HTML String", label2 + "</LABEL>", LabelTag.toHtml()); LabelTag = (LabelTag) node[2]; assertStringEquals("HTML String", label3, LabelTag.toHtml()); LabelTag = (LabelTag) node[3]; assertStringEquals("HTML String", label4, LabelTag.toHtml()); LabelTag = (LabelTag) node[4]; assertStringEquals("HTML String", label5, LabelTag.toHtml()); LabelTag = (LabelTag) node[5]; assertStringEquals("HTML String", label6 + "</LABEL>",LabelTag.toHtml()); LabelTag = (LabelTag) node[6]; assertStringEquals("HTML String", label7, LabelTag.toHtml()); LabelTag = (LabelTag) node[7]; assertStringEquals("HTML String", label8, LabelTag.toHtml()); LabelTag = (LabelTag) node[8]; assertStringEquals("HTML String", label9, LabelTag.toHtml()); LabelTag = (LabelTag) node[9]; assertStringEquals("HTML String", label10 + "</LABEL>",LabelTag.toHtml()); LabelTag = (LabelTag) node[10]; assertStringEquals("HTML String", label11 + "</LABEL>",LabelTag.toHtml()); LabelTag = (LabelTag) node[11]; assertStringEquals("HTML String", label12, LabelTag.toHtml()); LabelTag = (LabelTag) node[12]; assertStringEquals("HTML String", label13 + "</LABEL>",LabelTag.toHtml()); } } --- NEW FILE: SpanTagTest.java --- // HTMLParser Library $Name: $ - A java-based parser for HTML // http://sourceforge.org/projects/htmlparser // Copyright (C) 2003 Derrick Oswald // // Revision Control Information // // $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tests/tagTests/SpanTagTest.java,v $ // $Author: derrickoswald $ // $Date: 2003/12/07 23:41:43 $ // $Revision: 1.1 $ // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // package org.htmlparser.tests.tagTests; import org.htmlparser.Node; import org.htmlparser.PrototypicalNodeFactory; import org.htmlparser.tags.Span; import org.htmlparser.tags.TableColumn; import org.htmlparser.tags.Tag; import org.htmlparser.tests.ParserTestCase; public class SpanTagTest extends ParserTestCase { static { System.setProperty ("org.htmlparser.tests.tagTests.SpanTagTest", "SpanTagTest"); } private static final String HTML_WITH_SPAN = "<TD BORDER=\"0.0\" VALIGN=\"Top\" COLSPAN=\"4\" WIDTH=\"33.33%\">" + " <DIV>" + " <SPAN>Flavor: small(90 to 120 minutes)<BR /></SPAN>" + " <SPAN>The short version of our Refactoring Challenge gives participants a general feel for the smells in the code base and includes time for participants to find and implement important refactorings.
<BR /></SPAN>" + " </DIV>" + "</TD>"; public SpanTagTest (String name) { super(name); } public void testScan() throws Exception { createParser( HTML_WITH_SPAN ); parser.setNodeFactory ( new PrototypicalNodeFactory ( new Tag[] { new TableColumn (), new Span (), })); parseAndAssertNodeCount(1); assertType("node",TableColumn.class,node[0]); TableColumn col = (TableColumn)node[0]; Node spans [] = col.searchFor(Span.class).toNodeArray(); assertEquals("number of spans found",2,spans.length); assertStringEquals( "span 1", "Flavor: small(90 to 120 minutes)", spans[0].toPlainTextString() ); assertStringEquals( "span 2", "The short version of our Refactoring Challenge gives participants a general feel for the smells in the code base and includes time for participants to find and implement important refactorings.
", spans[1].toPlainTextString() ); } } --- NEW FILE: TableTagTest.java --- // HTMLParser Library $Name: $ - A java-based parser for HTML // http://sourceforge.org/projects/htmlparser // Copyright (C) 2003 Derrick Oswald // // Revision Control Information // // $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tests/tagTests/TableTagTest.java,v $ // $Author: derrickoswald $ // $Date: 2003/12/07 23:41:43 $ // $Revision: 1.1 $ // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // package org.htmlparser.tests.tagTests; import org.htmlparser.Node; import org.htmlparser.Parser; import org.htmlparser.tags.Html; import org.htmlparser.tags.TableColumn; import org.htmlparser.tags.TableRow; import org.htmlparser.tags.TableTag; import org.htmlparser.tests.ParserTestCase; import org.htmlparser.util.NodeIterator; import org.htmlparser.util.ParserException; public class TableTagTest extends ParserTestCase { static { System.setProperty ("org.htmlparser.tests.tagTests.TableTagTest", "TableTagTest"); } public TableTagTest (String name) { super(name); } private String createHtmlWithTable() { return "<table width=\"100.0%\" align=\"Center\" cellpadding=\"5.0\" cellspacing=\"0.0\" border=\"0.0\">"+ " <tr>" + " <td border=\"0.0\" valign=\"Top\" colspan=\"5\">" + " <img src=\"file:/c:/data/dev/eclipse_workspace/ShoppingServerTests/resources/pictures/fishbowl.jpg\" width=\"446.0\" height=\"335.0\" />" + " </td>" + " <td border=\"0.0\" align=\"Justify\" valign=\"Top\" colspan=\"7\">" + " <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>" + " </td>" + " </tr>" + "</table>"; } public void testScan() throws ParserException { createParser(createHtmlWithTable()); parseAndAssertNodeCount(1); assertTrue(node[0] instanceof TableTag); TableTag tableTag = (TableTag)node[0]; assertEquals("rows",1,tableTag.getRowCount()); TableRow row = tableTag.getRow(0); assertEquals("columns in row 1",2,row.getColumnCount()); assertEquals("table width","100.0%",tableTag.getAttribute("WIDTH")); } public void testErroneousTables() throws ParserException { createParser( "<HTML>\n"+ "<table border>\n"+ "<tr>\n"+ "<td>Head1</td>\n"+ "<td>Val1</td>\n"+ "</tr>\n"+ "<tr>\n"+ "<td>Head2</td>\n"+ "<td>Val2</td>\n"+ "</tr>\n"+ "<tr>\n"+ "<td>\n"+ "<table border>\n"+ "<tr>\n"+ "<td>table2 Head1</td>\n"+ "<td>table2 Val1</td>\n"+ "</tr>\n"+ "</table>\n"+ "</td>\n"+ "</tr>\n"+ "</BODY>\n"+ "</HTML>" ); parseAndAssertNodeCount(1); assertType("only tag should be a HTML tag", Html.class,node[0]); Html html = (Html)node[0]; assertEquals("html tag should have 4 children", 4, html.getChildCount ()); assertType("second tag",TableTag.class,html.getChild (1)); TableTag table = (TableTag)html.getChild (1); assertEquals("rows",3,table.getRowCount()); TableRow tr = table.getRow(2); assertEquals("columns",1,tr.getColumnCount()); TableColumn td = tr.getColumns()[0]; Node node = td.childAt(1); assertType("node",TableTag.class,node); TableTag table2 = (TableTag)node; assertEquals("second table row count",1,table2.getRowCount()); tr = table2.getRow(0); assertEquals("second table col count",2,tr.getColumnCount()); } /** * Test many unclosed tags (causes heavy recursion). * See feature request #729259 Increase maximum recursion depth. * Only perform this test if it's version 1.4 or higher. */ public void testRecursionDepth () throws ParserException { Parser parser; String url = "http://htmlparser.sourceforge.net/test/badtable2.html"; parser = new Parser (url); for (NodeIterator e = parser.elements();e.hasMoreNodes();) e.nextNode(); // Note: The test will throw a StackOverFlowException, // so we are successful if we get to here... assertTrue ("Crash", true); } /** * See bug #742254 Nested <TR> &<TD> tags should not be allowed */ public void testUnClosed1 () throws ParserException { createParser ("<TABLE><TR><TR></TR></TABLE>"); parseAndAssertNodeCount (1); String s = node[0].toHtml (); assertEquals ("Unclosed","<TABLE><TR></TR><TR></TR></TABLE>",s); } /** * See bug #742254 Nested <TR> &<TD> tags should not be allowed */ public void testUnClosed2 () throws ParserException { createParser ("<TABLE><TR><TD><TD></TD></TR></TABLE>"); parseAndAssertNodeCount (1); String s = node[0].toHtml (); assertEquals ("Unclosed","<TABLE><TR><TD></TD><TD></TD></TR></TABLE>",s); } /** * See bug #742254 Nested <TR> &<TD> tags should not be allowed */ public void testUnClosed3 () throws ParserException { createParser ("<TABLE><TR><TD>blah blah</TD><TR><TD>blah blah</TD></TR></TABLE>"); parseAndAssertNodeCount (1); String s = node[0].toHtml (); assertEquals ("Unclosed","<TABLE><TR><TD>blah blah</TD></TR><TR><TD>blah blah</TD></TR></TABLE>",s); } /** * See bug #750117 StackOverFlow while Node-Iteration * Not reproducible. */ public void testOverFlow () throws ParserException { parser = new Parser( "http://www.sec.gov/Archives/edgar/data/30554/000089322002000287/w57038e10-k.htm" ); Node node; for (NodeIterator e = parser.elements(); e.hasMoreNodes(); ) node = e.nextNode(); } } Index: AllTests.java =================================================================== RCS file: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tests/tagTests/AllTests.java,v retrieving revision 1.47 retrieving revision 1.48 diff -C2 -d -r1.47 -r1.48 *** AllTests.java 9 Nov 2003 17:07:15 -0000 1.47 --- AllTests.java 7 Dec 2003 23:41:42 -0000 1.48 *************** *** 46,71 **** public static TestSuite suite() { TestSuite suite = new TestSuite("Tag Tests"); ! suite.addTestSuite(JspTagTest.class); ! suite.addTestSuite(ScriptTagTest.class); ! suite.addTestSuite(ImageTagTest.class); ! suite.addTestSuite(LinkTagTest.class); ! suite.addTestSuite(TagTest.class); ! suite.addTestSuite(TitleTagTest.class); suite.addTestSuite(DoctypeTagTest.class); suite.addTestSuite(EndTagTest.class); ! suite.addTestSuite(MetaTagTest.class); ! suite.addTestSuite(StyleTagTest.class); ! suite.addTestSuite(AppletTagTest.class); ! suite.addTestSuite(FrameTagTest.class); suite.addTestSuite(FrameSetTagTest.class); suite.addTestSuite(InputTagTest.class); suite.addTestSuite(OptionTagTest.class); suite.addTestSuite(SelectTagTest.class); suite.addTestSuite(TextareaTagTest.class); ! suite.addTestSuite(FormTagTest.class); ! suite.addTestSuite(BaseHrefTagTest.class); ! suite.addTestSuite(ObjectCollectionTest.class); ! suite.addTestSuite(BodyTagTest.class); ! suite.addTestSuite(CompositeTagTest.class); return suite; } --- 46,79 ---- public static TestSuite suite() { TestSuite suite = new TestSuite("Tag Tests"); ! suite.addTestSuite(AppletTagTest.class); ! suite.addTestSuite(BaseHrefTagTest.class); ! suite.addTestSuite(BodyTagTest.class); ! suite.addTestSuite(BulletListTagTest.class); ! suite.addTestSuite(BulletTagTest.class); ! suite.addTestSuite(CompositeTagTest.class); ! suite.addTestSuite(DivTagTest.class); suite.addTestSuite(DoctypeTagTest.class); suite.addTestSuite(EndTagTest.class); ! suite.addTestSuite(FormTagTest.class); suite.addTestSuite(FrameSetTagTest.class); + suite.addTestSuite(FrameTagTest.class); + suite.addTestSuite(HeadTagTest.class); + suite.addTestSuite(HtmlTagTest.class); + suite.addTestSuite(ImageTagTest.class); suite.addTestSuite(InputTagTest.class); + suite.addTestSuite(JspTagTest.class); + suite.addTestSuite(LabelTagTest.class); + suite.addTestSuite(LinkTagTest.class); + suite.addTestSuite(MetaTagTest.class); + suite.addTestSuite(ObjectCollectionTest.class); suite.addTestSuite(OptionTagTest.class); + suite.addTestSuite(ScriptTagTest.class); suite.addTestSuite(SelectTagTest.class); + suite.addTestSuite(SpanTagTest.class); + suite.addTestSuite(StyleTagTest.class); + suite.addTestSuite(TableTagTest.class); + suite.addTestSuite(TagTest.class); suite.addTestSuite(TextareaTagTest.class); ! suite.addTestSuite(TitleTagTest.class); return suite; } Index: AppletTagTest.java =================================================================== RCS file: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tests/tagTests/AppletTagTest.java,v retrieving revision 1.34 retrieving revision 1.35 diff -C2 -d -r1.34 -r1.35 *** AppletTagTest.java 9 Nov 2003 17:07:15 -0000 1.34 --- AppletTagTest.java 7 Dec 2003 23:41:43 -0000 1.35 *************** *** 29,32 **** --- 29,33 ---- package org.htmlparser.tests.tagTests; + import java.util.Enumeration; import java.util.Hashtable; *************** *** 59,63 **** "</HTML>"; createParser(testHTML); - parser.registerScanners(); parseAndAssertNodeCount(3); assertTrue("Node should be an applet tag",node[0] instanceof AppletTag); --- 60,63 ---- *************** *** 73,76 **** --- 73,108 ---- } + public void testScan() throws ParserException + { + String [][]paramsData = {{"Param1","Value1"},{"Name","Somik"},{"Age","23"}}; + Hashtable paramsMap = new Hashtable(); + String testHTML = new String("<APPLET CODE=Myclass.class ARCHIVE=test.jar CODEBASE=www.kizna.com>\n"); + for (int i = 0;i<paramsData.length;i++) + { + testHTML+="<PARAM NAME=\""+paramsData[i][0]+"\" VALUE=\""+paramsData[i][1]+"\">\n"; + paramsMap.put(paramsData[i][0],paramsData[i][1]); + } + testHTML+= + "</APPLET></HTML>"; + createParser(testHTML); + parseAndAssertNodeCount(2); + assertTrue("Node should be an applet tag",node[0] instanceof AppletTag); + // Check the data in the applet tag + AppletTag appletTag = (AppletTag)node[0]; + assertEquals("Class Name","Myclass.class",appletTag.getAppletClass()); + assertEquals("Archive","test.jar",appletTag.getArchive()); + assertEquals("Codebase","www.kizna.com",appletTag.getCodeBase()); + // Check the params data + int cnt = 0; + for (Enumeration e = appletTag.getParameterNames();e.hasMoreElements();) + { + String paramName = (String)e.nextElement(); + String paramValue = appletTag.getParameter(paramName); + assertEquals("Param "+cnt+" value",paramsMap.get(paramName),paramValue); + cnt++; + } + assertEquals("Number of params",new Integer(paramsData.length),new Integer(cnt)); + } + public void testChangeCodebase() throws ParserException { String [][]paramsData = {{"Param1","Value1"},{"Name","Somik"},{"Age","23"}}; *************** *** 86,90 **** "</HTML>"; createParser(testHTML); - parser.registerScanners(); parseAndAssertNodeCount(3); assertTrue("Node should be an applet tag",node[0] instanceof AppletTag); --- 118,121 ---- *************** *** 113,117 **** "</APPLET>"; createParser(testHTML + "\n</HTML>"); - parser.registerScanners(); parseAndAssertNodeCount(3); assertTrue("Node should be an applet tag",node[0] instanceof AppletTag); --- 144,147 ---- *************** *** 137,141 **** "</APPLET>"; createParser(testHTML + "\n</HTML>"); - parser.registerScanners(); parseAndAssertNodeCount(3); assertTrue("Node should be an applet tag",node[0] instanceof AppletTag); --- 167,170 ---- *************** *** 162,166 **** "</HTML>"; createParser(testHTML); - parser.registerScanners(); parseAndAssertNodeCount(3); assertTrue("Node should be an applet tag",node[0] instanceof AppletTag); --- 191,194 ---- Index: BaseHrefTagTest.java =================================================================== RCS file: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tests/tagTests/BaseHrefTagTest.java,v retrieving revision 1.34 retrieving revision 1.35 diff -C2 -d -r1.34 -r1.35 *** BaseHrefTagTest.java 9 Nov 2003 17:07:15 -0000 1.34 --- BaseHrefTagTest.java 7 Dec 2003 23:41:43 -0000 1.35 *************** *** 30,35 **** --- 30,41 ---- import java.util.Vector; + + import org.htmlparser.PrototypicalNodeFactory; import org.htmlparser.tags.BaseHrefTag; + import org.htmlparser.tags.LinkTag; + import org.htmlparser.tags.Tag; + import org.htmlparser.tags.TitleTag; import org.htmlparser.tests.ParserTestCase; + import org.htmlparser.util.LinkProcessor; import org.htmlparser.util.ParserException; *************** *** 51,59 **** } public void testNotHREFBaseTag() throws ParserException { String html = "<base target=\"_top\">"; createParser(html); - parser.registerScanners(); parseAndAssertNodeCount(1); assertTrue("Should be a base tag but was "+node[0].getClass().getName(),node[0] instanceof BaseHrefTag); --- 57,89 ---- } + public void testRemoveLastSlash() { + String url1 = "http://www.yahoo.com/"; + String url2 = "http://www.google.com"; + String modifiedUrl1 = LinkProcessor.removeLastSlash(url1); + String modifiedUrl2 = LinkProcessor.removeLastSlash(url2); + assertEquals("Url1","http://www.yahoo.com",modifiedUrl1); + assertEquals("Url2","http://www.google.com",modifiedUrl2); + } + + public void testScan() throws ParserException{ + createParser("<html><head><TITLE>test page</TITLE><BASE HREF=\"http://www.abc.com/\"><a href=\"home.cfm\">Home</a>...</html>","http://www.google.com/test/index.html"); + parser.setNodeFactory ( + new PrototypicalNodeFactory ( + new Tag[] + { + new TitleTag (), + new LinkTag (), + new BaseHrefTag (), + })); + parseAndAssertNodeCount(7); + assertTrue("Base href tag should be the 4th tag", node[3] instanceof BaseHrefTag); + BaseHrefTag baseRefTag = (BaseHrefTag)node[3]; + assertEquals("Base HREF Url","http://www.abc.com",baseRefTag.getBaseUrl()); + } + public void testNotHREFBaseTag() throws ParserException { String html = "<base target=\"_top\">"; createParser(html); parseAndAssertNodeCount(1); assertTrue("Should be a base tag but was "+node[0].getClass().getName(),node[0] instanceof BaseHrefTag); Index: BodyTagTest.java =================================================================== RCS file: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tests/tagTests/BodyTagTest.java,v retrieving revision 1.17 retrieving revision 1.18 diff -C2 -d -r1.17 -r1.18 *** BodyTagTest.java 9 Nov 2003 17:07:15 -0000 1.17 --- BodyTagTest.java 7 Dec 2003 23:41:43 -0000 1.18 *************** *** 33,38 **** import org.htmlparser.Node; ! import org.htmlparser.scanners.BodyScanner; import org.htmlparser.tags.BodyTag; import org.htmlparser.tests.ParserTestCase; import org.htmlparser.util.NodeIterator; --- 33,41 ---- import org.htmlparser.Node; ! import org.htmlparser.PrototypicalNodeFactory; import org.htmlparser.tags.BodyTag; + import org.htmlparser.tags.Html; + import org.htmlparser.tags.Tag; + import org.htmlparser.tags.TitleTag; import org.htmlparser.tests.ParserTestCase; import org.htmlparser.util.NodeIterator; *************** *** 56,64 **** super.setUp(); createParser("<html><head><title>body tag test</title></head>" + html + "</html>"); ! parser.registerScanners(); ! parser.addScanner(new BodyScanner("-b")); ! parseAndAssertNodeCount(6); ! assertTrue(node[4] instanceof BodyTag); ! bodyTag = (BodyTag) node[4]; } --- 59,68 ---- super.setUp(); createParser("<html><head><title>body tag test</title></head>" + html + "</html>"); ! parseAndAssertNodeCount(1); ! assertTrue("Only node should be an HTML node",node[0] instanceof Html); ! Html html = (Html)node[0]; ! assertTrue("HTML node should have two children",2 == html.getChildCount ()); ! assertTrue("Second node should be an BODY tag",html.getChild(1) instanceof BodyTag); ! bodyTag = (BodyTag)html.getChild(1); } *************** *** 85,89 **** { createParser("<body style=\"margin-top:4px; margin-left:20px;\" title=\"body\">"); ! parser.addScanner (new BodyScanner ("-b")); iterator = parser.elements (); node = null; --- 89,93 ---- { createParser("<body style=\"margin-top:4px; margin-left:20px;\" title=\"body\">"); ! parser.setNodeFactory (new PrototypicalNodeFactory (new BodyTag ())); iterator = parser.elements (); node = null; *************** *** 107,110 **** --- 111,170 ---- fail ("exception thrown " + pe.getMessage ()); } + } + + public void testSimpleBody() throws ParserException { + createParser("<html><head><title>Test 1</title></head><body>This is a body tag</body></html>"); + parser.setNodeFactory ( + new PrototypicalNodeFactory ( + new Tag[] + { + new BodyTag (), + new TitleTag (), + })); + parseAndAssertNodeCount(6); + assertTrue(node[4] instanceof BodyTag); + // check the body node + BodyTag bodyTag = (BodyTag) node[4]; + assertEquals("Body","This is a body tag",bodyTag.getBody()); + assertEquals("Body","<body>This is a body tag</body>",bodyTag.toHtml()); + } + + public void testBodywithJsp() throws ParserException { + String body = "<body><%=BodyValue%></body>"; + createParser("<html><head><title>Test 1</title></head>" + body + "</html>"); + parser.setNodeFactory (new PrototypicalNodeFactory (new BodyTag ())); + parseAndAssertNodeCount(8); + assertTrue(node[6] instanceof BodyTag); + // check the body node + BodyTag bodyTag = (BodyTag) node[6]; + assertStringEquals("Body",body,bodyTag.toHtml()); + } + + public void testBodyMixed() throws ParserException { + String body = "<body>before jsp<%=BodyValue%>after jsp</body>"; + createParser("<html><head><title>Test 1</title></head>" + body + "</html>"); + parser.setNodeFactory ( + new PrototypicalNodeFactory ( + new Tag[] + { + new BodyTag (), + new TitleTag (), + })); + parseAndAssertNodeCount(6); + assertTrue(node[4] instanceof BodyTag); + // check the body node + BodyTag bodyTag = (BodyTag) node[4]; + assertEquals("Body",body,bodyTag.toHtml()); + } + + public void testBodyEnding() throws ParserException { + String body = "<body>before jsp<%=BodyValue%>after jsp"; + createParser("<html>" + body + "</html>"); + parser.setNodeFactory (new PrototypicalNodeFactory (new BodyTag ())); + parseAndAssertNodeCount(3); + assertTrue(node[1] instanceof BodyTag); + // check the body node + BodyTag bodyTag = (BodyTag) node[1]; + assertEquals("Body",body + "</body>",bodyTag.toHtml()); } Index: CompositeTagTest.java =================================================================== RCS file: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tests/tagTests/CompositeTagTest.java,v retrieving revision 1.13 retrieving revision 1.14 diff -C2 -d -r1.13 -r1.14 *** CompositeTagTest.java 9 Nov 2003 17:07:16 -0000 1.13 --- CompositeTagTest.java 7 Dec 2003 23:41:43 -0000 1.14 *************** *** 29,36 **** package org.htmlparser.tests.tagTests; ! import org.htmlparser.*; ! import org.htmlparser.tags.*; ! import org.htmlparser.tests.*; ! import org.htmlparser.util.*; --- 29,40 ---- package org.htmlparser.tests.tagTests; ! import org.htmlparser.Node; ! import org.htmlparser.StringNode; ! import org.htmlparser.tags.CompositeTag; ! import org.htmlparser.tags.TableColumn; ! import org.htmlparser.tags.TableRow; ! import org.htmlparser.tags.TableTag; ! import org.htmlparser.tests.ParserTestCase; ! import org.htmlparser.util.ParserException; *************** *** 58,62 **** "</table>" ); - parser.registerScanners(); parseAndAssertNodeCount(1); TableTag tableTag = (TableTag)node[0]; --- 62,65 ---- *************** *** 90,94 **** "</table>" ); - parser.registerScanners(); parseAndAssertNodeCount(1); TableTag tableTag = (TableTag)node[0]; --- 93,96 ---- Index: DoctypeTagTest.java =================================================================== RCS file: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tests/tagTests/DoctypeTagTest.java,v retrieving revision 1.32 retrieving revision 1.33 diff -C2 -d -r1.32 -r1.33 *** DoctypeTagTest.java 9 Nov 2003 17:07:16 -0000 1.32 --- DoctypeTagTest.java 7 Dec 2003 23:41:43 -0000 1.33 *************** *** 56,63 **** "</HTML>\n"); createParser(testHTML); ! parser.registerScanners(); ! parseAndAssertNodeCount(16); ! // The node should be an DoctypeTag ! assertTrue("Node should be a DoctypeTag",node[0] instanceof DoctypeTag); DoctypeTag docTypeTag = (DoctypeTag)node[0]; assertStringEquals("toHTML()","<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\">",docTypeTag.toHtml()); --- 56,62 ---- "</HTML>\n"); createParser(testHTML); ! parseAndAssertNodeCount(4); ! // The first node should be an DoctypeTag ! assertTrue("First node should be a DoctypeTag",node[0] instanceof DoctypeTag); DoctypeTag docTypeTag = (DoctypeTag)node[0]; assertStringEquals("toHTML()","<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\">",docTypeTag.toHtml()); Index: EndTagTest.java =================================================================== RCS file: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tests/tagTests/EndTagTest.java,v retrieving revision 1.35 retrieving revision 1.36 diff -C2 -d -r1.35 -r1.36 *** EndTagTest.java 9 Nov 2003 17:07:16 -0000 1.35 --- EndTagTest.java 7 Dec 2003 23:41:43 -0000 1.36 *************** *** 30,33 **** --- 30,34 ---- package org.htmlparser.tests.tagTests; + import org.htmlparser.PrototypicalNodeFactory; import org.htmlparser.tags.Tag; import org.htmlparser.tests.ParserTestCase; *************** *** 47,54 **** public void testToHTML() throws ParserException { createParser("<HTML></HTML>"); ! // Register the image scanner ! parser.registerScanners(); parseAndAssertNodeCount(2); ! // The node should be an HTMLLinkTag assertTrue("Node should be a Tag",node[1] instanceof Tag); Tag endTag = (Tag)node[1]; --- 48,54 ---- public void testToHTML() throws ParserException { createParser("<HTML></HTML>"); ! parser.setNodeFactory (new PrototypicalNodeFactory (true)); parseAndAssertNodeCount(2); ! // The node should be a tag assertTrue("Node should be a Tag",node[1] instanceof Tag); Tag endTag = (Tag)node[1]; *************** *** 61,64 **** --- 61,65 ---- "<SCRIPT>document.write(d+\".com\")</SCRIPT><BR>"; createParser(testHtml); + parser.setNodeFactory (new PrototypicalNodeFactory (true)); int pos = testHtml.indexOf("</SCRIPT>"); parseAndAssertNodeCount(4); Index: FormTagTest.java =================================================================== RCS file: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tests/tagTests/FormTagTest.java,v retrieving revision 1.38 retrieving revision 1.39 diff -C2 -d -r1.38 -r1.39 *** FormTagTest.java 9 Nov 2003 17:07:16 -0000 1.38 --- FormTagTest.java 7 Dec 2003 23:41:43 -0000 1.39 *************** *** 29,40 **** package org.htmlparser.tests.tagTests; import org.htmlparser.Node; import org.htmlparser.StringNode; ! import org.htmlparser.scanners.FormScanner; import org.htmlparser.tags.FormTag; import org.htmlparser.tags.InputTag; import org.htmlparser.tags.Tag; import org.htmlparser.tests.ParserTestCase; ! import org.htmlparser.tests.scannersTests.FormScannerTest; import org.htmlparser.util.NodeList; import org.htmlparser.util.ParserException; --- 29,52 ---- package org.htmlparser.tests.tagTests; + import org.htmlparser.AbstractNode; import org.htmlparser.Node; + import org.htmlparser.Parser; import org.htmlparser.StringNode; ! import org.htmlparser.PrototypicalNodeFactory; ! import org.htmlparser.RemarkNode; ! import org.htmlparser.filters.NodeClassFilter; ! import org.htmlparser.tags.BodyTag; import org.htmlparser.tags.FormTag; + import org.htmlparser.tags.HeadTag; + import org.htmlparser.tags.Html; import org.htmlparser.tags.InputTag; + import org.htmlparser.tags.LinkTag; + import org.htmlparser.tags.OptionTag; + import org.htmlparser.tags.SelectTag; + import org.htmlparser.tags.TableTag; import org.htmlparser.tags.Tag; + import org.htmlparser.tags.TextareaTag; import org.htmlparser.tests.ParserTestCase; ! import org.htmlparser.util.NodeIterator; import org.htmlparser.util.NodeList; import org.htmlparser.util.ParserException; *************** *** 47,58 **** } public FormTagTest(String name) { super(name); } ! public void testSetFormLocation() throws ParserException{ ! createParser(FormScannerTest.FORM_HTML); ! parser.registerScanners(); parseAndAssertNodeCount(1); assertTrue("Node 0 should be Form Tag",node[0] instanceof FormTag); --- 59,338 ---- } + public static final String FORM_HTML = + "<FORM METHOD=\""+FormTag.POST+"\" ACTION=\"do_login.php\" NAME=\"login_form\" onSubmit=\"return CheckData()\">\n"+ + "<TR><TD ALIGN=\"center\"> </TD></TR>\n"+ + "<TR><TD ALIGN=\"center\"><FONT face=\"Arial, verdana\" size=2><b>User Name</b></font></TD></TR>\n"+ + "<TR><TD ALIGN=\"center\"><INPUT TYPE=\"text\" NAME=\"name\" SIZE=\"20\"></TD></TR>\n"+ + "<TR><TD ALIGN=\"center\"><FONT face=\"Arial, verdana\" size=2><b>Password</b></font></TD></TR>\n"+ + "<TR><TD ALIGN=\"center\"><INPUT TYPE=\"password\" NAME=\"passwd\" SIZE=\"20\"></TD></TR>\n"+ + "<TR><TD ALIGN=\"center\"> </TD></TR>\n"+ + "<TR><TD ALIGN=\"center\"><INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"Login\"></TD></TR>\n"+ + "<TR><TD ALIGN=\"center\"> </TD></TR>\n"+ + "<TEXTAREA name=\"Description\" rows=\"15\" cols=\"55\" wrap=\"virtual\" class=\"composef\" tabindex=\"5\">Contents of TextArea</TEXTAREA>\n"+ + // "<TEXTAREA name=\"AnotherDescription\" rows=\"15\" cols=\"55\" wrap=\"virtual\" class=\"composef\" tabindex=\"5\">\n"+ + "<INPUT TYPE=\"hidden\" NAME=\"password\" SIZE=\"20\">\n"+ + "<INPUT TYPE=\"submit\">\n"+ + "</FORM>"; + public FormTagTest(String name) { super(name); } ! public void assertTypeNameSize(String description,String type,String name,String size,InputTag inputTag) ! { ! assertEquals(description+" type",type,inputTag.getAttribute("TYPE")); ! assertEquals(description+" name",name,inputTag.getAttribute("NAME")); ! assertEquals(description+" size",size,inputTag.getAttribute("SIZE")); ! } ! public void assertTypeNameValue(String description,String type,String name,String value,InputTag inputTag) ! { ! assertEquals(description+" type",type,inputTag.getAttribute("TYPE")); ! assertEquals(description+" name",name,inputTag.getAttribute("NAME")); ! assertEquals(description+" value",value,inputTag.getAttribute("VALUE")); ! } ! ! public void testScan() throws ParserException ! { ! createParser(FORM_HTML,"http://www.google.com/test/index.html"); ! parseAndAssertNodeCount(1); ! assertTrue("Node 0 should be Form Tag",node[0] instanceof FormTag); ! FormTag formTag = (FormTag)node[0]; ! assertStringEquals("Method",FormTag.POST,formTag.getFormMethod()); ! assertStringEquals("Location","http://www.google.com/test/do_login.php",formTag.getFormLocation()); ! assertStringEquals("Name","login_form",formTag.getFormName()); ! InputTag nameTag = formTag.getInputTag("name"); ! InputTag passwdTag = formTag.getInputTag("passwd"); ! InputTag submitTag = formTag.getInputTag("submit"); ! InputTag dummyTag = formTag.getInputTag("dummy"); ! assertNotNull("Input Name Tag should not be null",nameTag); ! assertNotNull("Input Password Tag should not be null",passwdTag); ! assertNotNull("Input Submit Tag should not be null",submitTag); ! assertNull("Input dummy tag should be null",dummyTag); ! ! assertTypeNameSize("Input Name Tag","text","name","20",nameTag); ! assertTypeNameSize("Input Password Tag","password","passwd","20",passwdTag); ! assertTypeNameValue("Input Submit Tag","submit","submit","Login",submitTag); ! ! TextareaTag textAreaTag = formTag.getTextAreaTag("Description"); ! assertNotNull("Text Area Tag should have been found",textAreaTag); ! assertEquals("Text Area Tag Contents","Contents of TextArea",textAreaTag.getValue()); ! assertNull("Should have been null",formTag.getTextAreaTag("junk")); ! ! ... [truncated message content] |