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

hazendaz / httpunit / 636

05 Dec 2025 03:27AM UTC coverage: 80.509%. Remained the same
636

push

github

hazendaz
Cleanup more old since tags

you guessed it, at this point going to jautodoc the rest so the warnings on builds go away ;)

3213 of 4105 branches covered (78.27%)

Branch coverage included in aggregate %.

8249 of 10132 relevant lines covered (81.42%)

0.81 hits per line

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

79.41
/src/main/java/com/meterware/httpunit/FormParameter.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;
21

22
import com.meterware.httpunit.protocol.UploadFileSpec;
23
import com.meterware.httpunit.scripting.ScriptableDelegate;
24

25
import java.util.ArrayList;
26
import java.util.Arrays;
27

28
/**
29
 * Represents the aggregate of all form controls with a particular name. This permits us to abstract setting values so
30
 * that changing a control type does not break a test.
31
 **/
32
public class FormParameter {
1✔
33

34
    private static final FormParameter UNKNOWN_PARAMETER = new FormParameter();
1✔
35

36
    private FormControl[] _controls;
37
    private ArrayList _controlList = new ArrayList<>();
1✔
38
    private RadioGroupFormControl _group;
39
    private String _name;
40

41
    /**
42
     * @return the uNKNOWN_PARAMETER
43
     */
44
    public static FormParameter getUNKNOWN_PARAMETER() {
45
        return UNKNOWN_PARAMETER;
1✔
46
    }
47

48
    /**
49
     * return whether I am the unknown parameter
50
     *
51
     * @return
52
     */
53
    public boolean isUnknown() {
54
        return this == UNKNOWN_PARAMETER;
1✔
55
    }
56

57
    /**
58
     * add the given form control
59
     *
60
     * @param control
61
     */
62
    void addControl(FormControl control) {
63
        _controls = null;
1✔
64
        if (_name == null) {
1✔
65
            _name = control.getName();
1✔
66
        }
67
        if (!_name.equalsIgnoreCase(control.getName())) {
1!
68
            throw new RuntimeException("all controls should have the same name");
×
69
        }
70
        if (control.isExclusive()) {
1✔
71
            getRadioGroup(control.getForm()).addRadioButton((RadioButtonFormControl) control);
1✔
72
        } else {
73
            _controlList.add(control);
1✔
74
        }
75
    }
1✔
76

77
    /**
78
     * get the controls for this form Parameter
79
     *
80
     * @return the controls
81
     */
82
    public FormControl[] getControls() {
83
        if (_controls == null) {
1✔
84
            _controls = (FormControl[]) _controlList.toArray(new FormControl[_controlList.size()]);
1✔
85
        }
86
        return _controls;
1✔
87
    }
88

89
    /**
90
     * get the control for this form Parameter (assuming it has only one as for a text control
91
     *
92
     * @return the controls
93
     */
94
    public FormControl getControl() {
95
        FormControl[] controls = getControls();
×
96
        if (controls.length != 1) {
×
97
            throw new RuntimeException("getControl can only be called if the number of controls is 1 but it is "
×
98
                    + controls.length + " you might want to use getControls instead");
99
        }
100
        return controls[0];
×
101
    }
102

103
    Object getScriptableObject() {
104
        if (getControls().length == 1) {
1✔
105
            return getControls()[0].getDelegate();
1✔
106
        }
107
        ArrayList list = new ArrayList<>();
1✔
108
        for (FormControl control : _controls) {
1✔
109
            list.add(control.getScriptingHandler());
1✔
110
        }
111
        return list.toArray(new ScriptableDelegate[list.size()]);
1✔
112
    }
113

114
    String[] getValues() {
115
        ArrayList valueList = new ArrayList<>();
1✔
116
        FormControl[] controls = getControls();
1✔
117
        for (FormControl control : controls) {
1✔
118
            valueList.addAll(Arrays.asList(control.getValues()));
1✔
119
        }
120
        return (String[]) valueList.toArray(new String[valueList.size()]);
1✔
121
    }
122

123
    /**
124
     * set values to the given values
125
     *
126
     * @param values
127
     */
128
    void setValues(String[] values) {
129
        ArrayList list = new ArrayList(values.length);
1✔
130
        list.addAll(Arrays.asList(values));
1✔
131
        FormControl[] controls = getControls();
1✔
132
        for (FormControl control : controls) {
1✔
133
            control.claimRequiredValues(list);
1✔
134
        }
135
        for (FormControl control : controls) {
1✔
136
            control.claimUniqueValue(list);
1✔
137
        }
138
        for (FormControl control : controls) {
1✔
139
            control.claimValue(list);
1✔
140
        }
141
        if (!list.isEmpty()) {
1✔
142
            throw new UnusedParameterValueException(_name, (String) list.get(0));
1✔
143
        }
144
    }
1✔
145

146
    public void toggleCheckbox() {
147
        FormControl[] controls = getControls();
1✔
148
        if (controls.length != 1) {
1!
149
            throw new IllegalCheckboxParameterException(_name, "toggleCheckbox");
×
150
        }
151
        controls[0].toggle();
1✔
152
    }
1✔
153

154
    public void toggleCheckbox(String value) {
155
        FormControl[] controls = getControls();
1✔
156
        for (FormControl control : controls) {
1!
157
            if (value.equals(control.getValueAttribute())) {
1!
158
                control.toggle();
1✔
159
                return;
1✔
160
            }
161
        }
162
        throw new IllegalCheckboxParameterException(_name + "/" + value, "toggleCheckbox");
×
163
    }
164

165
    public void setValue(boolean state) {
166
        FormControl[] controls = getControls();
1✔
167
        if (controls.length != 1) {
1✔
168
            throw new IllegalCheckboxParameterException(_name, "setCheckbox");
1✔
169
        }
170
        controls[0].setState(state);
1✔
171
    }
1✔
172

173
    public void setValue(String value, boolean state) {
174
        FormControl[] controls = getControls();
1✔
175
        for (FormControl control : controls) {
1✔
176
            if (value.equals(control.getValueAttribute())) {
1✔
177
                control.setState(state);
1✔
178
                return;
1✔
179
            }
180
        }
181
        throw new IllegalCheckboxParameterException(_name + "/" + value, "setCheckbox");
1✔
182
    }
183

184
    void setFiles(UploadFileSpec[] fileArray) {
185
        ArrayList list = new ArrayList(fileArray.length);
1✔
186
        list.addAll(Arrays.asList(fileArray));
1✔
187
        for (int i = 0; i < getControls().length; i++) {
1✔
188
            getControls()[i].claimUploadSpecification(list);
1✔
189
        }
190
        if (!list.isEmpty()) {
1✔
191
            throw new UnusedUploadFileException(_name, fileArray.length - list.size(), fileArray.length);
1✔
192
        }
193
    }
1✔
194

195
    String[] getOptions() {
196
        ArrayList optionList = new ArrayList<>();
1✔
197
        FormControl[] controls = getControls();
1✔
198
        for (FormControl control : controls) {
1✔
199
            optionList.addAll(Arrays.asList(control.getDisplayedOptions()));
1✔
200
        }
201
        return (String[]) optionList.toArray(new String[optionList.size()]);
1✔
202
    }
203

204
    String[] getOptionValues() {
205
        ArrayList valueList = new ArrayList<>();
1✔
206
        for (int i = 0; i < getControls().length; i++) {
1✔
207
            valueList.addAll(Arrays.asList(getControls()[i].getOptionValues()));
1✔
208
        }
209
        return (String[]) valueList.toArray(new String[valueList.size()]);
1✔
210
    }
211

212
    boolean isMultiValuedParameter() {
213
        FormControl[] controls = getControls();
×
214
        for (FormControl control : controls) {
×
215
            if (control.isMultiValued() || !control.isExclusive() && controls.length > 1) {
×
216
                return true;
×
217
            }
218
        }
219
        return false;
×
220
    }
221

222
    int getNumTextParameters() {
223
        int result = 0;
1✔
224
        FormControl[] controls = getControls();
1✔
225
        for (FormControl control : controls) {
1✔
226
            if (control.isTextControl()) {
1!
227
                result++;
1✔
228
            }
229
        }
230
        return result;
1✔
231
    }
232

233
    boolean isTextParameter() {
234
        FormControl[] controls = getControls();
×
235
        for (FormControl control : controls) {
×
236
            if (control.isTextControl()) {
×
237
                return true;
×
238
            }
239
        }
240
        return false;
×
241
    }
242

243
    boolean isFileParameter() {
244
        FormControl[] controls = getControls();
1✔
245
        for (FormControl control : controls) {
1✔
246
            if (control.isFileParameter()) {
1✔
247
                return true;
1✔
248
            }
249
        }
250
        return false;
1✔
251
    }
252

253
    /**
254
     * is this a disabled parameter
255
     *
256
     * @return false if one of the controls is not disabled or this is the unknown parameter
257
     */
258
    boolean isDisabledParameter() {
259
        FormControl[] controls = getControls();
1✔
260
        for (FormControl control : controls) {
1✔
261
            if (!control.isDisabled()) {
1✔
262
                return false;
1✔
263
            }
264
        }
265
        return !this.isUnknown();
1✔
266
    }
267

268
    /**
269
     * is this a read only parameter
270
     *
271
     * @return false if one of the controls is not read only or this is the unknown parameter
272
     */
273
    boolean isReadOnlyParameter() {
274
        FormControl[] controls = getControls();
1✔
275
        for (FormControl control : controls) {
1✔
276
            if (!control.isReadOnly()) {
1✔
277
                return false;
1✔
278
            }
279
        }
280
        return !this.isUnknown();
1✔
281
    }
282

283
    /**
284
     * is this a hidden parameter?
285
     *
286
     * @return false if one of the controls is not hidden or this is the unknown parameter
287
     */
288
    public boolean isHiddenParameter() {
289
        FormControl[] controls = getControls();
1✔
290
        for (FormControl control : controls) {
1✔
291
            if (!control.isHidden()) {
1✔
292
                return false;
1✔
293
            }
294
        }
295
        return !this.isUnknown();
1✔
296
    }
297

298
    private RadioGroupFormControl getRadioGroup(WebForm form) {
299
        if (_group == null) {
1✔
300
            _group = new RadioGroupFormControl(form);
1✔
301
            _controlList.add(_group);
1✔
302
        }
303
        return _group;
1✔
304
    }
305

306
    // ============================= exception class UnusedParameterValueException
307
    // ======================================
308

309
    /**
310
     * This exception is thrown on an attempt to set a parameter to a value not permitted to it by the form.
311
     **/
312
    public class UnusedParameterValueException extends IllegalRequestParameterException {
313

314
        private static final long serialVersionUID = 1L;
315

316
        /**
317
         * construct an exception for an unused parameter with the given name and the value that is bad
318
         *
319
         * @param parameterName
320
         * @param badValue
321
         */
322
        UnusedParameterValueException(String parameterName, String badValue) {
1✔
323
            _parameterName = parameterName;
1✔
324
            _badValue = badValue;
1✔
325
        }
1✔
326

327
        /**
328
         * get the message for this exception
329
         *
330
         * @return the message
331
         */
332
        @Override
333
        public String getMessage() {
334
            StringBuilder sb = new StringBuilder(HttpUnitUtils.DEFAULT_TEXT_BUFFER_SIZE);
1✔
335
            sb.append("Attempted to assign to parameter '").append(_parameterName);
1✔
336
            sb.append("' the extraneous value '").append(_badValue).append("'.");
1✔
337
            return sb.toString();
1✔
338
        }
339

340
        private String _parameterName;
341
        private String _badValue;
342
    }
343

344
    // ============================= exception class UnusedUploadFileException ======================================
345

346
    /**
347
     * This exception is thrown on an attempt to upload more files than permitted by the form.
348
     **/
349
    class UnusedUploadFileException extends IllegalRequestParameterException {
350

351
        private static final long serialVersionUID = 1L;
352

353
        /**
354
         * construct a new UnusedUploadFileException exception base on the parameter Name the number of files expected
355
         * and supplied
356
         *
357
         * @param parameterName
358
         * @param numFilesExpected
359
         * @param numFilesSupplied
360
         */
361
        UnusedUploadFileException(String parameterName, int numFilesExpected, int numFilesSupplied) {
1✔
362
            _parameterName = parameterName;
1✔
363
            _numExpected = numFilesExpected;
1✔
364
            _numSupplied = numFilesSupplied;
1✔
365
        }
1✔
366

367
        /**
368
         * get the message for this exception
369
         */
370
        @Override
371
        public String getMessage() {
372
            StringBuilder sb = new StringBuilder(HttpUnitUtils.DEFAULT_TEXT_BUFFER_SIZE);
×
373
            sb.append("Attempted to upload ").append(_numSupplied).append(" files using parameter '")
×
374
                    .append(_parameterName);
×
375
            if (_numExpected == 0) {
×
376
                sb.append("' which is not a file parameter.");
×
377
            } else {
378
                sb.append("' which only has room for ").append(_numExpected).append('.');
×
379
            }
380
            return sb.toString();
×
381
        }
382

383
        private String _parameterName;
384
        private int _numExpected;
385
        private int _numSupplied;
386
    }
387

388
    // ============================= exception class IllegalCheckboxParameterException
389
    // ======================================
390

391
    /**
392
     * This exception is thrown on an attempt to set a parameter to a value not permitted to it by the form.
393
     **/
394
    static class IllegalCheckboxParameterException extends IllegalRequestParameterException {
395

396
        private static final long serialVersionUID = 1L;
397

398
        IllegalCheckboxParameterException(String parameterName, String methodName) {
1✔
399
            _parameterName = parameterName;
1✔
400
            _methodName = methodName;
1✔
401
        }
1✔
402

403
        @Override
404
        public String getMessage() {
405
            StringBuilder sb = new StringBuilder(HttpUnitUtils.DEFAULT_TEXT_BUFFER_SIZE);
×
406
            sb.append("Attempted to invoke method '").append(_methodName);
×
407
            sb.append("' for parameter '").append(_parameterName).append("', which is not a unique checkbox control.");
×
408
            return sb.toString();
×
409
        }
410

411
        private String _parameterName;
412
        private String _methodName;
413
    }
414

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