1 package org.melati;
2
3 import org.mortbay.jetty.Server;
4 import org.mortbay.jetty.webapp.WebAppContext;
5 import org.mortbay.resource.FileResource;
6
7 import net.sourceforge.jwebunit.junit.WebTestCase;
8
9
10
11
12
13
14
15
16
17
18 public class JettyWebTestCase extends WebTestCase {
19
20 private static Server server;
21 private static boolean started = false;
22 protected static String contextName = "melatitest";
23 protected static String webAppDirName = "src/test/webapp";
24
25
26
27
28 public JettyWebTestCase() {
29 super();
30 }
31
32
33
34
35
36 public JettyWebTestCase(String name) {
37 super(name);
38 }
39
40 protected void setUp() throws Exception {
41
42 startServer(8083);
43
44
45 int actualPort = server.getConnectors()[0].getLocalPort();
46 getTestContext().setBaseUrl("http://localhost:" + actualPort + "/" );
47 }
48
49 protected void tearDown() throws Exception {
50 super.tearDown();
51 }
52
53
54
55
56
57
58 public static void main(String[] args) throws Exception {
59 startServer(8080);
60 }
61
62 protected static void startServer(int port) throws Exception {
63 if (!started) {
64 server = new Server(port);
65 WebAppContext wac = new WebAppContext(
66 getWebAppDirName(), "/" + getContextName());
67 FileResource.setCheckAliases(false);
68 server.addHandler(wac);
69 server.start();
70 wac.dumpUrl();
71 started = true;
72 }
73
74 }
75
76
77
78
79 public void testIndex() {
80 beginAt("/index.html");
81 assertTextPresent("Hello World!");
82 }
83
84
85
86
87
88 public void beginAt(String url) {
89 super.beginAt(contextUrl(url));
90 }
91
92
93
94
95 public void gotoPage(String url) {
96 super.gotoPage(contextUrl(url));
97 }
98 protected String contextUrl(String url) {
99 return "/" + getContextName() + url;
100 }
101
102
103
104
105 public static String getContextName() {
106 return contextName;
107 }
108
109
110
111
112 public static String getWebAppDirName() {
113 return webAppDirName;
114 }
115
116 }