提交 a930d31f 编写于 作者: T tfennelly

HelpLinkTest fixes

上级 7ba1798a
package com.gargoylesoftware.htmlunit;
import org.junit.Assert;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
/**
* @author <a href="mailto:tom.fennelly@gmail.com">tom.fennelly@gmail.com</a>
*/
public interface WebResponseListener {
void onLoadWebResponse(WebRequest webRequest, WebResponse webResponse) throws IOException;
public final class StatusListener implements WebResponseListener {
private final int statusCode;
private final List<WebResponse> responses = new CopyOnWriteArrayList<>();
public StatusListener(final int statusCode) {
this.statusCode = statusCode;
}
@Override
public void onLoadWebResponse(WebRequest webRequest, WebResponse webResponse) throws IOException {
if (webResponse.getStatusCode() == statusCode) {
responses.add(webResponse);
}
}
public void assertHasResponses() {
Assert.assertTrue(!responses.isEmpty());
}
public List<WebResponse> getResponses() {
return responses;
}
}
}
...@@ -32,7 +32,7 @@ import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException; ...@@ -32,7 +32,7 @@ import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.Page; import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.WebRequest; import com.gargoylesoftware.htmlunit.WebRequest;
import com.gargoylesoftware.htmlunit.WebResponse; import com.gargoylesoftware.htmlunit.WebResponse;
import com.gargoylesoftware.htmlunit.WebWindow; import com.gargoylesoftware.htmlunit.WebResponseListener;
import com.gargoylesoftware.htmlunit.html.DomNode; import com.gargoylesoftware.htmlunit.html.DomNode;
import com.gargoylesoftware.htmlunit.html.HtmlButton; import com.gargoylesoftware.htmlunit.html.HtmlButton;
import com.gargoylesoftware.htmlunit.html.HtmlElement; import com.gargoylesoftware.htmlunit.html.HtmlElement;
...@@ -148,7 +148,6 @@ import java.util.TimerTask; ...@@ -148,7 +148,6 @@ import java.util.TimerTask;
import java.util.TreeSet; import java.util.TreeSet;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
...@@ -161,7 +160,6 @@ import java.util.logging.Level; ...@@ -161,7 +160,6 @@ import java.util.logging.Level;
import java.util.logging.LogRecord; import java.util.logging.LogRecord;
import java.util.logging.Logger; import java.util.logging.Logger;
import javax.annotation.CheckForNull; import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextEvent;
...@@ -1859,6 +1857,8 @@ public class JenkinsRule implements TestRule, MethodRule, RootAction { ...@@ -1859,6 +1857,8 @@ public class JenkinsRule implements TestRule, MethodRule, RootAction {
public class WebClient extends com.gargoylesoftware.htmlunit.WebClient { public class WebClient extends com.gargoylesoftware.htmlunit.WebClient {
private static final long serialVersionUID = 5808915989048338267L; private static final long serialVersionUID = 5808915989048338267L;
private List<WebResponseListener> webResponseListeners = new ArrayList<>();
public WebClient() { public WebClient() {
// default is IE6, but this causes 'n.doScroll('left')' to fail in event-debug.js:1907 as HtmlUnit doesn't implement such a method, // default is IE6, but this causes 'n.doScroll('left')' to fail in event-debug.js:1907 as HtmlUnit doesn't implement such a method,
// so trying something else, until we discover another problem. // so trying something else, until we discover another problem.
...@@ -1918,6 +1918,22 @@ public class JenkinsRule implements TestRule, MethodRule, RootAction { ...@@ -1918,6 +1918,22 @@ public class JenkinsRule implements TestRule, MethodRule, RootAction {
//setTimeout(60*1000); //setTimeout(60*1000);
} }
public void addWebResponseListener(WebResponseListener listener) {
webResponseListeners.add(listener);
}
@Override
public WebResponse loadWebResponse(final WebRequest webRequest) throws IOException {
WebResponse webResponse = super.loadWebResponse(webRequest);
if (!webResponseListeners.isEmpty()) {
for (WebResponseListener listener : webResponseListeners) {
listener.onLoadWebResponse(webRequest, webResponse);
}
}
return webResponse;
}
/** /**
* Logs in to Jenkins. * Logs in to Jenkins.
*/ */
......
package hudson.model; package hudson.model;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import com.gargoylesoftware.htmlunit.WebResponseListener;
import com.gargoylesoftware.htmlunit.html.DomNodeUtil; import com.gargoylesoftware.htmlunit.html.DomNodeUtil;
import com.gargoylesoftware.htmlunit.html.HtmlElementUtil;
import hudson.tasks.BuildStepMonitor; import hudson.tasks.BuildStepMonitor;
import org.junit.Assert;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import com.gargoylesoftware.htmlunit.html.HtmlPage; import com.gargoylesoftware.htmlunit.html.HtmlPage;
...@@ -49,7 +51,12 @@ public class HelpLinkTest { ...@@ -49,7 +51,12 @@ public class HelpLinkTest {
private void clickAllHelpLinks(AbstractProject p) throws Exception { private void clickAllHelpLinks(AbstractProject p) throws Exception {
// TODO: how do we add all the builders and publishers so that we can test this meaningfully? // TODO: how do we add all the builders and publishers so that we can test this meaningfully?
clickAllHelpLinks(j.createWebClient().getPage(p, "configure")); clickAllHelpLinks(j.createWebClient(), p);
}
private void clickAllHelpLinks(JenkinsRule.WebClient webClient, AbstractProject p) throws Exception {
// TODO: how do we add all the builders and publishers so that we can test this meaningfully?
clickAllHelpLinks(webClient.getPage(p, "configure"));
} }
private void clickAllHelpLinks(HtmlPage p) throws Exception { private void clickAllHelpLinks(HtmlPage p) throws Exception {
...@@ -57,8 +64,9 @@ public class HelpLinkTest { ...@@ -57,8 +64,9 @@ public class HelpLinkTest {
assertTrue(helpLinks.size()>0); assertTrue(helpLinks.size()>0);
System.out.println("Clicking "+helpLinks.size()+" help links"); System.out.println("Clicking "+helpLinks.size()+" help links");
for (HtmlAnchor helpLink : (List<HtmlAnchor>)helpLinks) for (HtmlAnchor helpLink : (List<HtmlAnchor>)helpLinks) {
helpLink.click(); HtmlElementUtil.click(helpLink);
}
} }
public static class HelpNotFoundBuilder extends Publisher { public static class HelpNotFoundBuilder extends Publisher {
...@@ -93,13 +101,15 @@ public class HelpLinkTest { ...@@ -93,13 +101,15 @@ public class HelpLinkTest {
try { try {
FreeStyleProject p = j.createFreeStyleProject(); FreeStyleProject p = j.createFreeStyleProject();
p.getPublishersList().add(new HelpNotFoundBuilder()); p.getPublishersList().add(new HelpNotFoundBuilder());
clickAllHelpLinks(p); JenkinsRule.WebClient webclient = j.createWebClient();
fail("should detect a failure"); WebResponseListener.StatusListener statusListener = new WebResponseListener.StatusListener(404);
} catch(AssertionError e) { webclient.addWebResponseListener(statusListener);
if(e.getMessage().contains(d.getHelpFile()))
; // expected clickAllHelpLinks(webclient, p);
else
throw e; statusListener.assertHasResponses();
String contentAsString = statusListener.getResponses().get(0).getContentAsString();
Assert.assertTrue(contentAsString.contains(d.getHelpFile()));
} finally { } finally {
Publisher.all().remove(d); Publisher.all().remove(d);
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册