• 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

93.85
/src/main/java/com/meterware/httpunit/dom/HTMLInputElementImpl.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.ParameterProcessor;
23

24
import java.io.IOException;
25
import java.util.Locale;
26

27
import org.w3c.dom.DOMException;
28
import org.w3c.dom.Node;
29
import org.w3c.dom.html.HTMLCollection;
30
import org.w3c.dom.html.HTMLInputElement;
31

32
/**
33
 * The Class HTMLInputElementImpl.
34
 */
35
public class HTMLInputElementImpl extends HTMLControl implements HTMLInputElement {
1✔
36

37
    /** The Constant serialVersionUID. */
38
    private static final long serialVersionUID = 1L;
39

40
    /** The value. */
41
    private String _value;
42

43
    /** The checked. */
44
    private Boolean _checked;
45

46
    /** The behavior. */
47
    private TypeSpecificBehavior _behavior;
48

49
    @Override
50
    ElementImpl create() {
51
        return new HTMLInputElementImpl();
1✔
52
    }
53

54
    /**
55
     * simulate blur.
56
     */
57
    @Override
58
    public void blur() {
59
        handleEvent("onblur");
×
60
    }
×
61

62
    /**
63
     * simulate focus;.
64
     */
65
    @Override
66
    public void focus() {
67
        handleEvent("onfocus");
×
68
    }
×
69

70
    @Override
71
    public void doClickAction() {
72
        getBehavior().click();
1✔
73
    }
1✔
74

75
    /**
76
     * Select.
77
     */
78
    @Override
79
    public void select() {
80
    }
×
81

82
    /**
83
     * Gets the accept.
84
     *
85
     * @return the accept
86
     */
87
    @Override
88
    public String getAccept() {
89
        return getAttributeWithNoDefault("accept");
1✔
90
    }
91

92
    /**
93
     * Gets the access key.
94
     *
95
     * @return the access key
96
     */
97
    @Override
98
    public String getAccessKey() {
99
        return getAttributeWithNoDefault("accessKey");
1✔
100
    }
101

102
    /**
103
     * Gets the align.
104
     *
105
     * @return the align
106
     */
107
    @Override
108
    public String getAlign() {
109
        return getAttributeWithDefault("align", "bottom");
1✔
110
    }
111

112
    /**
113
     * Gets the alt.
114
     *
115
     * @return the alt
116
     */
117
    @Override
118
    public String getAlt() {
119
        return getAttributeWithNoDefault("alt");
1✔
120
    }
121

122
    /**
123
     * Gets the checked.
124
     *
125
     * @return the checked
126
     */
127
    @Override
128
    public boolean getChecked() {
129
        return getBehavior().getChecked();
1✔
130
    }
131

132
    /**
133
     * Gets the default checked.
134
     *
135
     * @return the default checked
136
     */
137
    @Override
138
    public boolean getDefaultChecked() {
139
        return getBooleanAttribute("checked");
1✔
140
    }
141

142
    /**
143
     * Gets the default value.
144
     *
145
     * @return the default value
146
     */
147
    @Override
148
    public String getDefaultValue() {
149
        return getAttributeWithNoDefault("value");
1✔
150
    }
151

152
    /**
153
     * Gets the max length.
154
     *
155
     * @return the max length
156
     */
157
    @Override
158
    public int getMaxLength() {
159
        return getIntegerAttribute("maxlength");
1✔
160
    }
161

162
    /**
163
     * Gets the size.
164
     *
165
     * @return the size
166
     */
167
    @Override
168
    public String getSize() {
169
        return getAttributeWithNoDefault("size");
1✔
170
    }
171

172
    /**
173
     * Gets the src.
174
     *
175
     * @return the src
176
     */
177
    @Override
178
    public String getSrc() {
179
        return getAttributeWithNoDefault("src");
1✔
180
    }
181

182
    /**
183
     * Gets the use map.
184
     *
185
     * @return the use map
186
     */
187
    @Override
188
    public String getUseMap() {
189
        return getAttributeWithNoDefault("useMap");
1✔
190
    }
191

192
    /**
193
     * Sets the accept.
194
     *
195
     * @param accept
196
     *            the new accept
197
     */
198
    @Override
199
    public void setAccept(String accept) {
200
        setAttribute("accept", accept);
1✔
201
    }
1✔
202

203
    /**
204
     * Sets the access key.
205
     *
206
     * @param accessKey
207
     *            the new access key
208
     */
209
    @Override
210
    public void setAccessKey(String accessKey) {
211
        setAttribute("accessKey", accessKey);
1✔
212
    }
1✔
213

214
    /**
215
     * Sets the align.
216
     *
217
     * @param align
218
     *            the new align
219
     */
220
    @Override
221
    public void setAlign(String align) {
222
        setAttribute("align", align);
1✔
223
    }
1✔
224

225
    /**
226
     * Sets the alt.
227
     *
228
     * @param alt
229
     *            the new alt
230
     */
231
    @Override
232
    public void setAlt(String alt) {
233
        setAttribute("alt", alt);
1✔
234
    }
1✔
235

236
    /**
237
     * Sets the checked.
238
     *
239
     * @param checked
240
     *            the new checked
241
     */
242
    @Override
243
    public void setChecked(boolean checked) {
244
        getBehavior().setChecked(checked);
1✔
245
    }
1✔
246

247
    /**
248
     * Sets the default checked.
249
     *
250
     * @param defaultChecked
251
     *            the new default checked
252
     */
253
    @Override
254
    public void setDefaultChecked(boolean defaultChecked) {
255
        setAttribute("checked", defaultChecked);
1✔
256
    }
1✔
257

258
    /**
259
     * Sets the default value.
260
     *
261
     * @param defaultValue
262
     *            the new default value
263
     */
264
    @Override
265
    public void setDefaultValue(String defaultValue) {
266
        setAttribute("value", defaultValue);
1✔
267
    }
1✔
268

269
    /**
270
     * Sets the max length.
271
     *
272
     * @param maxLength
273
     *            the new max length
274
     */
275
    @Override
276
    public void setMaxLength(int maxLength) {
277
        setAttribute("maxlength", maxLength);
1✔
278
    }
1✔
279

280
    /**
281
     * Sets the size.
282
     *
283
     * @param size
284
     *            the new size
285
     */
286
    @Override
287
    public void setSize(String size) {
288
        setAttribute("size", size);
1✔
289
    }
1✔
290

291
    /**
292
     * Sets the src.
293
     *
294
     * @param src
295
     *            the new src
296
     */
297
    @Override
298
    public void setSrc(String src) {
299
        setAttribute("src", src);
1✔
300
    }
1✔
301

302
    /**
303
     * Sets the use map.
304
     *
305
     * @param useMap
306
     *            the new use map
307
     */
308
    @Override
309
    public void setUseMap(String useMap) {
310
        setAttribute("useMap", useMap);
1✔
311
    }
1✔
312

313
    /**
314
     * Gets the value.
315
     *
316
     * @return the value
317
     */
318
    @Override
319
    public String getValue() {
320
        return getBehavior().getValue();
1✔
321
    }
322

323
    /**
324
     * Sets the value.
325
     *
326
     * @param value
327
     *            the new value
328
     */
329
    @Override
330
    public void setValue(String value) {
331
        getBehavior().setValue(value);
1✔
332
    }
1✔
333

334
    @Override
335
    public void reset() {
336
        getBehavior().reset();
1✔
337
    }
1✔
338

339
    @Override
340
    public void setAttribute(String name, String value) throws DOMException {
341
        super.setAttribute(name, value);
1✔
342
        if (name.equalsIgnoreCase("type")) {
1✔
343
            selectBehavior(getType().toLowerCase(Locale.ENGLISH));
1✔
344
        }
345
    }
1✔
346

347
    @Override
348
    void addValues(ParameterProcessor processor, String characterSet) throws IOException {
349
        getBehavior().addValues(getName(), processor, characterSet);
1✔
350
    }
1✔
351

352
    @Override
353
    public void silenceSubmitButton() {
354
        getBehavior().silenceSubmitButton();
1✔
355
    }
1✔
356

357
    /**
358
     * Sets the state.
359
     *
360
     * @param checked
361
     *            the new state
362
     */
363
    void setState(boolean checked) {
364
        _checked = checked ? Boolean.TRUE : Boolean.FALSE;
1✔
365
    }
1✔
366

367
    /**
368
     * Equals.
369
     *
370
     * @param s1
371
     *            the s 1
372
     * @param s2
373
     *            the s 2
374
     *
375
     * @return true, if successful
376
     */
377
    static boolean equals(String s1, String s2) {
378
        return s1 == null ? s2 == null : s1.equals(s2);
1!
379
    }
380

381
    /**
382
     * Select behavior.
383
     *
384
     * @param type
385
     *            the type
386
     */
387
    private void selectBehavior(String type) {
388
        if (type == null || type.equals("text") || type.equals("password") || type.equals("hidden")) {
1!
389
            _behavior = new EditableTextBehavior(this);
1✔
390
        } else if (type.equals("checkbox")) {
1✔
391
            _behavior = new CheckboxBehavior(this);
1✔
392
        } else if (type.equals("radio")) {
1✔
393
            _behavior = new RadioButtonBehavior(this);
1✔
394
        } else if (type.equals("reset")) {
1✔
395
            _behavior = new ResetButtonBehavior(this);
1✔
396
        } else if (type.equals("submit")) {
1✔
397
            _behavior = new SubmitButtonBehavior(this);
1✔
398
        } else {
399
            _behavior = new DefaultBehavior(this);
1✔
400
        }
401
    }
1✔
402

403
    /**
404
     * Gets the behavior.
405
     *
406
     * @return the behavior
407
     */
408
    private TypeSpecificBehavior getBehavior() {
409
        if (_behavior == null) {
1✔
410
            selectBehavior(getType().toLowerCase(Locale.ENGLISH));
1✔
411
        }
412
        return _behavior;
1✔
413
    }
414

415
    /**
416
     * The Interface TypeSpecificBehavior.
417
     */
418
    interface TypeSpecificBehavior {
419

420
        /**
421
         * Sets the value.
422
         *
423
         * @param value
424
         *            the new value
425
         */
426
        void setValue(String value);
427

428
        /**
429
         * Gets the value.
430
         *
431
         * @return the value
432
         */
433
        String getValue();
434

435
        /**
436
         * Reset.
437
         */
438
        void reset();
439

440
        /**
441
         * Click.
442
         */
443
        void click();
444

445
        /**
446
         * Gets the checked.
447
         *
448
         * @return the checked
449
         */
450
        boolean getChecked();
451

452
        /**
453
         * Sets the checked.
454
         *
455
         * @param checked
456
         *            the new checked
457
         */
458
        void setChecked(boolean checked);
459

460
        /**
461
         * Adds the values.
462
         *
463
         * @param name
464
         *            the name
465
         * @param processor
466
         *            the processor
467
         * @param characterSet
468
         *            the character set
469
         *
470
         * @throws IOException
471
         *             Signals that an I/O exception has occurred.
472
         */
473
        void addValues(String name, ParameterProcessor processor, String characterSet) throws IOException;
474

475
        /**
476
         * Silence submit button.
477
         */
478
        void silenceSubmitButton();
479
    }
480

481
    /**
482
     * The Class DefaultBehavior.
483
     */
484
    class DefaultBehavior implements TypeSpecificBehavior {
485

486
        /** The element. */
487
        private HTMLElementImpl _element;
488

489
        /**
490
         * Instantiates a new default behavior.
491
         *
492
         * @param element
493
         *            the element
494
         */
495
        public DefaultBehavior(HTMLElementImpl element) {
1✔
496
            _element = element;
1✔
497
        }
1✔
498

499
        @Override
500
        public String getValue() {
501
            return _value != null ? _value : getDefaultValue();
1✔
502
        }
503

504
        @Override
505
        public void setValue(String value) {
506
            if (HTMLInputElementImpl.equals(value, _value)) {
1✔
507
                return;
1✔
508
            }
509

510
            _value = value;
1✔
511
            reportPropertyChanged("value");
1✔
512
        }
1✔
513

514
        @Override
515
        public boolean getChecked() {
516
            return getDefaultChecked();
×
517
        }
518

519
        @Override
520
        public void setChecked(boolean checked) {
521
        }
×
522

523
        @Override
524
        public void reset() {
525
        }
1✔
526

527
        @Override
528
        public void click() {
529
        }
×
530

531
        /**
532
         * Report property changed.
533
         *
534
         * @param propertyName
535
         *            the property name
536
         */
537
        protected void reportPropertyChanged(String propertyName) {
538
            _element.reportPropertyChanged(propertyName);
1✔
539
        }
1✔
540

541
        @Override
542
        public void addValues(String name, ParameterProcessor processor, String characterSet) throws IOException {
543
            processor.addParameter(name, getValue(), characterSet);
1✔
544
        }
1✔
545

546
        @Override
547
        public void silenceSubmitButton() {
548
        }
1✔
549
    }
550

551
    /**
552
     * The Class EditableTextBehavior.
553
     */
554
    class EditableTextBehavior extends DefaultBehavior {
555

556
        /**
557
         * Instantiates a new editable text behavior.
558
         *
559
         * @param element
560
         *            the element
561
         */
562
        public EditableTextBehavior(HTMLElementImpl element) {
1✔
563
            super(element);
1✔
564
        }
1✔
565

566
        @Override
567
        public void reset() {
568
            _value = null;
1✔
569
        }
1✔
570

571
    }
572

573
    /**
574
     * The Class SubmitButtonBehavior.
575
     */
576
    class SubmitButtonBehavior extends DefaultBehavior {
577

578
        /** The send with submit. */
579
        private boolean _sendWithSubmit;
580

581
        /**
582
         * Instantiates a new submit button behavior.
583
         *
584
         * @param element
585
         *            the element
586
         */
587
        public SubmitButtonBehavior(HTMLElementImpl element) {
1✔
588
            super(element);
1✔
589
        }
1✔
590

591
        @Override
592
        public void click() {
593
            _sendWithSubmit = true;
1✔
594
            ((HTMLFormElementImpl) getForm()).doSubmitAction();
1✔
595
        }
1✔
596

597
        @Override
598
        public void addValues(String name, ParameterProcessor processor, String characterSet) throws IOException {
599
            if (!_sendWithSubmit) {
1✔
600
                return;
1✔
601
            }
602
            super.addValues(name, processor, characterSet);
1✔
603
        }
1✔
604

605
        @Override
606
        public void silenceSubmitButton() {
607
            _sendWithSubmit = false;
1✔
608
        }
1✔
609

610
    }
611

612
    /**
613
     * The Class CheckboxBehavior.
614
     */
615
    class CheckboxBehavior extends DefaultBehavior {
616

617
        /**
618
         * Instantiates a new checkbox behavior.
619
         *
620
         * @param element
621
         *            the element
622
         */
623
        public CheckboxBehavior(HTMLElementImpl element) {
1✔
624
            super(element);
1✔
625
        }
1✔
626

627
        @Override
628
        public boolean getChecked() {
629
            return _checked != null ? _checked.booleanValue() : getDefaultChecked();
1✔
630
        }
631

632
        @Override
633
        public void setChecked(boolean checked) {
634
            setState(checked);
1✔
635
        }
1✔
636

637
        @Override
638
        public void reset() {
639
            _checked = null;
1✔
640
        }
1✔
641

642
        @Override
643
        public void click() {
644
            setChecked(!getChecked());
1✔
645
        }
1✔
646

647
        @Override
648
        public void addValues(String name, ParameterProcessor processor, String characterSet) throws IOException {
649
            if (!getDisabled() && getChecked()) {
1!
650
                processor.addParameter(name, getFormValue(), characterSet);
1✔
651
            }
652
        }
1✔
653

654
        /**
655
         * Gets the form value.
656
         *
657
         * @return the form value
658
         */
659
        private String getFormValue() {
660
            return _value == null ? "on" : _value;
1✔
661
        }
662
    }
663

664
    /**
665
     * The Class RadioButtonBehavior.
666
     */
667
    class RadioButtonBehavior extends CheckboxBehavior {
668

669
        /**
670
         * Instantiates a new radio button behavior.
671
         *
672
         * @param element
673
         *            the element
674
         */
675
        public RadioButtonBehavior(HTMLElementImpl element) {
1✔
676
            super(element);
1✔
677
        }
1✔
678

679
        @Override
680
        public void setChecked(boolean checked) {
681
            if (checked) {
1✔
682
                HTMLCollection elements = getForm().getElements();
1✔
683
                for (int i = 0; i < elements.getLength(); i++) {
1✔
684
                    Node node = elements.item(i);
1✔
685
                    if (!(node instanceof HTMLInputElementImpl)) {
1✔
686
                        continue;
1✔
687
                    }
688
                    HTMLInputElementImpl input = (HTMLInputElementImpl) node;
1✔
689
                    if (getName().equals(input.getName()) && input.getType().equalsIgnoreCase("radio")) {
1!
690
                        input.setState(false);
1✔
691
                    }
692
                }
693
            }
694
            setState(checked);
1✔
695
        }
1✔
696

697
        @Override
698
        public void click() {
699
            setChecked(true);
1✔
700
        }
1✔
701
    }
702

703
    /**
704
     * The Class ResetButtonBehavior.
705
     */
706
    class ResetButtonBehavior extends DefaultBehavior {
707

708
        /**
709
         * Instantiates a new reset button behavior.
710
         *
711
         * @param element
712
         *            the element
713
         */
714
        public ResetButtonBehavior(HTMLElementImpl element) {
1✔
715
            super(element);
1✔
716
        }
1✔
717

718
        @Override
719
        public void click() {
720
            getForm().reset();
1✔
721
        }
1✔
722

723
    }
724

725
}
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