KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > htmlparser > tests > tagTests > TitleTagTest


1 // HTMLParser Library $Name: v1_5_20050313 $ - A java-based parser for HTML
2
// http://sourceforge.org/projects/htmlparser
3
// Copyright (C) 2004 Somik Raha
4
//
5
// Revision Control Information
6
//
7
// $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tests/tagTests/TitleTagTest.java,v $
8
// $Author: derrickoswald $
9
// $Date: 2004/07/02 00:49:31 $
10
// $Revision: 1.36 $
11
//
12
// This library is free software; you can redistribute it and/or
13
// modify it under the terms of the GNU Lesser General Public
14
// License as published by the Free Software Foundation; either
15
// version 2.1 of the License, or (at your option) any later version.
16
//
17
// This library is distributed in the hope that it will be useful,
18
// but WITHOUT ANY WARRANTY; without even the implied warranty of
19
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20
// Lesser General Public License for more details.
21
//
22
// You should have received a copy of the GNU Lesser General Public
23
// License along with this library; if not, write to the Free Software
24
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
//
26

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 JavaDoc prefix = "<html><head>";
49     private String JavaDoc tag1 = "<title>Yahoo!</title>";
50     private String JavaDoc tag2 = "<base HREF=http://www.yahoo.com/ target=_top>";
51     private String JavaDoc 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 JavaDoc tag4 = "<style>a.h{background-color:#ffee99}</style>";
53     private String JavaDoc suffix = "</head>";
54
55     public TitleTagTest(String JavaDoc name) {
56         super(name);
57     }
58     protected void setUp() throws Exception JavaDoc {
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         // check the title node
73
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         // check the title node
97
TitleTag titleTag = (TitleTag) node[2];
98         assertEquals("Title","Yahoo!",titleTag.getTitle());
99     }
100
101     /**
102      * Testcase to reproduce a bug reported by Cedric Rosa,
103      * on not ending the title tag correctly, we would get
104      * null pointer exceptions..
105      */

106     public void testIncompleteTitle() throws ParserException {
107         String JavaDoc text =
108             "<HTML>\n"+
109             "<HEAD>\n"+
110             // note the missing angle bracket on the close title:
111
"<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 // Note: this will fail because of the extra > inserted to finish the /TITLE tag:
129
// assertStringEquals ("toHtml", text, html.toHtml ());
130
}
131
132     /**
133      * If there are duplicates of the title tag, the parser crashes.
134      * This bug was reported by Claude Duguay
135      */

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

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 JavaDoc 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