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.junit.jupiter.api.Test;
23  
24  /**
25   * Tests for {@link HtmlElement} attributes.
26   *
27   * @author David D. Kilzer
28   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
29   * @author Ronald Brill
30   */
31  public class AttributeCaseTest extends SimpleWebTestCase {
32  
33      private static final String ATTRIBUTE_NAME = "randomAttribute";
34      private static final String ATTRIBUTE_VALUE = "someValue";
35      private static final String ATTRIBUTE_VALUE_NEW = "newValue";
36  
37      private HtmlElement element_;
38      private HtmlPage page_;
39  
40      /**
41       * Tests {@link HtmlElement#getAttribute(String)} with a lower case name.
42       * @throws Exception if the test fails
43       */
44      @Test
45      public void getAttributeLowerCase() throws Exception {
46          setupGetAttributeTest(ATTRIBUTE_NAME, ATTRIBUTE_VALUE);
47          assertEquals(page_.asXml(), ATTRIBUTE_VALUE,
48                  element_.getAttribute(ATTRIBUTE_NAME.toLowerCase(Locale.ROOT)));
49      }
50  
51      /**
52       * Tests {@link HtmlElement#getAttribute(String)} with a mixed case name.
53       * @throws Exception if the test fails
54       */
55      @Test
56      public void getAttributeMixedCase() throws Exception {
57          setupGetAttributeTest(ATTRIBUTE_NAME, ATTRIBUTE_VALUE);
58          assertEquals(page_.asXml(), ATTRIBUTE_VALUE, element_.getAttribute(ATTRIBUTE_NAME));
59      }
60  
61      /**
62       * Tests {@link HtmlElement#getAttribute(String)} with an upper case name.
63       * @throws Exception if the test fails
64       */
65      @Test
66      public void getAttributeUpperCase() throws Exception {
67          setupGetAttributeTest(ATTRIBUTE_NAME, ATTRIBUTE_VALUE);
68          assertEquals(page_.asXml(), ATTRIBUTE_VALUE,
69                  element_.getAttribute(ATTRIBUTE_NAME.toUpperCase(Locale.ROOT)));
70      }
71  
72      /**
73       * Tests {@link HtmlElement#setAttribute(String,String)} with a lower case name.
74       * @throws Exception if the test fails
75       */
76      @Test
77      public void setAttributeLowerCase() throws Exception {
78          setupSetAttributeTest(ATTRIBUTE_NAME.toLowerCase(Locale.ROOT), ATTRIBUTE_VALUE, ATTRIBUTE_VALUE_NEW);
79          assertEquals(page_.asXml(), ATTRIBUTE_VALUE_NEW,
80              element_.getAttribute(ATTRIBUTE_NAME.toLowerCase(Locale.ROOT)));
81      }
82  
83      /**
84       * Tests {@link HtmlElement#setAttribute(String,String)} with a mixed case name.
85       * @throws Exception if the test fails
86       */
87      @Test
88      public void setAttributeMixedCase() throws Exception {
89          setupSetAttributeTest(ATTRIBUTE_NAME, ATTRIBUTE_VALUE, ATTRIBUTE_VALUE_NEW);
90          assertEquals(page_.asXml(), ATTRIBUTE_VALUE_NEW,
91              element_.getAttribute(ATTRIBUTE_NAME.toLowerCase(Locale.ROOT)));
92      }
93  
94      /**
95       * Tests {@link HtmlElement#setAttribute(String,String)} with an upper case name.
96       * @throws Exception if the test fails
97       */
98      @Test
99      public void setAttributeUpperCase() throws Exception {
100         setupSetAttributeTest(ATTRIBUTE_NAME.toUpperCase(Locale.ROOT), ATTRIBUTE_VALUE, ATTRIBUTE_VALUE_NEW);
101         assertEquals(page_.asXml(), ATTRIBUTE_VALUE_NEW,
102             element_.getAttribute(ATTRIBUTE_NAME.toLowerCase(Locale.ROOT)));
103     }
104 
105     /**
106      * Tests {@link HtmlElement#removeAttribute(String)} with a lower case name.
107      * @throws Exception if the test fails
108      */
109     @Test
110     public void removeAttributeLowerCase() throws Exception {
111         setupGetAttributeTest(ATTRIBUTE_NAME, ATTRIBUTE_VALUE);
112         element_.removeAttribute(ATTRIBUTE_NAME.toLowerCase(Locale.ROOT));
113         assertEquals(page_.asXml(), "", element_.getAttribute(ATTRIBUTE_NAME.toLowerCase(Locale.ROOT)));
114     }
115 
116     /**
117      * Tests {@link HtmlElement#removeAttribute(String)} with a mixed case name.
118      * @throws Exception if the test fails
119      */
120     @Test
121     public void removeAttributeMixedCase() throws Exception {
122         setupGetAttributeTest(ATTRIBUTE_NAME, ATTRIBUTE_VALUE);
123         element_.removeAttribute(ATTRIBUTE_NAME);
124         assertEquals(page_.asXml(), "", element_.getAttribute(ATTRIBUTE_NAME.toLowerCase(Locale.ROOT)));
125     }
126 
127     /**
128      * Tests {@link HtmlElement#removeAttribute(String)} with an upper case name.
129      * @throws Exception if the test fails
130      */
131     @Test
132     public void removeAttributeUpperCase() throws Exception {
133         setupGetAttributeTest(ATTRIBUTE_NAME, ATTRIBUTE_VALUE);
134         element_.removeAttribute(ATTRIBUTE_NAME.toUpperCase(Locale.ROOT));
135         assertEquals(page_.asXml(), "", element_.getAttribute(ATTRIBUTE_NAME.toLowerCase(Locale.ROOT)));
136     }
137 
138     /**
139      * Tests {@link HtmlElement#hasAttribute(String)} with a lower case name.
140      * @throws Exception if the test fails
141      */
142     @Test
143     public void hasAttributeLowerCase() throws Exception {
144         setupGetAttributeTest(ATTRIBUTE_NAME, ATTRIBUTE_VALUE);
145         assertTrue(page_.asXml(), element_.hasAttribute(ATTRIBUTE_NAME.toLowerCase(Locale.ROOT)));
146     }
147 
148     /**
149      * Tests {@link HtmlElement#hasAttribute(String)} with a mixed case name.
150      * @throws Exception if the test fails
151      */
152     @Test
153     public void hasAttributeMixedCase() throws Exception {
154         setupGetAttributeTest(ATTRIBUTE_NAME, ATTRIBUTE_VALUE);
155         assertTrue(page_.asXml(), element_.hasAttribute(ATTRIBUTE_NAME));
156     }
157 
158     /**
159      * Tests {@link HtmlElement#hasAttribute(String)} with an upper case name.
160      * @throws Exception if the test fails
161      */
162     @Test
163     public void hasAttributeUpperCase() throws Exception {
164         setupGetAttributeTest(ATTRIBUTE_NAME, ATTRIBUTE_VALUE);
165         assertTrue(page_.asXml(), element_.hasAttribute(ATTRIBUTE_NAME.toUpperCase(Locale.ROOT)));
166     }
167 
168     private void setupAttributeTest(final String content, final String elementId) throws Exception {
169         page_ = loadPage(content);
170         element_ = page_.getHtmlElementById(elementId);
171     }
172 
173     private void setupGetAttributeTest(final String attributeName, final String attributeValue) throws Exception {
174         final String elementId = "p-id";
175         final String content = DOCTYPE_HTML
176                           + "<html><head><title>AttributeCaseTest</title></head><body>\n"
177                           + "<p id=\"" + elementId + "\" " + attributeName + "=\"" + attributeValue + "\">\n"
178                           + "</body></html>";
179 
180         setupAttributeTest(content, elementId);
181     }
182 
183     private void setupSetAttributeTest(
184             final String attributeName, final String attributeValue,
185             final String newAttributeValue)
186         throws Exception {
187 
188         final String elementId = "p-id";
189         final String content = DOCTYPE_HTML
190              + "<html><head><title>AttributeCaseTest</title></head><body>\n"
191              + "<p id=\"" + elementId + "\" " + attributeName + "=\"" + attributeValue + "\">\n"
192              + "<script language=\"javascript\" type=\"text/javascript\">\n<!--\n"
193              + "  document.getElementById(\"" + elementId + "\").setAttribute(\"" + attributeName + "\", \""
194              + newAttributeValue + "\");\n"
195              + "\n// -->\n</script>\n"
196              + "</body></html>";
197 
198         setupAttributeTest(content, elementId);
199     }
200 }