KickJava   Java API By Example, From Geeks To Geeks.

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


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/MetaTagTest.java,v $
8
// $Author: derrickoswald $
9
// $Date: 2004/07/02 00:49:31 $
10
// $Revision: 1.39 $
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.HeadTag;
32 import org.htmlparser.tags.Html;
33 import org.htmlparser.tags.LinkTag;
34 import org.htmlparser.tags.MetaTag;
35 import org.htmlparser.tags.TitleTag;
36 import org.htmlparser.tests.ParserTestCase;
37 import org.htmlparser.util.ParserException;
38
39 public class MetaTagTest extends ParserTestCase {
40
41     static
42     {
43         System.setProperty ("org.htmlparser.tests.tagTests.MetaTagTest", "MetaTagTest");
44     }
45
46     public MetaTagTest(String JavaDoc name) {
47         super(name);
48     }
49
50     public void testToHTML() throws ParserException {
51         String JavaDoc description = "description";
52         String JavaDoc content = "Protecting the internet community through technology, not legislation. SpamCop eliminates spam. Automatically file spam reports with the network administrators who can stop spam at the source. Subscribe, and filter your email through powerful statistical analysis before it reaches your inbox.";
53         String JavaDoc tag = "<META name=\"" + description + "\" content=\"" + content + "\">";
54         createParser(
55         "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\">\n"+
56         "<html>\n"+
57         "<head><title>SpamCop - Welcome to SpamCop\n"+
58         "</title>\n"+
59         tag + "\n"+
60         "<META name=\"keywords\" content=\"SpamCop spam cop email filter abuse header headers parse parser utility script net net-abuse filter mail program system trace traceroute dns\">\n"+
61         "<META name=\"language\" content=\"en\">\n"+
62         "<META name=\"owner\" content=\"service@admin.spamcop.net\">\n"+
63         "<META HTTP-EQUIV=\"content-type\" CONTENT=\"text/html; charset=ISO-8859-1\">");
64         parseAndAssertNodeCount(3);
65         assertTrue("Third node should be an HTML node",node[2] instanceof Html);
66         Html html = (Html)node[2];
67         assertTrue("HTML node should have two children",2 == html.getChildCount ());
68         assertTrue("Second node should be an HEAD node",html.getChild(1) instanceof HeadTag);
69         HeadTag head = (HeadTag)html.getChild(1);
70         assertTrue("HEAD node should have eleven children",11 == head.getChildCount ());
71         assertTrue("Third child should be a title tag",head.getChild(2) instanceof MetaTag);
72         MetaTag metaTag = (MetaTag)head.getChild(2);
73         assertStringEquals("Meta Tag Name",description,metaTag.getMetaTagName());
74         assertStringEquals("Meta Tag Contents",content,metaTag.getMetaContent());
75         assertStringEquals("toHTML()",tag,metaTag.toHtml());
76     }
77
78     public void testScan() throws ParserException {
79         String JavaDoc description = "description";
80         String JavaDoc content = "Protecting the internet community through technology, not legislation. SpamCop eliminates spam. Automatically file spam reports with the network administrators who can stop spam at the source. Subscribe, and filter your email through powerful statistical analysis before it reaches your inbox.";
81         String JavaDoc tag = "<META name=\"" + description + "\" content=\"" + content + "\">";
82         createParser(
83         "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\">\n"+
84         "<html>\n"+
85         "<head><title>SpamCop - Welcome to SpamCop\n"+
86         "</title>\n"+
87         tag + "\n"+
88         "<META name=\"keywords\" content=\"SpamCop spam cop email filter abuse header headers parse parser utility script net net-abuse filter mail program system trace traceroute dns\">\n"+
89         "<META name=\"language\" content=\"en\">\n"+
90         "<META name=\"owner\" content=\"service@admin.spamcop.net\">\n"+
91         "<META HTTP-EQUIV=\"content-type\" CONTENT=\"text/html; charset=ISO-8859-1\">");
92         parser.setNodeFactory (new PrototypicalNodeFactory (new MetaTag ()));
93         parseAndAssertNodeCount(18);
94         assertTrue("Node 8 should be End Tag",node[7] instanceof Tag && ((Tag)node[7]).isEndTag ());
95         assertTrue("Node 10 should be META Tag",node[9] instanceof MetaTag);
96         MetaTag metaTag;
97         metaTag = (MetaTag) node[9];
98         assertEquals("Meta Tag 10 Name",description,metaTag.getMetaTagName());
99         assertEquals("Meta Tag 10 Contents",content,metaTag.getMetaContent());
100
101         assertTrue("Node 12 should be META Tag",node[11] instanceof MetaTag);
102         assertTrue("Node 14 should be META Tag",node[13] instanceof MetaTag);
103         assertTrue("Node 16 should be META Tag",node[15] instanceof MetaTag);
104         assertTrue("Node 18 should be META Tag",node[17] instanceof MetaTag);
105
106         metaTag = (MetaTag) node[11];
107         assertEquals("Meta Tag 12 Name","keywords",metaTag.getMetaTagName());
108         assertEquals("Meta Tag 12 Contents","SpamCop spam cop email filter abuse header headers parse parser utility script net net-abuse filter mail program system trace traceroute dns",metaTag.getMetaContent());
109         assertNull("Meta Tag 12 Http-Equiv",metaTag.getHttpEquiv());
110
111         metaTag = (MetaTag) node[13];
112         assertEquals("Meta Tag 14 Name","language",metaTag.getMetaTagName());
113         assertEquals("Meta Tag 14 Contents","en",metaTag.getMetaContent());
114         assertNull("Meta Tag 14 Http-Equiv",metaTag.getHttpEquiv());
115
116         metaTag = (MetaTag) node[15];
117         assertEquals("Meta Tag 16 Name","owner",metaTag.getMetaTagName());
118         assertEquals("Meta Tag 16 Contents","service@admin.spamcop.net",metaTag.getMetaContent());
119         assertNull("Meta Tag 16 Http-Equiv",metaTag.getHttpEquiv());
120
121         metaTag = (MetaTag) node[17];
122         assertNull("Meta Tag 18 Name",metaTag.getMetaTagName());
123         assertEquals("Meta Tag 18 Contents","text/html; charset=ISO-8859-1",metaTag.getMetaContent());
124         assertEquals("Meta Tag 18 Http-Equiv","content-type",metaTag.getHttpEquiv());
125     }
126
127     public void testScanTagsInMeta() throws ParserException {
128         String JavaDoc description = "Description";
129         String JavaDoc content = "Ethnoburb </I>versus Chinatown: Two Types of Urban Ethnic Communities in Los Angeles";
130         createParser(
131         "<META NAME=\"" + description + "\" CONTENT=\"" + content + "\">",
132         "http://www.google.com/test/index.html"
133         );
134         parser.setNodeFactory (
135             new PrototypicalNodeFactory (
136                 new Tag[] {
137                     new MetaTag (),
138                 }));
139         parseAndAssertNodeCount(1);
140         assertTrue("Node should be meta tag",node[0] instanceof MetaTag);
141         MetaTag metaTag = (MetaTag)node[0];
142         assertEquals("Meta Tag Name",description,metaTag.getMetaTagName());
143         assertEquals("Content",content,metaTag.getMetaContent());
144     }
145
146     /**
147      * Tried to reproduce bug 707447 but test passes
148      * @throws ParserException
149      */

150     public void testMetaTagBug() throws ParserException {
151         String JavaDoc equiv = "content-type";
152         String JavaDoc content = "text/html; charset=windows-1252";
153         createParser(
154             "<html>" +
155             "<head>" +
156             "<meta http-equiv=\"" + equiv + "\" " +
157             "content=\"" + content + "\">" +
158             "</head>" +
159             "</html>"
160         );
161         parser.setNodeFactory (new PrototypicalNodeFactory (new MetaTag ()));
162         parseAndAssertNodeCount(5);
163         assertType("Meta Tag expected", MetaTag.class, node[2]);
164         MetaTag metaTag = (MetaTag)node[2];
165
166         assertStringEquals("http-equiv",equiv,metaTag.getHttpEquiv());
167         assertStringEquals("content",content,metaTag.getMetaContent());
168     }
169
170     /**
171      * Bug report 702547 by Joe Robbins being reproduced.
172      * @throws ParserException
173      */

174     public void testMetaTagWithOpenTagSymbol() throws ParserException {
175         String JavaDoc content = "a<b";
176         createParser(
177             "<html>" +
178             "<head>" +
179             "<title>Parser Test 2</title>" +
180             "<meta name=\"foo\" content=\"" + content + "\">" +
181             "</head>" +
182             "<body>" +
183             "<a HREF=\"http://www.yahoo.com/\">Yahoo!</a><br>" +
184             "<a HREF=\"http://www.excite.com\">Excite</a>" +
185             "</body>" +
186             "</html>"
187         );
188         parser.setNodeFactory (new PrototypicalNodeFactory (
189             new Tag[] {
190                 new MetaTag (),
191                 new TitleTag (),
192                 new LinkTag (),
193             }));
194         parseAndAssertNodeCount(11);
195         assertType("meta tag",MetaTag.class,node[3]);
196         MetaTag metaTag = (MetaTag)node[3];
197         assertStringEquals(
198             "meta content",
199             content,
200             metaTag.getMetaContent()
201         );
202     }
203 }
204
Popular Tags