[Httpunit-commit] CVS: httpunit/src/com/meterware/httpunit FormParameter.java,NONE,1.1 WebWindow.jav
Brought to you by:
russgold
From: Russell G. <rus...@us...> - 2002-10-03 12:35:21
|
Update of /cvsroot/httpunit/httpunit/src/com/meterware/httpunit In directory usw-pr-cvs1:/tmp/cvs-serv18765/src/com/meterware/httpunit Modified Files: WebClient.java WebForm.java Added Files: FormParameter.java WebWindow.java Log Message: Refactored to facilitate multiple window feature ***** Error reading new file[Errno 2] No such file or directory: 'FormParameter.java' ***** Error reading new file[Errno 2] No such file or directory: 'WebWindow.java' Index: WebClient.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/WebClient.java,v retrieving revision 1.32 retrieving revision 1.33 diff -u -r1.32 -r1.33 --- WebClient.java 1 Oct 2002 16:11:47 -0000 1.32 +++ WebClient.java 3 Oct 2002 12:35:18 -0000 1.33 @@ -21,13 +21,8 @@ *******************************************************************************************************************/ import java.io.IOException; import java.io.OutputStream; - import java.net.HttpURLConnection; import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLStreamHandler; -import java.net.URLConnection; - import java.util.*; import org.xml.sax.SAXException; @@ -46,12 +41,34 @@ abstract public class WebClient { + + /** The current main window. **/ + private WebWindow _mainWindow = new WebWindow( this ); + private ArrayList _openWindows = new ArrayList(); + + + public WebWindow getMainWindow() { + return _mainWindow; + } + + + public void setMainWindow( WebWindow mainWindow ) { + if (!_openWindows.contains( mainWindow )) throw new IllegalArgumentException( "May only select an open window owned by this client" ); + _mainWindow = mainWindow; + } + + + public WebWindow[] getOpenWindows() { + return (WebWindow[]) _openWindows.toArray( new WebWindow[ _openWindows.size() ] ); + } + + /** * Submits a GET method request and returns a response. * @exception SAXException thrown if there is an error parsing the retrieved page **/ public WebResponse getResponse( String urlString ) throws MalformedURLException, IOException, SAXException { - return getResponse( new GetMethodWebRequest( urlString ) ); + return _mainWindow.getResponse( urlString ); } @@ -59,15 +76,15 @@ * Submits a web request and returns a response. This is an alternate name for the getResponse method. */ public WebResponse sendRequest( WebRequest request ) throws MalformedURLException, IOException, SAXException { - return getResponse( request ); + return _mainWindow.sendRequest( request ); } /** - * Returns the response representing the current main page. + * Returns the response representing the current top page in the main window. */ public WebResponse getCurrentPage() { - return getFrameContents( WebRequest.TOP_FRAME ); + return _mainWindow.getCurrentPage(); } @@ -77,15 +94,24 @@ * @exception SAXException thrown if there is an error parsing the retrieved page **/ public WebResponse getResponse( WebRequest request ) throws MalformedURLException, IOException, SAXException { - tellListeners( request ); + return _mainWindow.getResponse( request ); + } - WebResponse response = getResource( request ); - if (response != null) { - tellListeners( response ); - updateClient( response ); - } - return getFrameContents( request.getTarget() ); + /** + * Returns the name of the currently active frames in the main window. + **/ + public String[] getFrameNames() { + return _mainWindow.getFrameNames(); + } + + + /** + * Returns the response associated with the specified frame name in the main window. + * Throws a runtime exception if no matching frame is defined. + **/ + public WebResponse getFrameContents( String frameName ) { + return _mainWindow.getFrameContents( frameName ); } @@ -94,6 +120,19 @@ * May return null if the resource is a JavaScript URL which would normally leave the client unchanged. */ public WebResponse getResource( WebRequest request ) throws IOException { + return getResourceForWindow( request, _mainWindow ); + } + + + WebResponse getResourceForWindow( WebRequest request, WebWindow window ) throws IOException { + tellListeners( request ); + WebResponse response = getResource( request, window ); + if (response != null) tellListeners( response ); + return response; + } + + + private WebResponse getResource( WebRequest request, final WebWindow window ) throws IOException { String urlString = request.getURLString().trim(); if (urlString.startsWith( "about:" )) { return WebResponse.BLANK_RESPONSE; @@ -101,7 +140,7 @@ return newResponse( request ); } else { WebRequestSource wrs = request.getWebRequestSource(); - String result = (wrs == null) ? getCurrentPage().getScriptableObject().evaluateURL( urlString ) + String result = (wrs == null) ? window.getCurrentPage().getScriptableObject().evaluateURL( urlString ) : wrs.getScriptableDelegate().evaluateURL( urlString ); if (result == null) return null; @@ -141,33 +180,13 @@ * any listeners or preferences which may have been set. **/ public void clearContents() { - _frameContents = new FrameHolder( this, WebRequest.TOP_FRAME ); + _mainWindow = new WebWindow( this ); _cookies = new Hashtable(); _headers = new HeaderDictionary(); } /** - * Returns the name of the currently active frames. - **/ - public String[] getFrameNames() { - final List names = _frameContents.getActiveFrameNames(); - return (String[]) names.toArray( new String[ names.size() ] ); - } - - - /** - * Returns the response associated with the specified frame name. - * Throws a runtime exception if no matching frame is defined. - **/ - public WebResponse getFrameContents( String frameName ) { - WebResponse response = (WebResponse) _frameContents.get( frameName ); - if (response == null) throw new NoSuchFrameException( frameName ); - return response; - } - - - /** * Defines a cookie to be sent to the server on every request. **/ public void addCookie( String name, String value ) { @@ -316,12 +335,7 @@ if (HttpUnitOptions.isAcceptGzip()) { setHeaderField( "Accept-Encoding", "gzip" ); } - - try { - _frameContents.updateFrames( new DefaultWebResponse( this, null, WebResponse.BLANK_HTML ) ); - } catch (IOException e) { - } catch (SAXException e) { - } + _openWindows.add( _mainWindow ); } @@ -373,25 +387,14 @@ * cookies and frames. **/ final - protected void updateClient( WebResponse response ) throws MalformedURLException, IOException, SAXException { - updateCookies( response ); - validateHeaders( response ); - if (HttpUnitOptions.getAutoRefresh() && response.getRefreshRequest() != null) { - getResponse( response.getRefreshRequest() ); - } else if (shouldFollowRedirect( response )) { - delay( HttpUnitOptions.getRedirectDelay() ); - getResponse( new RedirectWebRequest( response ) ); - } else { - _frameContents.updateFrames( response ); - } + protected void updateMainWindow( WebResponse response ) throws MalformedURLException, IOException, SAXException { + _mainWindow.updateWindow( response ); } - private boolean shouldFollowRedirect( WebResponse response ) { - return HttpUnitOptions.getAutoRedirect() - && response.getResponseCode() >= HttpURLConnection.HTTP_MOVED_PERM - && response.getResponseCode() <= HttpURLConnection.HTTP_MOVED_TEMP - && response.getHeaderField( "Location" ) != null; + void updateClient( WebResponse response ) throws IOException { + updateCookies( response ); + validateHeaders( response ); } @@ -421,10 +424,6 @@ private Hashtable _cookies = new Hashtable(); - /** A map of frame names to current contents. **/ - private FrameHolder _frameContents = new FrameHolder( this, WebRequest.TOP_FRAME ); - - /** A map of header names to values. **/ private HeaderDictionary _headers = new HeaderDictionary(); @@ -464,19 +463,6 @@ String[] names = response.getNewCookieNames(); for (int i = 0; i < names.length; i++) { addCookie( names[i], response.getNewCookieValue( names[i] ) ); - } - } - - - /** - * Delays the specified amount of time. - **/ - private void delay( int numMilliseconds ) { - if (numMilliseconds == 0) return; - try { - Thread.sleep( numMilliseconds ); - } catch (InterruptedException e) { - // ignore the exception } } Index: WebForm.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/WebForm.java,v retrieving revision 1.65 retrieving revision 1.66 diff -u -r1.65 -r1.66 --- WebForm.java 2 Oct 2002 13:57:55 -0000 1.65 +++ WebForm.java 3 Oct 2002 12:35:18 -0000 1.66 @@ -686,236 +686,175 @@ } } -} +//===========================---===== exception class NoSuchParameterException ========================================= + /** + * This exception is thrown on an attempt to set a parameter to a value not permitted to it by the form. + **/ + class NoSuchParameterException extends IllegalRequestParameterException { -//========================================== class PresetFormParameter ================================================= - - class PresetFormParameter extends FormControl { - - PresetFormParameter( String name, String value ) { - _name = name; - _value = value; + NoSuchParameterException( String parameterName ) { + _parameterName = parameterName; } - /** - * Returns the name of this control.. - **/ - public String getName() { - return _name; + public String getMessage() { + return "No parameter named '" + _parameterName + "' is defined in the form"; } - /** - * Returns true if this control is read-only. - **/ - public boolean isReadOnly() { - return true; - } - + private String _parameterName; - /** - * Returns true if this control accepts free-form text. - **/ - public boolean isTextControl() { - return true; - } + } - /** - * Remove any required values for this control from the list, throwing an exception if they are missing. - **/ - void claimRequiredValues( List values ) { - if (_value != null) claimValueIsRequired( values, _value ); - } +//============================= exception class IllegalUnnamedSubmitButtonException ====================================== - /** - * 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() { - if (_values == null) _values = new String[] { _value }; - return _values; - } + /** + * This exception is thrown on an attempt to define a form request with a button not defined on that form. + **/ + class IllegalUnnamedSubmitButtonException extends IllegalRequestParameterException { - void addValues( ParameterProcessor processor, String characterSet ) throws IOException { - processor.addParameter( _name, _value, characterSet ); + IllegalUnnamedSubmitButtonException() { } - private String _name; - private String _value; - private String[] _values; - } - - -//===========================---===== exception class NoSuchParameterException ========================================= - - -/** - * This exception is thrown on an attempt to set a parameter to a value not permitted to it by the form. - **/ -class NoSuchParameterException extends IllegalRequestParameterException { - + public String getMessage() { + return "This form has more than one submit button, none unnamed. You must specify the button to be used."; + } - NoSuchParameterException( String parameterName ) { - _parameterName = parameterName; } - public String getMessage() { - return "No parameter named '" + _parameterName + "' is defined in the form"; - } +//============================= exception class IllegalSubmitButtonException ====================================== - private String _parameterName; + /** + * This exception is thrown on an attempt to define a form request with a button not defined on that form. + **/ + class IllegalSubmitButtonException extends IllegalRequestParameterException { -} + IllegalSubmitButtonException( SubmitButton button ) { + _name = button.getName(); + _value = button.getValue(); + } -//============================= exception class UnusedParameterValueException ====================================== + IllegalSubmitButtonException( String name, String value ) { + _name = name; + _value = value; + } -/** - * This exception is thrown on an attempt to set a parameter to a value not permitted to it by the form. - **/ -class UnusedParameterValueException extends IllegalRequestParameterException { + public String getMessage() { + return "Specified submit button (name=\"" + _name + "\" value=\"" + _value + "\") not part of this form."; + } - UnusedParameterValueException( String parameterName, String badValue ) { - _parameterName = parameterName; - _badValue = badValue; - } + private String _name; + private String _value; - public String getMessage() { - StringBuffer sb = new StringBuffer(HttpUnitUtils.DEFAULT_TEXT_BUFFER_SIZE); - sb.append( "Attempted to assign to parameter '" ).append( _parameterName ); - sb.append( "' the extraneous value '" ).append( _badValue ).append( "'." ); - return sb.toString(); } +//============================= exception class IllegalUnnamedSubmitButtonException ====================================== - private String _parameterName; - private String _badValue; -} - - -//============================= exception class UnusedUploadFileException ====================================== - - -/** - * This exception is thrown on an attempt to upload more files than permitted by the form. - **/ -class UnusedUploadFileException extends IllegalRequestParameterException { + /** + * This exception is thrown on an attempt to define a form request with a button not defined on that form. + **/ + class DisabledSubmitButtonException extends IllegalRequestParameterException { - UnusedUploadFileException( String parameterName, int numFilesExpected, int numFilesSupplied ) { - _parameterName = parameterName; - _numExpected = numFilesExpected; - _numSupplied = numFilesSupplied; - } - - public String getMessage() { - StringBuffer sb = new StringBuffer( HttpUnitUtils.DEFAULT_TEXT_BUFFER_SIZE ); - sb.append( "Attempted to upload " ).append( _numSupplied ).append( " files using parameter '" ).append( _parameterName ); - if (_numExpected == 0) { - sb.append( "' which is not a file parameter." ); - } else { - sb.append( "' which only has room for " ).append( _numExpected ).append( '.' ); + DisabledSubmitButtonException( SubmitButton button ) { + _name = button.getName(); + _value = button.getValue(); } - return sb.toString(); - } - private String _parameterName; - private int _numExpected; - private int _numSupplied; -} - - -//============================= exception class IllegalUnnamedSubmitButtonException ====================================== - - -/** - * This exception is thrown on an attempt to define a form request with a button not defined on that form. - **/ -class IllegalUnnamedSubmitButtonException extends IllegalRequestParameterException { - + public String getMessage() { + return "The specified button (name='" + _name + "' value='" + _value + + "' is disabled and may not be used to submit this form."; + } - IllegalUnnamedSubmitButtonException() { - } + private String _name; + private String _value; - public String getMessage() { - return "This form has more than one submit button, none unnamed. You must specify the button to be used."; } } -//============================= exception class IllegalSubmitButtonException ====================================== +//========================================== class PresetFormParameter ================================================= -/** - * This exception is thrown on an attempt to define a form request with a button not defined on that form. - **/ -class IllegalSubmitButtonException extends IllegalRequestParameterException { + class PresetFormParameter extends FormControl { - IllegalSubmitButtonException( SubmitButton button ) { - _name = button.getName(); - _value = button.getValue(); - } + PresetFormParameter( String name, String value ) { + _name = name; + _value = value; + } - IllegalSubmitButtonException( String name, String value ) { - _name = name; - _value = value; - } + /** + * Returns the name of this control.. + **/ + public String getName() { + return _name; + } - public String getMessage() { - return "Specified submit button (name=\"" + _name + "\" value=\"" + _value + "\") not part of this form."; - } + /** + * Returns true if this control is read-only. + **/ + public boolean isReadOnly() { + return true; + } - private String _name; - private String _value; + /** + * Returns true if this control accepts free-form text. + **/ + public boolean isTextControl() { + return true; + } -} -//============================= exception class IllegalUnnamedSubmitButtonException ====================================== + /** + * Remove any required values for this control from the list, throwing an exception if they are missing. + **/ + void claimRequiredValues( List values ) { + if (_value != null) claimValueIsRequired( values, _value ); + } -/** - * This exception is thrown on an attempt to define a form request with a button not defined on that form. - **/ -class DisabledSubmitButtonException extends IllegalRequestParameterException { + /** + * 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() { + if (_values == null) _values = new String[] { _value }; + return _values; + } - DisabledSubmitButtonException( SubmitButton button ) { - _name = button.getName(); - _value = button.getValue(); - } + void addValues( ParameterProcessor processor, String characterSet ) throws IOException { + processor.addParameter( _name, _value, characterSet ); + } - public String getMessage() { - return "The specified button (name='" + _name + "' value='" + _value - + "' is disabled and may not be used to submit this form."; + private String _name; + private String _value; + private String[] _values; } - private String _name; - private String _value; - -} |