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

hazendaz / httpunit / 755

14 Feb 2026 07:14PM UTC coverage: 80.526%. Remained the same
755

push

github

hazendaz
[ci] Fix badge

3213 of 4105 branches covered (78.27%)

Branch coverage included in aggregate %.

8245 of 10124 relevant lines covered (81.44%)

0.81 hits per line

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

90.1
/src/main/java/com/meterware/httpunit/SubmitButton.java
1
/*
2
 * SPDX-License-Identifier: MIT
3
 * See LICENSE file for details.
4
 *
5
 * Copyright 2000-2026 Russell Gold
6
 * Copyright 2021-2000 hazendaz
7
 */
8
package com.meterware.httpunit;
9

10
import com.meterware.httpunit.dom.HTMLControl;
11
import com.meterware.httpunit.protocol.ParameterProcessor;
12

13
import java.io.IOException;
14

15
import org.xml.sax.SAXException;
16

17
/**
18
 * This class represents a submit button in an HTML form.
19
 **/
20
public class SubmitButton extends Button {
21

22
    /** The fake. */
23
    private boolean _fake;
24

25
    @Override
26
    public String getType() {
27
        return isImageButton() ? IMAGE_BUTTON_TYPE : SUBMIT_BUTTON_TYPE;
1✔
28
    }
29

30
    /**
31
     * Returns true if this submit button is an image map.
32
     *
33
     * @return true, if is image button
34
     */
35
    public boolean isImageButton() {
36
        return _isImageButton;
1✔
37
    }
38

39
    /**
40
     * Performs the action associated with clicking this button after running any 'onClick' script. For a submit button
41
     * this typically submits the form.
42
     *
43
     * @param x
44
     *            the x
45
     * @param y
46
     *            the y
47
     *
48
     * @throws IOException
49
     *             Signals that an I/O exception has occurred.
50
     * @throws SAXException
51
     *             the SAX exception
52
     */
53
    public void click(int x, int y) throws IOException, SAXException {
54
        if (!isImageButton()) {
1!
55
            throw new IllegalStateException("May only specify positions for an image button");
×
56
        }
57
        doOnClickSequence(x, y);
1✔
58
    }
1✔
59

60
    // --------------------------------- Button methods ----------------------------------------------
61

62
    /**
63
     * do the button Action
64
     *
65
     * @param x
66
     *            - x coordinate
67
     * @param y
68
     *            - y coordinate
69
     */
70
    @Override
71
    protected void doButtonAction(int x, int y) throws IOException, SAXException {
72
        getForm().doFormSubmit(this, x, y);
1✔
73
    }
1✔
74

75
    // ------------------------------------ Object methods ----------------------------------------
76

77
    @Override
78
    public String toString() {
79
        return "Submit with " + getName() + "=" + getValue();
×
80
    }
81

82
    @Override
83
    public int hashCode() {
84
        return getName().hashCode() + getValue().hashCode();
×
85
    }
86

87
    @Override
88
    public boolean equals(Object o) {
89
        return getClass().equals(o.getClass()) && equals((SubmitButton) o);
1!
90
    }
91

92
    // ------------------------------------------ package members ----------------------------------
93

94
    /**
95
     * Instantiates a new submit button.
96
     *
97
     * @param form
98
     *            the form
99
     * @param control
100
     *            the control
101
     */
102
    SubmitButton(WebForm form, HTMLControl control) {
103
        super(form, control);
1✔
104
        _isImageButton = control.getType().equalsIgnoreCase(IMAGE_BUTTON_TYPE);
1✔
105
    }
1✔
106

107
    /**
108
     * Instantiates a new submit button.
109
     *
110
     * @param form
111
     *            the form
112
     */
113
    SubmitButton(WebForm form) {
114
        super(form);
1✔
115
        _isImageButton = false;
1✔
116
    }
1✔
117

118
    /**
119
     * Creates the fake submit button.
120
     *
121
     * @param form
122
     *            the form
123
     *
124
     * @return the submit button
125
     */
126
    static SubmitButton createFakeSubmitButton(WebForm form) {
127
        return new SubmitButton(form, /* fake */ true);
1✔
128
    }
129

130
    /**
131
     * Instantiates a new submit button.
132
     *
133
     * @param form
134
     *            the form
135
     * @param fake
136
     *            the fake
137
     */
138
    private SubmitButton(WebForm form, boolean fake) {
139
        this(form);
1✔
140
        _fake = fake;
1✔
141
    }
1✔
142

143
    /**
144
     * getter for the fake flag Returns true for synthetic submit buttons, created by HttpUnit in forms that contain no
145
     * submit buttons, or used during {@link WebForm#submitNoButton()} call.
146
     *
147
     * @return - whether this button is a faked button inserted by httpunit
148
     */
149
    public boolean isFake() {
150
        return _fake;
1✔
151
    }
152

153
    /**
154
     * flag that the button was pressed.
155
     *
156
     * @param pressed
157
     *            the new pressed
158
     */
159
    void setPressed(boolean pressed) {
160
        _pressed = pressed;
1✔
161
    }
1✔
162

163
    /**
164
     * Sets the location.
165
     *
166
     * @param x
167
     *            the x
168
     * @param y
169
     *            the y
170
     */
171
    void setLocation(int x, int y) {
172
        _x = x;
1✔
173
        _y = y;
1✔
174
    }
1✔
175

176
    // --------------------------------- FormControl methods
177
    // ----------------------------------------------------------------
178

179
    /**
180
     * Returns the current value(s) associated with this control. These values will be transmitted to the server if the
181
     * control is 'successful'.
182
     **/
183
    @Override
184
    protected String[] getValues() {
185
        return isDisabled() || !_pressed ? NO_VALUE : toArray(getValue());
1!
186
    }
187

188
    /** should we allow unnamed Image Buttons?. */
189
    private static boolean allowUnnamedImageButton = false;
1✔
190

191
    /**
192
     * Checks if is allow unnamed image button.
193
     *
194
     * @return the allowUnnamedImageButton
195
     */
196
    public static boolean isAllowUnnamedImageButton() {
197
        return allowUnnamedImageButton;
1✔
198
    }
199

200
    /**
201
     * Sets the allow unnamed image button.
202
     *
203
     * @param allowUnnamedImageButton
204
     *            the allowUnnamedImageButton to set
205
     */
206
    public static void setAllowUnnamedImageButton(boolean allowUnnamedImageButton) {
207
        SubmitButton.allowUnnamedImageButton = allowUnnamedImageButton;
1✔
208
    }
1✔
209

210
    /**
211
     * return whether this is a validImageButton.
212
     *
213
     * @return true if it is an image Button
214
     */
215
    public boolean isValidImageButton() {
216
        String buttonName = getName();
1✔
217
        boolean valid = this.isImageButton();
1✔
218
        if (!allowUnnamedImageButton) {
1✔
219
            valid = valid && buttonName != null && buttonName.length() > 0;
1!
220
        }
221
        return valid;
1✔
222
    }
223

224
    /**
225
     * return the name of the positionParameter for this button (if this is an image Button).
226
     *
227
     * @param direction
228
     *            e.g. "x" or "y"
229
     *
230
     * @return the name e.g. "image.x" or just "x"
231
     */
232
    public String positionParameterName(String direction) {
233
        // [ 1443333 ] Allow unnamed Image input elments to submit x,y values
234
        String buttonName = getName();
1✔
235
        String buttonPrefix = "";
1✔
236
        if (buttonName != null && buttonName.length() > 0) {
1!
237
            buttonPrefix = buttonName + ".";
1✔
238
        }
239
        return buttonPrefix + direction;
1✔
240
    }
241

242
    /**
243
     * addValues if not disabled and pressed
244
     *
245
     * @param processor
246
     *            - the ParameterProcessor used
247
     * @param characterSet
248
     *            - the active character set
249
     *
250
     * @throws IOException
251
     *             if addValues fails
252
     */
253
    @Override
254
    protected void addValues(ParameterProcessor processor, String characterSet) throws IOException {
255
        if (_pressed && !isDisabled()) {
1✔
256
            String buttonName = getName();
1✔
257
            if (buttonName != null && buttonName.length() > 0 && getValue().length() > 0) {
1!
258
                processor.addParameter(getName(), getValue(), characterSet);
1✔
259
            }
260
            if (isValidImageButton()) {
1✔
261
                processor.addParameter(positionParameterName("x"), Integer.toString(_x), characterSet);
1✔
262
                processor.addParameter(positionParameterName("y"), Integer.toString(_y), characterSet);
1✔
263
            }
264
        } // if
265
    }
1✔
266

267
    // ------------------------------------------ private members ----------------------------------
268

269
    /** The value. */
270
    private String[] _value = new String[1];
1✔
271

272
    /** The is image button. */
273
    private final boolean _isImageButton;
274

275
    /** The pressed. */
276
    private boolean _pressed;
277

278
    /** The x. */
279
    private int _x;
280

281
    /** The y. */
282
    private int _y;
283

284
    /**
285
     * To array.
286
     *
287
     * @param value
288
     *            the value
289
     *
290
     * @return the string[]
291
     */
292
    private String[] toArray(String value) {
293
        _value[0] = value;
1✔
294
        return _value;
1✔
295
    }
296

297
    /**
298
     * Equals.
299
     *
300
     * @param button
301
     *            the button
302
     *
303
     * @return true, if successful
304
     */
305
    private boolean equals(SubmitButton button) {
306
        return getName().equals(button.getName()) && (getName().isEmpty() || getValue().equals(button.getValue()));
1✔
307
    }
308

309
    @Override
310
    public void throwDisabledException() {
311
        throw new DisabledSubmitButtonException(this);
1✔
312
    }
313

314
    /**
315
     * This exception is thrown on an attempt to define a form request with a button not defined on that form.
316
     **/
317
    class DisabledSubmitButtonException extends DisabledButtonException {
318

319
        /** The Constant serialVersionUID. */
320
        private static final long serialVersionUID = 1L;
321

322
        /**
323
         * Instantiates a new disabled submit button exception.
324
         *
325
         * @param button
326
         *            the button
327
         */
328
        DisabledSubmitButtonException(SubmitButton button) {
1✔
329
            super(button);
1✔
330
        }
1✔
331

332
        @Override
333
        public String getMessage() {
334
            return "The specified button (name='" + _name + "' value='" + _value
1✔
335
                    + "' is disabled and may not be used to submit this form.";
336
        }
337

338
    }
339

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