KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Header: /home/cvs/jakarta-jmeter/src/htmlparser/org/htmlparser/tests/scannersTests/StyleScannerTest.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.StyleScanner;
35 import org.htmlparser.tags.StyleTag;
36 import org.htmlparser.tests.ParserTestCase;
37 import org.htmlparser.util.ParserException;
38
39 public class StyleScannerTest extends ParserTestCase
40 {
41
42     public StyleScannerTest(String JavaDoc name)
43     {
44         super(name);
45     }
46
47     public void testEvaluate()
48     {
49         StyleScanner scanner = new StyleScanner("-s");
50         boolean retVal = scanner.evaluate("style ", null);
51         assertEquals(
52             "Evaluation of STYLE tag",
53             new Boolean JavaDoc(true),
54             new Boolean JavaDoc(retVal));
55     }
56
57     public void testScan()
58     {
59         createParser(
60             "<STYLE TYPE=\"text/css\"><!--\n\n" + "</STYLE>",
61             "http://www.yle.fi/");
62         parser.addScanner(new StyleScanner("-s"));
63         try
64         {
65             parseAndAssertNodeCount(1);
66             assertTrue("Should've thrown exception", false);
67         }
68         catch (ParserException e)
69         {
70
71         }
72     }
73
74     public void testScanBug() throws ParserException
75     {
76         createParser(
77             "<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>",
78             "http://www.google.com/test/index.html");
79         parser.registerScanners();
80         parseAndAssertNodeCount(7);
81         assertTrue(
82             "Second last node should be a style tag",
83             node[5] instanceof StyleTag);
84         StyleTag styleTag = (StyleTag) node[5];
85         assertEquals(
86             "Style Code",
87             "a.h{background-color:#ffee99}",
88             styleTag.getStyleCode());
89     }
90
91     /**
92      * This is a bug reported by Kaarle Kaaila.
93      */

94     public void testScanBug2() throws ParserException
95     {
96         createParser(
97             "<STYLE TYPE=\"text/css\"><!--\n\n"
98                 + "input{font-family: arial, helvetica, sans-serif; font-size:11px;}\n\n"
99                 + "i {font-family: times; font-size:10pt; font-weight:normal;}\n\n"
100                 + ".ruuhka {font-family: arial, helvetica, sans-serif; font-size:11px;}\n\n"
101                 + ".paalinkit {font-family: arial, helvetica, sans-serif; font-size:12px;}\n\n"
102                 + ".shortselect{font-family: arial, helvetica, sans-serif; font-size:12px; width:130;}\n\n"
103                 + ".cityselect{font-family: arial, helvetica, sans-serif; font-size:11px; width:100;}\n\n"
104                 + ".longselect{font-family: arial, helvetica, sans-serif; font-size:12px;}\n\n"
105                 + "---></STYLE>",
106             "http://www.yle.fi/");
107         parser.addScanner(new StyleScanner("-s"));
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     {
117         createParser(
118             "<html><head><META HTTP-EQUIV=\"content-type\" CONTENT=\"text/html; charset=ISO-8859-1\"><title>Google</title><style><!--\n"
119                 + "body,td,a,p,.h{font-family:arial,sans-serif;} .h{font-size: 20px;} .h{color:} .q{text-decoration:none; color:#0000cc;}\n"
120                 + "//--></style>",
121             "http://www.yle.fi/");
122         parser.registerScanners();
123         parseAndAssertNodeCount(5);
124         assertTrue(node[4] instanceof StyleTag);
125         StyleTag styleTag = (StyleTag) node[4];
126         String JavaDoc expectedCode =
127             "<!--\r\n"
128                 + "body,td,a,p,.h{font-family:arial,sans-serif;} .h{font-size: 20px;} .h{color:} .q{text-decoration:none; color:#0000cc;}\r\n"
129                 + "//-->";
130         assertStringEquals(
131             "Expected Style Code",
132             expectedCode,
133             styleTag.getStyleCode());
134     }
135
136 }
137
Popular Tags