1 package org.melati;
2
3 import org.mortbay.jetty.Server;
4 import org.mortbay.jetty.webapp.WebAppContext;
5
6 import net.sourceforge.jwebunit.junit.WebTestCase;
7
8 /**
9 *
10 * Much thanks to
11 * http://today.java.net/pub/a/today/2007/04/12/embedded-integration-testing-of-web-applications.html
12 *
13 * @author timp
14 * @since 2008/01/01
15 *
16 */
17 public abstract class JettyWebTestCase extends WebTestCase {
18
19 private static Server server;
20 private static String contextName = "melatitest";
21 private static boolean started = false;
22
23 /**
24 * Constructor.
25 */
26 public JettyWebTestCase() {
27 super();
28 }
29
30 /**
31 * Constructor, with name.
32 * @param name
33 */
34 public JettyWebTestCase(String name) {
35 super(name);
36 }
37
38 protected void setUp() throws Exception {
39
40 server = new Server(0);
41 startServer();
42
43
44 int actualPort = server.getConnectors()[0].getLocalPort();
45 getTestContext().setBaseUrl(
46 "http://localhost:" + actualPort + "/" );
47 }
48 protected void tearDown() throws Exception {
49 super.tearDown();
50 }
51 /**
52 * If you don't know by now.
53 * @param args
54 * @throws Exception
55 */
56 public static void main(String[] args) throws Exception {
57 server = new Server(8080);
58 startServer();
59 }
60
61 private static void startServer() throws Exception {
62 if (!started) {
63 WebAppContext wac = new WebAppContext(
64 "src/test/webapp", "/" + contextName);
65 org.mortbay.resource.FileResource.setCheckAliases(false);
66 server.addHandler(wac);
67 server.start();
68 wac.dumpUrl();
69 started = true;
70 }
71 }
72
73 /**
74 * Just to say hello.
75 */
76 public void testIndex() {
77 beginAt("/index.html");
78 assertTextPresent("Hello World!");
79 }
80
81 /**
82 * {@inheritDoc}
83 * @see net.sourceforge.jwebunit.junit.WebTestCase#beginAt(java.lang.String)
84 */
85 public void beginAt(String url) {
86 super.beginAt(contextUrl(url));
87 }
88 /**
89 * {@inheritDoc}
90 * @see net.sourceforge.jwebunit.junit.WebTestCase#gotoPage(java.lang.String)
91 */
92 public void gotoPage(String url) {
93 super.gotoPage(contextUrl(url));
94 }
95 protected String contextUrl(String url) {
96 return "/" + contextName + url;
97 }
98 }