KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > gargoylesoftware > htmlunit > javascript > AttributeCaseTest


1 /*
2  * Copyright (c) 2002, 2005 Gargoyle Software Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12  * 3. The end-user documentation included with the redistribution, if any, must
13  * include the following acknowledgment:
14  *
15  * "This product includes software developed by Gargoyle Software Inc.
16  * (http://www.GargoyleSoftware.com/)."
17  *
18  * Alternately, this acknowledgment may appear in the software itself, if
19  * and wherever such third-party acknowledgments normally appear.
20  * 4. The name "Gargoyle Software" must not be used to endorse or promote
21  * products derived from this software without prior written permission.
22  * For written permission, please contact info@GargoyleSoftware.com.
23  * 5. Products derived from this software may not be called "HtmlUnit", nor may
24  * "HtmlUnit" appear in their name, without prior written permission of
25  * Gargoyle Software Inc.
26  *
27  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
28  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
29  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
30  * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
33  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
36  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37  */

38 package com.gargoylesoftware.htmlunit.javascript;
39
40 import java.io.IOException;
41
42 import com.gargoylesoftware.htmlunit.MockWebConnection;
43 import com.gargoylesoftware.htmlunit.WebClient;
44 import com.gargoylesoftware.htmlunit.WebTestCase;
45 import com.gargoylesoftware.htmlunit.html.HtmlElement;
46 import com.gargoylesoftware.htmlunit.html.HtmlPage;
47
48
49 /**
50  * Tests for {@link HtmlElement} attributes.
51  *
52  * @version $Revision: 1.5 $
53  * @author David D. Kilzer
54  * @author <a HREF="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
55  */

56 public class AttributeCaseTest extends WebTestCase {
57
58     private static final String ATTRIBUTE_NAME = "randomAttribute";
59     private static final String ATTRIBUTE_VALUE = "someValue";
60     private static final String ATTRIBUTE_VALUE_NEW = "newValue";
61
62     private HtmlElement element_;
63     private HtmlPage page_;
64
65
66     /**
67      * Create an instance
68      *
69      * @param name The name of the test
70      */

71     public AttributeCaseTest(final String name) {
72         super(name);
73     }
74
75     /**
76      * Test {@link HtmlElement#getAttributeValue(String)} with a lower case name
77      * @throws IOException If the test fails
78      */

79     public void testGetAttributeLowerCase() throws IOException {
80         setupGetAttributeTest(ATTRIBUTE_NAME, ATTRIBUTE_VALUE);
81         assertEquals(page_.asXml(), ATTRIBUTE_VALUE, element_.getAttributeValue(ATTRIBUTE_NAME.toLowerCase()));
82     }
83
84     /**
85      * Test {@link HtmlElement#getAttributeValue(String)} with a mixed case name
86      * @throws IOException If the test fails
87      */

88     public void testGetAttributeMixedCase() throws IOException {
89         setupGetAttributeTest(ATTRIBUTE_NAME, ATTRIBUTE_VALUE);
90         assertEquals(page_.asXml(), ATTRIBUTE_VALUE, element_.getAttributeValue(ATTRIBUTE_NAME));
91     }
92
93     /**
94      * Test {@link HtmlElement#getAttributeValue(String)} with an upper case name
95      * @throws IOException If the test fails
96      */

97     public void testGetAttributeUpperCase() throws IOException {
98         setupGetAttributeTest(ATTRIBUTE_NAME, ATTRIBUTE_VALUE);
99         assertEquals(page_.asXml(), ATTRIBUTE_VALUE, element_.getAttributeValue(ATTRIBUTE_NAME.toUpperCase()));
100     }
101
102     /**
103      * Test {@link HtmlElement#setAttributeValue(String,String)} with a lower case name
104      * @throws IOException If the test fails
105      */

106     public void testSetAttributeLowerCase() throws IOException {
107         setupSetAttributeTest(ATTRIBUTE_NAME.toLowerCase(), ATTRIBUTE_VALUE, ATTRIBUTE_VALUE_NEW);
108         assertEquals(page_.asXml(), ATTRIBUTE_VALUE_NEW, element_.getAttributeValue(ATTRIBUTE_NAME.toLowerCase()));
109     }
110
111     /**
112      * Test {@link HtmlElement#setAttributeValue(String,String)} with a mixed case name
113      * @throws IOException If the test fails
114      */

115     public void testSetAttributeMixedCase() throws IOException {
116         setupSetAttributeTest(ATTRIBUTE_NAME, ATTRIBUTE_VALUE, ATTRIBUTE_VALUE_NEW);
117         assertEquals(page_.asXml(), ATTRIBUTE_VALUE_NEW, element_.getAttributeValue(ATTRIBUTE_NAME.toLowerCase()));
118     }
119
120     /**
121      * Test {@link HtmlElement#setAttributeValue(String,String)} with an upper case name
122      * @throws IOException If the test fails
123      */

124     public void testSetAttributeUpperCase() throws IOException {
125         setupSetAttributeTest(ATTRIBUTE_NAME.toUpperCase(), ATTRIBUTE_VALUE, ATTRIBUTE_VALUE_NEW);
126         assertEquals(page_.asXml(), ATTRIBUTE_VALUE_NEW, element_.getAttributeValue(ATTRIBUTE_NAME.toLowerCase()));
127     }
128
129     /**
130      * Test {@link HtmlElement#removeAttribute(String)} with a lower case name
131      * @throws IOException If the test fails
132      */

133     public void testRemoveAttributeLowerCase() throws IOException {
134         setupGetAttributeTest(ATTRIBUTE_NAME, ATTRIBUTE_VALUE);
135         element_.removeAttribute(ATTRIBUTE_NAME.toLowerCase());
136         assertEquals(page_.asXml(), "", element_.getAttributeValue(ATTRIBUTE_NAME.toLowerCase()));
137     }
138
139     /**
140      * Test {@link HtmlElement#removeAttribute(String)} with a mixed case name
141      * @throws IOException If the test fails
142      */

143     public void testRemoveAttributeMixedCase() throws IOException {
144         setupGetAttributeTest(ATTRIBUTE_NAME, ATTRIBUTE_VALUE);
145         element_.removeAttribute(ATTRIBUTE_NAME);
146         assertEquals(page_.asXml(), "", element_.getAttributeValue(ATTRIBUTE_NAME.toLowerCase()));
147     }
148
149     /**
150      * Test {@link HtmlElement#removeAttribute(String)} with an upper case name
151      * @throws IOException If the test fails
152      */

153     public void testRemoveAttributeUpperCase() throws IOException {
154         setupGetAttributeTest(ATTRIBUTE_NAME, ATTRIBUTE_VALUE);
155         element_.removeAttribute(ATTRIBUTE_NAME.toUpperCase());
156         assertEquals(page_.asXml(), "", element_.getAttributeValue(ATTRIBUTE_NAME.toLowerCase()));
157     }
158
159     /**
160      * Test {@link HtmlElement#isAttributeDefined(String)} with a lower case name
161      * @throws IOException If the test fails
162      */

163     public void testIsAttributeDefinedLowerCase() throws IOException {
164         setupGetAttributeTest(ATTRIBUTE_NAME, ATTRIBUTE_VALUE);
165         assertTrue(page_.asXml(), element_.isAttributeDefined(ATTRIBUTE_NAME.toLowerCase()));
166     }
167
168     /**
169      * Test {@link HtmlElement#isAttributeDefined(String)} with a mixed case name
170      * @throws IOException If the test fails
171      */

172     public void testIsAttributeDefinedMixedCase() throws IOException {
173         setupGetAttributeTest(ATTRIBUTE_NAME, ATTRIBUTE_VALUE);
174         assertTrue(page_.asXml(), element_.isAttributeDefined(ATTRIBUTE_NAME));
175     }
176
177     /**
178      * Test {@link HtmlElement#isAttributeDefined(String)} with an upper case name
179      * @throws IOException If the test fails
180      */

181     public void testIsAttributeDefinedUpperCase() throws IOException {
182         setupGetAttributeTest(ATTRIBUTE_NAME, ATTRIBUTE_VALUE);
183         assertTrue(page_.asXml(), element_.isAttributeDefined(ATTRIBUTE_NAME.toUpperCase()));
184     }
185
186
187     private void setupAttributeTest(final String content, final String elementId) throws IOException {
188
189         final WebClient client = new WebClient();
190         final MockWebConnection webConnection = new MockWebConnection(client);
191
192         webConnection.setDefaultResponse(content);
193         client.setWebConnection(webConnection);
194
195         page_ = (HtmlPage) client.getPage(URL_GARGOYLE);
196
197         element_ = page_.getHtmlElementById(elementId);
198     }
199
200
201     private void setupGetAttributeTest(final String attributeName, final String attributeValue) throws IOException {
202
203         final String elementId = "p-id";
204         final String content = "<html><head><title>AttributeCaseTest</title></head><body>\n"
205                           + "<p id=\"" + elementId + "\" " + attributeName + "=\"" + attributeValue + "\">\n"
206                           + "</body></html>";
207
208         setupAttributeTest(content, elementId);
209     }
210
211
212     private void setupSetAttributeTest(
213             final String attributeName, final String attributeValue,
214             final String newAttributeValue)
215         throws IOException {
216
217         final String elementId = "p-id";
218         final String content
219             = "<html><head><title>AttributeCaseTest</title></head><body>\n"
220              + "<p id=\"" + elementId + "\" " + attributeName + "=\"" + attributeValue + "\">\n"
221              + "<script language=\"javascript\" type=\"text/javascript\">\n<!--\n"
222              + " document.getElementById(\"" + elementId + "\").setAttribute(\"" + attributeName + "\", \"" +
223              newAttributeValue + "\");\n"
224              + "\n// -->\n</script>\n"
225              + "</body></html>";
226
227         setupAttributeTest(content, elementId);
228     }
229 }
230
Popular Tags