1 19 20 33 package org.htmlparser.tests.scannersTests; 34 import org.htmlparser.scanners.MetaTagScanner; 35 import org.htmlparser.scanners.StyleScanner; 36 import org.htmlparser.scanners.TitleScanner; 37 import org.htmlparser.tags.TitleTag; 38 import org.htmlparser.tests.ParserTestCase; 39 import org.htmlparser.util.ParserException; 40 41 public class TitleScannerTest extends ParserTestCase 42 { 43 44 public TitleScannerTest(String name) 45 { 46 super(name); 47 } 48 49 public void testScan() throws ParserException 50 { 51 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>"); 52 TitleScanner titleScanner = new TitleScanner("-t"); 53 parser.addScanner(titleScanner); 54 parser.addScanner(new StyleScanner("-s")); 55 parser.addScanner(new MetaTagScanner("-m")); 56 parseAndAssertNodeCount(7); 57 assertTrue(node[2] instanceof TitleTag); 58 TitleTag titleTag = (TitleTag) node[2]; 60 assertEquals("Title", "Yahoo!", titleTag.getTitle()); 61 assertEquals("Title Scanner", titleScanner, titleTag.getThisScanner()); 62 } 63 64 69 public void testIncompleteTitle() throws ParserException 70 { 71 createParser( 72 "<TITLE>SISTEMA TERRA, VOL. VI , No. 1-3, December 1997</TITLE\n" 73 + "</HEAD>"); 74 TitleScanner titleScanner = new TitleScanner("-t"); 75 parser.addScanner(titleScanner); 76 parseAndAssertNodeCount(2); 77 assertTrue("First Node is a title tag", node[0] instanceof TitleTag); 78 TitleTag titleTag = (TitleTag) node[0]; 79 assertEquals( 80 "Title", 81 "SISTEMA TERRA, VOL. VI , No. 1-3, December 1997", 82 titleTag.getTitle()); 83 84 } 85 86 90 public void testDoubleTitleTag() throws ParserException 91 { 92 createParser( 93 "<html><head><TITLE>\n" 94 + "<html><head><TITLE>\n" 95 + "Double tags can hang the code\n" 96 + "</TITLE></head><body>\n" 97 + "<body><html>"); 98 TitleScanner titleScanner = new TitleScanner("-t"); 99 parser.addScanner(titleScanner); 100 parseAndAssertNodeCount(7); 101 assertTrue( 102 "Third tag should be a title tag", 103 node[2] instanceof TitleTag); 104 TitleTag titleTag = (TitleTag) node[2]; 105 assertEquals( 106 "Title", 107 "Double tags can hang the code\r\n", 108 titleTag.getTitle()); 109 110 } 111 112 116 public void testNoEndTitleTag() throws ParserException 117 { 118 createParser("<TITLE>KRP VALIDATION<PROCESS/TITLE>"); 119 TitleScanner titleScanner = new TitleScanner("-t"); 120 parser.addScanner(titleScanner); 121 parseAndAssertNodeCount(1); 122 TitleTag titleTag = (TitleTag) node[0]; 123 assertEquals("Expected title", "KRP VALIDATION", titleTag.getTitle()); 124 } 125 126 public void testTitleTagContainsJspTag() throws ParserException 127 { 128 createParser("<html><head><title><%=gTitleString%></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>"); 129 parser.registerScanners(); 130 parseAndAssertNodeCount(7); 131 assertTrue(node[2] instanceof TitleTag); 132 TitleTag titleTag = (TitleTag) node[2]; 133 assertStringEquals( 134 "HTML Rendering", 135 "<TITLE><%=gTitleString%></TITLE>", 136 titleTag.toHtml()); 137 } 138 } 139 | Popular Tags |