KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Header: /home/cvs/jakarta-jmeter/src/htmlparser/org/htmlparser/tests/scannersTests/LabelScannerTest.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
35 import java.util.*;
36 import junit.framework.TestSuite;
37 import org.htmlparser.scanners.LabelScanner;
38 import org.htmlparser.tags.LabelTag;
39 import org.htmlparser.tests.ParserTestCase;
40 import org.htmlparser.util.ParserException;
41
42 public class LabelScannerTest extends ParserTestCase
43 {
44
45     public LabelScannerTest(String JavaDoc name)
46     {
47         super(name);
48     }
49     public void testSimpleLabels() throws ParserException
50     {
51         createParser("<label>This is a label tag</label>");
52         LabelScanner labelScanner = new LabelScanner("-l");
53         parser.addScanner(labelScanner);
54         parseAndAssertNodeCount(1);
55         assertTrue(node[0] instanceof LabelTag);
56         // check the title node
57
LabelTag labelTag = (LabelTag) node[0];
58         assertEquals(
59             "Label",
60             "This is a label tag",
61             labelTag.getChildrenHTML());
62         assertEquals("Label", "This is a label tag", labelTag.getLabel());
63         assertStringEquals(
64             "Label",
65             "<LABEL>This is a label tag</LABEL>",
66             labelTag.toHtml());
67         assertEquals("Label Scanner", labelScanner, labelTag.getThisScanner());
68     }
69
70     public void testLabelWithJspTag() throws ParserException
71     {
72         createParser("<label><%=labelValue%></label>");
73         parser.registerScanners();
74         LabelScanner labelScanner = new LabelScanner("-l");
75         parser.addScanner(labelScanner);
76         parseAndAssertNodeCount(1);
77         assertTrue(node[0] instanceof LabelTag);
78         // check the title node
79
LabelTag labelTag = (LabelTag) node[0];
80         assertStringEquals(
81             "Label",
82             "<LABEL><%=labelValue%></LABEL>",
83             labelTag.toHtml());
84         assertEquals("Label Scanner", labelScanner, labelTag.getThisScanner());
85     }
86
87     public void testLabelWithOtherTags() throws ParserException
88     {
89         createParser("<label><span>Span within label</span></label>");
90         parser.registerScanners();
91         LabelScanner labelScanner = new LabelScanner("-l");
92         parser.addScanner(labelScanner);
93         parseAndAssertNodeCount(1);
94         assertTrue(node[0] instanceof LabelTag);
95         // check the title node
96
LabelTag labelTag = (LabelTag) node[0];
97         assertEquals("Label value", "Span within label", labelTag.getLabel());
98         assertStringEquals(
99             "Label",
100             "<LABEL><SPAN>Span within label</SPAN></LABEL>",
101             labelTag.toHtml());
102         assertEquals("Label Scanner", labelScanner, labelTag.getThisScanner());
103     }
104
105     public void testLabelWithManyCompositeTags() throws ParserException
106     {
107         createParser("<label><span>Jane <b> Doe </b> Smith</span></label>");
108         parser.registerScanners();
109         LabelScanner labelScanner = new LabelScanner("-l");
110         parser.addScanner(labelScanner);
111         parseAndAssertNodeCount(1);
112         assertTrue(node[0] instanceof LabelTag);
113         LabelTag labelTag = (LabelTag) node[0];
114         assertEquals(
115             "Label value",
116             "<SPAN>Jane <B> Doe </B> Smith</SPAN>",
117             labelTag.getChildrenHTML());
118         assertEquals("Label value", "Jane Doe Smith", labelTag.getLabel());
119         assertStringEquals(
120             "Label",
121             "<LABEL><SPAN>Jane <B> Doe </B> Smith</SPAN></LABEL>",
122             labelTag.toHtml());
123         assertEquals("Label Scanner", labelScanner, labelTag.getThisScanner());
124     }
125
126     public void testLabelsID() throws ParserException
127     {
128         createParser("<label>John Doe</label>");
129         parser.registerScanners();
130         LabelScanner labelScanner = new LabelScanner("-l");
131         parser.addScanner(labelScanner);
132         parseAndAssertNodeCount(1);
133         assertTrue(node[0] instanceof LabelTag);
134
135         LabelTag labelTag = (LabelTag) node[0];
136         assertStringEquals(
137             "Label",
138             "<LABEL>John Doe</LABEL>",
139             labelTag.toHtml());
140         Hashtable attr = labelTag.getAttributes();
141         assertNull("ID", attr.get("id"));
142     }
143
144     public void testNestedLabels() throws ParserException
145     {
146         createParser("<label id=\"attr1\"><label>Jane Doe");
147         parser.registerScanners();
148         LabelScanner labelScanner = new LabelScanner("-l");
149         parser.addScanner(labelScanner);
150         parseAndAssertNodeCount(2);
151         assertTrue(node[0] instanceof LabelTag);
152         assertTrue(node[1] instanceof LabelTag);
153
154         LabelTag labelTag = (LabelTag) node[0];
155         assertStringEquals(
156             "Label",
157             "<LABEL ID=\"attr1\" ></LABEL>",
158             labelTag.toHtml());
159         labelTag = (LabelTag) node[1];
160         assertStringEquals(
161             "Label",
162             "<LABEL>Jane Doe</LABEL>",
163             labelTag.toHtml());
164         Hashtable attr = labelTag.getAttributes();
165         assertNull("ID", attr.get("id"));
166     }
167
168     public void testNestedLabels2() throws ParserException
169     {
170         String JavaDoc testHTML =
171             new String JavaDoc(
172                 "<LABEL value=\"Google Search\">Google</LABEL>"
173                     + "<LABEL value=\"AltaVista Search\">AltaVista"
174                     + "<LABEL value=\"Lycos Search\"></LABEL>"
175                     + "<LABEL>Yahoo!</LABEL>"
176                     + "<LABEL>\nHotmail</LABEL>"
177                     + "<LABEL value=\"ICQ Messenger\">"
178                     + "<LABEL>Mailcity\n</LABEL>"
179                     + "<LABEL>\nIndiatimes\n</LABEL>"
180                     + "<LABEL>\nRediff\n</LABEL>\n"
181                     + "<LABEL>Cricinfo"
182                     + "<LABEL value=\"Microsoft Passport\">"
183                     + "<LABEL value=\"AOL\"><SPAN>AOL</SPAN></LABEL>"
184                     + "<LABEL value=\"Time Warner\">Time <B>Warner <SPAN>AOL </SPAN>Inc.</B>");
185         createParser(testHTML);
186         //parser.registerScanners();
187
LabelScanner labelScanner = new LabelScanner("-l");
188         parser.addScanner(labelScanner);
189         parseAndAssertNodeCount(13);
190
191         // for(int j=0;j<nodeCount;j++)
192
// {
193
// //assertTrue("Node " + j + " should be Label Tag",node[j] instanceof LabelTag);
194
// System.out.println(node[j].getClass().getName());
195
// System.out.println(node[j].toHtml());
196
// }
197

198         LabelTag LabelTag;
199         LabelTag = (LabelTag) node[0];
200         assertStringEquals(
201             "HTML String",
202             "<LABEL VALUE=\"Google Search\">Google</LABEL>",
203             LabelTag.toHtml());
204         LabelTag = (LabelTag) node[1];
205         assertStringEquals(
206             "HTML String",
207             "<LABEL VALUE=\"AltaVista Search\">AltaVista</LABEL>",
208             LabelTag.toHtml());
209         LabelTag = (LabelTag) node[2];
210         assertStringEquals(
211             "HTML String",
212             "<LABEL VALUE=\"Lycos Search\"></LABEL>",
213             LabelTag.toHtml());
214         LabelTag = (LabelTag) node[3];
215         assertStringEquals(
216             "HTML String",
217             "<LABEL>Yahoo!</LABEL>",
218             LabelTag.toHtml());
219         LabelTag = (LabelTag) node[4];
220         assertStringEquals(
221             "HTML String",
222             "<LABEL>\r\nHotmail</LABEL>",
223             LabelTag.toHtml());
224         LabelTag = (LabelTag) node[5];
225         assertStringEquals(
226             "HTML String",
227             "<LABEL VALUE=\"ICQ Messenger\"></LABEL>",
228             LabelTag.toHtml());
229         LabelTag = (LabelTag) node[6];
230         assertStringEquals(
231             "HTML String",
232             "<LABEL>Mailcity\r\n</LABEL>",
233             LabelTag.toHtml());
234         LabelTag = (LabelTag) node[7];
235         assertStringEquals(
236             "HTML String",
237             "<LABEL>\r\nIndiatimes\r\n</LABEL>",
238             LabelTag.toHtml());
239         LabelTag = (LabelTag) node[8];
240         assertStringEquals(
241             "HTML String",
242             "<LABEL>\r\nRediff\r\n</LABEL>",
243             LabelTag.toHtml());
244         LabelTag = (LabelTag) node[9];
245         assertStringEquals(
246             "HTML String",
247             "<LABEL>Cricinfo</LABEL>",
248             LabelTag.toHtml());
249         LabelTag = (LabelTag) node[10];
250         assertStringEquals(
251             "HTML String",
252             "<LABEL VALUE=\"Microsoft Passport\"></LABEL>",
253             LabelTag.toHtml());
254         LabelTag = (LabelTag) node[11];
255         assertStringEquals(
256             "HTML String",
257             "<LABEL VALUE=\"AOL\"><SPAN>AOL</SPAN></LABEL>",
258             LabelTag.toHtml());
259         LabelTag = (LabelTag) node[12];
260         assertStringEquals(
261             "HTML String",
262             "<LABEL VALUE=\"Time Warner\">Time <B>Warner <SPAN>AOL </SPAN>Inc.</B></LABEL>",
263             LabelTag.toHtml());
264     }
265
266     public static TestSuite suite()
267     {
268         return new TestSuite(LabelScannerTest.class);
269     }
270
271     public static void main(String JavaDoc[] args)
272     {
273         new junit.awtui.TestRunner().start(
274             new String JavaDoc[] { LabelScannerTest.class.getName()});
275     }
276
277 }
278
Popular Tags