View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.javascript;
16  
17  import java.util.Locale;
18  
19  import org.htmlunit.SimpleWebTestCase;
20  import org.htmlunit.html.HtmlElement;
21  import org.htmlunit.html.HtmlPage;
22  import org.htmlunit.junit.BrowserRunner;
23  import org.junit.Test;
24  import org.junit.runner.RunWith;
25  
26  /**
27   * Tests for {@link HtmlElement} attributes.
28   *
29   * @author David D. Kilzer
30   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
31   */
32  @RunWith(BrowserRunner.class)
33  public class AttributeCaseTest extends SimpleWebTestCase {
34  
35      private static final String ATTRIBUTE_NAME = "randomAttribute";
36      private static final String ATTRIBUTE_VALUE = "someValue";
37      private static final String ATTRIBUTE_VALUE_NEW = "newValue";
38  
39      private HtmlElement element_;
40      private HtmlPage page_;
41  
42      /**
43       * Tests {@link HtmlElement#getAttribute(String)} with a lower case name.
44       * @throws Exception if the test fails
45       */
46      @Test
47      public void getAttributeLowerCase() throws Exception {
48          setupGetAttributeTest(ATTRIBUTE_NAME, ATTRIBUTE_VALUE);
49          assertEquals(page_.asXml(), ATTRIBUTE_VALUE,
50                  element_.getAttribute(ATTRIBUTE_NAME.toLowerCase(Locale.ROOT)));
51      }
52  
53      /**
54       * Tests {@link HtmlElement#getAttribute(String)} with a mixed case name.
55       * @throws Exception if the test fails
56       */
57      @Test
58      public void getAttributeMixedCase() throws Exception {
59          setupGetAttributeTest(ATTRIBUTE_NAME, ATTRIBUTE_VALUE);
60          assertEquals(page_.asXml(), ATTRIBUTE_VALUE, element_.getAttribute(ATTRIBUTE_NAME));
61      }
62  
63      /**
64       * Tests {@link HtmlElement#getAttribute(String)} with an upper case name.
65       * @throws Exception if the test fails
66       */
67      @Test
68      public void getAttributeUpperCase() throws Exception {
69          setupGetAttributeTest(ATTRIBUTE_NAME, ATTRIBUTE_VALUE);
70          assertEquals(page_.asXml(), ATTRIBUTE_VALUE,
71                  element_.getAttribute(ATTRIBUTE_NAME.toUpperCase(Locale.ROOT)));
72      }
73  
74      /**
75       * Tests {@link HtmlElement#setAttribute(String,String)} with a lower case name.
76       * @throws Exception if the test fails
77       */
78      @Test
79      public void setAttributeLowerCase() throws Exception {
80          setupSetAttributeTest(ATTRIBUTE_NAME.toLowerCase(Locale.ROOT), ATTRIBUTE_VALUE, ATTRIBUTE_VALUE_NEW);
81          assertEquals(page_.asXml(), ATTRIBUTE_VALUE_NEW,
82              element_.getAttribute(ATTRIBUTE_NAME.toLowerCase(Locale.ROOT)));
83      }
84  
85      /**
86       * Tests {@link HtmlElement#setAttribute(String,String)} with a mixed case name.
87       * @throws Exception if the test fails
88       */
89      @Test
90      public void setAttributeMixedCase() throws Exception {
91          setupSetAttributeTest(ATTRIBUTE_NAME, ATTRIBUTE_VALUE, ATTRIBUTE_VALUE_NEW);
92          assertEquals(page_.asXml(), ATTRIBUTE_VALUE_NEW,
93              element_.getAttribute(ATTRIBUTE_NAME.toLowerCase(Locale.ROOT)));
94      }
95  
96      /**
97       * Tests {@link HtmlElement#setAttribute(String,String)} with an upper case name.
98       * @throws Exception if the test fails
99       */
100     @Test
101     public void setAttributeUpperCase() throws Exception {
102         setupSetAttributeTest(ATTRIBUTE_NAME.toUpperCase(Locale.ROOT), ATTRIBUTE_VALUE, ATTRIBUTE_VALUE_NEW);
103         assertEquals(page_.asXml(), ATTRIBUTE_VALUE_NEW,
104             element_.getAttribute(ATTRIBUTE_NAME.toLowerCase(Locale.ROOT)));
105     }
106 
107     /**
108      * Tests {@link HtmlElement#removeAttribute(String)} with a lower case name.
109      * @throws Exception if the test fails
110      */
111     @Test
112     public void removeAttributeLowerCase() throws Exception {
113         setupGetAttributeTest(ATTRIBUTE_NAME, ATTRIBUTE_VALUE);
114         element_.removeAttribute(ATTRIBUTE_NAME.toLowerCase(Locale.ROOT));
115         assertEquals(page_.asXml(), "", element_.getAttribute(ATTRIBUTE_NAME.toLowerCase(Locale.ROOT)));
116     }
117 
118     /**
119      * Tests {@link HtmlElement#removeAttribute(String)} with a mixed case name.
120      * @throws Exception if the test fails
121      */
122     @Test
123     public void removeAttributeMixedCase() throws Exception {
124         setupGetAttributeTest(ATTRIBUTE_NAME, ATTRIBUTE_VALUE);
125         element_.removeAttribute(ATTRIBUTE_NAME);
126         assertEquals(page_.asXml(), "", element_.getAttribute(ATTRIBUTE_NAME.toLowerCase(Locale.ROOT)));
127     }
128 
129     /**
130      * Tests {@link HtmlElement#removeAttribute(String)} with an upper case name.
131      * @throws Exception if the test fails
132      */
133     @Test
134     public void removeAttributeUpperCase() throws Exception {
135         setupGetAttributeTest(ATTRIBUTE_NAME, ATTRIBUTE_VALUE);
136         element_.removeAttribute(ATTRIBUTE_NAME.toUpperCase(Locale.ROOT));
137         assertEquals(page_.asXml(), "", element_.getAttribute(ATTRIBUTE_NAME.toLowerCase(Locale.ROOT)));
138     }
139 
140     /**
141      * Tests {@link HtmlElement#hasAttribute(String)} with a lower case name.
142      * @throws Exception if the test fails
143      */
144     @Test
145     public void hasAttributeLowerCase() throws Exception {
146         setupGetAttributeTest(ATTRIBUTE_NAME, ATTRIBUTE_VALUE);
147         assertTrue(page_.asXml(), element_.hasAttribute(ATTRIBUTE_NAME.toLowerCase(Locale.ROOT)));
148     }
149 
150     /**
151      * Tests {@link HtmlElement#hasAttribute(String)} with a mixed case name.
152      * @throws Exception if the test fails
153      */
154     @Test
155     public void hasAttributeMixedCase() throws Exception {
156         setupGetAttributeTest(ATTRIBUTE_NAME, ATTRIBUTE_VALUE);
157         assertTrue(page_.asXml(), element_.hasAttribute(ATTRIBUTE_NAME));
158     }
159 
160     /**
161      * Tests {@link HtmlElement#hasAttribute(String)} with an upper case name.
162      * @throws Exception if the test fails
163      */
164     @Test
165     public void hasAttributeUpperCase() throws Exception {
166         setupGetAttributeTest(ATTRIBUTE_NAME, ATTRIBUTE_VALUE);
167         assertTrue(page_.asXml(), element_.hasAttribute(ATTRIBUTE_NAME.toUpperCase(Locale.ROOT)));
168     }
169 
170     private void setupAttributeTest(final String content, final String elementId) throws Exception {
171         page_ = loadPage(content);
172         element_ = page_.getHtmlElementById(elementId);
173     }
174 
175     private void setupGetAttributeTest(final String attributeName, final String attributeValue) throws Exception {
176         final String elementId = "p-id";
177         final String content = DOCTYPE_HTML
178                           + "<html><head><title>AttributeCaseTest</title></head><body>\n"
179                           + "<p id=\"" + elementId + "\" " + attributeName + "=\"" + attributeValue + "\">\n"
180                           + "</body></html>";
181 
182         setupAttributeTest(content, elementId);
183     }
184 
185     private void setupSetAttributeTest(
186             final String attributeName, final String attributeValue,
187             final String newAttributeValue)
188         throws Exception {
189 
190         final String elementId = "p-id";
191         final String content = DOCTYPE_HTML
192              + "<html><head><title>AttributeCaseTest</title></head><body>\n"
193              + "<p id=\"" + elementId + "\" " + attributeName + "=\"" + attributeValue + "\">\n"
194              + "<script language=\"javascript\" type=\"text/javascript\">\n<!--\n"
195              + "  document.getElementById(\"" + elementId + "\").setAttribute(\"" + attributeName + "\", \""
196              + newAttributeValue + "\");\n"
197              + "\n// -->\n</script>\n"
198              + "</body></html>";
199 
200         setupAttributeTest(content, elementId);
201     }
202 }