📄 linktagtest.java
字号:
// HTMLParser Library $Name: v1_6 $ - A java-based parser for HTML// http://sourceforge.org/projects/htmlparser// Copyright (C) 2004 Somik Raha//// Revision Control Information//// $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tests/tagTests/LinkTagTest.java,v $// $Author: derrickoswald $// $Date: 2006/06/04 19:17:20 $// $Revision: 1.54 $//// 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.Tag;import org.htmlparser.Text;import org.htmlparser.filters.NodeClassFilter;import org.htmlparser.tags.HeadTag;import org.htmlparser.tags.Html;import org.htmlparser.tags.ImageTag;import org.htmlparser.tags.LinkTag;import org.htmlparser.tests.ParserTestCase;import org.htmlparser.util.NodeList;import org.htmlparser.util.ParserException;import org.htmlparser.util.SimpleNodeIterator;public class LinkTagTest extends ParserTestCase { static { System.setProperty ("org.htmlparser.tests.tagTests.LinkTagTest", "LinkTagTest"); } public LinkTagTest(String name) { super(name); } /** * The bug being reproduced is this : <BR> * <BODY aLink=#ff0000 bgColor=#ffffff link=#0000cc onload=setfocus() text=#000000 <BR> * vLink=#551a8b> * The above line is incorrectly parsed in that, the BODY tag is not identified. * Creation date: (6/17/2001 4:01:06 PM) */ public void testLinkNodeBug() throws ParserException { createParser("<A HREF=\"../test.html\">abcd</A>","http://www.google.com/test/index.html"); parseAndAssertNodeCount(1); // The node should be an LinkTag assertTrue("Node should be a LinkTag",node[0] instanceof LinkTag); LinkTag linkNode = (LinkTag)node[0]; assertEquals("The image locn","http://www.google.com/test.html",linkNode.getLink()); } /** * The bug being reproduced is this : <BR> * <BODY aLink=#ff0000 bgColor=#ffffff link=#0000cc onload=setfocus() text=#000000 <BR> * vLink=#551a8b> * The above line is incorrectly parsed in that, the BODY tag is not identified. * Creation date: (6/17/2001 4:01:06 PM) */ public void testLinkNodeBug2() throws ParserException { createParser("<A HREF=\"../../test.html\">abcd</A>","http://www.google.com/test/test/index.html"); parseAndAssertNodeCount(1); // The node should be an LinkTag assertTrue("Node should be a LinkTag",node[0] instanceof LinkTag); LinkTag linkNode = (LinkTag)node[0]; assertEquals("The image locn","http://www.google.com/test.html",linkNode.getLink()); } /** * The bug being reproduced is this : <BR> * When a url ends with a slash, and the link begins with a slash,the parser puts two slashes * This bug was submitted by Roget Kjensrud * Creation date: (6/17/2001 4:01:06 PM) */ public void testLinkNodeBug3() throws ParserException { createParser("<A HREF=\"/mylink.html\">abcd</A>","http://www.cj.com/"); parseAndAssertNodeCount(1); // The node should be an LinkTag assertTrue("Node should be a LinkTag",node[0] instanceof LinkTag); LinkTag linkNode = (LinkTag)node[0]; assertEquals("Link incorrect","http://www.cj.com/mylink.html",linkNode.getLink()); } /** * The bug being reproduced is this : <BR> * Simple url without index.html, doesent get appended to link * This bug was submitted by Roget Kjensrud * Creation date: (6/17/2001 4:01:06 PM) */ public void testLinkNodeBug4() throws ParserException { createParser("<A HREF=\"/mylink.html\">abcd</A>","http://www.cj.com"); parseAndAssertNodeCount(1); // The node should be an LinkTag assertTrue("Node should be a LinkTag",node[0] instanceof LinkTag); LinkTag linkNode = (LinkTag)node[0]; assertEquals("Link incorrect!!","http://www.cj.com/mylink.html",linkNode.getLink()); } public void testLinkNodeBug5() throws ParserException { String link1 = "http://note.kimo.com.tw/"; String link2 = "http://photo.kimo.com.tw/"; String link3 = "http://address.kimo.com.tw/"; createParser("<a href=" + link1 + ">���O</a> <a \n"+ "href=" + link2 + ">��ï</a> <a\n"+ "href=" + link3 + ">�q�T��</a> ","http://www.cj.com"); parseAndAssertNodeCount(6); assertTrue("Node should be a LinkTag",node[2] instanceof LinkTag); LinkTag linkNode = (LinkTag)node[2]; assertStringEquals("Link incorrect!!",link2,linkNode.getLink()); assertTrue("Node should be a LinkTag",node[4] instanceof LinkTag); LinkTag linkNode2 = (LinkTag)node[4]; assertStringEquals("Link incorrect!!",link3,linkNode2.getLink()); } /** * This bug occurs when there is a null pointer exception thrown while scanning a tag using LinkScanner. * Creation date: (7/1/2001 2:42:13 PM) */ public void testLinkNodeBugNullPointerException() throws ParserException { createParser("<FORM action=http://search.yahoo.com/bin/search name=f><MAP name=m><AREA\n"+ "coords=0,0,52,52 href=\"http://www.yahoo.com/r/c1\" shape=RECT><AREA"+ "coords=53,0,121,52 href=\"http://www.yahoo.com/r/p1\" shape=RECT><AREA"+ "coords=122,0,191,52 href=\"http://www.yahoo.com/r/m1\" shape=RECT><AREA"+ "coords=441,0,510,52 href=\"http://www.yahoo.com/r/wn\" shape=RECT>","http://www.cj.com/"); parser.setNodeFactory (new PrototypicalNodeFactory (new LinkTag ())); parseAndAssertNodeCount(6); } /** * This bug occurs when there is a null pointer exception thrown while scanning a tag using LinkScanner. * Creation date: (7/1/2001 2:42:13 PM) */ public void testLinkNodeMailtoBug() throws ParserException { createParser("<A HREF='mailto:somik@yahoo.com'>hello</A>","http://www.cj.com/"); parseAndAssertNodeCount(1); assertTrue("Node should be a LinkTag",node[0] instanceof LinkTag); LinkTag linkNode = (LinkTag)node[0]; assertStringEquals("Link incorrect","somik@yahoo.com",linkNode.getLink()); assertEquals("Link Type",new Boolean(true),new Boolean(linkNode.isMailLink())); } /** * This bug occurs when there is a null pointer exception thrown while scanning a tag using LinkScanner. * Creation date: (7/1/2001 2:42:13 PM) */ public void testLinkNodeSingleQuoteBug() throws ParserException { createParser("<A HREF='abcd.html'>hello</A>","http://www.cj.com/"); parseAndAssertNodeCount(1); assertTrue("Node should be a LinkTag",node[0] instanceof LinkTag); LinkTag linkNode = (LinkTag)node[0]; assertEquals("Link incorrect","http://www.cj.com/abcd.html",linkNode.getLink()); } /** * The bug being reproduced is this : <BR> * <BODY aLink=#ff0000 bgColor=#ffffff link=#0000cc onload=setfocus() text=#000000 <BR> * vLink=#551a8b> * The above line is incorrectly parsed in that, the BODY tag is not identified. * Creation date: (6/17/2001 4:01:06 PM) */ public void testLinkTag() throws ParserException { createParser("<A HREF=\"test.html\">abcd</A>","http://www.google.com/test/index.html"); parseAndAssertNodeCount(1); // The node should be an LinkTag assertTrue("Node should be a LinkTag",node[0] instanceof LinkTag); LinkTag LinkTag = (LinkTag)node[0]; assertEquals("The image locn","http://www.google.com/test/test.html",LinkTag.getLink()); } /** * The bug being reproduced is this : <BR> * <BODY aLink=#ff0000 bgColor=#ffffff link=#0000cc onload=setfocus() text=#000000 <BR> * vLink=#551a8b> * The above line is incorrectly parsed in that, the BODY tag is not identified. * Creation date: (6/17/2001 4:01:06 PM) */ public void testLinkTagBug() throws ParserException { createParser("<A HREF=\"../test.html\">abcd</A>","http://www.google.com/test/index.html"); parseAndAssertNodeCount(1); // The node should be an LinkTag assertTrue("Node should be a LinkTag",node[0] instanceof LinkTag); LinkTag LinkTag = (LinkTag)node[0]; assertEquals("The image locn","http://www.google.com/test.html",LinkTag.getLink()); } /** * The bug being reproduced is this : <BR> * <A HREF=>Something<A><BR> * vLink=#551a8b> * The above line is incorrectly parsed in that, the BODY tag is not identified. * Creation date: (6/17/2001 4:01:06 PM) */ public void testNullTagBug() throws ParserException { createParser("<A HREF=>Something</A>","http://www.google.com/test/index.html"); parseAndAssertNodeCount(1); // The node should be an LinkTag assertTrue("Node should be a LinkTag",node[0] instanceof LinkTag); LinkTag linkTag = (LinkTag)node[0]; assertEquals("The link location","",linkTag.getLink()); assertEquals("The link text","Something",linkTag.getLinkText()); } public void testToPlainTextString() throws ParserException { createParser("<A HREF='mailto:somik@yahoo.com'>hello</A>","http://www.cj.com/"); parseAndAssertNodeCount(1); assertTrue("Node should be a LinkTag",node[0] instanceof LinkTag); LinkTag linkTag = (LinkTag)node[0]; assertEquals("Link Plain Text","hello",linkTag.toPlainTextString()); } public void testToHTML() throws ParserException { String link1 = "<A HREF='mailto:somik@yahoo.com'>hello</A>"; String link2 = "<a \n"+ "href=\"http://ads.samachar.com/bin/redirect/tech.txt?http://www.samachar.com/tech\n"+ "nical.html\"> Journalism 3.0</a>"; createParser(link1 + "\n"+ "<LI><font color=\"FF0000\" size=-1><b>Tech Samachar:</b></font>" + link2 + " by Rajesh Jain","http://www.cj.com/"); parser.setNodeFactory (new PrototypicalNodeFactory (new LinkTag ())); parseAndAssertNodeCount(10); assertTrue("First Node should be a LinkTag",node[0] instanceof LinkTag); LinkTag linkTag = (LinkTag)node[0]; assertStringEquals("Link Raw Text",link1,linkTag.toHtml()); assertTrue("Ninth Node should be a LinkTag",node[8] instanceof LinkTag); linkTag = (LinkTag)node[8]; assertStringEquals("Link Raw Text",link2,linkTag.toHtml()); } public void testTypeHttps() throws ParserException { LinkTag link; createParser ("<A HREF='https://www.someurl.com'>Try https.</A>","http://sourceforge.net"); parseAndAssertNodeCount (1); assertTrue ("Node should be a LinkTag", node[0] instanceof LinkTag); link = (LinkTag)node[0]; assertTrue("This is a https link",link.isHTTPSLink()); } public void testTypeFtp() throws ParserException { LinkTag link; createParser ("<A HREF='ftp://www.someurl.com'>Try ftp.</A>","http://sourceforge.net"); parseAndAssertNodeCount (1); assertTrue ("Node should be a LinkTag", node[0] instanceof LinkTag); link = (LinkTag)node[0]; assertTrue("This is an ftp link",link.isFTPLink()); } public void testTypeJavaScript() throws ParserException { LinkTag link; createParser ("<A HREF='javascript://www.someurl.com'>Try javascript.</A>","http://sourceforge.net"); parseAndAssertNodeCount (1); assertTrue ("Node should be a LinkTag", node[0] instanceof LinkTag); link = (LinkTag)node[0]; assertTrue("This is a javascript link",link.isJavascriptLink()); } public void testTypeHttpLink() throws ParserException
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -