📄 linktagtest.java
字号:
{ LinkTag link; createParser ("<A HREF='http://www.someurl.com'>Try http.</A>","http://sourceforge.net"); parseAndAssertNodeCount (1); assertTrue ("Node should be a LinkTag", node[0] instanceof LinkTag); link = (LinkTag)node[0]; assertTrue("This is a http link : "+link.getLink(),link.isHTTPLink()); } public void testRelativeTypeHttpLink() throws ParserException { LinkTag link; createParser ("<A HREF='somePage.html'>Try relative http.</A>","http://sourceforge.net"); parseAndAssertNodeCount (1); assertTrue ("Node should be a LinkTag", node[0] instanceof LinkTag); link = (LinkTag)node[0]; assertTrue("This relative link is also a http link : "+link.getLink(),link.isHTTPLink()); } public void testTypeNonHttp() throws ParserException { LinkTag link; createParser ("<A HREF='ftp://www.someurl.com'>Try non-http.</A>","http://sourceforge.net"); parseAndAssertNodeCount (1); assertTrue ("Node should be a LinkTag", node[0] instanceof LinkTag); link = (LinkTag)node[0]; assertTrue("This is not a http link : "+link.getLink(),!link.isHTTPLink()); } public void testTypeHttpLikeLink() throws ParserException { LinkTag link; createParser ("<A HREF='http://'>Try basic http.</A>","http://sourceforge.net"); parseAndAssertNodeCount (1); assertTrue ("Node should be a LinkTag", node[0] instanceof LinkTag); link = (LinkTag)node[0]; assertTrue("This is a http link",link.isHTTPLikeLink()); 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.isHTTPLikeLink()); } /** * Test mail link. * Bug #738504 MailLink != HTTPLink */ public void testMailToIsNotAHTTPLink () throws ParserException { LinkTag link; createParser ("<A HREF='mailto:derrickoswald@users.sourceforge.net'>Derrick</A>","http://sourceforge.net"); parseAndAssertNodeCount (1); assertTrue ("Node should be a LinkTag", node[0] instanceof LinkTag); link = (LinkTag)node[0]; assertTrue ("bug #738504 MailLink != HTTPLink", !link.isHTTPLink ()); assertTrue ("bug #738504 MailLink != HTTPSLink", !link.isHTTPSLink ()); } /** * Bug #784767 irc://server/channel urls are HTTPLike? */ public void testIrcIsNotAHTTPLink () throws ParserException { LinkTag link; createParser ("<A HREF='irc://server/channel'>Try irc.</A>","http://sourceforge.net"); parseAndAssertNodeCount (1); assertTrue ("Node should be a LinkTag", node[0] instanceof LinkTag); link = (LinkTag)node[0]; assertTrue("This is not a http link", !link.isHTTPLikeLink ()); } public void testAccessKey() throws ParserException { createParser("<a href=\"http://www.kizna.com/servlets/SomeServlet?name=Sam Joseph\" accessKey=1>Click Here</A>"); parseAndAssertNodeCount(1); assertTrue("The node should be a link tag",node[0] instanceof LinkTag); LinkTag linkTag = (LinkTag)node[0]; assertEquals("Link URL of link tag","http://www.kizna.com/servlets/SomeServlet?name=Sam Joseph",linkTag.getLink()); assertEquals("Link Text of link tag","Click Here",linkTag.getLinkText()); assertEquals("Access key","1",linkTag.getAccessKey()); } public void testErroneousLinkBug() throws ParserException { createParser( "Site Comments?<br>" + "<a href=\"mailto:sam@neurogrid.com?subject=Site Comments\">" + "Mail Us" + "<a>" ); parseAndAssertNodeCount(4); // The first node should be a Text assertTrue("First node should be a Text",node[0] instanceof Text); Text stringNode = (Text)node[0]; assertEquals("Text of the Text","Site Comments?",stringNode.getText()); assertTrue("Second node should be a tag",node[1] instanceof Tag); assertTrue("Third node should be a link",node[2] instanceof LinkTag); // LinkScanner.evaluate() says no HREF means it isn't a link: assertTrue("Fourth node should be a tag",node[3] instanceof Tag); } /** * Test case based on a report by Raghavender Srimantula, of the parser giving out of memory exceptions. Found to occur * on the following piece of html * <pre> * <a href=s/8741><img src="http://us.i1.yimg.com/us.yimg.com/i/i16/mov_popc.gif" height=16 width=16 border=0></img></td><td nowrap> * <a href=s/7509> * </pre> */ public void testErroneousLinkBugFromYahoo2() throws ParserException { String link = "<a href=s/8741>" + "<img src=\"http://us.i1.yimg.com/us.yimg.com/i/i16/mov_popc.gif\" height=16 width=16 border=0>"; createParser( "<td>" + link + "</td>" + "<td nowrap> \n"+ "<a href=s/7509><b>Yahoo! Movies</b></a>" + "</td>","http://www.yahoo.com"); NodeList linkNodes = parser.extractAllNodesThatMatch (new NodeClassFilter (LinkTag.class)); assertEquals("number of links", 2, linkNodes.size ()); LinkTag linkTag = (LinkTag)linkNodes.elementAt (0); assertStringEquals("Link","http://www.yahoo.com/s/8741",linkTag.getLink()); // Verify the link data assertStringEquals("Link Text","",linkTag.getLinkText()); // Verify the reconstruction html assertStringEquals("toHTML",link + "</a>",linkTag.toHtml()); } /** * Test case based on a report by Raghavender Srimantula, of the parser giving out of memory exceptions. Found to occur * on the following piece of html * <pre> * <a href=s/8741><img src="http://us.i1.yimg.com/us.yimg.com/i/i16/mov_popc.gif" height=16 width=16 border=0></img>This is test * <a href=s/7509> * </pre> */ public void testErroneousLinkBugFromYahoo() throws ParserException { String link = "<a href=s/8741>" + "<img src=\"http://us.i1.yimg.com/us.yimg.com/i/i16/mov_popc.gif\" " + "height=16 " + "width=16 " + "border=0>" + "This is a test\n"; createParser( link + "<a href=s/7509>" + "<b>Yahoo! Movies</b>" + "</a>", "http://www.yahoo.com" ); parseAndAssertNodeCount(2); assertTrue("First node should be a LinkTag",node[0] instanceof LinkTag); assertTrue("Second node should be a LinkTag",node[1] instanceof LinkTag); LinkTag linkTag = (LinkTag)node[0]; assertEquals("Link","http://www.yahoo.com/s/8741",linkTag.getLink()); // Verify the link data assertEquals("Link Text","This is a test\n",linkTag.getLinkText()); // Verify the reconstruction html assertStringEquals("toHTML()",link + "</a>",linkTag.toHtml()); } /** * This is the reproduction of a bug which produces multiple text copies. */ public void testExtractLinkInvertedCommasBug2() throws ParserException { createParser("<a href=\"http://cbc.ca/artsCanada/stories/greatnorth271202\" class=\"lgblacku\">Vancouver schools plan 'Great Northern Way'</a>"); parseAndAssertNodeCount(1); assertTrue("The node should be a link tag",node[0] instanceof LinkTag); LinkTag linkTag = (LinkTag)node[0]; assertStringEquals("Extracted Text","Vancouver schools plan 'Great Northern Way'", linkTag.getLinkText ()); } /** * Bug pointed out by Sam Joseph (sam@neurogrid.net) * Links with spaces in them will get their spaces absorbed */ public void testLinkSpacesBug() throws ParserException{ createParser("<a href=\"http://www.kizna.com/servlets/SomeServlet?name=Sam Joseph\">Click Here</A>"); parseAndAssertNodeCount(1); assertTrue("The node should be a link tag",node[0] instanceof LinkTag); LinkTag linkTag = (LinkTag)node[0]; assertEquals("Link URL of link tag","http://www.kizna.com/servlets/SomeServlet?name=Sam Joseph",linkTag.getLink()); assertEquals("Link Text of link tag","Click Here",linkTag.getLinkText()); } /** * Bug reported by Raj Sharma,5-Apr-2002, upon parsing * http://www.samachar.com, the entire page could not be picked up. * The problem was occurring after parsing a particular link * after which the parsing would not proceed. This link was spread over three lines. * The bug has been reproduced and fixed. */ public void testMultipleLineBug() throws ParserException { createParser("<LI><font color=\"FF0000\" size=-1><b>Tech Samachar:</b></font><a \n"+ "href=\"http://ads.samachar.com/bin/redirect/tech.txt?http://www.samachar.com/tech\n"+ "nical.html\"> Journalism 3.0</a> by Rajesh Jain"); parser.setNodeFactory (new PrototypicalNodeFactory (new LinkTag ())); parseAndAssertNodeCount(8); assertTrue("Seventh node should be a link tag",node[6] instanceof LinkTag); LinkTag linkTag = (LinkTag)node[6]; String exp = new String("http://ads.samachar.com/bin/redirect/tech.txt?http://www.samachar.com/technical.html"); //assertEquals("Length of link tag",exp.length(), linkTag.getLink().length()); assertStringEquals("Link URL of link tag",exp,linkTag.getLink()); assertEquals("Link Text of link tag"," Journalism 3.0",linkTag.getLinkText()); assertTrue("Eight node should be a string node",node[7] instanceof Text); Text stringNode = (Text)node[7]; assertEquals("String node contents"," by Rajesh Jain",stringNode.getText()); } public void testRelativeLinkScan() throws ParserException { createParser("<A HREF=\"mytest.html\"> Hello World</A>","http://www.yahoo.com"); parseAndAssertNodeCount(1); assertTrue("Node identified should be HTMLLinkTag",node[0] instanceof LinkTag); LinkTag linkTag = (LinkTag)node[0]; assertEquals("Expected Link","http://www.yahoo.com/mytest.html",linkTag.getLink()); } public void testRelativeLinkScan2() throws ParserException { createParser("<A HREF=\"abc/def/mytest.html\"> Hello World</A>","http://www.yahoo.com"); parseAndAssertNodeCount(1); assertTrue("Node identified should be HTMLLinkTag",node[0] instanceof LinkTag); LinkTag linkTag = (LinkTag)node[0]; assertStringEquals("Expected Link","http://www.yahoo.com/abc/def/mytest.html",linkTag.getLink()); } public void testRelativeLinkScan3() throws ParserException { createParser("<A HREF=\"../abc/def/mytest.html\"> Hello World</A>","http://www.yahoo.com/ghi"); parseAndAssertNodeCount(1); assertTrue("Node identified should be HTMLLinkTag",node[0] instanceof LinkTag); LinkTag linkTag = (LinkTag)node[0]; assertStringEquals("Expected Link","http://www.yahoo.com/abc/def/mytest.html",linkTag.getLink()); } /** * Test scan with data which is of diff nodes type */ public void testScan() throws ParserException { createParser("<A HREF=\"mytest.html\"><IMG SRC=\"abcd.jpg\">Hello World</A>","http://www.yahoo.com"); parser.setNodeFactory ( new PrototypicalNodeFactory ( new Tag[] { new LinkTag (), new ImageTag (), })); parseAndAssertNodeCount(1); assertTrue("Node should be a link node",node[0] instanceof LinkTag); LinkTag linkTag = (LinkTag)node[0]; // Get the link data and cross-check Node [] dataNode= new Node[10]; int i = 0; for (SimpleNodeIterator e = linkTag.children();e.hasMoreNodes();) { dataNode[i++] = e.nextNode(); } assertEquals("Number of data nodes",new Integer(2),new Integer(i)); assertTrue("First data node should be an Image Node",dataNode[0] instanceof ImageTag); assertTrue("Second data node shouls be a String Node",dataNode[1] instanceof Text); // Check the contents of each data node ImageTag imageTag = (ImageTag)dataNode[0]; assertEquals("Image URL","http://www.yahoo.com/abcd.jpg",imageTag.getImageURL()); Text stringNode = (Text)dataNode[1]; assertEquals("String Contents","Hello World",stringNode.getText()); } /** * A bug in the freshmeat page - really bad html * tag - <A>Revision<\a> * Reported by Mazlan Mat * Note: Actually, this is completely legal HTML - Derrick */ public void testFreshMeatBug() throws ParserException { String html = "<a>Revision</a>"; createParser(html,"http://www.yahoo.com"); parseAndAssertNodeCount(1); assertTrue("Node 0 should be a tag",node[0] instanceof Tag); Tag tag = (Tag)node[0];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -