[Httpunit-commit] CVS: httpunit/src/com/meterware/httpunit HttpUnitUtils.java,1.9,1.10 UncheckedPara
Brought to you by:
russgold
From: Russell G. <rus...@us...> - 2002-08-29 18:39:14
|
Update of /cvsroot/httpunit/httpunit/src/com/meterware/httpunit In directory usw-pr-cvs1:/tmp/cvs-serv17008/src/com/meterware/httpunit Modified Files: HttpUnitUtils.java UncheckedParameterHolder.java Log Message: Only preload unvalidated requests with active parameters Index: HttpUnitUtils.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/HttpUnitUtils.java,v retrieving revision 1.9 retrieving revision 1.10 diff -u -r1.9 -r1.10 --- HttpUnitUtils.java 24 Jul 2002 17:28:56 -0000 1.9 +++ HttpUnitUtils.java 29 Aug 2002 18:39:11 -0000 1.10 @@ -117,4 +117,20 @@ } return result; } + + + /** + * Returns a string array created by appending an object to an existing array. The existing array may be null. + **/ + static Object[] withNewValue( Object[] oldValue, Object newValue ) { + Object[] result; + if (oldValue == null) { + result = new Object[] { newValue }; + } else { + result = new Object[ oldValue.length+1 ]; + System.arraycopy( oldValue, 0, result, 0, oldValue.length ); + result[ oldValue.length ] = newValue; + } + return result; + } } Index: UncheckedParameterHolder.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/UncheckedParameterHolder.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- UncheckedParameterHolder.java 4 Feb 2002 22:33:55 -0000 1.5 +++ UncheckedParameterHolder.java 29 Aug 2002 18:39:11 -0000 1.6 @@ -32,7 +32,7 @@ * * @author <a href="mailto:rus...@ac...">Russell Gold</a> **/ -class UncheckedParameterHolder extends ParameterHolder { +final class UncheckedParameterHolder extends ParameterHolder implements ParameterProcessor { private static final String[] NO_VALUES = new String[ 0 ]; private final String _characterSet; @@ -49,15 +49,34 @@ UncheckedParameterHolder( WebRequestSource source ) { _characterSet = source.getCharacterSet(); _submitAsMime = source.isSubmitAsMime(); - String[] names = source.getParameterNames(); - for (int i = 0; i < names.length; i++) { - if (!source.isFileParameter( names[i] )) { - _parameters.put( names[i], source.getParameterValues( names[i] ) ); - } + + try { + source.recordPredefinedParameters( this ); + source.recordParameters( this ); + } catch (IOException e) { + throw new RuntimeException( "This should never happen" ); } } +//----------------------------------- ParameterProcessor methods ------------------------------------------------------- + + + public void addParameter( String name, String value, String characterSet ) throws IOException { + Object[] values = (Object[]) _parameters.get( name ); + _parameters.put( name, HttpUnitUtils.withNewValue( values, value ) ); + } + + + public void addFile( String parameterName, UploadFileSpec fileSpec ) throws IOException { + Object[] values = (Object[]) _parameters.get( parameterName ); + _parameters.put( parameterName, HttpUnitUtils.withNewValue( values, fileSpec ) ); + } + + +//----------------------------------- ParameterHolder methods ---------------------------------------------------------- + + /** * Specifies the position at which an image button (if any) was clicked. **/ @@ -82,15 +101,13 @@ while (e.hasMoreElements()) { String name = (String) e.nextElement(); - Object value = _parameters.get( name ); - if (value instanceof String) { - processor.addParameter( name, (String) value, _characterSet ); - } else if (value instanceof String[]) { - String[] values = (String[]) value; - for (int i = 0; i < values.length; i++) processor.addParameter( name, values[i], _characterSet ); - } else if (value instanceof UploadFileSpec[]) { - UploadFileSpec[] files = (UploadFileSpec[]) value; - for (int i = 0; i < files.length; i++) processor.addFile( name, files[i] ); + Object[] values = (Object[]) _parameters.get( name ); + for (int i = 0; i < values.length; i++) { + if (values[i] instanceof String) { + processor.addParameter( name, (String) values[i], _characterSet ); + } else if (values[i] instanceof UploadFileSpec) { + processor.addFile( name, (UploadFileSpec) values[i] ); + } } } } @@ -108,11 +125,14 @@ 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 NO_VALUES; + Object[] values = (Object[]) _parameters.get( name ); + if (values == null) return NO_VALUES; + + String[] result = new String[ values.length ]; + for (int i = 0; i < result.length; i++) { + result[i] = values[i].toString(); + } + return result; } @@ -122,7 +142,7 @@ void setParameter( String name, String value ) { - _parameters.put( name, value ); + _parameters.put( name, new Object[] { value } ); } |