1 27 package org.htmlparser.tests.tagTests; 28 29 import org.htmlparser.PrototypicalNodeFactory; 30 import org.htmlparser.Tag; 31 import org.htmlparser.tags.BaseHrefTag; 32 import org.htmlparser.tags.HeadTag; 33 import org.htmlparser.tags.Html; 34 import org.htmlparser.tags.MetaTag; 35 import org.htmlparser.tags.StyleTag; 36 import org.htmlparser.tags.TitleTag; 37 import org.htmlparser.tests.ParserTestCase; 38 import org.htmlparser.util.ParserException; 39 40 public class TitleTagTest extends ParserTestCase { 41 42 static 43 { 44 System.setProperty ("org.htmlparser.tests.tagTests.TitleTagTest", "TitleTagTest"); 45 } 46 47 private TitleTag titleTag; 48 private String prefix = "<html><head>"; 49 private String tag1 = "<title>Yahoo!</title>"; 50 private String tag2 = "<base HREF=http://www.yahoo.com/ target=_top>"; 51 private String tag3 = "<meta http-equiv=\"PICS-Label\" content='(PICS-1.1 \"http://www.icra.org/ratingsv02.html\" l r (cz 1 lz 1 nz 1 oz 1 vz 1) gen true for \"http://www.yahoo.com\" r (cz 1 lz 1 nz 1 oz 1 vz 1) \"http://www.rsac.org/ratingsv01.html\" l r (n 0 s 0 v 0 l 0) gen true for \"http://www.yahoo.com\" r (n 0 s 0 v 0 l 0))'>"; 52 private String tag4 = "<style>a.h{background-color:#ffee99}</style>"; 53 private String suffix = "</head>"; 54 55 public TitleTagTest(String name) { 56 super(name); 57 } 58 protected void setUp() throws Exception { 59 super.setUp(); 60 createParser(prefix + tag1 + tag2 + tag3 + tag4 + suffix); 61 parseAndAssertNodeCount(1); 62 assertTrue("Only node should be an HTML node",node[0] instanceof Html); 63 Html html = (Html)node[0]; 64 assertTrue("HTML node should have one child",1 == html.getChildCount ()); 65 assertTrue("Only node should be an HEAD node",html.getChild(0) instanceof HeadTag); 66 HeadTag head = (HeadTag)html.getChild(0); 67 assertTrue("HEAD node should have four children",4 == head.getChildCount ()); 68 assertTrue("First child should be a title tag",head.getChild(0) instanceof TitleTag); 69 titleTag = (TitleTag)head.getChild(0); 70 } 71 public void testToPlainTextString() throws ParserException { 72 assertEquals("Title","Yahoo!",titleTag.toPlainTextString()); 74 } 75 76 public void testToHTML() throws ParserException { 77 assertStringEquals("Raw String",tag1,titleTag.toHtml()); 78 } 79 80 public void testToString() throws ParserException { 81 assertEquals("Title","TITLE: Yahoo!",titleTag.toString()); 82 } 83 84 public void testScan() throws ParserException { 85 createParser("<html><head><title>Yahoo!</title><base HREF=http://www.yahoo.com/ target=_top><meta http-equiv=\"PICS-Label\" content='(PICS-1.1 \"http://www.icra.org/ratingsv02.html\" l r (cz 1 lz 1 nz 1 oz 1 vz 1) gen true for \"http://www.yahoo.com\" r (cz 1 lz 1 nz 1 oz 1 vz 1) \"http://www.rsac.org/ratingsv01.html\" l r (n 0 s 0 v 0 l 0) gen true for \"http://www.yahoo.com\" r (n 0 s 0 v 0 l 0))'><style>a.h{background-color:#ffee99}</style></head>"); 86 parser.setNodeFactory ( 87 new PrototypicalNodeFactory ( 88 new Tag[] 89 { 90 new TitleTag (), 91 new StyleTag (), 92 new MetaTag (), 93 })); 94 parseAndAssertNodeCount(7); 95 assertTrue(node[2] instanceof TitleTag); 96 TitleTag titleTag = (TitleTag) node[2]; 98 assertEquals("Title","Yahoo!",titleTag.getTitle()); 99 } 100 101 106 public void testIncompleteTitle() throws ParserException { 107 String text = 108 "<HTML>\n"+ 109 "<HEAD>\n"+ 110 "<TITLE>SISTEMA TERRA, VOL. VI , No. 1-3, December 1997</TITLE\n"+ 112 "</HEAD>\n"+ 113 "<BODY>\n"+ 114 "The body.\n"+ 115 "</BODY>\n"+ 116 "</HTML>"; 117 createParser(text); 118 parseAndAssertNodeCount(1); 119 assertTrue ("Only node is a html tag",node[0] instanceof Html); 120 Html html = (Html)node[0]; 121 assertEquals ("Html node has five children", 5, html.getChildCount ()); 122 assertTrue ("Second child is a head tag", html.childAt (1) instanceof HeadTag); 123 HeadTag head = (HeadTag)html.childAt (1); 124 assertEquals ("Head node has two children", 2, head.getChildCount ()); 125 assertTrue ("Second child is a title tag", head.childAt (1) instanceof TitleTag); 126 TitleTag titleTag = (TitleTag)head.childAt (1); 127 assertEquals("Title","SISTEMA TERRA, VOL. VI , No. 1-3, December 1997",titleTag.getTitle()); 128 } 131 132 136 public void testDoubleTitleTag() throws ParserException{ 137 createParser( 138 "<html><head><TITLE>\n"+ 139 "<html><head><TITLE>\n"+ 140 "Double tags can hang the code\n"+ 141 "</TITLE></head><body>\n"+ 142 "<body><html>"); 143 parser.setNodeFactory (new PrototypicalNodeFactory (new TitleTag ())); 144 parseAndAssertNodeCount(9); 145 assertTrue("Third tag should be a title tag",node[2] instanceof TitleTag); 146 TitleTag titleTag = (TitleTag)node[2]; 147 assertEquals("Title","\n",titleTag.getTitle()); 148 assertTrue("Fourth tag should be a title tag",node[3] instanceof TitleTag); 149 titleTag = (TitleTag)node[3]; 150 assertEquals("Title","\nDouble tags can hang the code\n",titleTag.getTitle()); 151 } 152 153 157 public void testNoEndTitleTag() throws ParserException { 158 createParser( 159 "<TITLE>KRP VALIDATION<PROCESS/TITLE>"); 160 parseAndAssertNodeCount(1); 161 TitleTag titleTag = (TitleTag)node[0]; 162 assertEquals("Expected title","KRP VALIDATION",titleTag.getTitle()); 163 } 164 165 public void testTitleTagContainsJspTag() throws ParserException { 166 String title = "<title><%=gTitleString%></title>"; 167 createParser("<html><head>" + title + "<base HREF=http://www.yahoo.com/ target=_top><meta http-equiv=\"PICS-Label\" content='(PICS-1.1 \"http://www.icra.org/ratingsv02.html\" l r (cz 1 lz 1 nz 1 oz 1 vz 1) gen true for \"http://www.yahoo.com\" r (cz 1 lz 1 nz 1 oz 1 vz 1) \"http://www.rsac.org/ratingsv01.html\" l r (n 0 s 0 v 0 l 0) gen true for \"http://www.yahoo.com\" r (n 0 s 0 v 0 l 0))'><style>a.h{background-color:#ffee99}</style></head>"); 168 parser.setNodeFactory ( 169 new PrototypicalNodeFactory ( 170 new Tag[] 171 { 172 new TitleTag (), 173 new BaseHrefTag (), 174 new MetaTag (), 175 new StyleTag (), 176 })); 177 parseAndAssertNodeCount(7); 178 assertTrue(node[2] instanceof TitleTag); 179 TitleTag titleTag = (TitleTag) node[2]; 180 assertStringEquals("HTML Rendering",title,titleTag.toHtml()); 181 } 182 } 183
| Popular Tags
|