KickJava   Java API By Example, From Geeks To Geeks.

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


1 // HTMLParser Library $Name: v1_5_20050313 $ - A java-based parser for HTML
2
// http://sourceforge.org/projects/htmlparser
3
// Copyright (C) 2004 Dhaval Udani
4
//
5
// Revision Control Information
6
//
7
// $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tests/tagTests/SelectTagTest.java,v $
8
// $Author: derrickoswald $
9
// $Date: 2004/01/02 16:24:57 $
10
// $Revision: 1.40 $
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.tags.OptionTag;
30 import org.htmlparser.tags.SelectTag;
31 import org.htmlparser.tests.ParserTestCase;
32 import org.htmlparser.util.ParserException;
33
34 public class SelectTagTest extends ParserTestCase
35 {
36     static
37     {
38         System.setProperty ("org.htmlparser.tests.tagTests.SelectTagTest", "SelectTagTest");
39     }
40
41     private String JavaDoc testHTML = "<SELECT name=\"Nominees\">\n"+
42                                     "<option value=\"Spouse\">Spouse"+
43                                     "<option value=\"Father\"></option>\n"+
44                                     "<option value=\"Mother\">Mother\n" +
45                                     "<option value=\"Son\">\nSon\n</option>"+
46                                     "<option value=\"Daughter\">\nDaughter\n"+
47                                     "<option value=\"Nephew\">\nNephew</option>\n"+
48                                     "<option value=\"Niece\">Niece\n" +
49                                     "</select>";
50
51     private String JavaDoc correctedHTML = "<SELECT name=\"Nominees\">\n"+
52                                     "<option value=\"Spouse\">Spouse</option>"+
53                                     "<option value=\"Father\"></option>\n"+
54                                     "<option value=\"Mother\">Mother\n</option>" +
55                                     "<option value=\"Son\">\nSon\n</option>"+
56                                     "<option value=\"Daughter\">\nDaughter\n</option>"+
57                                     "<option value=\"Nephew\">\nNephew</option>\n"+
58                                     "<option value=\"Niece\">Niece\n</option>" +
59                                     "</select>";
60
61     private SelectTag selectTag;
62     private String JavaDoc html = "<Select name=\"Remarks\">" +
63                                     "<option value='option1'>option1</option>" +
64                                     "</Select>" +
65                                     "<Select name=\"something\">" +
66                                         "<option value='option2'>option2</option>" +
67                                     "</Select>" +
68                                     "<Select></Select>" +
69                                     "<Select name=\"Remarks\">The death threats of the organization\n" +
70                                     "refused to intimidate the soldiers</Select>" +
71                                     "<Select name=\"Remarks\">The death threats of the LTTE\n" +
72                                     "refused to intimidate the Tamilians\n</Select>";
73
74     public SelectTagTest(String JavaDoc name)
75     {
76         super(name);
77     }
78
79     protected void setUp() throws Exception JavaDoc{
80         super.setUp();
81         createParser(testHTML);
82         parseAndAssertNodeCount(1);
83         assertTrue("Node 1 should be Select Tag",node[0] instanceof SelectTag);
84         selectTag = (SelectTag) node[0];
85     }
86
87     public void testToHTML() throws ParserException
88     {
89         assertStringEquals("HTML String", correctedHTML, selectTag.toHtml());
90     }
91
92     public void testGetOptionTags() {
93         OptionTag [] optionTags = selectTag.getOptionTags();
94         assertEquals("option tag array length",7,optionTags.length);
95         assertEquals("option tag 1","Spouse",optionTags[0].getOptionText());
96         assertEquals("option tag 7","Niece\n",optionTags[6].getOptionText());
97     }
98
99     public void testScan() throws ParserException
100     {
101
102         createParser(html,"http://www.google.com/test/index.html");
103         parseAndAssertNodeCount(5);
104
105         // check the Select node
106
for(int j=0;j<nodeCount;j++)
107             assertTrue(node[j] instanceof SelectTag);
108
109         SelectTag selectTag = (SelectTag)node[0];
110         OptionTag [] optionTags = selectTag.getOptionTags();
111         assertEquals("option tag array length",1,optionTags.length);
112         assertEquals("option tag value","option1",optionTags[0].getOptionText());
113     }
114
115     /**
116      * Bug reproduction based on report by gumirov@ccfit.nsu.ru
117      */

118     public void testSelectTagWithComments() throws Exception JavaDoc {
119         createParser(
120             "<form>" +
121             "<select> " +
122             "<!-- 1 --><option selected>123 " +
123             "<option>345 " +
124             "</select> " +
125             "</form>"
126         );
127         parseAndAssertNodeCount(1);
128     }
129 }
130
Popular Tags