[Httpunit-commit] CVS: httpunit/src/com/meterware/httpunit ParameterHolder.java,NONE,1.1 UncheckedPa
Brought to you by:
russgold
Update of /cvsroot/httpunit/httpunit/src/com/meterware/httpunit
In directory usw-pr-cvs1:/tmp/cvs-serv13413/src/com/meterware/httpunit
Modified Files:
FormControl.java NodeUtils.java WebForm.java WebRequest.java
Added Files:
ParameterHolder.java UncheckedParameterCollection.java
Log Message:
Started moving the manipulation of parameters to a separate class to allow alternative strategies
***** Error reading new file[Errno 2] No such file or directory: 'ParameterHolder.java'
***** Error reading new file[Errno 2] No such file or directory: 'UncheckedParameterCollection.java'
Index: FormControl.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/FormControl.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- FormControl.java 2002/01/08 14:29:27 1.2
+++ FormControl.java 2002/01/08 23:21:53 1.3
@@ -2,7 +2,7 @@
/********************************************************************************************************************
* $Id$
*
-* Copyright (c) 2000-2001, Russell Gold
+* Copyright (c) 2001-2002, Russell Gold
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
@@ -26,6 +26,7 @@
import java.util.Hashtable;
import java.util.Vector;
+import java.util.ArrayList;
/**
@@ -33,6 +34,8 @@
**/
abstract class FormControl {
+ final static String[] NO_VALUE = new String[0];
+
private String _name;
private boolean _readOnly;
private boolean _disabled;
@@ -48,7 +51,7 @@
/**
* Returns the name of this control. If no name is specified, defaults to the empty string.
**/
- String getName() {
+ public String getName() {
return _name;
}
@@ -56,14 +59,30 @@
/**
* 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();
+
+
+ /**
+ * Returns the values permitted in this control. Does not apply to text or file controls.
+ **/
+ public String[] getOptionValues() {
+ return NO_VALUE;
+ }
+
+
+ /**
+ * Returns the list of values displayed by this control, if any.
**/
- abstract String[] getValues();
+ public String[] getDisplayedOptions() {
+ return NO_VALUE;
+ }
/**
* Returns true if this control is read-only.
**/
- boolean isReadOnly() {
+ public boolean isReadOnly() {
return _readOnly;
}
@@ -71,7 +90,7 @@
/**
* 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() {
+ public boolean isDisabled() {
return _disabled;
}
@@ -79,7 +98,7 @@
/**
* Returns true if this control accepts free-form text.
**/
- boolean isTextControl() {
+ public boolean isTextControl() {
return false;
}
@@ -87,7 +106,7 @@
/**
* Returns true if only one control of this kind can have a value. This is true for radio buttons.
**/
- boolean isExclusive() {
+ public boolean isExclusive() {
return false;
}
@@ -95,7 +114,7 @@
/**
* Returns true if a single control can have multiple values.
**/
- boolean isMultiValued() {
+ public boolean isMultiValued() {
return false;
}
@@ -103,7 +122,7 @@
/**
* Returns true if this control accepts a file for upload.
**/
- boolean isFileParameter() {
+ public boolean isFileParameter() {
return false;
}
@@ -112,14 +131,6 @@
}
- void updateParameterOptions( Hashtable options ) {
- }
-
-
- void updateParameterOptionValues( Hashtable options ) {
- }
-
-
protected void addValue( Hashtable valueMap, String name, String value ) {
String[] currentValues = (String[]) valueMap.get( name );
if (currentValues == null) {
@@ -172,8 +183,6 @@
abstract
class BooleanFormControl extends FormControl {
- final static String[] NO_VALUE = new String[0];
-
private boolean _isChecked;
private String[] _value = new String[1];
@@ -195,31 +204,33 @@
/**
- * Returns the default value of this control in the form. 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 getValueAttribute() {
- return _valueAttribute;
+ public String[] getValues() {
+ return isChecked() ? toArray( getQueryValue() ) : NO_VALUE;
}
/**
- * Returns the current value(s) associated with this control. These values will be transmitted to the server
- * if the control is 'successful'.
+ * Returns the values permitted in this control.
**/
- String[] getValues() {
- return isChecked() ? toArray( getQueryValue() ) : NO_VALUE;
+ public String[] getOptionValues() {
+ return (isReadOnly() && !isChecked()) ? NO_VALUE : toArray( getQueryValue() );
}
- abstract String getQueryValue();
+ /**
+ * Returns the default value of this control in the form. If no value is specified, defaults to the empty string.
+ **/
+ String getValueAttribute() {
+ return _valueAttribute;
+ }
- void updateParameterOptionValues( Hashtable options ) {
- if (isChecked() || !isReadOnly()) {
- addValue( options, getName(), getQueryValue() );
- }
- }
+ abstract String getQueryValue();
+
private String[] toArray( String value ) {
_value[0] = value;
return _value;
@@ -238,7 +249,7 @@
/**
* Returns true if only one control of this kind can have a value.
**/
- boolean isExclusive() {
+ public boolean isExclusive() {
return true;
}
@@ -292,7 +303,7 @@
* Returns the current value(s) associated with this control. These values will be transmitted to the server
* if the control is 'successful'.
**/
- String[] getValues() {
+ public String[] getValues() {
return (_value[0] != null) ? _value : _defaultValue;
}
@@ -300,7 +311,7 @@
/**
* Returns true to indicate that this control accepts free-form text.
**/
- boolean isTextControl() {
+ public boolean isTextControl() {
return true;
}
@@ -347,81 +358,84 @@
/**
* Returns true if this control accepts a file for upload.
**/
- boolean isFileParameter() {
+ public boolean isFileParameter() {
return true;
}
- String[] getValues() {
+ public String[] getValues() {
return null; // XXX what should this really do?
}
}
class SelectionFormControl extends FormControl {
- private boolean _multiSelect;
- private Node _node;
+ private final boolean _multiSelect;
+ private final String[] _optionValues;
+ private final String[] _displayedOptions;
+
+ private String[] _values;
SelectionFormControl( Node node ) {
super( node );
if (!node.getNodeName().equalsIgnoreCase( "select" )) throw new RuntimeException( "Not a select element" );
- _node = node;
- _multiSelect = NodeUtils.isNodeAttributePresent( node, "multiple" );
+ _multiSelect = NodeUtils.isNodeAttributePresent( node, "multiple" );
+ _optionValues = getInitialOptionValues( node );
+ _displayedOptions = getInitialDisplayedOptions( node );
+ _values = getInitialValues( node, _optionValues );
}
- String[] getValues() {
- return getSelected();
+ public String[] getValues() {
+ return _values;
}
- /**
- * Returns true if a single control can have multiple values.
- **/
- boolean isMultiValued() {
- return _multiSelect;
+ public String[] getOptionValues() {
+ return _optionValues;
}
- void updateRequiredParameters( Hashtable required ) {
- if (isReadOnly()) required.put( getName(), getSelected() );
+ public String[] getDisplayedOptions() {
+ return _displayedOptions;
}
- void updateParameterOptions( Hashtable options ) {
- options.put( getName(), getOptions() );
+ /**
+ * Returns true if a single control can have multiple values.
+ **/
+ public boolean isMultiValued() {
+ return _multiSelect;
}
- void updateParameterOptionValues( Hashtable options ) {
- options.put( getName(), getOptionValues() );
+ void updateRequiredParameters( Hashtable required ) {
+ if (isReadOnly()) required.put( getName(), getValues() );
}
- String[] getSelected() {
- Vector selected = new Vector();
- NodeList nl = ((Element) _node).getElementsByTagName( "option" );
+ private String[] getInitialValues( Node selectionNode, String[] optionValues ) {
+ ArrayList selected = new ArrayList();
+ NodeList nl = ((Element) selectionNode).getElementsByTagName( "option" );
for (int i = 0; i < nl.getLength(); i++) {
if (nl.item(i).getAttributes().getNamedItem( "selected" ) != null) {
- selected.addElement( getOptionValue( nl.item(i) ) );
+ selected.add( optionValues[i] );
}
}
if (!isMultiValued() && selected.size() == 0 && nl.getLength() > 0) {
- selected.addElement( getOptionValue( nl.item(0) ) );
+ selected.add( optionValues[0] );
}
- String[] result = new String[ selected.size() ];
- selected.copyInto( result );
- return result;
+ return (String[]) selected.toArray( new String[ selected.size() ] );
}
- String[] getOptions() {
+ private String[] getInitialDisplayedOptions( Node node ) {
Vector options = new Vector();
- NodeList nl = ((Element) _node).getElementsByTagName( "option" );
+ NodeList nl = ((Element) node).getElementsByTagName( "option" );
for (int i = 0; i < nl.getLength(); i++) {
options.addElement( getValue( nl.item(i).getFirstChild() ) );
}
@@ -431,15 +445,13 @@
}
- String[] getOptionValues() {
- Vector options = new Vector();
- NodeList nl = ((Element) _node).getElementsByTagName( "option" );
+ private String[] getInitialOptionValues( Node selectNode ) {
+ NodeList nl = ((Element) selectNode).getElementsByTagName( "option" );
+ ArrayList options = new ArrayList( nl.getLength() );
for (int i = 0; i < nl.getLength(); i++) {
- options.addElement( getOptionValue( nl.item(i) ) );
+ options.add( getOptionValue( nl.item(i) ) );
}
- String[] result = new String[ options.size() ];
- options.copyInto( result );
- return result;
+ return (String[]) options.toArray( new String[ options.size() ] );
}
Index: NodeUtils.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/NodeUtils.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- NodeUtils.java 2002/01/08 14:29:27 1.9
+++ NodeUtils.java 2002/01/08 23:21:53 1.10
@@ -2,7 +2,7 @@
/********************************************************************************************************************
* $Id$
*
-* Copyright (c) 2000-2001, Russell Gold
+* Copyright (c) 2000-2002, Russell Gold
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
Index: WebForm.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/WebForm.java,v
retrieving revision 1.37
retrieving revision 1.38
diff -u -r1.37 -r1.38
--- WebForm.java 2002/01/08 14:29:27 1.37
+++ WebForm.java 2002/01/08 23:21:53 1.38
@@ -2,14 +2,14 @@
/********************************************************************************************************************
* $Id$
*
-* Copyright (c) 2000-2001, Russell Gold
+* Copyright (c) 2000-2002, Russell Gold
*
-* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
-* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
+* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
+* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
* to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
-* The above copyright notice and this permission notice shall be included in all copies or substantial portions
+* The above copyright notice and this permission notice shall be included in all copies or substantial portions
* of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
@@ -68,6 +68,13 @@
/**
+ * Sets the value of a parameter in this form.
+ **/
+ public void setParameter( String name, String value ) {
+ }
+
+
+ /**
* Returns true if a parameter with given name exists in this form.
**/
public boolean hasParameterNamed( String soughtName ) {
@@ -270,7 +277,7 @@
if (HttpUnitOptions.getParameterValuesValidated()) {
if (button == null) {
throw new IllegalUnnamedSubmitButtonException();
- } else if (!getSubmitButtonVector().contains( button )) {
+ } else if (!getSubmitButtonVector().contains( button )) {
throw new IllegalSubmitButtonException( button );
} else if (button.isDisabled()) {
throw new DisabledSubmitButtonException( button );
@@ -330,9 +337,12 @@
* Returns the displayed options defined for the specified parameter name.
**/
public String[] getOptions( String name ) {
- Object result = getParameterOptions().get( name );
- if (result instanceof String[]) return (String[]) result;
- return new String[0];
+ ArrayList optionList = new ArrayList();
+ FormControl[] controls = getParameter( name ).getControls();
+ for (int i = 0; i < controls.length; i++) {
+ optionList.addAll( Arrays.asList( controls[i].getDisplayedOptions() ) );
+ }
+ return (String[]) optionList.toArray( new String[ optionList.size() ] );
}
@@ -340,9 +350,12 @@
* Returns the option values defined for the specified parameter name.
**/
public String[] getOptionValues( String name ) {
- Object result = getParameterOptionValues().get( name );
- 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].getOptionValues() ) );
+ }
+ return (String[]) valueList.toArray( new String[ valueList.size() ] );
}
@@ -433,12 +446,6 @@
/** The attributes of the form parameters. **/
private FormControl[] _parameters;
- /** The parameters with their displayed options. **/
- private Hashtable _options;
-
- /** The parameters with their options. **/
- private Hashtable _optionValues;
-
/** The submit buttons in this form. **/
private SubmitButton[] _submitButtons;
@@ -447,10 +454,11 @@
/** A map of parameter names to form parameter objects. **/
private Map _formParameters;
-
+ /** A map of parameter names to their required values. **/
private Hashtable _required;
+
private Hashtable getParameterRequiredValues() {
if (_required == null) {
FormControl[] parameters = getFormControls();
@@ -464,35 +472,7 @@
}
- private Hashtable getParameterOptions() {
- if (_options == null) {
- Hashtable options = new Hashtable();
- FormControl[] parameters = getFormControls();
- for (int i = 0; i < parameters.length; i++) {
- parameters[i].updateParameterOptions( options );
- }
- _options = options;
- }
- return _options;
- }
-
-
- private Hashtable getParameterOptionValues() {
- if (_optionValues == null) {
- Hashtable options = new Hashtable();
- FormControl[] parameters = getFormControls();
- for (int i = 0; i < parameters.length; i++) {
- parameters[i].updateParameterOptionValues( options );
- }
- _optionValues = options;
- }
- return _optionValues;
- }
-
- /**
- * Returns an array of select control descriptors for this form.
- **/
/**
* Returns an array of form parameter attributes for this form.
**/
Index: WebRequest.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/WebRequest.java,v
retrieving revision 1.31
retrieving revision 1.32
diff -u -r1.31 -r1.32
--- WebRequest.java 2001/11/27 19:51:27 1.31
+++ WebRequest.java 2002/01/08 23:21:53 1.32
@@ -2,7 +2,7 @@
/********************************************************************************************************************
* $Id$
*
-* Copyright (c) 2000-2001, Russell Gold
+* Copyright (c) 2000-2002, Russell Gold
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
@@ -43,7 +43,7 @@
* A request sent to a web server.
**/
abstract
-public class WebRequest {
+public class WebRequest implements ParameterHolder {
/**
@@ -63,11 +63,46 @@
/**
+ * Returns the final URL associated with this web request.
+ **/
+ public URL getURL() throws MalformedURLException {
+ if (getURLBase() == null || getURLString().indexOf( ':' ) > 0) validateProtocol( getURLString() );
+ return new URL( getURLBase(), getURLString() );
+ }
+
+
+ /**
+ * Returns the target for this web request.
+ **/
+ public String getTarget() {
+ return _target;
+ }
+
+
+ /**
+ * Returns the query string defined for this request.
+ **/
+ public String getQueryString() {
+ return getParameterString();
+ }
+
+
+ /**
+ * Returns the HTTP method defined for this request.
+ **/
+ abstract
+ public String getMethod();
+
+
+//------------------------------------- ParameterCollection methods ------------------------------------
+
+
+ /**
* Sets the value of a parameter in a web request.
**/
public void setParameter( String name, String value ) {
if (HttpUnitOptions.getParameterValuesValidated()) validateParameterValue( name, value );
- _parameters.put( name, value );
+ _parameterCollection.setParameter( name, value );
}
@@ -76,7 +111,7 @@
**/
public void setParameter( String name, String[] values ) {
if (HttpUnitOptions.getParameterValuesValidated()) validateParameterValues( name, values );
- _parameters.put( name, values );
+ _parameterCollection.setParameter( name, values );
}
@@ -128,7 +163,7 @@
* Returns an enumeration of all parameters in this web request.
**/
public Enumeration getParameterNames() {
- return _parameters.keys();
+ return _parameterCollection.getParameterNames();
}
@@ -138,14 +173,8 @@
* if it is not set.
**/
public String getParameter( String name ) {
- Object value = _parameters.get( name );
- if (value instanceof String[]) {
- return ((String[]) value)[0];
- } else if (value == null) {
- return "";
- } else {
- return value.toString();
- }
+ String[] values = getParameterValues( name );
+ return values.length == 0 ? "" : values[0];
}
@@ -153,11 +182,7 @@
* Returns the multiple default values of the named parameter.
**/
public String[] getParameterValues( String name ) {
- Object result = _parameters.get( name );
- if (result instanceof String) return new String[] { (String) result };
- if (result instanceof String[]) return (String[]) result;
- if (result instanceof UploadFileSpec) return new String[] { result.toString() };
- return new String[0];
+ return _parameterCollection.getParameterValues( name );
}
@@ -165,41 +190,11 @@
* Removes a parameter from this web request.
**/
public void removeParameter( String name ) {
- _parameters.remove( name );
+ _parameterCollection.removeParameter( name );
}
-
- /**
- * Returns the final URL associated with this web request.
- **/
- public URL getURL() throws MalformedURLException {
- if (getURLBase() == null || getURLString().indexOf( ':' ) > 0) validateProtocol( getURLString() );
- return new URL( getURLBase(), getURLString() );
- }
-
-
- /**
- * Returns the target for this web request.
- **/
- public String getTarget() {
- return _target;
- }
-
-
- /**
- * Returns the query string defined for this request.
- **/
- public String getQueryString() {
- return getParameterString();
- }
-
-
- /**
- * Returns the HTTP method defined for this request.
- **/
- abstract
- public String getMethod();
+//------------------------------------- Object methods ------------------------------------
public String toString() {
@@ -245,7 +240,7 @@
_sourceForm = sourceForm;
if (button != null && button.getName().length() > 0) {
- _parameters.put( button.getName(), button.getValue() );
+ _parameterCollection._parameters.put( button.getName(), button.getValue() );
if (button.isImageButton()) {
_imageButtonName = button.getName();
setSubmitPosition( 0, 0 );
@@ -330,17 +325,17 @@
final
protected boolean hasNoParameters() {
- return _parameters.size() == 0;
+ return _parameterCollection._parameters.size() == 0;
}
final
protected String getParameterString() {
StringBuffer sb = new StringBuffer(HttpUnitUtils.DEFAULT_BUFFER_SIZE);
- Enumeration e = _parameters.keys();
+ Enumeration e = _parameterCollection._parameters.keys();
while (e.hasMoreElements()) {
String name = (String) e.nextElement();
- Object value = _parameters.get( name );
+ Object value = _parameterCollection._parameters.get( name );
if (value instanceof String) {
appendParameter( sb, name, (String) value, e.hasMoreElements() );
} else {
@@ -359,8 +354,8 @@
void setSubmitPosition( int x, int y ) {
if (_imageButtonName == null) return;
- _parameters.put( _imageButtonName + ".x", Integer.toString( x ) );
- _parameters.put( _imageButtonName + ".y", Integer.toString( y ) );
+ _parameterCollection._parameters.put( _imageButtonName + ".x", Integer.toString( x ) );
+ _parameterCollection._parameters.put( _imageButtonName + ".y", Integer.toString( y ) );
}
@@ -460,10 +455,10 @@
/** The name of the JSSE class which supports the https protocol. **/
private final static String SSL_PROTOCOL_HANDLER = "com.sun.net.ssl.internal.www.protocol";
+ private UncheckedParameterCollection _parameterCollection = new UncheckedParameterCollection();
private URL _urlBase;
private String _urlString;
- private Hashtable _parameters = new Hashtable();
private WebForm _sourceForm;
private String _imageButtonName;
private String _target = TOP_FRAME;
|