morgand 2002/10/08 12:29:01 Modified: latka/src/java/org/apache/commons/latka/jelly JellyUtils.java RequestTag.java SuiteTag.java ValidateTag.java latka/src/java/org/apache/commons/latka/jelly/validators HttpValidatorTagSupport.java RegexpTag.java Log: worked out event model more and implemented regexp validator Revision Changes Path 1.3 +13 -3 jakarta-commons/latka/src/java/org/apache/commons/latka/jelly/JellyUtils.java Index: JellyUtils.java =================================================================== RCS file: /home/cvs/jakarta-commons/latka/src/java/org/apache/commons/latka/jelly/JellyUtils.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- JellyUtils.java 8 Oct 2002 17:28:21 -0000 1.2 +++ JellyUtils.java 8 Oct 2002 19:29:01 -0000 1.3 @@ -96,4 +96,14 @@ return (LatkaEventInfo) context.getVariable(EVENT_INFO_VAR); } + /** + * Provide a LatkaEventInfo object. + * + * @param context LatkaEventInfo object + * @param info + */ + public void setLatkaEventInfo(JellyContext context, LatkaEventInfo info) { + context.setVariable(EVENT_INFO_VAR,info); + } + } 1.4 +27 -27 jakarta-commons/latka/src/java/org/apache/commons/latka/jelly/RequestTag.java Index: RequestTag.java =================================================================== RCS file: /home/cvs/jakarta-commons/latka/src/java/org/apache/commons/latka/jelly/RequestTag.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- RequestTag.java 8 Oct 2002 17:28:21 -0000 1.3 +++ RequestTag.java 8 Oct 2002 19:29:01 -0000 1.4 @@ -121,25 +121,13 @@ // will throw an unrecoverable LatkaException if the request could not // be created, typically because of a malformed URL - if (_request == null) { - LatkaEventInfo listener = - JellyUtils.getInstance().getLatkaEventInfo(getContext()); - try { - Response response = getResponse(); - listener.requestSucceeded(new RequestSucceededEvent( - response.getRequest(), response)); - - _log.warn("Eventually this debug needs to go."); - if (_log.isDebugEnabled()) { - _log.debug(response.getResource()); - } - - } catch (IOException e) { - listener.requestError(new RequestErrorEvent(_request, null, e)); - } - } - + Response response = getResponse(); + LatkaEventInfo listener = JellyUtils.getInstance().getLatkaEventInfo(getContext()); + if (listener.didRequestSucceed(_request)) { + listener.requestSucceeded(new RequestSucceededEvent( + response.getRequest(), response)); + } } @@ -192,20 +180,32 @@ /** * The first time this method is called, a live HTTP - * call will be made to the server, throwing an IOException + * call will be made to the server, returning null * if the response cannot be obtained. Subsequent * calls return a cached response. * * @return Response for the request specified by the Latka script - * @exception IOException - * error accessing the server (recoverable) * @exception LatkaException * error creating the Request (unrecoverable) */ - public Response getResponse() throws IOException, LatkaException { - if (_response == null) { + public Response getResponse() throws LatkaException { + if (_request == null) { _request = createRequest(); - _response = _request.execute(); + + LatkaEventInfo listener = + JellyUtils.getInstance().getLatkaEventInfo(getContext()); + try { + _response = _request.execute(); + + _log.warn("Eventually this debug needs to go."); + if (_log.isDebugEnabled()) { + _log.debug(_response.getResource()); + } + } catch (IOException e) { + listener.requestError(new RequestErrorEvent(_request, null, e)); + return null; + } + // hack because sometimes we need to generate a new request after // a redirect _request = _response.getRequest(); 1.14 +10 -6 jakarta-commons/latka/src/java/org/apache/commons/latka/jelly/SuiteTag.java Index: SuiteTag.java =================================================================== RCS file: /home/cvs/jakarta-commons/latka/src/java/org/apache/commons/latka/jelly/SuiteTag.java,v retrieving revision 1.13 retrieving revision 1.14 diff -u -r1.13 -r1.14 --- SuiteTag.java 8 Oct 2002 17:28:21 -0000 1.13 +++ SuiteTag.java 8 Oct 2002 19:29:01 -0000 1.14 @@ -64,7 +64,8 @@ import org.apache.commons.jelly.TagSupport; import org.apache.commons.jelly.XMLOutput; -import org.apache.commons.latka.event.LatkaEventListener; +import org.apache.commons.latka.XMLReporter; +import org.apache.commons.latka.event.LatkaEventInfo; import org.apache.commons.latka.event.SuiteCompletedEvent; /** @@ -92,10 +93,12 @@ * @throws Exception when any error occurs */ public void doTag(XMLOutput xmlOutput) throws Exception { - LatkaEventListener reporter = + LatkaEventInfo reporter = JellyUtils.getInstance().getLatkaEventInfo(getContext()); + // if an enclosing tag does not specify a reporter, provide a default if (reporter == null) { - throw new NullPointerException("An enclosing tag must set the LatkaEventListener."); + reporter = new XMLReporter(); + JellyUtils.getInstance().setLatkaEventInfo(getContext(),reporter); } _settings = @@ -104,6 +107,7 @@ invokeBody(xmlOutput); reporter.suiteCompleted(new SuiteCompletedEvent()); + } public SuiteSettings getSuiteSettings() { 1.2 +17 -6 jakarta-commons/latka/src/java/org/apache/commons/latka/jelly/ValidateTag.java Index: ValidateTag.java =================================================================== RCS file: /home/cvs/jakarta-commons/latka/src/java/org/apache/commons/latka/jelly/ValidateTag.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- ValidateTag.java 8 Oct 2002 17:16:35 -0000 1.1 +++ ValidateTag.java 8 Oct 2002 19:29:01 -0000 1.2 @@ -64,6 +64,9 @@ import org.apache.commons.jelly.TagSupport; import org.apache.commons.jelly.XMLOutput; +import org.apache.commons.latka.LatkaException; +import org.apache.commons.latka.http.Response; + import org.apache.log4j.Category; /** @@ -81,11 +84,19 @@ public void doTag(XMLOutput xmlOutput) throws Exception { // execute the request + Response response = getResponse(); - - // perform the validations - invokeBody(xmlOutput); - + // perform the validations, skip the validations + // if a response could not be obtained + if (response != null) { + invokeBody(xmlOutput); + } + + } + + protected Response getResponse() throws LatkaException { + RequestTag tag = (RequestTag) findAncestorWithClass(RequestTag.class); + return tag.getResponse(); } } 1.4 +45 -9 jakarta-commons/latka/src/java/org/apache/commons/latka/jelly/validators/HttpValidatorTagSupport.java Index: HttpValidatorTagSupport.java =================================================================== RCS file: /home/cvs/jakarta-commons/latka/src/java/org/apache/commons/latka/jelly/validators/HttpValidatorTagSupport.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- HttpValidatorTagSupport.java 8 Oct 2002 17:16:35 -0000 1.3 +++ HttpValidatorTagSupport.java 8 Oct 2002 19:29:01 -0000 1.4 @@ -62,12 +62,16 @@ package org.apache.commons.latka.jelly.validators; import org.apache.commons.jelly.TagSupport; +import org.apache.commons.jelly.XMLOutput; +import org.apache.commons.latka.LatkaException; import org.apache.commons.latka.Validator; import org.apache.commons.latka.ValidationException; -import org.apache.commons.latka.event.LatkaEventListener; +import org.apache.commons.latka.event.LatkaEventInfo; import org.apache.commons.latka.event.RequestFailedEvent; import org.apache.commons.latka.http.Response; +import org.apache.commons.latka.jelly.JellyUtils; +import org.apache.commons.latka.jelly.RequestTag; import org.apache.log4j.Category; @@ -81,14 +85,34 @@ public abstract class HttpValidatorTagSupport extends TagSupport { /** the response being validated */ - private Response _response = null; + protected Response _response = null; /** the listener handling request success/failure etc */ - private LatkaEventListener _listener = null; + protected LatkaEventInfo _listener = null; /** label for the test */ protected String _label = null; protected static final Category _log = Category.getInstance(HttpValidatorTagSupport.class); + public HttpValidatorTagSupport() { + } + + public abstract Validator getValidator(); + + /** + * Provides a default implementation for simple validation + * tags. Will execute the validator produced by + * getValidator(). Override if necessary. + * + * @param xmlOutput a place to write output + * @exception Exception when any error occurs + */ + public void doTag(XMLOutput xmlOutput) throws Exception { + invokeBody(xmlOutput); + + Validator validator = getValidator(); + validate(validator); + } + /** * Called by the ValidationFactory. * @@ -97,10 +121,15 @@ * @param reader xml to process * @param response response to validate */ - public void init(Response response, LatkaEventListener listener, - String tagName) { - _response = response; - _listener = listener; + public void init() { + RequestTag tag = + (RequestTag) findAncestorWithClass(RequestTag.class); + try { + _response = tag.getResponse(); + } catch (LatkaException e) { + _log.error("Unexpected exception, should have been caught earlier."); + } + _listener = JellyUtils.getInstance().getLatkaEventInfo(getContext()); } public void setLabel(String label) { @@ -118,16 +147,23 @@ /** * validate the response using the validator provided. * This method will notify the listener in the event - * of a failure. + * of a failure. Will return false and not execute + * the validation if the request is already invalid. * * @param validator the object that performs validation * @return whether or not the request passed validation */ public boolean validate(Validator validator) { + init(); if (_log.isDebugEnabled()) { _log.debug("performing custom validation"); _log.debug("validator = " + validator); _log.debug("response = " + _response); + } + + if (_listener.didRequestSucceed(_response.getRequest()) == false) { + _log.debug("Validator skipped"); + return false; } boolean valid = true; 1.5 +6 -15 jakarta-commons/latka/src/java/org/apache/commons/latka/jelly/validators/RegexpTag.java Index: RegexpTag.java =================================================================== RCS file: /home/cvs/jakarta-commons/latka/src/java/org/apache/commons/latka/jelly/validators/RegexpTag.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- RegexpTag.java 8 Oct 2002 17:16:35 -0000 1.4 +++ RegexpTag.java 8 Oct 2002 19:29:01 -0000 1.5 @@ -61,9 +61,6 @@ package org.apache.commons.latka.jelly.validators; -import org.apache.commons.jelly.MissingAttributeException; -import org.apache.commons.jelly.XMLOutput; - import org.apache.commons.latka.Validator; import org.apache.commons.latka.validators.RegexpValidator; @@ -86,14 +83,8 @@ protected static final Category _log = Category.getInstance(RegexpTag.class); - /** - * - * @param xmlOutput a place to write output - * @throws Exception when any error occurs - */ - public void doTag(XMLOutput xmlOutput) throws Exception { - Validator validator = new RegexpValidator(_label,_pattern,_condition,_ignoreCase); - boolean isValid = validate(validator); + public Validator getValidator() { + return new RegexpValidator(_label,_pattern,_condition,_ignoreCase); } /** Setter for property ignoreCase.
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>