001: package org.mortbay.jetty.testing;
002:
003: import java.util.Enumeration;
004: import java.util.EventListener;
005:
006: import org.mortbay.jetty.LocalConnector;
007: import org.mortbay.jetty.Server;
008: import org.mortbay.jetty.servlet.Context;
009: import org.mortbay.jetty.servlet.FilterHolder;
010: import org.mortbay.jetty.servlet.ServletHolder;
011: import org.mortbay.util.Attributes;
012:
013: /* ------------------------------------------------------------ */
014: /** Testing support for servlets and filters.
015: *
016: * Allows a programatic setup of a context with servlets and filters for
017: * testing. Raw HTTP requests may be sent to the context and responses received.
018: * To avoid handling raw HTTP see {@link org.mortbay.jetty.testing.HttpTester}.
019: * <pre>
020: * ServletTester tester=new ServletTester();
021: * tester.setContextPath("/context");
022: * tester.addServlet(TestServlet.class, "/servlet/*");
023: * tester.addServlet("org.mortbay.jetty.servlet.DefaultServlet", "/");
024: * tester.start();
025: * String response = tester.getResponses("GET /context/servlet/info HTTP/1.0\r\n\r\n");
026: * </pre>
027: *
028: * @see org.mortbay.jetty.testing.HttpTester
029: * @author gregw
030: *
031: */
032: public class ServletTester {
033: Server server = new Server();
034: LocalConnector connector = new LocalConnector();
035: Context context = new Context(Context.SESSIONS | Context.SECURITY);
036:
037: public ServletTester() {
038: try {
039: server.setSendServerVersion(false);
040: server.addConnector(connector);
041: server.addHandler(context);
042: } catch (Error e) {
043: throw e;
044: } catch (RuntimeException e) {
045: throw e;
046: } catch (Exception e) {
047: throw new RuntimeException(e);
048: }
049: }
050:
051: /* ------------------------------------------------------------ */
052: public void start() throws Exception {
053: server.start();
054: }
055:
056: /* ------------------------------------------------------------ */
057: public void stop() throws Exception {
058: server.stop();
059: }
060:
061: /* ------------------------------------------------------------ */
062: /** Get raw HTTP responses from raw HTTP requests.
063: * Multiple requests and responses may be handled, but only if
064: * persistent connections conditions apply.
065: * @param rawRequests String of raw HTTP requests
066: * @return String of raw HTTP responses
067: * @throws Exception
068: */
069: public String getResponses(String rawRequests) throws Exception {
070: connector.reopen();
071: //System.err.println(">>>>\n"+rawRequests);
072: String responses = connector.getResponses(rawRequests);
073: //System.err.println("<<<<\n"+responses);
074: return responses;
075: }
076:
077: /* ------------------------------------------------------------ */
078: /**
079: * @param listener
080: * @see org.mortbay.jetty.handler.ContextHandler#addEventListener(java.util.EventListener)
081: */
082: public void addEventListener(EventListener listener) {
083: context.addEventListener(listener);
084: }
085:
086: /* ------------------------------------------------------------ */
087: /**
088: * @param filterClass
089: * @param pathSpec
090: * @param dispatches
091: * @return
092: * @see org.mortbay.jetty.servlet.Context#addFilter(java.lang.Class, java.lang.String, int)
093: */
094: public FilterHolder addFilter(Class filterClass, String pathSpec,
095: int dispatches) {
096: return context.addFilter(filterClass, pathSpec, dispatches);
097: }
098:
099: /* ------------------------------------------------------------ */
100: /**
101: * @param filterClass
102: * @param pathSpec
103: * @param dispatches
104: * @return
105: * @see org.mortbay.jetty.servlet.Context#addFilter(java.lang.String, java.lang.String, int)
106: */
107: public FilterHolder addFilter(String filterClass, String pathSpec,
108: int dispatches) {
109: return context.addFilter(filterClass, pathSpec, dispatches);
110: }
111:
112: /* ------------------------------------------------------------ */
113: /**
114: * @param servlet
115: * @param pathSpec
116: * @return
117: * @see org.mortbay.jetty.servlet.Context#addServlet(java.lang.Class, java.lang.String)
118: */
119: public ServletHolder addServlet(Class servlet, String pathSpec) {
120: return context.addServlet(servlet, pathSpec);
121: }
122:
123: /* ------------------------------------------------------------ */
124: /**
125: * @param className
126: * @param pathSpec
127: * @return
128: * @see org.mortbay.jetty.servlet.Context#addServlet(java.lang.String, java.lang.String)
129: */
130: public ServletHolder addServlet(String className, String pathSpec) {
131: return context.addServlet(className, pathSpec);
132: }
133:
134: /* ------------------------------------------------------------ */
135: /**
136: * @param name
137: * @return
138: * @see org.mortbay.jetty.handler.ContextHandler#getAttribute(java.lang.String)
139: */
140: public Object getAttribute(String name) {
141: return context.getAttribute(name);
142: }
143:
144: /* ------------------------------------------------------------ */
145: /**
146: * @return
147: * @see org.mortbay.jetty.handler.ContextHandler#getAttributeNames()
148: */
149: public Enumeration getAttributeNames() {
150: return context.getAttributeNames();
151: }
152:
153: /* ------------------------------------------------------------ */
154: /**
155: * @return
156: * @see org.mortbay.jetty.handler.ContextHandler#getAttributes()
157: */
158: public Attributes getAttributes() {
159: return context.getAttributes();
160: }
161:
162: /* ------------------------------------------------------------ */
163: /**
164: * @return
165: * @see org.mortbay.jetty.handler.ContextHandler#getResourceBase()
166: */
167: public String getResourceBase() {
168: return context.getResourceBase();
169: }
170:
171: /* ------------------------------------------------------------ */
172: /**
173: * @param name
174: * @param value
175: * @see org.mortbay.jetty.handler.ContextHandler#setAttribute(java.lang.String, java.lang.Object)
176: */
177: public void setAttribute(String name, Object value) {
178: context.setAttribute(name, value);
179: }
180:
181: /* ------------------------------------------------------------ */
182: /**
183: * @param classLoader
184: * @see org.mortbay.jetty.handler.ContextHandler#setClassLoader(java.lang.ClassLoader)
185: */
186: public void setClassLoader(ClassLoader classLoader) {
187: context.setClassLoader(classLoader);
188: }
189:
190: /* ------------------------------------------------------------ */
191: /**
192: * @param contextPath
193: * @see org.mortbay.jetty.handler.ContextHandler#setContextPath(java.lang.String)
194: */
195: public void setContextPath(String contextPath) {
196: context.setContextPath(contextPath);
197: }
198:
199: /* ------------------------------------------------------------ */
200: /**
201: * @param eventListeners
202: * @see org.mortbay.jetty.handler.ContextHandler#setEventListeners(java.util.EventListener[])
203: */
204: public void setEventListeners(EventListener[] eventListeners) {
205: context.setEventListeners(eventListeners);
206: }
207:
208: /* ------------------------------------------------------------ */
209: /**
210: * @param resourceBase
211: * @see org.mortbay.jetty.handler.ContextHandler#setResourceBase(java.lang.String)
212: */
213: public void setResourceBase(String resourceBase) {
214: context.setResourceBase(resourceBase);
215: }
216:
217: }
|