📄 compositetagscannertest.java
字号:
// HTMLParser Library $Name: v1_6_20051112 $ - 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/scannersTests/CompositeTagScannerTest.java,v $// $Author: derrickoswald $// $Date: 2004/07/31 16:42:32 $// $Revision: 1.62 $//// 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.scannersTests;import org.htmlparser.Node;import org.htmlparser.PrototypicalNodeFactory;import org.htmlparser.Tag;import org.htmlparser.Text;import org.htmlparser.nodes.AbstractNode;import org.htmlparser.scanners.CompositeTagScanner;import org.htmlparser.tags.CompositeTag;import org.htmlparser.tags.Div;import org.htmlparser.tags.LinkTag;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;public class CompositeTagScannerTest extends ParserTestCase { static { System.setProperty ("org.htmlparser.tests.scannersTests.CompositeTagScannerTest", "CompositeTagScannerTest"); } public CompositeTagScannerTest(String name) { super(name); } private CustomTag parseCustomTag(int expectedNodeCount) throws ParserException { parser.setNodeFactory (new PrototypicalNodeFactory (new CustomTag ())); parseAndAssertNodeCount(expectedNodeCount); assertType("node",CustomTag.class,node[0]); CustomTag customTag = (CustomTag)node[0]; return customTag; } public void testEmptyCompositeTag() throws ParserException { String html = "<Custom/>"; createParser(html); CustomTag customTag = parseCustomTag(1); assertEquals("child count",0,customTag.getChildCount()); assertTrue("custom tag should be xml end tag",customTag.isEmptyXmlTag()); assertEquals("starting loc",0,customTag.getStartPosition ()); assertEquals("ending loc",9,customTag.getEndPosition ()); assertEquals("starting line position",0,customTag.getStartingLineNumber()); assertEquals("ending line position",0,customTag.getEndingLineNumber()); assertStringEquals("html",html,customTag.toHtml()); } public void testEmptyCompositeTagAnotherStyle() throws ParserException { String html = "<Custom></Custom>"; createParser(html); CustomTag customTag = parseCustomTag(1); assertEquals("child count",0,customTag.getChildCount()); assertFalse("custom tag should not be xml end tag",customTag.isEmptyXmlTag()); assertEquals("starting loc",0,customTag.getStartPosition ()); assertEquals("ending loc",8,customTag.getEndPosition ()); assertEquals("starting line position",0,customTag.getStartingLineNumber()); assertEquals("ending line position",0,customTag.getEndingLineNumber()); assertEquals("html",html,customTag.toHtml()); } public void testCompositeTagWithOneTextChild() throws ParserException { String html = "<Custom>" + "Hello" + "</Custom>"; createParser(html); CustomTag customTag = parseCustomTag(1); assertEquals("child count",1,customTag.getChildCount()); assertFalse("custom tag should not be xml end tag",customTag.isEmptyXmlTag()); assertEquals("starting loc",0,customTag.getStartPosition ()); assertEquals("ending loc",8,customTag.getEndPosition ()); assertEquals("starting line position",0,customTag.getStartingLineNumber()); assertEquals("ending line position",0,customTag.getEndingLineNumber()); Node child = customTag.childAt(0); assertType("child",Text.class,child); assertStringEquals("child text","Hello",child.toPlainTextString()); } public void testCompositeTagWithTagChild() throws ParserException { String childtag = "<Hello>"; createParser( "<Custom>" + childtag + "</Custom>" ); CustomTag customTag = parseCustomTag(1); assertEquals("child count",1,customTag.getChildCount()); assertFalse("custom tag should not be xml end tag",customTag.isEmptyXmlTag()); assertEquals("starting loc",0,customTag.getStartPosition ()); assertEquals("ending loc",8,customTag.getEndPosition ()); assertEquals("custom tag starting loc",0,customTag.getStartPosition ()); assertEquals("custom tag ending loc",24,customTag.getEndTag ().getEndPosition ()); Node child = customTag.childAt(0); assertType("child",Tag.class,child); assertStringEquals("child html",childtag,child.toHtml()); } public void testCompositeTagWithAnotherTagChild() throws ParserException { String childtag = "<Another/>"; createParser( "<Custom>" + childtag + "</Custom>" ); parser.setNodeFactory ( new PrototypicalNodeFactory ( new Tag[] { new CustomTag (), new AnotherTag (true), })); parseAndAssertNodeCount(1); assertType("node",CustomTag.class,node[0]); CustomTag customTag = (CustomTag)node[0]; assertEquals("child count",1,customTag.getChildCount()); assertFalse("custom tag should not be xml end tag",customTag.isEmptyXmlTag()); assertEquals("starting loc",0,customTag.getStartPosition ()); assertEquals("ending loc",8,customTag.getEndPosition ()); assertEquals("custom tag starting loc",0,customTag.getStartPosition ()); assertEquals("custom tag ending loc",27,customTag.getEndTag ().getEndPosition ()); Node child = customTag.childAt(0); assertType("child",AnotherTag.class,child); AnotherTag tag = (AnotherTag)child; assertEquals("another tag start pos",8,tag.getStartPosition ()); assertEquals("another tag ending pos",18,tag.getEndPosition ()); assertEquals("custom end tag start pos",18,customTag.getEndTag().getStartPosition ()); assertStringEquals("child html",childtag,child.toHtml()); } public void testParseTwoCompositeTags() throws ParserException { createParser( "<Custom>" + "</Custom>" + "<Custom/>" ); parser.setNodeFactory (new PrototypicalNodeFactory (new CustomTag ())); parseAndAssertNodeCount(2); assertType("tag 1",CustomTag.class,node[0]); assertType("tag 2",CustomTag.class,node[1]); } public void testXmlTypeCompositeTags() throws ParserException { createParser( "<Custom>" + "<Another name=\"subtag\"/>" + "<Custom />" + "</Custom>" + "<Custom/>" ); parser.setNodeFactory ( new PrototypicalNodeFactory ( new Tag[] { new CustomTag (), new AnotherTag (false), })); parseAndAssertNodeCount(2); assertType("first node",CustomTag.class,node[0]); assertType("second node",CustomTag.class,node[1]); CustomTag customTag = (CustomTag)node[0]; Node node = customTag.childAt(0); assertType("first child",AnotherTag.class,node); node = customTag.childAt(1); assertType("second child",CustomTag.class,node); } public void testCompositeTagWithNestedTag() throws ParserException { createParser( "<Custom>" + "<Another>" + "Hello" + "</Another>" + "<Custom/>" + "</Custom>" + "<Custom/>" ); parser.setNodeFactory ( new PrototypicalNodeFactory ( new Tag[] { new CustomTag (), new AnotherTag (false), })); parseAndAssertNodeCount(2); assertType("first node",CustomTag.class,node[0]); assertType("second node",CustomTag.class,node[1]); CustomTag customTag = (CustomTag)node[0]; Node node = customTag.childAt(0); assertType("first child",AnotherTag.class,node); AnotherTag anotherTag = (AnotherTag)node; assertEquals("another tag children count",1,anotherTag.getChildCount()); node = anotherTag.childAt(0); assertType("nested child",Text.class,node); Text text = (Text)node; assertEquals("text","Hello",text.toPlainTextString()); } public void testCompositeTagWithTwoNestedTags() throws ParserException { createParser( "<Custom>" + "<Another>" + "Hello" + "</Another>" + "<unknown>" + "World" + "</unknown>" + "<Custom/>" + "</Custom>" + "<Custom/>" ); parser.setNodeFactory ( new PrototypicalNodeFactory ( new Tag[] { new CustomTag (), new AnotherTag (false), })); parseAndAssertNodeCount(2); assertType("first node",CustomTag.class,node[0]); assertType("second node",CustomTag.class,node[1]); CustomTag customTag = (CustomTag)node[0]; assertEquals("first custom tag children count",5,customTag.getChildCount()); Node node = customTag.childAt(0); assertType("first child",AnotherTag.class,node); AnotherTag anotherTag = (AnotherTag)node; assertEquals("another tag children count",1,anotherTag.getChildCount()); node = anotherTag.childAt(0); assertType("nested child",Text.class,node); Text text = (Text)node; assertEquals("text","Hello",text.toPlainTextString()); } public void testErroneousCompositeTag() throws ParserException { String html = "<custom>"; createParser(html); CustomTag customTag = parseCustomTag(1); assertEquals("child count",0,customTag.getChildCount()); assertFalse("custom tag should not be xml end tag",customTag.isEmptyXmlTag()); assertEquals("starting loc",0,customTag.getStartPosition ()); assertEquals("ending loc",8,customTag.getEndPosition ()); assertEquals("starting line position",0,customTag.getStartingLineNumber()); assertEquals("ending line position",0,customTag.getEndingLineNumber()); assertStringEquals("html",html + "</custom>",customTag.toHtml()); } public void testErroneousCompositeTagWithChildren() throws ParserException { String html = "<custom>" + "<firstChild>" + "<secondChild>"; createParser(html); CustomTag customTag = parseCustomTag(1); assertEquals("child count",2,customTag.getChildCount()); assertFalse("custom tag should not be xml end tag",customTag.isEmptyXmlTag()); assertEquals("starting loc",0,customTag.getStartPosition ()); assertEquals("ending loc",8,customTag.getEndPosition ()); assertEquals("starting line position",0,customTag.getStartingLineNumber()); assertEquals("ending line position",0,customTag.getEndingLineNumber()); assertStringEquals("html",html + "</custom>",customTag.toHtml()); } public void testErroneousCompositeTagWithChildrenAndLineBreak() throws ParserException { String html = "<custom>" + "<firstChild>" + "\n" + "<secondChild>"; createParser(html); CustomTag customTag = parseCustomTag(1); assertEquals("child count",3,customTag.getChildCount()); assertFalse("custom tag should not be xml end tag",customTag.isEmptyXmlTag()); assertEquals("starting loc",0,customTag.getStartPosition ()); assertEquals("ending loc",8,customTag.getEndPosition ()); assertEquals("starting line position",0,customTag.getStartingLineNumber()); assertEquals("ending line position",1,customTag.getEndTag ().getEndingLineNumber()); assertStringEquals("html", html + "</custom>", customTag.toHtml() ); } public void testTwoConsecutiveErroneousCompositeTags() throws ParserException { String tag1 = "<custom>something"; String tag2 = "<custom></endtag>"; createParser(tag1 + tag2); parser.setNodeFactory (new PrototypicalNodeFactory (new CustomTag (false))); parseAndAssertNodeCount(2); CustomTag customTag = (CustomTag)node[0]; assertEquals("child count",1,customTag.getChildCount()); assertFalse("custom tag should not be xml end tag",customTag.isEmptyXmlTag()); assertEquals("starting loc",0,customTag.getStartPosition ()); assertEquals("ending loc",8,customTag.getEndPosition ()); assertEquals("ending loc of custom tag",17,customTag.getEndTag ().getEndPosition ()); assertEquals("starting line position",0,customTag.getStartingLineNumber()); assertEquals("ending line position",0,customTag.getEndTag ().getEndingLineNumber()); assertStringEquals("1st custom tag", tag1 + "</custom>", customTag.toHtml()); customTag = (CustomTag)node[1]; assertStringEquals("2nd custom tag", tag2 + "</custom>", customTag.toHtml()); } public void testCompositeTagWithErroneousAnotherTagAndLineBreak() throws ParserException { String another = "<another>"; String custom = "<custom>\n</custom>"; createParser( another + custom ); parser.setNodeFactory ( new PrototypicalNodeFactory ( new Tag[] { new CustomTag (), new AnotherTag (false), })); parseAndAssertNodeCount(2); AnotherTag anotherTag = (AnotherTag)node[0]; assertEquals("another tag child count",0,anotherTag.getChildCount()); CustomTag customTag = (CustomTag)node[1]; assertEquals("child count",1,customTag.getChildCount()); assertFalse("custom tag should not be xml end tag",customTag.isEmptyXmlTag()); assertEquals("starting loc",9,customTag.getStartPosition ()); assertEquals("ending loc",17,customTag.getEndPosition ()); assertEquals("starting line position",0,customTag.getStartingLineNumber()); assertEquals("ending line position",1,customTag.getEndTag ().getEndingLineNumber()); assertStringEquals("another tag html",another + "</another>",anotherTag.toHtml()); assertStringEquals("custom tag html",custom,customTag.toHtml()); } public void testCompositeTagWithErroneousAnotherTag() throws ParserException { createParser( "<custom>" + "<another>" + "</custom>" ); parser.setNodeFactory ( new PrototypicalNodeFactory ( new Tag[] { new CustomTag (), new AnotherTag (true), })); parseAndAssertNodeCount(1); assertType("node",CustomTag.class,node[0]); CustomTag customTag = (CustomTag)node[0]; assertEquals("child count",1,customTag.getChildCount()); assertFalse("custom tag should be xml end tag",customTag.isEmptyXmlTag()); assertEquals("starting loc",0,customTag.getStartPosition ()); assertEquals("ending loc",8,customTag.getEndPosition ()); AnotherTag anotherTag = (AnotherTag)customTag.childAt(0); assertEquals("another tag ending loc",17,anotherTag.getEndPosition ()); assertEquals("starting line position",0,customTag.getStartingLineNumber()); assertEquals("ending line position",0,customTag.getEndingLineNumber()); assertStringEquals("html","<custom><another></another></custom>",customTag.toHtml()); } public void testCompositeTagWithDeadlock() throws ParserException { createParser( "<custom>" + "<another>something" + "</custom>"+ "<custom>" + "<another>else</another>" + "</custom>" ); parser.setNodeFactory ( new PrototypicalNodeFactory (
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -