• 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

90.1
/src/main/java/com/meterware/httpunit/SubmitButton.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.dom.HTMLControl;
23
import com.meterware.httpunit.protocol.ParameterProcessor;
24

25
import java.io.IOException;
26

27
import org.xml.sax.SAXException;
28

29
/**
30
 * This class represents a submit button in an HTML form.
31
 **/
32
public class SubmitButton extends Button {
33

34
    /** The fake. */
35
    private boolean _fake;
36

37
    @Override
38
    public String getType() {
39
        return isImageButton() ? IMAGE_BUTTON_TYPE : SUBMIT_BUTTON_TYPE;
1✔
40
    }
41

42
    /**
43
     * Returns true if this submit button is an image map.
44
     *
45
     * @return true, if is image button
46
     */
47
    public boolean isImageButton() {
48
        return _isImageButton;
1✔
49
    }
50

51
    /**
52
     * Performs the action associated with clicking this button after running any 'onClick' script. For a submit button
53
     * this typically submits the form.
54
     *
55
     * @param x
56
     *            the x
57
     * @param y
58
     *            the y
59
     *
60
     * @throws IOException
61
     *             Signals that an I/O exception has occurred.
62
     * @throws SAXException
63
     *             the SAX exception
64
     */
65
    public void click(int x, int y) throws IOException, SAXException {
66
        if (!isImageButton()) {
1!
67
            throw new IllegalStateException("May only specify positions for an image button");
×
68
        }
69
        doOnClickSequence(x, y);
1✔
70
    }
1✔
71

72
    // --------------------------------- Button methods ----------------------------------------------
73

74
    /**
75
     * do the button Action
76
     *
77
     * @param x
78
     *            - x coordinate
79
     * @param y
80
     *            - y coordinate
81
     */
82
    @Override
83
    protected void doButtonAction(int x, int y) throws IOException, SAXException {
84
        getForm().doFormSubmit(this, x, y);
1✔
85
    }
1✔
86

87
    // ------------------------------------ Object methods ----------------------------------------
88

89
    @Override
90
    public String toString() {
91
        return "Submit with " + getName() + "=" + getValue();
×
92
    }
93

94
    @Override
95
    public int hashCode() {
96
        return getName().hashCode() + getValue().hashCode();
×
97
    }
98

99
    @Override
100
    public boolean equals(Object o) {
101
        return getClass().equals(o.getClass()) && equals((SubmitButton) o);
1!
102
    }
103

104
    // ------------------------------------------ package members ----------------------------------
105

106
    /**
107
     * Instantiates a new submit button.
108
     *
109
     * @param form
110
     *            the form
111
     * @param control
112
     *            the control
113
     */
114
    SubmitButton(WebForm form, HTMLControl control) {
115
        super(form, control);
1✔
116
        _isImageButton = control.getType().equalsIgnoreCase(IMAGE_BUTTON_TYPE);
1✔
117
    }
1✔
118

119
    /**
120
     * Instantiates a new submit button.
121
     *
122
     * @param form
123
     *            the form
124
     */
125
    SubmitButton(WebForm form) {
126
        super(form);
1✔
127
        _isImageButton = false;
1✔
128
    }
1✔
129

130
    /**
131
     * Creates the fake submit button.
132
     *
133
     * @param form
134
     *            the form
135
     *
136
     * @return the submit button
137
     */
138
    static SubmitButton createFakeSubmitButton(WebForm form) {
139
        return new SubmitButton(form, /* fake */ true);
1✔
140
    }
141

142
    /**
143
     * Instantiates a new submit button.
144
     *
145
     * @param form
146
     *            the form
147
     * @param fake
148
     *            the fake
149
     */
150
    private SubmitButton(WebForm form, boolean fake) {
151
        this(form);
1✔
152
        _fake = fake;
1✔
153
    }
1✔
154

155
    /**
156
     * getter for the fake flag Returns true for synthetic submit buttons, created by HttpUnit in forms that contain no
157
     * submit buttons, or used during {@link WebForm#submitNoButton()} call.
158
     *
159
     * @return - whether this button is a faked button inserted by httpunit
160
     */
161
    public boolean isFake() {
162
        return _fake;
1✔
163
    }
164

165
    /**
166
     * flag that the button was pressed.
167
     *
168
     * @param pressed
169
     *            the new pressed
170
     */
171
    void setPressed(boolean pressed) {
172
        _pressed = pressed;
1✔
173
    }
1✔
174

175
    /**
176
     * Sets the location.
177
     *
178
     * @param x
179
     *            the x
180
     * @param y
181
     *            the y
182
     */
183
    void setLocation(int x, int y) {
184
        _x = x;
1✔
185
        _y = y;
1✔
186
    }
1✔
187

188
    // --------------------------------- FormControl methods
189
    // ----------------------------------------------------------------
190

191
    /**
192
     * Returns the current value(s) associated with this control. These values will be transmitted to the server if the
193
     * control is 'successful'.
194
     **/
195
    @Override
196
    protected String[] getValues() {
197
        return isDisabled() || !_pressed ? NO_VALUE : toArray(getValue());
1!
198
    }
199

200
    /** should we allow unnamed Image Buttons?. */
201
    private static boolean allowUnnamedImageButton = false;
1✔
202

203
    /**
204
     * Checks if is allow unnamed image button.
205
     *
206
     * @return the allowUnnamedImageButton
207
     */
208
    public static boolean isAllowUnnamedImageButton() {
209
        return allowUnnamedImageButton;
1✔
210
    }
211

212
    /**
213
     * Sets the allow unnamed image button.
214
     *
215
     * @param allowUnnamedImageButton
216
     *            the allowUnnamedImageButton to set
217
     */
218
    public static void setAllowUnnamedImageButton(boolean allowUnnamedImageButton) {
219
        SubmitButton.allowUnnamedImageButton = allowUnnamedImageButton;
1✔
220
    }
1✔
221

222
    /**
223
     * return whether this is a validImageButton.
224
     *
225
     * @return true if it is an image Button
226
     */
227
    public boolean isValidImageButton() {
228
        String buttonName = getName();
1✔
229
        boolean valid = this.isImageButton();
1✔
230
        if (!allowUnnamedImageButton) {
1✔
231
            valid = valid && buttonName != null && buttonName.length() > 0;
1!
232
        }
233
        return valid;
1✔
234
    }
235

236
    /**
237
     * return the name of the positionParameter for this button (if this is an image Button).
238
     *
239
     * @param direction
240
     *            e.g. "x" or "y"
241
     *
242
     * @return the name e.g. "image.x" or just "x"
243
     */
244
    public String positionParameterName(String direction) {
245
        // [ 1443333 ] Allow unnamed Image input elments to submit x,y values
246
        String buttonName = getName();
1✔
247
        String buttonPrefix = "";
1✔
248
        if (buttonName != null && buttonName.length() > 0) {
1!
249
            buttonPrefix = buttonName + ".";
1✔
250
        }
251
        return buttonPrefix + direction;
1✔
252
    }
253

254
    /**
255
     * addValues if not disabled and pressed
256
     *
257
     * @param processor
258
     *            - the ParameterProcessor used
259
     * @param characterSet
260
     *            - the active character set
261
     *
262
     * @throws IOException
263
     *             if addValues fails
264
     */
265
    @Override
266
    protected void addValues(ParameterProcessor processor, String characterSet) throws IOException {
267
        if (_pressed && !isDisabled()) {
1✔
268
            String buttonName = getName();
1✔
269
            if (buttonName != null && buttonName.length() > 0 && getValue().length() > 0) {
1!
270
                processor.addParameter(getName(), getValue(), characterSet);
1✔
271
            }
272
            if (isValidImageButton()) {
1✔
273
                processor.addParameter(positionParameterName("x"), Integer.toString(_x), characterSet);
1✔
274
                processor.addParameter(positionParameterName("y"), Integer.toString(_y), characterSet);
1✔
275
            }
276
        } // if
277
    }
1✔
278

279
    // ------------------------------------------ private members ----------------------------------
280

281
    /** The value. */
282
    private String[] _value = new String[1];
1✔
283

284
    /** The is image button. */
285
    private final boolean _isImageButton;
286

287
    /** The pressed. */
288
    private boolean _pressed;
289

290
    /** The x. */
291
    private int _x;
292

293
    /** The y. */
294
    private int _y;
295

296
    /**
297
     * To array.
298
     *
299
     * @param value
300
     *            the value
301
     *
302
     * @return the string[]
303
     */
304
    private String[] toArray(String value) {
305
        _value[0] = value;
1✔
306
        return _value;
1✔
307
    }
308

309
    /**
310
     * Equals.
311
     *
312
     * @param button
313
     *            the button
314
     *
315
     * @return true, if successful
316
     */
317
    private boolean equals(SubmitButton button) {
318
        return getName().equals(button.getName()) && (getName().isEmpty() || getValue().equals(button.getValue()));
1✔
319
    }
320

321
    @Override
322
    public void throwDisabledException() {
323
        throw new DisabledSubmitButtonException(this);
1✔
324
    }
325

326
    /**
327
     * This exception is thrown on an attempt to define a form request with a button not defined on that form.
328
     **/
329
    class DisabledSubmitButtonException extends DisabledButtonException {
330

331
        /** The Constant serialVersionUID. */
332
        private static final long serialVersionUID = 1L;
333

334
        /**
335
         * Instantiates a new disabled submit button exception.
336
         *
337
         * @param button
338
         *            the button
339
         */
340
        DisabledSubmitButtonException(SubmitButton button) {
1✔
341
            super(button);
1✔
342
        }
1✔
343

344
        @Override
345
        public String getMessage() {
346
            return "The specified button (name='" + _name + "' value='" + _value
1✔
347
                    + "' is disabled and may not be used to submit this form.";
348
        }
349

350
    }
351

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