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

hazendaz / httpunit / #155

20 Aug 2024 11:42PM UTC coverage: 80.622% (+0.004%) from 80.618%
#155

push

github

hazendaz
[ci] format the code

3231 of 4119 branches covered (78.44%)

Branch coverage included in aggregate %.

68 of 80 new or added lines in 21 files covered. (85.0%)

4 existing lines in 4 files now uncovered.

8285 of 10165 relevant lines covered (81.51%)

0.82 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-2024 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
 * @author <a href="mailto:russgold@httpunit.org">Russell Gold</a>
33
 **/
34
public class FormParameter {
1✔
35

36
    private static final FormParameter UNKNOWN_PARAMETER = new FormParameter();
1✔
37

38
    private FormControl[] _controls;
39
    private ArrayList _controlList = new ArrayList();
1✔
40
    private RadioGroupFormControl _group;
41
    private String _name;
42

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

308
    // ============================= exception class UnusedParameterValueException
309
    // ======================================
310

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

316
        private static final long serialVersionUID = 1L;
317

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

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

342
        private String _parameterName;
343
        private String _badValue;
344
    }
345

346
    // ============================= exception class UnusedUploadFileException ======================================
347

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

353
        private static final long serialVersionUID = 1L;
354

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

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

385
        private String _parameterName;
386
        private int _numExpected;
387
        private int _numSupplied;
388
    }
389

390
    // ============================= exception class IllegalCheckboxParameterException
391
    // ======================================
392

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

398
        private static final long serialVersionUID = 1L;
399

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

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

413
        private String _parameterName;
414
        private String _methodName;
415
    }
416

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