1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sourceforge.jwebunit.tests.util;
20
21 import java.net.URL;
22 import net.sourceforge.jwebunit.tests.JWebUnitAPITestCase;
23 import org.eclipse.jetty.http.MimeTypes;
24 import org.eclipse.jetty.security.HashLoginService;
25 import org.eclipse.jetty.server.Connector;
26 import org.eclipse.jetty.server.Handler;
27 import org.eclipse.jetty.server.Server;
28 import org.eclipse.jetty.server.ServerConnector;
29 import org.eclipse.jetty.server.handler.DefaultHandler;
30 import org.eclipse.jetty.server.handler.HandlerCollection;
31 import org.eclipse.jetty.webapp.WebAppContext;
32 import org.junit.BeforeClass;
33
34 import static org.junit.Assert.fail;
35
36
37
38
39
40
41
42 public class JettySetup {
43
44
45
46 private static Server jettyServer = null;
47
48 private static boolean started = false;
49
50
51
52
53
54
55
56
57 @BeforeClass
58 public static void startup() throws Exception {
59 if (!started) {
60 try {
61 jettyServer = new Server();
62 ServerConnector connector = new ServerConnector(jettyServer);
63 connector.setPort(JWebUnitAPITestCase.JETTY_PORT);
64 jettyServer.setConnectors(new Connector[] { connector });
65
66 WebAppContext wah = new WebAppContext();
67
68
69 MimeTypes mimeTypes = new MimeTypes();
70 mimeTypes.addMimeMapping("html_utf-8", "text/html; charset=UTF-8");
71 mimeTypes.addMimeMapping("txt", "text/plain");
72 mimeTypes.addMimeMapping("bin", "application/octet-stream");
73 wah.setMimeTypes(mimeTypes);
74
75
76 HandlerCollection handlers= new HandlerCollection();
77 handlers.setHandlers(new Handler[]{wah, new DefaultHandler()});
78
79 jettyServer.setHandler(wah);
80 HashLoginService myrealm = new HashLoginService("MyRealm");
81 URL config = JettySetup.class.getResource("/jetty-users.properties");
82 myrealm.setConfig(config.toString());
83 jettyServer.addBean(myrealm);
84
85 wah.setContextPath(JWebUnitAPITestCase.JETTY_URL);
86
87 URL url = JettySetup.class.getResource("/testcases/");
88 wah.setWar(url.toString());
89
90 jettyServer.start();
91
92 started = true;
93
94 } catch (Exception e) {
95 e.printStackTrace();
96 fail("Could not start the Jetty server: " + e);
97 }
98 }
99 }
100
101 protected static void shutdown() throws Exception {
102 try {
103 jettyServer.stop();
104 started = false;
105 } catch (InterruptedException e) {
106 e.printStackTrace();
107 fail("Jetty server was interrupted: " + e);
108 } catch (Exception e) {
109 e.printStackTrace();
110 fail("Could not stop the Jetty server: " + e);
111 }
112 }
113 }