Skip to content

Commit

Permalink
removed networking
Browse files Browse the repository at this point in the history
  • Loading branch information
ivaynberg committed Nov 24, 2010
1 parent c24b1e4 commit 1a39328
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 23 deletions.
22 changes: 15 additions & 7 deletions README.md
@@ -1,7 +1,15 @@
The Idea 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. This project tries to replace WicketTester with a better approach. The biggest problem
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. 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 What there is now
----------------- -----------------
Expand All @@ -10,11 +18,11 @@ HtmlUnitTest which demonstrates that ajax links work
Todo Todo
---- ----
- Test the extent of HtmlUnit's compatibility by testing more advanced Ajax usecases - 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 - 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 - 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.-
6 changes: 6 additions & 0 deletions pom.xml
Expand Up @@ -58,6 +58,12 @@
<version>${jetty.version}</version> <version>${jetty.version}</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-servlet-tester</artifactId>
<version>${jetty.version}</version>
<scope>provided</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>
<resources> <resources>
Expand Down
157 changes: 141 additions & 16 deletions src/test/java/com/vaynberg/tester/HtmlUnitTest.java
@@ -1,10 +1,19 @@
package com.vaynberg.tester; 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.ArrayList;
import java.util.Enumeration;
import java.util.List; 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.After;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.Before; import org.junit.Before;
Expand All @@ -13,16 +22,24 @@
import org.mortbay.jetty.Connector; import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Server; import org.mortbay.jetty.Server;
import org.mortbay.jetty.bio.SocketConnector; 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.mortbay.jetty.webapp.WebAppContext;
import org.mozilla.javascript.Context; import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable; import org.mozilla.javascript.Scriptable;


import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController; import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.WebClient; 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.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlPage; import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSpan; import com.gargoylesoftware.htmlunit.html.HtmlSpan;
import com.gargoylesoftware.htmlunit.util.NameValuePair;


public class HtmlUnitTest { public class HtmlUnitTest {
private static List<Timer> timers; private static List<Timer> timers;
Expand All @@ -31,6 +48,8 @@ public class HtmlUnitTest {
private Context js; private Context js;
private Scriptable scope; private Scriptable scope;


private WicketTester tester;

@BeforeClass @BeforeClass
public static void initTimers() { public static void initTimers() {
timers = new ArrayList<Timer>(); timers = new ArrayList<Timer>();
Expand All @@ -50,7 +69,6 @@ private static Timer timer(String name) {
return timer; return timer;
} }


@Before
public void startServer() throws Exception { public void startServer() throws Exception {
Timer timer = timer("server-startup"); Timer timer = timer("server-startup");
server = new Server(); server = new Server();
Expand All @@ -67,38 +85,145 @@ public void startServer() throws Exception {
bb.setContextPath("/"); bb.setContextPath("/");
bb.setWar("src/main/webapp"); bb.setWar("src/main/webapp");



server.addHandler(bb); server.addHandler(bb);


server.start(); server.start();



timer.stop(); timer.stop();
} }


@After
public void stopServer() throws Exception { public void stopServer() throws Exception {
Timer timer = timer("server-shutdown"); Timer timer = timer("server-shutdown");
server.stop(); server.stop();
server.join(); server.join();
timer.stop(); 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 @Test
public void testThis() throws Exception { public void testThis() throws Exception {
Timer timer = timer("request1"); Timer timer = timer("first-request");

WebClient client=new WebClient();
client.setAjaxController(new NicelyResynchronizingAjaxController());
HtmlPage page=client.getPage("http://localhost:8080");


HtmlAnchor anchor=(HtmlAnchor) page.getElementById("link"); Timer tmp=timer("first-page-render");
HtmlSpan span=(HtmlSpan) page.getElementById("counter"); 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()); assertEquals("0", span.getTextContent().trim());
page=anchor.click(); tmp.stop();

tmp=timer("first-click");
page = anchor.click();
client.waitForBackgroundJavaScript(10000); client.waitForBackgroundJavaScript(10000);
span=(HtmlSpan) page.getElementById("counter"); span = (HtmlSpan) page.getElementById("counter");
assertEquals("1", span.getTextContent().trim()); 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(); timer.stop();
} }
Expand Down

0 comments on commit 1a39328

Please sign in to comment.