morgand 2002/10/08 10:16:35
Modified: latka/src/java/org/apache/commons/latka/jelly
LatkaTagLibrary.java RequestTag.java SuiteTag.java
latka/src/java/org/apache/commons/latka/jelly/validators
HttpValidatorTagSupport.java RegexpTag.java
Added: latka/src/java/org/apache/commons/latka/jelly
JellyUtils.java ValidateTag.java
Removed: latka/src/java/org/apache/commons/latka/jelly/validators
ByteLengthTag.java CookieTag.java
HttpValidatorTagLibrary.java
MaxResponseTimeTag.java ResponseHeaderTag.java
StatusCodeTag.java StatusTextTag.java
Log:
interim check-in
Revision Changes Path
1.22 +9 -7
jakarta-commons/latka/src/java/org/apache/commons/latka/jelly/LatkaTagLibrary.java
Index: LatkaTagLibrary.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/latka/src/java/org/apache/commons/latka/jelly/LatkaTagLibrary.java,v
retrieving revision 1.21
retrieving revision 1.22
diff -u -r1.21 -r1.22
--- LatkaTagLibrary.java 3 Oct 2002 18:10:18 -0000 1.21
+++ LatkaTagLibrary.java 8 Oct 2002 17:16:35 -0000 1.22
@@ -74,9 +74,11 @@
/** Creates a new instance of LatkaTagLibrary */
public LatkaTagLibrary() {
- registerTag("suite", SuiteTag.class);
- registerTag("session", SessionTag.class);
- registerTag("request", RequestTag.class);
+ registerTag("suite", SuiteTag.class);
+ registerTag("session", SessionTag.class);
+ registerTag("request", RequestTag.class);
+ registerTag("validate", ValidateTag.class);
+ registerTag("regexp",
org.apache.commons.latka.jelly.validators.RegexpTag.class);
}
}
1.2 +94 -21
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.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- RequestTag.java 3 Oct 2002 18:10:19 -0000 1.1
+++ RequestTag.java 8 Oct 2002 17:16:35 -0000 1.2
@@ -61,11 +61,16 @@
package org.apache.commons.latka.jelly;
+import java.io.IOException;
import java.net.URL;
import org.apache.commons.jelly.TagSupport;
import org.apache.commons.jelly.XMLOutput;
+import org.apache.commons.latka.LatkaException;
+import org.apache.commons.latka.event.LatkaEventListener;
+import org.apache.commons.latka.event.RequestErrorEvent;
+import org.apache.commons.latka.event.RequestSucceededEvent;
import org.apache.commons.latka.http.Proxy;
import org.apache.commons.latka.http.Request;
import org.apache.commons.latka.http.Response;
@@ -79,7 +84,7 @@
* @author Morgan Delagrange
*/
public class RequestTag extends TagSupport {
-
+
protected String _host = null;
protected int _port = -1;
protected String _proxyHost = null;
@@ -90,13 +95,17 @@
protected boolean _secure = false;
protected boolean _followRedirects = true;
protected String _httpVersion = "1.1";
+
+ protected Request _request = null;
+ protected Response _response = null;
+ protected LatkaEventListener _listener = null;
protected static final Category _log = Category.getInstance(RequestTag.class);
/** Creates a new instance of SuiteTag */
public RequestTag() {
}
-
+
/**
* Wraps Latka tests, provides some defaults for host, port etc.
*
@@ -104,14 +113,48 @@
* @throws Exception when any error occurs
*/
public void doTag(XMLOutput xmlOutput) throws Exception {
+
+ // may set headers and such
+ invokeBody(xmlOutput);
+
+ // even when there are no validations, we execute the request
+ // to make sure the URL is accessible
+
+ // will throw an unrecoverable LatkaException if the request could not
+ // be created, typically because of a malformed URL
+ Response response = null;
+ LatkaEventListener listener =
+ JellyUtils.getInstance().getLatkaEventListener(getContext());
+ try {
+ response = getResponse();
+ listener.requestSucceeded(new RequestSucceededEvent(
+ response.getRequest(), response));
+ } catch (IOException e) {
+ listener.requestError(new RequestErrorEvent(_request, null, e));
+ }
+
+ _log.warn("Eventually this debug needs to go.");
+ if (_log.isDebugEnabled()) {
+ _log.debug(response.getResource());
+ }
+
+ }
+
+ /**
+ *
+ * @return Request
+ * @exception IOException
+ * Error creating the request (unrecoverable, the script
+ * must fail)
+ */
+ protected Request createRequest() throws LatkaException {
String host = _host;
int port = _port;
String proxyHost = _proxyHost;
int proxyPort = _proxyPort;
-
+
if (host == null || port == -1 || proxyHost == null || proxyPort == -1) {
- SuiteSettings settings =
- (SuiteSettings)
getContext().getVariable(SuiteTag.SUITE_SETTINGS_VAR);
+ SuiteSettings settings = getSuiteSettings();
if (host == null) {
host = settings.getDefaultHost();
}
@@ -126,9 +169,7 @@
}
}
- invokeBody(xmlOutput);
-
- // for now, create an
+ // for now, create a new Session for each request
_log.warn("broken, needs session handling");
Session session = new SessionImpl();
@@ -136,17 +177,49 @@
if (proxyHost != null) {
proxy = new Proxy(proxyHost,proxyPort);
}
+
+ URL url = null;
+ try {
+ url = new URL(_secure ? "https" : "http", host, port, _path);
+ } catch (IOException e) {
+ throw new LatkaException(e);
+ }
+ return
session.createRequest(_label,url,_method,_httpVersion,_followRedirects,proxy);
+ }
- URL url = new URL(_secure ? "https" : "http", host, port, _path);
-
- Request request =
session.createRequest(_label,url,_method,_httpVersion,_followRedirects,proxy);
- Response response = request.execute();
- // hack for HttpClient behaviour sometimes generates a new request
- request = response.getRequest();
- _log.warn("Eventually this debug needs to go.");
- if (_log.isDebugEnabled()) {
- _log.debug(response.getResource());
+ /**
+ * The first time this method is called, a live HTTP
+ * call will be made to the server, throwing an IOException
+ * 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) {
+ _request = createRequest();
+ _response = _request.execute();
+ // hack because sometimes we need to generate a new request after
+ // a redirect
+ _request = _response.getRequest();
}
+
+ return _response;
+ }
+
+ /**
+ * get the suite settings from SuiteTag
+ *
+ * @return SuiteSettings object
+ */
+ protected SuiteSettings getSuiteSettings() {
+ SuiteTag tag =
+ (SuiteTag)
findAncestorWithClass(org.apache.commons.latka.jelly.SuiteTag.class);
+ return tag.getSuiteSettings();
}
/**
@@ -158,7 +231,7 @@
public void setHost(String host) {
_host = host;
}
-
+
/**
* Setter for port
*
1.12 +20 -8
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.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- SuiteTag.java 3 Oct 2002 18:10:19 -0000 1.11
+++ SuiteTag.java 8 Oct 2002 17:16:35 -0000 1.12
@@ -64,6 +64,9 @@
import org.apache.commons.jelly.TagSupport;
import org.apache.commons.jelly.XMLOutput;
+import org.apache.commons.latka.event.LatkaEventListener;
+import org.apache.commons.latka.event.SuiteCompletedEvent;
+
/**
*
* @author Morgan Delagrange
@@ -76,8 +79,7 @@
protected int _defaultProxyPort = -1;
protected String _label = null;
- protected static final String SUITE_SETTINGS_VAR =
- "org.apache.commons.latka.jelly.SuiteSettings";
+ protected SuiteSettings _settings = null;
/** Creates a new instance of SuiteTag */
public SuiteTag() {
@@ -90,12 +92,22 @@
* @throws Exception when any error occurs
*/
public void doTag(XMLOutput xmlOutput) throws Exception {
- SuiteSettings settings =
+ LatkaEventListener reporter =
+ JellyUtils.getInstance().getLatkaEventListener(getContext());
+ if (reporter == null) {
+ throw new NullPointerException("An enclosing tag must set the
LatkaEventListener.");
+ }
+
+ _settings =
new SuiteSettings(_defaultHost, _defaultPort, _defaultProxyHost,
_defaultProxyPort);
- getContext().setVariable(SUITE_SETTINGS_VAR,settings);
invokeBody(xmlOutput);
- getContext().removeVariable(SUITE_SETTINGS_VAR);
+
+ reporter.suiteCompleted(new SuiteCompletedEvent());
+ }
+
+ public SuiteSettings getSuiteSettings() {
+ return _settings;
}
/**
1.1
jakarta-commons/latka/src/java/org/apache/commons/latka/jelly/JellyUtils.java
Index: JellyUtils.java
===================================================================
/*
* $Header:
/home/cvs/jakarta-commons/latka/src/java/org/apache/commons/latka/jelly/JellyUtils.java,v
1.1 2002/10/08 17:16:35 morgand Exp $
* $Revision: 1.1 $
* $Date: 2002/10/08 17:16:35 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.latka.jelly;
import org.apache.commons.jelly.JellyContext;
import org.apache.commons.latka.event.LatkaEventListener;
/**
*
* @author Morgan Delagrange
*/
public class JellyUtils {
protected static final String EVENT_LISTENER_VAR =
"latkaEventListener";
protected static JellyUtils _utils = new JellyUtils();
/** Creates a new instance of SuiteTag */
public JellyUtils() {
}
public static JellyUtils getInstance() {
return _utils;
}
/**
* Return the LatkaEventListener for the context,
* or null if none has been set
*
* @param context Context for the current Jelly script
* @return LatkaEventListener for the context,
* or null if none has been set
*/
public LatkaEventListener getLatkaEventListener(JellyContext context) {
return (LatkaEventListener) context.getVariable(EVENT_LISTENER_VAR);
}
}
1.1
jakarta-commons/latka/src/java/org/apache/commons/latka/jelly/ValidateTag.java
Index: ValidateTag.java
===================================================================
/*
* $Header:
/home/cvs/jakarta-commons/latka/src/java/org/apache/commons/latka/jelly/ValidateTag.java,v
1.1 2002/10/08 17:16:35 morgand Exp $
* $Revision: 1.1 $
* $Date: 2002/10/08 17:16:35 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.latka.jelly;
import org.apache.commons.jelly.TagSupport;
import org.apache.commons.jelly.XMLOutput;
import org.apache.log4j.Category;
/**
*
* @author Morgan Delagrange
*/
public class ValidateTag extends TagSupport {
/**
* make sure the request executes, then call validators.
*
* @param xmlOutput a place to write output
* @throws Exception when any error occurs
*/
public void doTag(XMLOutput xmlOutput) throws Exception {
// execute the request
// perform the validations
invokeBody(xmlOutput);
}
}
1.3 +72 -52
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.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- HttpValidatorTagSupport.java 14 Jul 2002 16:51:33 -0000 1.2
+++ HttpValidatorTagSupport.java 8 Oct 2002 17:16:35 -0000 1.3
@@ -1,13 +1,13 @@
/*
- * $Header$
- * $Revision$
- * $Date$
+ *
+ *
+ *
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
- * Copyright (c) 1999-2001 The Apache Software Foundation. All rights
+ * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -61,68 +61,88 @@
package org.apache.commons.latka.jelly.validators;
-import org.apache.commons.httpclient.HttpUrlMethod;
-import org.apache.commons.jelly.MissingAttributeException;
import org.apache.commons.jelly.TagSupport;
-import org.apache.commons.jelly.XMLOutput;
+
+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.RequestFailedEvent;
+import org.apache.commons.latka.http.Response;
+
+import org.apache.log4j.Category;
/**
- * A base class for all Latka validation tags
+ * A base class for validation tags
*
- * @author dion
- * @version
- * $Id$
+ * @author Morgan Delagrange
+ * @author dIon Gillard
+ * @version $Id$
*/
-public class HttpValidatorTagSupport extends TagSupport {
-
- /** the name of the http method to validate */
- private String _var;
-
- /** Creates a new instance of ValidatorTag */
- public HttpValidatorTagSupport() {
- }
-
+public abstract class HttpValidatorTagSupport extends TagSupport {
+
+ /** the response being validated */
+ private Response _response = null;
+ /** the listener handling request success/failure etc */
+ private LatkaEventListener _listener = null;
+ /** label for the test */
+ protected String _label = null;
+
+ protected static final Category _log =
Category.getInstance(HttpValidatorTagSupport.class);
+
/**
- * By default process the tag body
+ * Called by the ValidationFactory.
*
- * @param xmlOutput a place to write output
- * @throws Exception when any error occurs
+ * @param listener supplier of information about the suite/progress
+ * @param tagName name of the validating tag
+ * @param reader xml to process
+ * @param response response to validate
*/
- public void doTag(XMLOutput xmlOutput) throws Exception {
- if (getVar() == null) {
- throw new MissingAttributeException("var not provided");
- }
-
- if (getRequest() == null) {
- throw new IllegalStateException("the http request: "
- + getVar() + " was not found");
- }
+ public void init(Response response, LatkaEventListener listener,
+ String tagName) {
+ _response = response;
+ _listener = listener;
}
- /**
- * @return the method used to execute the request with id given by
- * {@link getVar()}
- */
- protected HttpUrlMethod getRequest() {
- return (HttpUrlMethod) getContext().getVariable(getVar());
+ public void setLabel(String label) {
+ _label = label;
}
-
+
/**
- * Getter for property var.
- *
- * @return Value of property var.
+ * the response being validated
+ * @return the response being validated
*/
- public String getVar() {
- return _var;
+ public Response getResponse() {
+ return _response;
}
-
+
/**
- * Setter for property var.
- *
- * @param var New value of property var.
+ * validate the response using the validator provided.
+ * This method will notify the listener in the event
+ * of a failure.
+ *
+ * @param validator the object that performs validation
+ * @return whether or not the request passed validation
*/
- public void setVar(String var) {
- _var = var;
+ public boolean validate(Validator validator) {
+ if (_log.isDebugEnabled()) {
+ _log.debug("performing custom validation");
+ _log.debug("validator = " + validator);
+ _log.debug("response = " + _response);
+ }
+
+ boolean valid = true;
+
+ try {
+ validator.validate(_response);
+ } catch (ValidationException e) {
+ _listener.requestFailed(
+ new RequestFailedEvent(_response.getRequest(), _response, e));
+ valid = false;
+ }
+
+ _log.debug("custom validation complete");
+
+ return valid;
}
-
-}
+
+}
\ No newline at end of file
1.4 +22 -43
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.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- RegexpTag.java 14 Jul 2002 16:51:33 -0000 1.3
+++ RegexpTag.java 8 Oct 2002 17:16:35 -0000 1.4
@@ -63,7 +63,11 @@
import org.apache.commons.jelly.MissingAttributeException;
import org.apache.commons.jelly.XMLOutput;
-import org.apache.regexp.RE;
+
+import org.apache.commons.latka.Validator;
+import org.apache.commons.latka.validators.RegexpValidator;
+
+import org.apache.log4j.Category;
/**
* A class to match some part of the response body to a regular expression
@@ -74,46 +78,24 @@
public class RegexpTag extends HttpValidatorTagSupport {
/** the regular expression pattern to match with */
- private String _pattern;
+ private String _pattern = null;
/** whether the match is to be case sensitive */
- private boolean _ignoreCase;
-
- /** Creates a new instance of RegexpTag */
- public RegexpTag() {
- }
+ private boolean _ignoreCase = false;
+ /** whether to expect success or failure */
+ private boolean _condition = true;
+ protected static final Category _log = Category.getInstance(RegexpTag.class);
+
/**
- * Check if the responseBody matches the pattern. If it does, invoke
- * the body of the tag
*
* @param xmlOutput a place to write output
* @throws Exception when any error occurs
*/
public void doTag(XMLOutput xmlOutput) throws Exception {
- super.doTag(xmlOutput);
- if (getPattern() == null) {
- throw new MissingAttributeException("pattern attribute not set");
- }
-
- String body = getRequest().getResponseBodyAsString();
- RE r = new RE(getPattern()); // Compile expression
-
- if (isIgnoreCase()) {
- r.setMatchFlags(RE.MATCH_CASEINDEPENDENT);
- }
-
- if (!r.match(body)) {
- invokeBody(xmlOutput);
- }
- }
-
- /** Getter for property ignoreCase.
- * @return Value of property ignoreCase.
- */
- public boolean isIgnoreCase() {
- return _ignoreCase;
+ Validator validator = new
RegexpValidator(_label,_pattern,_condition,_ignoreCase);
+ boolean isValid = validate(validator);
}
-
+
/** Setter for property ignoreCase.
* @param ignoreCase New value of property ignoreCase.
*/
@@ -121,18 +103,15 @@
_ignoreCase = ignoreCase;
}
- /** Getter for property pattern.
- * @return Value of property pattern.
- */
- public String getPattern() {
- return _pattern;
- }
-
/** Setter for property pattern.
* @param pattern New value of property pattern.
*/
public void setPattern(String pattern) {
_pattern = pattern;
+ }
+
+ public void setCond(boolean condition) {
+ _condition = condition;
}
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>