Skip to content
Browse files

removed networking

  • Loading branch information...
1 parent c24b1e4 commit 1a39328aceb3c4af805f05c172c6ca2a6c9b8e8a @ivaynberg committed Nov 23, 2010
Showing with 162 additions and 23 deletions.
  1. +15 −7 README.md
  2. +6 −0 pom.xml
  3. +141 −16 src/test/java/com/vaynberg/tester/HtmlUnitTest.java
View
22 README.md
@@ -1,7 +1,15 @@
The Idea
---------
-This project tries to replace WicketTester with a better approach. The biggest problem with WicketTester is that most of the code tries to literally hack the request to emulate browser functions such as clicking a link or submitting a form. Even worse, there are a lot of hacks to emulate the same functionality when executed via AJAX.
-By using htmlunit we can completely elimiate all these hacks and simplify WicketTester by delegating all the client-side emulation hacks to hmltunit where the actual javascript code would be executed. WicketTester would then become a simple bridge between the user's code and the htmlunit runtime.
+This project tries to replace WicketTester with a better approach. The biggest problem
+with WicketTester is that most of the code tries to literally hack the request processing
+to emulate browser functions such as clicking a link or submitting a form.
+
+By using htmlunit we can completely elimiate all these hacks and simplify WicketTester
+by delegating all the client-side emulation hacks to hmltunit where the actual javascript
+code would be executed. WicketTester would then become a simple bridge between the user's code
+and the htmlunit runtime.
+
+As a bonus, we can also test Wicket's javascript, something that was difficult in the past.
What there is now
-----------------
@@ -10,11 +18,11 @@ HtmlUnitTest which demonstrates that ajax links work
Todo
----
- Test the extent of HtmlUnit's compatibility by testing more advanced Ajax usecases
+- See if its possible to improve performance
+ - first test takes 1.7 seconds
+ - com.gargoylesoftware.htmlunit.html.HTMLParser.parse takes 1.3 of those seconds
+Done
+----
- Remove the network - have a basic WicketTester like thing that can respond to URLs so all transfers are done in-memory to speed things up
- Need to see how pluggable that side of html unit is, replacing their WebClient with our DirectLinkClient or something like that
-Old
----
--By using env-js we can completely elimiate all these hacks and simplify WicketTester by delegating all the client-side emulation hacks to env-js where the actual code would be executed. WicketTester would then become a simple bridge between the user's code and the env-js runtime.-
--The current problem is that env-js is incomplete in some places and buggy in others. It does not yet properly support XHR which is a blocker for this project.-
--If you wish to play with this you have to use my env-js clone and run RhinoTest unit test.-
View
6 pom.xml
@@ -58,6 +58,12 @@
<version>${jetty.version}</version>
<scope>provided</scope>
</dependency>
+ <dependency>
+ <groupId>org.mortbay.jetty</groupId>
+ <artifactId>jetty-servlet-tester</artifactId>
+ <version>${jetty.version}</version>
+ <scope>provided</scope>
+ </dependency>
</dependencies>
<build>
<resources>
View
157 src/test/java/com/vaynberg/tester/HtmlUnitTest.java
@@ -1,10 +1,19 @@
package com.vaynberg.tester;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import java.io.IOException;
import java.util.ArrayList;
+import java.util.Enumeration;
import java.util.List;
+import javax.servlet.FilterConfig;
+
+import org.apache.wicket.protocol.http.IWebApplicationFactory;
+import org.apache.wicket.protocol.http.WebApplication;
+import org.apache.wicket.protocol.http.WicketFilter;
+import org.apache.wicket.protocol.http.WicketServlet;
+import org.apache.wicket.util.tester.WicketTester;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
@@ -13,16 +22,24 @@
import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.bio.SocketConnector;
-import org.mortbay.jetty.handler.HandlerCollection;
+import org.mortbay.jetty.servlet.DefaultServlet;
+import org.mortbay.jetty.testing.HttpTester;
+import org.mortbay.jetty.testing.ServletTester;
import org.mortbay.jetty.webapp.WebAppContext;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
+import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.WebClient;
+import com.gargoylesoftware.htmlunit.WebConnection;
+import com.gargoylesoftware.htmlunit.WebRequest;
+import com.gargoylesoftware.htmlunit.WebResponse;
+import com.gargoylesoftware.htmlunit.WebResponseData;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSpan;
+import com.gargoylesoftware.htmlunit.util.NameValuePair;
public class HtmlUnitTest {
private static List<Timer> timers;
@@ -31,6 +48,8 @@
private Context js;
private Scriptable scope;
+ private WicketTester tester;
+
@BeforeClass
public static void initTimers() {
timers = new ArrayList<Timer>();
@@ -50,7 +69,6 @@ private static Timer timer(String name) {
return timer;
}
- @Before
public void startServer() throws Exception {
Timer timer = timer("server-startup");
server = new Server();
@@ -67,38 +85,145 @@ public void startServer() throws Exception {
bb.setContextPath("/");
bb.setWar("src/main/webapp");
-
server.addHandler(bb);
server.start();
-
-
+
timer.stop();
}
- @After
public void stopServer() throws Exception {
Timer timer = timer("server-shutdown");
server.stop();
server.join();
timer.stop();
}
+ public void createTester() {
+ tester = new WicketTester(new WicketApplication());
+ }
+
+ public void destroyTester() {
+ tester.destroy();
+ }
+
+ ServletTester st;
+
+ @Before
+ public void createServletTester() throws Exception {
+ Timer timer=timer("servlet-tester-startup");
+ st = new ServletTester();
+ st.setContextPath("/");
+ st.addServlet(ConfiguredServlet.class, "/*");
+ st.addServlet(DefaultServlet.class, "/");
+ st.start();
+ timer.stop();
+ }
+
+ @After
+ public void destroyServletTester() throws Exception {
+ Timer timer=timer("servlet-tester-shutdown");
+ st.stop();
+ timer.stop();
+ }
+
+ public static class ConfiguredServlet extends WicketServlet {
+
+ @Override
+ protected WicketFilter newWicketFilter() {
+ return new WicketFilter() {
+
+ @Override
+ protected String getFilterPathFromConfig(
+ FilterConfig filterConfig) {
+ return "";
+ }
+
+ @Override
+ protected IWebApplicationFactory getApplicationFactory() {
+ return new IWebApplicationFactory() {
+
+ public WebApplication createApplication(
+ WicketFilter filter) {
+ return new WicketApplication();
+ }
+
+ public void destroy() {
+
+ }
+
+ };
+ }
+ };
+ }
+ }
+
+ private class LocalConnection implements WebConnection {
+
+ public WebResponse getResponse(WebRequest wrequest) throws IOException {
+ HttpTester request = new HttpTester();
+ request.setURI(wrequest.getUrl().toString()
+ .substring("http://localhost".length()));
+ request.setMethod(wrequest.getHttpMethod().name());
+ request.setVersion("HTTP/1.1");
+ request.setHeader("Host", "localhost");
+
+ try {
+ String buffer = request.generate();
+ buffer = st.getResponses(buffer);
+ HttpTester response = new HttpTester();
+ response.parse(buffer);
+
+ List<NameValuePair> headers = new ArrayList<NameValuePair>();
+ Enumeration names=response.getHeaderNames();
+ while (names.hasMoreElements()) {
+ String name=(String) names.nextElement();
+ headers.add(new NameValuePair(name, response.getHeader(name)));
+ }
+ String content=response.getContent();
+
+ WebResponseData data = new WebResponseData(
+ (content!=null)?content.getBytes():null, response.getStatus(),
+ response.getReason(), headers);
+ WebResponse wr = new WebResponse(data, wrequest, 100);
+ return wr;
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+ }
+
@Test
public void testThis() throws Exception {
- Timer timer = timer("request1");
-
- WebClient client=new WebClient();
- client.setAjaxController(new NicelyResynchronizingAjaxController());
- HtmlPage page=client.getPage("http://localhost:8080");
+ Timer timer = timer("first-request");
- HtmlAnchor anchor=(HtmlAnchor) page.getElementById("link");
- HtmlSpan span=(HtmlSpan) page.getElementById("counter");
+ Timer tmp=timer("first-page-render");
+ WebClient client = new WebClient();
+ client.setRedirectEnabled(true);
+ client.setWebConnection(new LocalConnection());
+
+ client.setAjaxController(new NicelyResynchronizingAjaxController());
+ Page rawpage = client
+ .getPage("http://localhost/wicket/bookmarkable/com.vaynberg.tester.HomePage");
+ HtmlPage page = (HtmlPage) rawpage;
+ HtmlAnchor anchor = (HtmlAnchor) page.getElementById("link");
+ HtmlSpan span = (HtmlSpan) page.getElementById("counter");
assertEquals("0", span.getTextContent().trim());
- page=anchor.click();
+ tmp.stop();
+
+ tmp=timer("first-click");
+ page = anchor.click();
client.waitForBackgroundJavaScript(10000);
- span=(HtmlSpan) page.getElementById("counter");
+ span = (HtmlSpan) page.getElementById("counter");
assertEquals("1", span.getTextContent().trim());
+ tmp.stop();
+
+ tmp=timer("second-click");
+ page = anchor.click();
+ client.waitForBackgroundJavaScript(10000);
+ span = (HtmlSpan) page.getElementById("counter");
+ assertEquals("2", span.getTextContent().trim());
+ tmp.stop();
timer.stop();
}

0 comments on commit 1a39328

Please sign in to comment.
Something went wrong with that request. Please try again.