01: package org.rapla;
02:
03: import java.io.File;
04: import java.net.ConnectException;
05: import java.net.HttpURLConnection;
06: import java.net.URL;
07:
08: import junit.framework.TestCase;
09:
10: import org.mortbay.jetty.Server;
11: import org.mortbay.jetty.servlet.Context;
12: import org.mortbay.jetty.servlet.ServletHolder;
13: import org.rapla.components.util.IOUtil;
14: import org.rapla.framework.RaplaContext;
15: import org.rapla.framework.RaplaLocale;
16:
17: public abstract class ServletTestBase extends TestCase {
18: MainServlet mainServlet = new MainServlet();
19: Server jettyServer;
20: final public static String WEBAPP_FOLDER_NAME = RaplaTestCase.TEST_FOLDER_NAME
21: + "/webapp";
22: final public static String WEBAPP_INF_FOLDER_NAME = WEBAPP_FOLDER_NAME
23: + "/WEB-INF";
24:
25: public ServletTestBase(String name) {
26: super (name);
27: new File("temp").mkdir();
28: File testFolder = new File(RaplaTestCase.TEST_FOLDER_NAME);
29: testFolder.mkdir();
30: }
31:
32: protected void setUp() throws Exception {
33: super .setUp();
34: mainServlet.setServerId(getStorageName());
35: new File(WEBAPP_FOLDER_NAME).mkdir();
36: new File(WEBAPP_INF_FOLDER_NAME).mkdir();
37:
38: IOUtil.copy("test-src/test.xconf", WEBAPP_INF_FOLDER_NAME
39: + "/raplaserver.xconf");
40: IOUtil.copy("test-src/test.xlog", WEBAPP_INF_FOLDER_NAME
41: + "/raplaserver.xlog");
42: IOUtil.copy("test-src/testdefault.xml", WEBAPP_INF_FOLDER_NAME
43: + "/test.xml");
44: IOUtil.copy("webapp/WEB-INF/web.xml", WEBAPP_INF_FOLDER_NAME
45: + "/web.xml");
46:
47: jettyServer = new Server(8051);
48: jettyServer.setAttribute(
49: "org.mortbay.jetty.Request.maxFormContentSize",
50: new Integer(64000000));
51: Context context = new Context(jettyServer, "/",
52: Context.SESSIONS);
53: context.setResourceBase(WEBAPP_FOLDER_NAME);
54: context.addServlet(new ServletHolder(mainServlet), "/*");
55: jettyServer.start();
56:
57: URL server = new URL("http://127.0.0.1:8051/rapla/ping");
58: HttpURLConnection connection = (HttpURLConnection) server
59: .openConnection();
60: int timeout = 10000;
61: int interval = 200;
62: for (int i = 0; i < timeout / interval; i++) {
63: try {
64: connection.connect();
65: } catch (ConnectException ex) {
66: Thread.sleep(interval);
67: }
68: }
69: }
70:
71: protected RaplaContext getContext() {
72: return mainServlet.getContext();
73: }
74:
75: protected RaplaLocale getRaplaLocale() throws Exception {
76: return (RaplaLocale) getContext().lookup(RaplaLocale.ROLE);
77: }
78:
79: protected void tearDown() throws Exception {
80: jettyServer.stop();
81: super .tearDown();
82: }
83:
84: protected String getStorageName() {
85: return "storage-file";
86: }
87: }
|