• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hazendaz / httpunit / 656

06 Dec 2025 09:11PM UTC coverage: 80.452% (+0.02%) from 80.435%
656

push

github

hazendaz
[maven-release-plugin] prepare for next development iteration

3213 of 4105 branches covered (78.27%)

Branch coverage included in aggregate %.

8245 of 10137 relevant lines covered (81.34%)

0.81 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.46
/src/main/java/com/meterware/httpunit/dom/HTMLFormElementImpl.java
1
/*
2
 * MIT License
3
 *
4
 * Copyright 2011-2025 Russell Gold
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
7
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
8
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
9
 * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
10
 *
11
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions
12
 * of the Software.
13
 *
14
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
15
 * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
17
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
18
 * DEALINGS IN THE SOFTWARE.
19
 */
20
package com.meterware.httpunit.dom;
21

22
import com.meterware.httpunit.protocol.URLEncodedString;
23
import com.meterware.httpunit.scripting.FormScriptable;
24

25
import java.io.IOException;
26
import java.net.URL;
27
import java.util.ArrayList;
28
import java.util.Iterator;
29

30
import org.mozilla.javascript.Scriptable;
31
import org.mozilla.javascript.ScriptableObject;
32
import org.w3c.dom.Element;
33
import org.w3c.dom.NamedNodeMap;
34
import org.w3c.dom.Node;
35
import org.w3c.dom.html.HTMLCollection;
36
import org.w3c.dom.html.HTMLFormElement;
37

38
/**
39
 * The Class HTMLFormElementImpl.
40
 */
