001: /*
002: * Copyright (c) 2002-2008 Gargoyle Software Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * 1. Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: * 2. Redistributions in binary form must reproduce the above copyright notice,
010: * this list of conditions and the following disclaimer in the documentation
011: * and/or other materials provided with the distribution.
012: * 3. The end-user documentation included with the redistribution, if any, must
013: * include the following acknowledgment:
014: *
015: * "This product includes software developed by Gargoyle Software Inc.
016: * (http://www.GargoyleSoftware.com/)."
017: *
018: * Alternately, this acknowledgment may appear in the software itself, if
019: * and wherever such third-party acknowledgments normally appear.
020: * 4. The name "Gargoyle Software" must not be used to endorse or promote
021: * products derived from this software without prior written permission.
022: * For written permission, please contact info@GargoyleSoftware.com.
023: * 5. Products derived from this software may not be called "HtmlUnit", nor may
024: * "HtmlUnit" appear in their name, without prior written permission of
025: * Gargoyle Software Inc.
026: *
027: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
028: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
029: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
030: * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
031: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
032: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
033: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
034: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
035: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
036: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
037: */
038: package com.gargoylesoftware.htmlunit.javascript;
039:
040: import java.io.IOException;
041:
042: import com.gargoylesoftware.htmlunit.MockWebConnection;
043: import com.gargoylesoftware.htmlunit.WebClient;
044: import com.gargoylesoftware.htmlunit.WebTestCase;
045: import com.gargoylesoftware.htmlunit.html.HtmlElement;
046: import com.gargoylesoftware.htmlunit.html.HtmlPage;
047:
048: /**
049: * Tests for {@link HtmlElement} attributes.
050: *
051: * @version $Revision: 2132 $
052: * @author David D. Kilzer
053: * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
054: */
055: public class AttributeCaseTest extends WebTestCase {
056:
057: private static final String ATTRIBUTE_NAME = "randomAttribute";
058: private static final String ATTRIBUTE_VALUE = "someValue";
059: private static final String ATTRIBUTE_VALUE_NEW = "newValue";
060:
061: private HtmlElement element_;
062: private HtmlPage page_;
063:
064: /**
065: * Create an instance
066: *
067: * @param name The name of the test
068: */
069: public AttributeCaseTest(final String name) {
070: super (name);
071: }
072:
073: /**
074: * Test {@link HtmlElement#getAttributeValue(String)} with a lower case name
075: * @throws IOException If the test fails
076: */
077: public void testGetAttributeLowerCase() throws IOException {
078: setupGetAttributeTest(ATTRIBUTE_NAME, ATTRIBUTE_VALUE);
079: assertEquals(page_.asXml(), ATTRIBUTE_VALUE, element_
080: .getAttributeValue(ATTRIBUTE_NAME.toLowerCase()));
081: }
082:
083: /**
084: * Test {@link HtmlElement#getAttributeValue(String)} with a mixed case name
085: * @throws IOException If the test fails
086: */
087: public void testGetAttributeMixedCase() throws IOException {
088: setupGetAttributeTest(ATTRIBUTE_NAME, ATTRIBUTE_VALUE);
089: assertEquals(page_.asXml(), ATTRIBUTE_VALUE, element_
090: .getAttributeValue(ATTRIBUTE_NAME));
091: }
092:
093: /**
094: * Test {@link HtmlElement#getAttributeValue(String)} with an upper case name
095: * @throws IOException If the test fails
096: */
097: public void testGetAttributeUpperCase() throws IOException {
098: setupGetAttributeTest(ATTRIBUTE_NAME, ATTRIBUTE_VALUE);
099: assertEquals(page_.asXml(), ATTRIBUTE_VALUE, element_
100: .getAttributeValue(ATTRIBUTE_NAME.toUpperCase()));
101: }
102:
103: /**
104: * Test {@link HtmlElement#setAttributeValue(String,String)} with a lower case name
105: * @throws IOException If the test fails
106: */
107: public void testSetAttributeLowerCase() throws IOException {
108: setupSetAttributeTest(ATTRIBUTE_NAME.toLowerCase(),
109: ATTRIBUTE_VALUE, ATTRIBUTE_VALUE_NEW);
110: assertEquals(page_.asXml(), ATTRIBUTE_VALUE_NEW, element_
111: .getAttributeValue(ATTRIBUTE_NAME.toLowerCase()));
112: }
113:
114: /**
115: * Test {@link HtmlElement#setAttributeValue(String,String)} with a mixed case name
116: * @throws IOException If the test fails
117: */
118: public void testSetAttributeMixedCase() throws IOException {
119: setupSetAttributeTest(ATTRIBUTE_NAME, ATTRIBUTE_VALUE,
120: ATTRIBUTE_VALUE_NEW);
121: assertEquals(page_.asXml(), ATTRIBUTE_VALUE_NEW, element_
122: .getAttributeValue(ATTRIBUTE_NAME.toLowerCase()));
123: }
124:
125: /**
126: * Test {@link HtmlElement#setAttributeValue(String,String)} with an upper case name
127: * @throws IOException If the test fails
128: */
129: public void testSetAttributeUpperCase() throws IOException {
130: setupSetAttributeTest(ATTRIBUTE_NAME.toUpperCase(),
131: ATTRIBUTE_VALUE, ATTRIBUTE_VALUE_NEW);
132: assertEquals(page_.asXml(), ATTRIBUTE_VALUE_NEW, element_
133: .getAttributeValue(ATTRIBUTE_NAME.toLowerCase()));
134: }
135:
136: /**
137: * Test {@link HtmlElement#removeAttribute(String)} with a lower case name
138: * @throws IOException If the test fails
139: */
140: public void testRemoveAttributeLowerCase() throws IOException {
141: setupGetAttributeTest(ATTRIBUTE_NAME, ATTRIBUTE_VALUE);
142: element_.removeAttribute(ATTRIBUTE_NAME.toLowerCase());
143: assertEquals(page_.asXml(), "", element_
144: .getAttributeValue(ATTRIBUTE_NAME.toLowerCase()));
145: }
146:
147: /**
148: * Test {@link HtmlElement#removeAttribute(String)} with a mixed case name
149: * @throws IOException If the test fails
150: */
151: public void testRemoveAttributeMixedCase() throws IOException {
152: setupGetAttributeTest(ATTRIBUTE_NAME, ATTRIBUTE_VALUE);
153: element_.removeAttribute(ATTRIBUTE_NAME);
154: assertEquals(page_.asXml(), "", element_
155: .getAttributeValue(ATTRIBUTE_NAME.toLowerCase()));
156: }
157:
158: /**
159: * Test {@link HtmlElement#removeAttribute(String)} with an upper case name
160: * @throws IOException If the test fails
161: */
162: public void testRemoveAttributeUpperCase() throws IOException {
163: setupGetAttributeTest(ATTRIBUTE_NAME, ATTRIBUTE_VALUE);
164: element_.removeAttribute(ATTRIBUTE_NAME.toUpperCase());
165: assertEquals(page_.asXml(), "", element_
166: .getAttributeValue(ATTRIBUTE_NAME.toLowerCase()));
167: }
168:
169: /**
170: * Test {@link HtmlElement#isAttributeDefined(String)} with a lower case name
171: * @throws IOException If the test fails
172: */
173: public void testIsAttributeDefinedLowerCase() throws IOException {
174: setupGetAttributeTest(ATTRIBUTE_NAME, ATTRIBUTE_VALUE);
175: assertTrue(page_.asXml(), element_
176: .isAttributeDefined(ATTRIBUTE_NAME.toLowerCase()));
177: }
178:
179: /**
180: * Test {@link HtmlElement#isAttributeDefined(String)} with a mixed case name
181: * @throws IOException If the test fails
182: */
183: public void testIsAttributeDefinedMixedCase() throws IOException {
184: setupGetAttributeTest(ATTRIBUTE_NAME, ATTRIBUTE_VALUE);
185: assertTrue(page_.asXml(), element_
186: .isAttributeDefined(ATTRIBUTE_NAME));
187: }
188:
189: /**
190: * Test {@link HtmlElement#isAttributeDefined(String)} with an upper case name
191: * @throws IOException If the test fails
192: */
193: public void testIsAttributeDefinedUpperCase() throws IOException {
194: setupGetAttributeTest(ATTRIBUTE_NAME, ATTRIBUTE_VALUE);
195: assertTrue(page_.asXml(), element_
196: .isAttributeDefined(ATTRIBUTE_NAME.toUpperCase()));
197: }
198:
199: private void setupAttributeTest(final String content,
200: final String elementId) throws IOException {
201: final WebClient client = new WebClient();
202: final MockWebConnection webConnection = new MockWebConnection(
203: client);
204:
205: webConnection.setDefaultResponse(content);
206: client.setWebConnection(webConnection);
207:
208: page_ = (HtmlPage) client.getPage(URL_GARGOYLE);
209:
210: element_ = page_.getHtmlElementById(elementId);
211: }
212:
213: private void setupGetAttributeTest(final String attributeName,
214: final String attributeValue) throws IOException {
215: final String elementId = "p-id";
216: final String content = "<html><head><title>AttributeCaseTest</title></head><body>\n"
217: + "<p id=\""
218: + elementId
219: + "\" "
220: + attributeName
221: + "=\"" + attributeValue + "\">\n" + "</body></html>";
222:
223: setupAttributeTest(content, elementId);
224: }
225:
226: private void setupSetAttributeTest(final String attributeName,
227: final String attributeValue, final String newAttributeValue)
228: throws IOException {
229:
230: final String elementId = "p-id";
231: final String content = "<html><head><title>AttributeCaseTest</title></head><body>\n"
232: + "<p id=\""
233: + elementId
234: + "\" "
235: + attributeName
236: + "=\""
237: + attributeValue
238: + "\">\n"
239: + "<script language=\"javascript\" type=\"text/javascript\">\n<!--\n"
240: + " document.getElementById(\""
241: + elementId
242: + "\").setAttribute(\""
243: + attributeName
244: + "\", \""
245: + newAttributeValue
246: + "\");\n"
247: + "\n// -->\n</script>\n" + "</body></html>";
248:
249: setupAttributeTest(content, elementId);
250: }
251: }
|