KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Header: /home/cvs/jakarta-jmeter/src/htmlparser/org/htmlparser/tests/scannersTests/SelectTagScannerTest.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 org.htmlparser.scanners.OptionTagScanner;
36 import org.htmlparser.scanners.SelectTagScanner;
37 import org.htmlparser.tags.OptionTag;
38 import org.htmlparser.tags.SelectTag;
39 import org.htmlparser.tests.ParserTestCase;
40 import org.htmlparser.util.ParserException;
41
42
43 public class SelectTagScannerTest extends ParserTestCase
44 {
45
46     private String JavaDoc testHTML =
47         new String JavaDoc(
48             "<Select name=\"Remarks\">"
49                 + "<option value='option1'>option1</option>"
50                 + "</Select>"
51                 + "<Select name=\"something\">"
52                 + "<option value='option2'>option2</option>"
53                 + "</Select>"
54                 + "<Select></Select>"
55                 + "<Select name=\"Remarks\">The death threats of the organization\n"
56                 + "refused to intimidate the soldiers</Select>"
57                 + "<Select name=\"Remarks\">The death threats of the LTTE\n"
58                 + "refused to intimidate the Tamilians\n</Select>");
59     private SelectTagScanner scanner;
60
61     public SelectTagScannerTest(String JavaDoc name)
62     {
63         super(name);
64     }
65
66     public void testScan() throws ParserException
67     {
68
69         scanner = new SelectTagScanner("-i");
70         createParser(testHTML, "http://www.google.com/test/index.html");
71         scanner = new SelectTagScanner("-ta");
72         parser.addScanner(scanner);
73         parser.addScanner(new OptionTagScanner(""));
74
75         parseAndAssertNodeCount(5);
76         assertTrue(node[0] instanceof SelectTag);
77         assertTrue(node[1] instanceof SelectTag);
78         assertTrue(node[2] instanceof SelectTag);
79         assertTrue(node[3] instanceof SelectTag);
80         assertTrue(node[4] instanceof SelectTag);
81
82         // check the Select node
83
for (int j = 0; j < nodeCount; j++)
84         {
85             SelectTag SelectTag = (SelectTag) node[j];
86             assertEquals("Select Scanner", scanner, SelectTag.getThisScanner());
87         }
88
89         SelectTag selectTag = (SelectTag) node[0];
90         OptionTag[] optionTags = selectTag.getOptionTags();
91         assertEquals("option tag array length", 1, optionTags.length);
92         assertEquals(
93             "option tag value",
94             "option1",
95             optionTags[0].getOptionText());
96     }
97
98     /**
99      * Bug reproduction based on report by gumirov@ccfit.nsu.ru
100      */

101     public void testSelectTagWithComments() throws Exception JavaDoc
102     {
103         createParser(
104             "<form>"
105                 + "<select> "
106                 + "<!-- 1 --><option selected>123 "
107                 + "<option>345 "
108                 + "</select> "
109                 + "</form>");
110         parser.registerScanners();
111         parseAndAssertNodeCount(1);
112
113     }
114 }
115
Popular Tags