[Httpunit-commit] CVS: httpunit/src/com/meterware/httpunit Button.java,NONE,1.1 FormControl.java,1.1
Brought to you by:
russgold
From: Russell G. <rus...@us...> - 2002-08-19 18:52:29
|
Update of /cvsroot/httpunit/httpunit/src/com/meterware/httpunit In directory usw-pr-cvs1:/tmp/cvs-serv7684/src/com/meterware/httpunit Modified Files: FormControl.java GetMethodWebRequest.java MessageBodyWebRequest.java PostMethodWebRequest.java SubmitButton.java WebForm.java Added Files: Button.java Log Message: Added support for form.submit and Button.onClick ***** Error reading new file[Errno 2] No such file or directory: 'Button.java' Index: FormControl.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/FormControl.java,v retrieving revision 1.17 retrieving revision 1.18 diff -u -r1.17 -r1.18 --- FormControl.java 13 Aug 2002 20:00:39 -0000 1.17 +++ FormControl.java 19 Aug 2002 18:52:25 -0000 1.18 @@ -44,12 +44,15 @@ final static String[] NO_VALUE = new String[0]; private String _name; + private final String _id; private final String _valueAttribute; private final boolean _readOnly; private final boolean _disabled; + private Scriptable _scriptable; FormControl() { + _id = ""; _name = ""; _valueAttribute = ""; _readOnly = false; @@ -58,6 +61,7 @@ FormControl( Node node ) { + _id = NodeUtils.getNodeAttribute( node, "id" ); _name = NodeUtils.getNodeAttribute( node, "name" ); _valueAttribute = NodeUtils.getNodeAttribute( node, "value" ); _readOnly = NodeUtils.isNodeAttributePresent( node, "readonly" ); @@ -74,17 +78,27 @@ /** + * Returns the ID associated with this control, if any. + * @return the control ID, or an empty string if no ID is defined. + **/ + public String getID() { + return _id; + } + + + /** * Returns the current value(s) associated with this control. These values will be transmitted to the server * if the control is 'successful'. **/ - abstract public String[] getValues(); + abstract String[] getValues(); /** * Returns a scriptable object which can act as a proxy for this control. */ public ScriptableDelegate getScriptableObject() { - return new Scriptable(); + if (_scriptable == null) _scriptable = newScriptable(); + return _scriptable; } @@ -191,6 +205,15 @@ /** + * Creates and returns a scriptable object for this control. Subclasses should override this if they use a different + * implementation of Scriptable. + */ + protected Scriptable newScriptable() { + return new Scriptable(); + } + + + /** * Returns the default value of this control in the form. If no value is specified, defaults to the empty string. **/ protected String getValueAttribute() { @@ -219,7 +242,7 @@ if (type.equalsIgnoreCase( "submit" )) { return new SubmitButton( form, node ); } else { - return null; + return new Button( form, node ); } } else if (!node.getNodeName().equals( "input" )) { return null; @@ -235,6 +258,10 @@ return new CheckboxFormControl( node ); } else if (type.equalsIgnoreCase( "submit" ) || type.equalsIgnoreCase( "image" )) { return new SubmitButton( form, node ); + } else if (type.equalsIgnoreCase( "button" )) { + return new Button( form, node ); + } else if (type.equalsIgnoreCase( "reset" )) { + return new Button( form, node ); } else if (type.equalsIgnoreCase( "file" )) { return new FileSubmitFormControl( node ); } else { @@ -453,7 +480,7 @@ } - public ScriptableDelegate getScriptableObject() { + protected FormControl.Scriptable newScriptable() { return new Scriptable(); } Index: GetMethodWebRequest.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/GetMethodWebRequest.java,v retrieving revision 1.14 retrieving revision 1.15 diff -u -r1.14 -r1.15 --- GetMethodWebRequest.java 4 Feb 2002 22:33:55 -0000 1.14 +++ GetMethodWebRequest.java 19 Aug 2002 18:52:26 -0000 1.15 @@ -87,6 +87,14 @@ /** + * Constructs a web request for a form submitted from JavaScript. + **/ + GetMethodWebRequest( WebForm sourceForm ) { + super( sourceForm ); + } + + + /** * Constructs a web request for a link. **/ GetMethodWebRequest( WebLink sourceLink ) { Index: MessageBodyWebRequest.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/MessageBodyWebRequest.java,v retrieving revision 1.8 retrieving revision 1.9 diff -u -r1.8 -r1.9 --- MessageBodyWebRequest.java 25 Jan 2002 19:09:08 -0000 1.8 +++ MessageBodyWebRequest.java 19 Aug 2002 18:52:26 -0000 1.9 @@ -54,10 +54,18 @@ /** - * Constructs a web request for a form. + * Constructs a web request for a form submitted via a button. **/ protected MessageBodyWebRequest( WebForm sourceForm, SubmitButton button, int x, int y ) { super( sourceForm, button, x, y ); + } + + + /** + * Constructs a web request for a form submitted via script. + **/ + protected MessageBodyWebRequest( WebForm sourceForm ) { + super( sourceForm ); } Index: PostMethodWebRequest.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/PostMethodWebRequest.java,v retrieving revision 1.24 retrieving revision 1.25 diff -u -r1.24 -r1.25 --- PostMethodWebRequest.java 20 May 2002 16:00:43 -0000 1.24 +++ PostMethodWebRequest.java 19 Aug 2002 18:52:26 -0000 1.25 @@ -117,10 +117,18 @@ /** - * Constructs a web request for a form. + * Constructs a web request for a form submitted by clicking a button. **/ PostMethodWebRequest( WebForm sourceForm, SubmitButton button, int x, int y ) { super( sourceForm, button, x, y ); + } + + + /** + * Constructs a web request for a form submitted via a script. + **/ + PostMethodWebRequest( WebForm sourceForm ) { + super( sourceForm ); } Index: SubmitButton.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/SubmitButton.java,v retrieving revision 1.10 retrieving revision 1.11 diff -u -r1.10 -r1.11 --- SubmitButton.java 13 Aug 2002 17:57:47 -0000 1.10 +++ SubmitButton.java 19 Aug 2002 18:52:26 -0000 1.11 @@ -27,27 +27,17 @@ /** * This class represents a submit button in an HTML form. **/ -public class SubmitButton extends FormControl { - - -// public static SubmitButton UNNAMED_BUTTON = new SubmitButton(); - +public class SubmitButton extends Button { /** - * Returns the value associated with this submit button. + * Returns true if this submit button is an image map. **/ - public String getValue() { - return getValueAttribute(); + public boolean isImageButton() { + return _isImageButton; } - /** - * Returns the ID associated with the button, if any. - * @return the button ID, or an empty string if no ID is defined. - **/ - public String getID() { - return _id; - } +//--------------------------------- FormButton methods ---------------------------------------------- /** @@ -59,36 +49,6 @@ } - /** - * Returns true if this submit button is an image map. - **/ - public boolean isImageButton() { - return _isImageButton; - } - - - /** - * Returns the current value(s) associated with this control. These values will be transmitted to the server - * if the control is 'successful'. - **/ - public String[] getValues() { - return (isDisabled() || !_pressed) ? NO_VALUE : toArray( getValueAttribute() ); - } - - - void addValues( ParameterProcessor processor, String characterSet ) throws IOException { - if (_pressed && !isDisabled() && getName().length() > 0) { - if (getValueAttribute().length() > 0) { - processor.addParameter( getName(), getValueAttribute(), characterSet ); - } - if (_isImageButton) { - processor.addParameter( getName() + ".x", Integer.toString( _x ), characterSet ); - processor.addParameter( getName() + ".y", Integer.toString( _y ), characterSet ); - } - } - } - - //------------------------------------ Object methods ---------------------------------------- @@ -111,16 +71,13 @@ SubmitButton( WebForm form, Node node ) { - super( node ); - _form = form; + super( form, node ); _isImageButton = NodeUtils.getNodeAttribute( node, "type" ).equalsIgnoreCase( "image" ); - _id = NodeUtils.getNodeAttribute( node, "id" ); } SubmitButton( WebForm form ) { - _form = form; - _id = ""; + super( form ); _isImageButton = false; } @@ -136,15 +93,39 @@ } +//--------------------------------- FormControl methods ---------------------------------------------------------------- + + + /** + * Returns the current value(s) associated with this control. These values will be transmitted to the server + * if the control is 'successful'. + **/ + String[] getValues() { + return (isDisabled() || !_pressed) ? NO_VALUE : toArray( getValueAttribute() ); + } + + + void addValues( ParameterProcessor processor, String characterSet ) throws IOException { + if (_pressed && !isDisabled() && getName().length() > 0) { + if (getValueAttribute().length() > 0) { + processor.addParameter( getName(), getValueAttribute(), characterSet ); + } + if (_isImageButton) { + processor.addParameter( getName() + ".x", Integer.toString( _x ), characterSet ); + processor.addParameter( getName() + ".y", Integer.toString( _y ), characterSet ); + } + } + } + + //------------------------------------------ private members ---------------------------------- - private final WebForm _form; - private final String _id; + + private String[] _value = new String[1]; private final boolean _isImageButton; private boolean _pressed; private int _x; private int _y; - private String[] _value = new String[1]; private String[] toArray( String value ) { Index: WebForm.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/WebForm.java,v retrieving revision 1.57 retrieving revision 1.58 diff -u -r1.57 -r1.58 --- WebForm.java 13 Aug 2002 20:00:39 -0000 1.57 +++ WebForm.java 19 Aug 2002 18:52:26 -0000 1.58 @@ -43,6 +43,7 @@ **/ public class WebForm extends WebRequestSource { private static final FormParameter UNKNOWN_PARAMETER = new FormParameter(); + private Button[] _buttons; /** @@ -100,6 +101,23 @@ /** + * Returns an array containing all of the buttons defined for this form. + **/ + public Button[] getButtons() { + if (_buttons == null) { + FormControl[] controls = getFormControls(); + ArrayList buttonList = new ArrayList(); + for (int i = 0; i < controls.length; i++) { + FormControl control = controls[ i ]; + if (control instanceof Button) buttonList.add( control ); + } + _buttons = (Button[]) buttonList.toArray( new Button[ buttonList.size() ] ); + } + return _buttons; + } + + + /** * Returns an array containing the submit buttons defined for this form. **/ public SubmitButton[] getSubmitButtons() { @@ -220,6 +238,21 @@ } + private WebRequest getScriptedSubmitRequest() { + SubmitButton[] buttons = getSubmitButtons(); + for (int i = 0; i < buttons.length; i++) { + buttons[i].setPressed( false ); + } + + if (getMethod().equalsIgnoreCase( "post" )) { + return new PostMethodWebRequest( this ); + } else { + return new GetMethodWebRequest( this ); + } + + } + + /** * Returns the default value of the named parameter. If the parameter does not exist returns null. **/ @@ -424,6 +457,11 @@ public class Scriptable extends ScriptableDelegate { public String getAction() { return WebForm.this.getAction(); } public void setAction( String newAction ) { setDestination( newAction ); } + + + public void submit() throws IOException, SAXException { + submitRequest( getScriptedSubmitRequest() ); + } public Object get( String propertyName ) { |