[Httpunit-commit] CVS: httpunit/src/com/meterware/httpunit FormControl.java,1.1,1.2 NodeUtils.java,1
Brought to you by:
russgold
|
From: Russell G. <rus...@us...> - 2002-01-08 14:29:30
|
Update of /cvsroot/httpunit/httpunit/src/com/meterware/httpunit
In directory usw-pr-cvs1:/tmp/cvs-serv5179/src/com/meterware/httpunit
Modified Files:
FormControl.java NodeUtils.java WebForm.java
Log Message:
More refactoring
Index: FormControl.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/FormControl.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- FormControl.java 2002/01/01 19:29:07 1.1
+++ FormControl.java 2002/01/08 14:29:27 1.2
@@ -33,26 +33,15 @@
**/
abstract class FormControl {
- protected Node _node;
- /** The type of a parameter which accepts files for upload. **/
- final static Integer TYPE_FILE = new Integer(4);
- /** The type of a parameter which accepts any text. **/
- final static Integer TYPE_TEXT = new Integer(1);
- /** The type of a parameter which accepts single predefined values. **/
- final static Integer TYPE_SCALAR = new Integer(2);
- /** The type of a parameter which accepts multiple predefined values. **/
- final static Integer TYPE_MULTI_VALUED = new Integer(3);
- /** A constant set to indicate one parameter. **/
- final static Integer ONE_PARAMETER = new Integer(1);
+ private String _name;
+ private boolean _readOnly;
+ private boolean _disabled;
FormControl( Node node ) {
- _node = node;
- }
-
-
- Node getNamedItem( String attributeName ) {
- return _node.getAttributes().getNamedItem( attributeName );
+ _name = NodeUtils.getNodeAttribute( node, "name" );
+ _readOnly = NodeUtils.isNodeAttributePresent( node, "readonly" );
+ _disabled = NodeUtils.isNodeAttributePresent( node, "disabled" );
}
@@ -60,47 +49,75 @@
* Returns the name of this control. If no name is specified, defaults to the empty string.
**/
String getName() {
- return NodeUtils.getNodeAttribute( _node, "name" );
+ return _name;
}
/**
- * Returns the value of this control. If no value is specified, defaults to the empty string.
+ * Returns the current value(s) associated with this control. These values will be transmitted to the server
+ * if the control is 'successful'.
**/
- String getValue() {
- return NodeUtils.getNodeAttribute( _node, "value" );
- }
+ abstract String[] getValues();
/**
* Returns true if this control is read-only.
**/
boolean isReadOnly() {
- return _node.getAttributes().getNamedItem( "readonly" ) != null;
+ return _readOnly;
}
- void updateParameterDefaults( Hashtable defaults ) {
+ /**
+ * Returns true if this control is disabled, meaning that it will not send a value to the server as part of a request.
+ **/
+ boolean isDisabled() {
+ return _disabled;
}
- void updateRequiredValues( Hashtable required ) {
+ /**
+ * Returns true if this control accepts free-form text.
+ **/
+ boolean isTextControl() {
+ return false;
}
- void updateParameterOptions( Hashtable options ) {
+ /**
+ * Returns true if only one control of this kind can have a value. This is true for radio buttons.
+ **/
+ boolean isExclusive() {
+ return false;
}
- void updateParameterOptionValues( Hashtable options ) {
+ /**
+ * Returns true if a single control can have multiple values.
+ **/
+ boolean isMultiValued() {
+ return false;
+ }
+
+
+ /**
+ * Returns true if this control accepts a file for upload.
+ **/
+ boolean isFileParameter() {
+ return false;
}
- void updateTextParameterCounts( Hashtable parameterCounts ) {
+ void updateRequiredValues( Hashtable required ) {
}
+
+ void updateParameterOptions( Hashtable options ) {
+ }
- abstract void updateParameterTypes( Hashtable types );
+
+ void updateParameterOptionValues( Hashtable options ) {
+ }
protected void addValue( Hashtable valueMap, String name, String value ) {
@@ -113,16 +130,6 @@
}
- protected static String getValue( Node node ) {
- return (node == null) ? "" : emptyIfNull( node.getNodeValue() );
- }
-
-
- private static String emptyIfNull( String value ) {
- return (value == null) ? "" : value;
- }
-
-
/**
* Adds a string to an array of strings and returns the result.
**/
@@ -164,21 +171,59 @@
abstract
class BooleanFormControl extends FormControl {
+
+ final static String[] NO_VALUE = new String[0];
+
+ private boolean _isChecked;
+ private String[] _value = new String[1];
+
+ private final boolean _isCheckedDefault;
+ private final String _valueAttribute;
+
+
+
public BooleanFormControl( Node node ) {
super( node );
+ _isChecked = _isCheckedDefault = NodeUtils.isNodeAttributePresent( node, "checked" );
+ _valueAttribute = NodeUtils.getNodeAttribute( node, "value" );
}
boolean isChecked() {
- return getNamedItem( "checked" ) != null;
+ return _isChecked;
}
+ /**
+ * Returns the default value of this control in the form. If no value is specified, defaults to the empty string.
+ **/
+ String getValueAttribute() {
+ return _valueAttribute;
+ }
+
+
+ /**
+ * 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 isChecked() ? toArray( getQueryValue() ) : NO_VALUE;
+ }
+
+
+ abstract String getQueryValue();
+
+
void updateParameterOptionValues( Hashtable options ) {
if (isChecked() || !isReadOnly()) {
- addValue( options, getName(), getValue() );
+ addValue( options, getName(), getQueryValue() );
}
}
+
+ private String[] toArray( String value ) {
+ _value[0] = value;
+ return _value;
+ }
}
@@ -190,20 +235,23 @@
}
- void updateParameterDefaults( Hashtable defaults ) {
- if (isChecked()) defaults.put( getName(), getValue() );
+ /**
+ * Returns true if only one control of this kind can have a value.
+ **/
+ boolean isExclusive() {
+ return true;
}
- void updateRequiredValues( Hashtable required ) {
- if (isReadOnly() && isChecked()) {
- required.put( getName(), getValue() );
- }
+ String getQueryValue() {
+ return getValueAttribute();
}
- void updateParameterTypes( Hashtable types ) {
- types.put( getName(), FormControl.TYPE_SCALAR );
+ void updateRequiredValues( Hashtable required ) {
+ if (isReadOnly() && isChecked()) {
+ required.put( getName(), getQueryValue() );
+ }
}
}
@@ -214,25 +262,15 @@
}
- void updateParameterDefaults( Hashtable defaults ) {
- if (isChecked()) addValue( defaults, getName(), getValue() );
- }
-
-
void updateRequiredValues( Hashtable required ) {
if (isReadOnly() && isChecked()) {
- addValue( required, getName(), getValue() );
+ addValue( required, getName(), getQueryValue() );
}
}
- void updateParameterTypes( Hashtable types ) {
- types.put( getName(), FormControl.TYPE_MULTI_VALUED );
- }
-
-
- String getValue() {
- final String value = super.getValue();
+ String getQueryValue() {
+ final String value = getValueAttribute();
return value.length() == 0 ? "on" : value;
}
}
@@ -240,41 +278,42 @@
class TextFormControl extends FormControl {
+ private String[] _value = new String[1];
+ private String[] _defaultValue;
+
- public TextFormControl( Node node ) {
+ public TextFormControl( Node node, String defaultValue ) {
super( node );
+ _defaultValue = new String[] { defaultValue };
}
- void updateParameterDefaults( Hashtable defaults ) {
- defaults.put( getName(), getValue() );
- }
+ /**
+ * 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 (_value[0] != null) ? _value : _defaultValue;
+ }
- void updateTextParameterCounts( Hashtable parameterCounts ) {
- Integer oldCount = (Integer) parameterCounts.get( getName() );
- if (oldCount == null) {
- parameterCounts.put( getName(), ONE_PARAMETER );
- } else {
- parameterCounts.put( getName(), new Integer( oldCount.intValue() + 1 ) );
- }
+ /**
+ * Returns true to indicate that this control accepts free-form text.
+ **/
+ boolean isTextControl() {
+ return true;
}
void updateRequiredValues( Hashtable required ) {
- if (isReadOnly()) required.put( getName(), getValue() );
+ if (isReadOnly()) required.put( getName(), _defaultValue );
}
-
-
- void updateParameterTypes( Hashtable types ) {
- types.put( getName(), FormControl.TYPE_TEXT );
- }
}
class TextFieldFormControl extends TextFormControl {
public TextFieldFormControl( Node node ) {
- super( node );
+ super( node, NodeUtils.getNodeAttribute( node, "value" ) );
}
}
@@ -283,7 +322,7 @@
class TextAreaFormControl extends TextFormControl {
public TextAreaFormControl( Node node ) {
- super( node );
+ super( node, getDefaultValue( node ) );
if (!node.getNodeName().equalsIgnoreCase( "textarea" )) {
throw new RuntimeException( "Not a textarea element" );
@@ -291,8 +330,8 @@
}
- String getValue() {
- return NodeUtils.asText( _node.getChildNodes() );
+ private static String getDefaultValue( Node node ) {
+ return NodeUtils.asText( node.getChildNodes() );
}
}
@@ -304,30 +343,50 @@
super( node );
}
+
+ /**
+ * Returns true if this control accepts a file for upload.
+ **/
+ boolean isFileParameter() {
+ return true;
+ }
+
- void updateParameterTypes( Hashtable types ) {
- types.put( getName(), FormControl.TYPE_FILE );
+ String[] getValues() {
+ return null; // XXX what should this really do?
}
}
class SelectionFormControl extends FormControl {
+ private boolean _multiSelect;
+ private Node _node;
+
SelectionFormControl( Node node ) {
super( node );
- if (!node.getNodeName().equalsIgnoreCase( "select" )) {
- throw new RuntimeException( "Not a select element" );
- }
+ if (!node.getNodeName().equalsIgnoreCase( "select" )) throw new RuntimeException( "Not a select element" );
+
+ _node = node;
+ _multiSelect = NodeUtils.isNodeAttributePresent( node, "multiple" );
}
- void updateParameterDefaults( Hashtable defaults ) {
- defaults.put( getName(), getSelected() );
+ String[] getValues() {
+ return getSelected();
}
+ /**
+ * Returns true if a single control can have multiple values.
+ **/
+ boolean isMultiValued() {
+ return _multiSelect;
+ }
+
+
void updateRequiredParameters( Hashtable required ) {
- if (isDisabled()) required.put( getName(), getSelected() );
+ if (isReadOnly()) required.put( getName(), getSelected() );
}
@@ -341,11 +400,6 @@
}
- void updateParameterTypes( Hashtable types ) {
- types.put( getName(), isMultiSelect() ? FormControl.TYPE_MULTI_VALUED : FormControl.TYPE_SCALAR );
- }
-
-
String[] getSelected() {
Vector selected = new Vector();
NodeList nl = ((Element) _node).getElementsByTagName( "option" );
@@ -355,7 +409,7 @@
}
}
- if (!isMultiSelect() && selected.size() == 0 && nl.getLength() > 0) {
+ if (!isMultiValued() && selected.size() == 0 && nl.getLength() > 0) {
selected.addElement( getOptionValue( nl.item(0) ) );
}
@@ -389,16 +443,6 @@
}
- boolean isMultiSelect() {
- return _node.getAttributes().getNamedItem( "multiple" ) != null;
- }
-
-
- boolean isDisabled() {
- return _node.getAttributes().getNamedItem( "disabled" ) != null;
- }
-
-
private String getOptionValue( Node optionNode ) {
NamedNodeMap nnm = optionNode.getAttributes();
if (nnm.getNamedItem( "value" ) != null) {
@@ -408,6 +452,14 @@
}
}
+ private static String getValue( Node node ) {
+ return (node == null) ? "" : emptyIfNull( node.getNodeValue() );
+ }
+
+
+ private static String emptyIfNull( String value ) {
+ return (value == null) ? "" : value;
+ }
}
Index: NodeUtils.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/NodeUtils.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- NodeUtils.java 2001/11/27 19:51:27 1.8
+++ NodeUtils.java 2002/01/08 14:29:27 1.9
@@ -64,12 +64,15 @@
public static String getNodeAttribute( Node node, String attributeName, String defaultValue ) {
- NamedNodeMap nnm = node.getAttributes();
- Node attribute = nnm.getNamedItem( attributeName );
+ Node attribute = node.getAttributes().getNamedItem( attributeName );
return (attribute == null) ? defaultValue : attribute.getNodeValue();
}
+ static boolean isNodeAttributePresent( Node node, final String attributeName ) {
+ return node.getAttributes().getNamedItem( attributeName ) != null;
+ }
+
/**
* Converts the DOM trees rooted at the specified nodes to text, ignoring
* any HTML tags.
@@ -125,5 +128,6 @@
}
}
}
+
}
Index: WebForm.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/WebForm.java,v
retrieving revision 1.36
retrieving revision 1.37
diff -u -r1.36 -r1.37
--- WebForm.java 2002/01/02 14:13:02 1.36
+++ WebForm.java 2002/01/08 14:29:27 1.37
@@ -24,6 +24,9 @@
import java.util.Hashtable;
import java.util.Vector;
import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Arrays;
+import java.util.Map;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
@@ -37,6 +40,7 @@
* may also create a {@link WebRequest} to simulate the submission of the form.
**/
public class WebForm extends WebRequestSource {
+ private static final FormParameter UNKNOWN_PARAMETER = new FormParameter();
/**
@@ -67,11 +71,7 @@
* Returns true if a parameter with given name exists in this form.
**/
public boolean hasParameterNamed( String soughtName ) {
- FormControl[] parameters = getParameters();
- for (int i = 0; i < parameters.length; i++) {
- if (parameters[i].getName().equals( soughtName )) return true;
- }
- return false;
+ return getFormParameters().containsKey( soughtName );
}
@@ -79,10 +79,9 @@
* Returns true if a parameter starting with a given name exists,
**/
public boolean hasParameterStartingWithPrefix( String prefix ) {
- FormControl[] parameters = getParameters();
- for (int i = 0; i < parameters.length; i++) {
- String name = parameters[i].getName();
- if (name.startsWith( prefix )) return true;
+ String[] names = getParameterNames();
+ for (int i = 0; i < names.length; i++) {
+ if (names[i].startsWith( prefix )) return true;
}
return false;
}
@@ -93,17 +92,29 @@
* in the order in which they appear.
**/
public String[] getParameterNames() {
- Vector parameterNames = new Vector();
- FormControl[] parameters = getParameters();
+ ArrayList parameterNames = new ArrayList( getFormParameters().keySet() );
+ return (String[]) parameterNames.toArray( new String[ parameterNames.size() ] );
+ }
- for (int i = 0; i < parameters.length; i++) {
- String name = parameters[i].getName();
- if (!parameterNames.contains( name )) parameterNames.addElement( name );
- }
- String[] result = new String[ parameterNames.size() ];
- parameterNames.copyInto( result );
- return result;
+ /**
+ * Returns a map of parameter name to form parameter objects. Each form parameter object represents the set of form
+ * controls with a particular name.
+ */
+ private Map getFormParameters() {
+ if (_formParameters == null) {
+ _formParameters = new HashMap();
+ FormControl[] controls = getFormControls();
+ for (int i = 0; i < controls.length; i++) {
+ FormParameter parameter = (FormParameter) _formParameters.get( controls[i].getName() );
+ if (parameter == null) {
+ parameter = new FormParameter();
+ _formParameters.put( controls[i].getName(), parameter );
+ }
+ parameter.addControl( controls[i] );
+ }
+ }
+ return _formParameters;
}
@@ -120,7 +131,6 @@
}
-
private Vector _buttonVector;
private Vector getSubmitButtonVector() {
@@ -279,13 +289,8 @@
String[] parameterNames = getParameterNames();
for (int i = 0; i < parameterNames.length; i++) {
- if (parameterNames[i].length() > 0 && getParameterDefaults().get( parameterNames[i] ) != null) {
- Object value = getParameterDefaults().get( parameterNames[i] );
- if (value instanceof String) {
- request.setParameter( parameterNames[i], (String) value);
- } else {
- request.setParameter( parameterNames[i], (String[]) value);
- }
+ if (parameterNames[i].length() > 0 && !isFileParameter( parameterNames[i] )) {
+ request.setParameter( parameterNames[i], getParameterValues( parameterNames[i] ) );
}
}
request.setHeaderField( "Referer", getBaseURL().toExternalForm() );
@@ -294,13 +299,11 @@
/**
- * Returns the default value of the named parameter.
+ * Returns the default value of the named parameter. If the parameter does not exist returns null.
**/
public String getParameterValue( String name ) {
- Object result = getParameterDefaults().get( name );
- if (result instanceof String) return (String) result;
- if (result instanceof String[] && ((String[]) result).length > 0) return ((String[]) result)[0];
- return "";
+ String[] values = getParameterValues( name );
+ return values.length == 0 ? null : values[0];
}
@@ -308,13 +311,21 @@
* Returns the multiple default values of the named parameter.
**/
public String[] getParameterValues( String name ) {
- Object result = getParameterDefaults().get( name );
- if (result instanceof String) return new String[] { (String) result };
- if (result instanceof String[]) return (String[]) result;
- return new String[0];
+ ArrayList valueList = new ArrayList();
+ FormControl[] controls = getParameter( name ).getControls();
+ for (int i = 0; i < controls.length; i++) {
+ valueList.addAll( Arrays.asList( controls[i].getValues() ) );
+ }
+ return (String[]) valueList.toArray( new String[ valueList.size() ] );
}
+ private FormParameter getParameter( String name ) {
+ final FormParameter parameter = ((FormParameter) getFormParameters().get( name ));
+ return parameter != null ? parameter : UNKNOWN_PARAMETER;
+ }
+
+
/**
* Returns the displayed options defined for the specified parameter name.
**/
@@ -339,38 +350,38 @@
* Returns true if the named parameter accepts multiple values.
**/
public boolean isMultiValuedParameter( String name ) {
- return FormControl.TYPE_MULTI_VALUED.equals( getParameterTypes().get( name ) );
- }
-
-
- /**
- * Returns true if the named parameter accepts free-form text.
- **/
- public boolean isTextParameter( String name ) {
- return FormControl.TYPE_TEXT.equals( getParameterTypes().get( name ) );
- }
+ FormControl[] controls = getParameter( name ).getControls();
+ for (int i = 0; i < controls.length; i++) {
+ if (controls[i].isMultiValued()) return true;
+ if (!controls[i].isExclusive() && controls.length > 1) return true;
+ }
+ return false;
+ }
/**
* Returns the number of text parameters in this form with the specified name.
**/
public int getNumTextParameters( String name ) {
- Object count = getTextParameterCounts().get( name );
- if (count == null) return 0;
- return ((Number) count ).intValue();
+ int result = 0;
+ FormControl[] controls = getParameter( name ).getControls();
+ for (int i = 0; i < controls.length; i++) {
+ if (controls[i].isTextControl()) result++;
+ }
+ return result;
}
- private Hashtable getTextParameterCounts() {
- if (_textParameterCounts == null) {
- Hashtable parameterCounts = new Hashtable();
- FormControl[] parameters = getParameters();
- for (int i = 0; i < parameters.length; i++) {
- parameters[i].updateTextParameterCounts( parameterCounts );
- }
- _textParameterCounts = parameterCounts;
+
+ /**
+ * Returns true if the named parameter accepts free-form text.
+ **/
+ public boolean isTextParameter( String name ) {
+ FormControl[] controls = getParameter( name ).getControls();
+ for (int i = 0; i < controls.length; i++) {
+ if (controls[i].isTextControl()) return true;
}
- return _textParameterCounts;
+ return false;
}
@@ -378,7 +389,11 @@
* Returns true if the named parameter accepts files for upload.
**/
public boolean isFileParameter( String name ) {
- return FormControl.TYPE_FILE.equals( getParameterTypes().get( name ) );
+ FormControl[] controls = getParameter( name ).getControls();
+ for (int i = 0; i < controls.length; i++) {
+ if (controls[i].isFileParameter()) return true;
+ }
+ return false;
}
@@ -418,45 +433,27 @@
/** The attributes of the form parameters. **/
private FormControl[] _parameters;
- /** The parameters with their default values. **/
- private Hashtable _defaults;
-
/** The parameters with their displayed options. **/
private Hashtable _options;
/** The parameters with their options. **/
private Hashtable _optionValues;
- /** The parameters mapped to the type of data which they accept. **/
- private Hashtable _dataTypes;
-
- /** The parameters mapped to their number of occurrences as text parameters. **/
- private Hashtable _textParameterCounts;
-
/** The submit buttons in this form. **/
private SubmitButton[] _submitButtons;
/** The character set in which the form will be submitted. **/
private String _characterSet;
+ /** A map of parameter names to form parameter objects. **/
+ private Map _formParameters;
- private Hashtable getParameterDefaults() {
- if (_defaults == null) {
- FormControl[] parameters = getParameters();
- Hashtable defaults = new Hashtable();
- for (int i = 0; i < parameters.length; i++) {
- parameters[i].updateParameterDefaults( defaults );
- }
- _defaults = defaults;
- }
- return _defaults;
- }
private Hashtable _required;
private Hashtable getParameterRequiredValues() {
if (_required == null) {
- FormControl[] parameters = getParameters();
+ FormControl[] parameters = getFormControls();
Hashtable required = new Hashtable();
for (int i = 0; i < parameters.length; i++) {
parameters[i].updateRequiredValues( required );
@@ -470,7 +467,7 @@
private Hashtable getParameterOptions() {
if (_options == null) {
Hashtable options = new Hashtable();
- FormControl[] parameters = getParameters();
+ FormControl[] parameters = getFormControls();
for (int i = 0; i < parameters.length; i++) {
parameters[i].updateParameterOptions( options );
}
@@ -483,7 +480,7 @@
private Hashtable getParameterOptionValues() {
if (_optionValues == null) {
Hashtable options = new Hashtable();
- FormControl[] parameters = getParameters();
+ FormControl[] parameters = getFormControls();
for (int i = 0; i < parameters.length; i++) {
parameters[i].updateParameterOptionValues( options );
}
@@ -493,26 +490,13 @@
}
- private Hashtable getParameterTypes() {
- if (_dataTypes == null) {
- FormControl[] parameters = getParameters();
- Hashtable types = new Hashtable();
- for (int i = 0; i < parameters.length; i++) {
- parameters[i].updateParameterTypes( types );
- }
- _dataTypes = types;
- }
- return _dataTypes;
- }
-
-
/**
* Returns an array of select control descriptors for this form.
**/
/**
* Returns an array of form parameter attributes for this form.
**/
- private FormControl[] getParameters() {
+ private FormControl[] getFormControls() {
if (_parameters == null) {
Vector list = new Vector();
if (getNode().hasChildNodes()) addFormParametersToList( getNode().getChildNodes(), list );
@@ -546,11 +530,19 @@
class FormParameter {
void addControl( FormControl control ) {
- _controls.add( control );
+ _controlList.add( control );
+ _controls = null;
+ }
+
+
+ FormControl[] getControls() {
+ if (_controls == null) _controls = (FormControl[]) _controlList.toArray( new FormControl[ _controlList.size() ] );
+ return _controls;
}
- private ArrayList _controls = new ArrayList();
+ private FormControl[] _controls;
+ private ArrayList _controlList = new ArrayList();
}
|