1
2
3
4
5
6
7
8
9
10
11
12
13
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
26
27
28
29
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
42
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
53
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
63
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
74
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
85
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
96
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
107
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
118
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
129
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
140
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
150
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
160
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 }