KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > htmlparser > tests > scannersTests > TitleScannerTest


1 // $Header: /home/cvs/jakarta-jmeter/src/htmlparser/org/htmlparser/tests/scannersTests/TitleScannerTest.java,v 1.2 2004/02/11 02:16:58 woolfel Exp $
2
/*
3  * ====================================================================
4  * Copyright 2002-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */

19
20 // The developers of JMeter and Apache are greatful to the developers
21
// of HTMLParser for giving Apache Software Foundation a non-exclusive
22
// license. The performance benefits of HTMLParser are clear and the
23
// users of JMeter will benefit from the hard work the HTMLParser
24
// team. For detailed information about HTMLParser, the project is
25
// hosted on sourceforge at http://htmlparser.sourceforge.net/.
26
//
27
// HTMLParser was originally created by Somik Raha in 2000. Since then
28
// a healthy community of users has formed and helped refine the
29
// design so that it is able to tackle the difficult task of parsing
30
// dirty HTML. Derrick Oswald is the current lead developer and was kind
31
// enough to assist JMeter.
32

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 JavaDoc 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         // check the title node
59
TitleTag titleTag = (TitleTag) node[2];
60         assertEquals("Title", "Yahoo!", titleTag.getTitle());
61         assertEquals("Title Scanner", titleScanner, titleTag.getThisScanner());
62     }
63
64     /**
65      * Testcase to reproduce a bug reported by Cedric Rosa,
66      * on not ending the title tag correctly, we would get
67      * null pointer exceptions..
68      */

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     /**
87      * If there are duplicates of the title tag, the parser crashes.
88      * This bug was reported by Claude Duguay
89      */

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     /**
113      * Testcase based on Claude Duguay's report. This proves
114      * that the parser throws exceptions when faced with malformed html
115      */

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