41
public class HTMLFormElementImpl extends HTMLElementImpl implements HTMLFormElement, FormScriptable {
1✔
42

43
    /** The Constant serialVersionUID. */
44
    private static final long serialVersionUID = 1L;
45

46
    @Override
47
    ElementImpl create() {
48
        return new HTMLFormElementImpl();
1✔
49
    }
50

51
    // ------------------------------- ScriptableObject methods
52
    // ----------------------------------------------------------
53

54
    @Override
55
    public Object get(String propertyName, Scriptable scriptable) {
56
        HTMLCollection elements = getElements();
1✔
57
        for (int i = 0; i < elements.getLength(); i++) {
1!
58
            Node node = elements.item(i);
1✔
59
            NamedNodeMap attributes = node.getAttributes();
1✔
60
            AttrImpl nameAttribute = (AttrImpl) attributes.getNamedItem("name");
1✔
61
            if (nameAttribute != null && propertyName.equals(nameAttribute.getValue())) {
1✔
62
                return node;
1✔
63
            }
64
            AttrImpl idAttribute = (AttrImpl) attributes.getNamedItem("id");
1✔
65
            if (idAttribute != null && propertyName.equals(idAttribute.getValue())) {
1✔
66
                return node;
1✔
67
            }
68
        }
69
        return super.get(propertyName, scriptable);
×
70
    }
71

72
    // ------------------------------- HTMLFormElement methods
73
    // ----------------------------------------------------------
74

75
    /**
76
     * Gets the accept charset.
77
     *
78
     * @return the accept charset
79
     */
80
    @Override
81
    public String getAcceptCharset() {
82
        return getAttributeWithDefault("accept-charset", "UNKNOWN");
1✔
83
    }
84

85
    /**
86
     * Sets the accept charset.
87
     *
88
     * @param acceptCharset
89
     *            the new accept charset
90
     */
91
    @Override
92
    public void setAcceptCharset(String acceptCharset) {
93
        setAttribute("accept-charset", acceptCharset);
1✔
94
    }
1✔
95

96
    /**
97
     * Gets the action.
98
     *
99
     * @return the action
100
     */
101
    @Override
102
    public String getAction() {
103
        return getAttribute("action");
1✔
104
    }
105

106
    @Override
107
    public void setAction(String action) {
108
        setAttribute("action", action);
1✔
109
    }
1✔
110

111
    @Override
112
    public void setParameterValue(String name, String value) {
113
        Object control = get(name, null);
×
114
        if (control instanceof ScriptableObject) {
×
115
            ((ScriptableObject) control).put("value", this, value);
×
116
        }
117
    }
×
118

119
    /**
120
     * Gets the enctype.
121
     *
122
     * @return the enctype
123
     */
124
    @Override
125
    public String getEnctype() {
126
        return getAttributeWithDefault("enctype", "application/x-www-form-urlencoded");
1✔
127
    }
128

129
    /**
130
     * Sets the enctype.
131
     *
132
     * @param enctype
133
     *            the new enctype
134
     */
135
    @Override
136
    public void setEnctype(String enctype) {
137
        setAttribute("enctype", enctype);
1✔
138
    }
1✔
139

140
    /**
141
     * Gets the method.
142
     *
143
     * @return the method
144
     */
145
    @Override
146
    public String getMethod() {
147
        return getAttributeWithDefault("method", "get");
1✔
148
    }
149

150
    /**
151
     * Sets the method.
152
     *
153
     * @param method
154
     *            the new method
155
     */
156
    @Override
157
    public void setMethod(String method) {
158
        setAttribute("method", method);
1✔
159
    }
1✔
160

161
    /**
162
     * getter for the name.
163
     *
164
     * @return the name
165
     *
166
     * @see org.w3c.dom.html.HTMLFormElement#getName()
167
     */
168
    @Override
169
    public String getName() {
170
        String result = getAttributeWithNoDefault("name");
1✔
171
        if (result == null) {
1✔
172
            result = this.getId();
1✔
173
        }
174
        return result;
1✔
175
    }
176

177
    /**
178
     * Sets the name.
179
     *
180
     * @param name
181
     *            the new name
182
     */
183
    @Override
184
    public void setName(String name) {
185
        setAttribute("name", name);
1✔
186
    }
1✔
187

188
    /**
189
     * Gets the target.
190
     *
191
     * @return the target
192
     */
193
    @Override
194
    public String getTarget() {
195
        return getAttributeWithNoDefault("target");
1✔
196
    }
197

198
    /**
199
     * Sets the target.
200
     *
201
     * @param target
202
     *            the new target
203
     */
204
    @Override
205
    public void setTarget(String target) {
206
        setAttribute("target", target);
1✔
207
    }
1✔
208

209
    /**
210
     * Gets the elements.
211
     *
212
     * @return the elements
213
     */
214
    @Override
215
    public HTMLCollection getElements() {
216
        ArrayList elements = new ArrayList<>();
1✔
217
        String[] names = { "INPUT", "TEXTAREA", "BUTTON", "SELECT" };
1✔
218
        for (Iterator each = preOrderIteratorAfterNode(); each.hasNext();) {
1✔
219
            Node node = (Node) each.next();
1✔
220
            if (node instanceof HTMLFormElement) {
1✔
221
                break;
1✔
222
            }
223

224
            if (node.getNodeType() != ELEMENT_NODE) {
1✔
225
                continue;
1✔
226
            }
227
            String tagName = ((Element) node).getTagName();
1✔
228
            for (String name : names) {
1✔
229
                if (tagName.equalsIgnoreCase(name)) {
1✔
230
                    elements.add(node);
1✔
231
                }
232
            }
233
        }
1✔
234
        return HTMLCollectionImpl.createHTMLCollectionImpl(new NodeListImpl(elements));
1✔
235
    }
236

237
    /**
238
     * Gets the length.
239
     *
240
     * @return the length
241
     */
242
    @Override
243
    public int getLength() {
244
        return 0;
1✔
245
    }
246

247
    /**
248
     * Reset.
249
     */
250
    @Override
251
    public void reset() {
252
        HTMLCollection elements = getElements();
1✔
253
        for (int i = 0; i < elements.getLength(); i++) {
1✔
254
            Node node = elements.item(i);
1✔
255
            if (node instanceof HTMLControl) {
1!
256
                ((HTMLControl) node).reset();
1✔
257
            }
258
        }
259
    }
1✔
260

261
    /**
262
     * Submit.
263
     */
264
    @Override
265
    public void submit() {
266
        doSubmitAction();
1✔
267
    }
1✔
268

269
    /**
270
     * Handles the actual form submission - does not handle the "submit" event.
271
     */
272
    void doSubmitAction() {
273
        try {
274
            if ("get".equalsIgnoreCase(getMethod())) {
1!
275
                getDomWindow().submitRequest(this, getMethod(), getEffectiveUrl(), getTarget(), new byte[0]);
1✔
276
            } else if ("post".equalsIgnoreCase(getMethod())) {
×
277
                getDomWindow().submitRequest(this, getMethod(), getAction(), getTarget(), new byte[0]);
×
278
            }
279
        } catch (Exception e) {
×
280
            throw new RuntimeException("Error submitting form: " + e);
×
281
        } finally {
282
            silenceSubmitButtons();
1✔
283
        }
284
    }
1✔
285

286
    /**
287
     * Silence submit buttons.
288
     */
289
    private void silenceSubmitButtons() {
290
        HTMLCollection controls = getElements();
1✔
291
        for (int i = 0; i < controls.getLength(); i++) {
1✔
292
            ((HTMLControl) controls.item(i)).silenceSubmitButton();
1✔
293
        }
294
    }
1✔
295

296
    /**
297
     * Gets the effective url.
298
     *
299
     * @return the effective url
300
     *
301
     * @throws IOException
302
     *             Signals that an I/O exception has occurred.
303
     */
304
    private String getEffectiveUrl() throws IOException {
305
        StringBuilder spec = new StringBuilder(getAction());
1✔
306
        if ("get".equalsIgnoreCase(getMethod())) {
1!
307
            URLEncodedString parameters = new URLEncodedString();
1✔
308
            HTMLCollection controls = getElements();
1✔
309
            for (int i = 0; i < controls.getLength(); i++) {
1✔
310
                ((HTMLControl) controls.item(i)).addValues(parameters, "us-ascii");
1✔
311
            }
312
            if (spec.indexOf("?") >= 0 && !spec.toString().endsWith("?")) {
1!
313
                spec.append('&');
×
314
            } else {
315
                spec.append('?');
1✔
316
            }
317
            spec.append(parameters.getString());
1✔
318
        }
319
        return new URL(getDomWindow().getUrl(), spec.toString()).toExternalForm();
1✔
320
    }
321

322
    /**
323
     * Gets the dom window.
324
     *
325
     * @return the dom window
326
     */
327
    private DomWindow getDomWindow() {
328
        return ((HTMLDocumentImpl) getOwnerDocument()).getWindow();
1✔
329
    }
330

331
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc