作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个用于运行测试的 Ant 目标,我需要在测试开始之前启动并运行一个应用服务器。我正在使用 waitFor
任务来 ping 一个 URL,直到它得到响应。这是有效的,只是我不仅需要确保 URL 易于访问,而且还要检查是否存在特定的状态字符串。第二个要求本身很简单,使用 get
或 telnet
任务,然后是子字符串搜索。但是 'waitfor' 任务不接受除标准 Ant 条件之外的任何嵌套元素,或者它们可以包装的东西(返回 bool 值的东西)。我曾尝试在使用 resourceconatins
条件运行“waitfor”任务之前将 URL 作为资源加载,但这会缓存响应,并且不会从服务器刷新。
有没有办法强制在每个循环中加载 URL?还是通过其他方式来实现?
这是我想用伪代码实现的:
<waitfor>
<and>
<!-- make sure the URL is accessible at all -->
<http url="theurl.jsp"/>
<!-- load the URL as a resource and make sure it contains a substring -->
<!-- moving the "url" task before the waitfor works if all conditions are met the first time through, but does not refresh the URL if it has to loop over again. having it inside like this, fails because waitfor/and don't support nexted "url" tasks -->
<url id="url.resource" url="url.jsp"/>
<resourcecontains refid="url.resource" substring="status"/>
</and>
<!-- else, keep on trying until timeout -->
</waitfor>
<!-- continue on to the tests to be run -->
最佳答案
用法:
<typedef
name="httpcontains"
classname="HttpContains"
/>
<waitfor maxwait="10" maxwaitunit="minute" checkevery="500">
<httpcontains contains="Running" url="http://myurl"/>
</waitfor>
下面的代码允许这样做:
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.ProjectComponent;
import org.apache.tools.ant.taskdefs.condition.Condition;
/**
* Condition to wait for a HTTP request to succeed. Its attribute(s) are:
* url - the URL of the request.
* errorsBeginAt - number at which errors begin at; default=400.
* @since Ant 1.5
*/
public class HttpContains extends ProjectComponent implements Condition {
private String spec = null;
private String contains = null;
/**
* Set the url attribute
*
* @param url the url of the request
*/
public void setUrl(String url) {
spec = url;
}
public void setContains(String s){
contains = s;
}
private int errorsBeginAt = 400;
/**
* Set the errorsBeginAt attribute
*
* @param errorsBeginAt number at which errors begin at, default is
* 400
*/
public void setErrorsBeginAt(int errorsBeginAt) {
this.errorsBeginAt = errorsBeginAt;
}
/**
* @return true if the HTTP request succeeds
* @exception BuildException if an error occurs
*/
public boolean eval() throws BuildException {
if (spec == null) {
throw new BuildException("No url specified in http condition");
}
log("Checking for " + spec, Project.MSG_VERBOSE);
try {
URL url = new URL(spec);
try {
URLConnection conn = url.openConnection();
if (conn instanceof HttpURLConnection) {
HttpURLConnection http = (HttpURLConnection) conn;
int code = http.getResponseCode();
log("Result code for " + spec + " was " + code,
Project.MSG_VERBOSE);
if (code > 0 && code < errorsBeginAt)
{
if ( evalContents(url) )
return true;
else
return false;
} else {
return false;
}
}
} catch (java.io.IOException e) {
return false;
}
} catch (MalformedURLException e) {
throw new BuildException("Badly formed URL: " + spec, e);
}
return true;
}
public boolean evalContents(URL mUrl )
{
try
{
String contents;
StringBuffer buffer = new StringBuffer();
byte[] buf = new byte[1024];
int amount = 0;
InputStream input = mUrl.openStream();
while ((amount = input.read(buf)) > 0)
{
buffer.append(new String(buf, 0, amount));
}
input.close();
contents = buffer.toString();
log("Result code for " + contents,
Project.MSG_VERBOSE);
if ( contents.indexOf(contains) > -1 )
{
log("Result code for " + contents.indexOf(contains),
Project.MSG_VERBOSE);
return true;
}
return false;
}
catch ( Exception e )
{
e.printStackTrace();
log("Result code for " + e.toString(),
Project.MSG_VERBOSE);
return false;
}
}
}
关于web-applications - 如何让 Ant 在 'waitfor' 任务中加载和检查 URL 资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2160514/
我是一名优秀的程序员,十分优秀!