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.htmlunit.junit.BrowserRunner;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25
26
27
28
29
30
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
44
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
55
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
65
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
76
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
87
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
98
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
109
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
120
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
131
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
142
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
152
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
162
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 }