KickJava   Java API By Example, From Geeks To Geeks.

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


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/StyleTagTest.java,v $
8
// $Author: derrickoswald $
9
// $Date: 2004/07/17 13:45:06 $
10
// $Revision: 1.38 $
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.Text;
30 import org.htmlparser.tags.HeadTag;
31 import org.htmlparser.tags.Html;
32 import org.htmlparser.tags.StyleTag;
33 import org.htmlparser.tests.ParserTestCase;
34 import org.htmlparser.util.ParserException;
35
36 public class StyleTagTest extends ParserTestCase {
37
38     static
39     {
40         System.setProperty ("org.htmlparser.tests.tagTests.StyleTagTest", "StyleTagTest");
41     }
42
43     public StyleTagTest(String JavaDoc name) {
44         super(name);
45     }
46
47     public void testToHTML() throws ParserException {
48         String JavaDoc html = "<style>a.h{background-color:#ffee99}</style>";
49         createParser(html);
50         parseAndAssertNodeCount(1);
51         assertTrue(node[0] instanceof StyleTag);
52         StyleTag styleTag = (StyleTag)node[0];
53         assertEquals("Raw String",html,styleTag.toHtml());
54     }
55
56     /**
57      * Reproducing a bug reported by Dhaval Udani relating to
58      * style tag attributes being missed
59      */

60     public void testToHtmlAttributes() throws ParserException {
61         String JavaDoc style = "<STYLE type=\"text/css\">\n"+
62         "<!--"+
63         "{something....something}"+
64         "-->"+
65         "</STYLE>";
66         createParser(style);
67         parseAndAssertNodeCount(1);
68         assertTrue(node[0] instanceof StyleTag);
69         StyleTag styleTag = (StyleTag)node[0];
70         assertStringEquals("toHtml",style,styleTag.toHtml());
71     }
72
73     public void testScan() throws ParserException
74     {
75         createParser("<STYLE TYPE=\"text/css\"><!--\n\n"+
76         "</STYLE>","http://www.yle.fi/");
77         parseAndAssertNodeCount(1);
78     }
79
80     public void testScanBug() throws ParserException {
81         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>",
82         "http://www.google.com/test/index.html");
83         parseAndAssertNodeCount(1);
84         assertTrue("First node should be a HTML tag", node[0] instanceof Html);
85         Html html = (Html)node[0];
86         assertTrue("HTML tag should have one child", 1 == html.getChildCount ());
87         assertTrue("First child should be a HEAD tag", html.childAt (0) instanceof HeadTag);
88         HeadTag head = (HeadTag)html.childAt (0);
89         assertTrue("HEAD tag should have four children", 4 == head.getChildCount ());
90         assertTrue("Fourth child should be a STYLE tag", head.childAt (3) instanceof StyleTag);
91         StyleTag styleTag = (StyleTag)head.childAt (3);
92         assertEquals("Style Code","a.h{background-color:#ffee99}",styleTag.getStyleCode());
93     }
94
95     /**
96      * This is a bug reported by Kaarle Kaaila.
97      */

98     public void testScanBug2() throws ParserException {
99         createParser("<STYLE TYPE=\"text/css\"><!--\n\n"+
100         "input{font-family: arial, helvetica, sans-serif; font-size:11px;}\n\n"+
101         "i {font-family: times; font-size:10pt; font-weight:normal;}\n\n"+
102         ".ruuhka {font-family: arial, helvetica, sans-serif; font-size:11px;}\n\n"+
103         ".paalinkit {font-family: arial, helvetica, sans-serif; font-size:12px;}\n\n"+
104         ".shortselect{font-family: arial, helvetica, sans-serif; font-size:12px; width:130;}\n\n"+
105         ".cityselect{font-family: arial, helvetica, sans-serif; font-size:11px; width:100;}\n\n"+
106         ".longselect{font-family: arial, helvetica, sans-serif; font-size:12px;}\n\n"+
107         "---></STYLE>","http://www.yle.fi/");
108         parseAndAssertNodeCount(1);
109         assertTrue(node[0] instanceof StyleTag);
110     }
111
112     /**
113      * This is a bug reported by Dr. Wes Munsil, with the parser crashing on Google
114      */

115     public void testScanBug3() throws ParserException {
116         String JavaDoc expectedCode = "<!--\nbody,td,a,p,.h{font-family:arial,sans-serif;} .h{font-size: 20px;} .h{color:} .q{text-decoration:none; color:#0000cc;}\n//-->";
117         createParser("<html><head><META HTTP-EQUIV=\"content-type\" CONTENT=\"text/html; charset=ISO-8859-1\"><title>Google</title><style>"+
118         expectedCode+
119         "</style>","http://www.yle.fi/");
120         parseAndAssertNodeCount(1);
121         assertTrue("First node should be a HTML tag", node[0] instanceof Html);
122         Html html = (Html)node[0];
123         assertTrue("HTML tag should have one child", 1 == html.getChildCount ());
124         assertTrue("First child should be a HEAD tag", html.childAt (0) instanceof HeadTag);
125         HeadTag head = (HeadTag)html.childAt (0);
126         assertTrue("HEAD tag should have three children", 3 == head.getChildCount ());
127         assertTrue("Third child should be a STYLE tag", head.childAt (2) instanceof StyleTag);
128         StyleTag styleTag = (StyleTag)head.childAt (2);
129         assertStringEquals("Expected Style Code",expectedCode,styleTag.getStyleCode());
130     }
131     
132     /**
133      * See bug #900125 Style Tag Children not grouped
134      */

135     public void testStyleChildren () throws ParserException
136     {
137         String JavaDoc style =
138             "\nbody {color:white}\n" +
139             "<!--\n" +
140             ".teliabox {\n" +
141             "color: #A9014E;\n" +
142             "text-align: center;\n" +
143             "background-image:url(hallo.gif);\n" +
144             "}\n" +
145             "-->";
146         String JavaDoc html =
147             "<style type=\"text/css\" media=\"screen\">" +
148             style +
149             "</style>";
150         StyleTag tag;
151         Text string;
152
153         createParser (html);
154         parseAndAssertNodeCount (1);
155         assertTrue ("Node should be a STYLE tag", node[0] instanceof StyleTag);
156         tag = (StyleTag)node[0];
157         assertTrue ("STYLE tag should have one child", 1 == tag.getChildCount ());
158         assertTrue ("Child should be a StringNode", tag.getChild (0) instanceof Text);
159         string = (Text)tag.getChild (0);
160         assertStringEquals ("Style text incorrect", style, string.toHtml ());
161     }
162 }
163
Popular Tags