KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > junit > internal > RemoteMediator


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.junit.internal;
8
9
10 import java.io.IOException JavaDoc;
11 import java.lang.reflect.Constructor JavaDoc;
12 import javax.servlet.ServletException JavaDoc;
13 import javax.servlet.http.HttpServletRequest JavaDoc;
14 import javax.servlet.http.HttpServletResponse JavaDoc;
15 import javax.servlet.jsp.PageContext JavaDoc;
16
17 import org.apache.log4j.Logger;
18
19 import org.jdom.Document;
20 import org.jdom.output.XMLOutputter;
21
22 import com.inversoft.junit.JspTestCase;
23 import com.inversoft.junit.Request;
24 import com.inversoft.junit.Response;
25 import com.inversoft.junit.Result;
26 import com.inversoft.junit.URL;
27 import com.inversoft.junit.WebTestCase;
28 import com.inversoft.junit.internal.http.HttpServletRequestWrapper;
29 import com.inversoft.junit.internal.http.HttpServletResponseWrapper;
30 import com.inversoft.junit.internal.http.ServletContextWrapper;
31
32
33 /**
34  * This class is the mediator for remote testing
35  *
36  * @author Brian Pontarelli
37  * @since 2.0
38  * @version 2.0
39  */

40 public class RemoteMediator implements Mediator {
41
42     /**
43      * The logger for the Proxy interface, which can be retrieved from any of
44      * the proxy implementations
45      */

46     private static final Logger logger = Logger.getLogger(RemoteMediator.class);
47
48
49     /**
50      * Conducts the remote test
51      *
52      * @param testCase The test case to mediate the test on
53      * @throws Throwable If the test failed or something else went wrong
54      */

55     public void mediate(WebTestCase testCase) throws Throwable JavaDoc {
56
57         // First construct a new Request object and call the begin method
58
// if one exists
59
Request JavaDoc request = new Request JavaDoc();
60         ClientCaller client = new ClientCaller();
61
62         client.callBeginMethod(testCase, request);
63
64         HttpCaller caller = new HttpCaller();
65         Response JavaDoc response = caller.runTest(testCase, request); // This throws the failures
66

67         client.callEndMethod(testCase, response);
68     }
69
70     /**
71      * Mediates on the server side
72      */

73     public void mediateServer(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
74     throws Throwable JavaDoc {
75
76         String JavaDoc type = request.getParameter(Constants.URL_REQUEST_TYPE_PARAM);
77         if (type == null) {
78             throw new ServletException JavaDoc("FATAL-No type specified!");
79         }
80
81         //boolean isTest = false;
82
if (type.equals(Constants.REQUEST_TYPE_TEST)) {
83             WebTestCase testCase;
84
85             try {
86                 testCase = getTestCaseInstance(request);
87             } catch (ClassNotFoundException JavaDoc cnfe) {
88                 Result result = new Result(cnfe);
89                 request.getSession().getServletContext().setAttribute(Constants.RESULT_PARAM, result);
90
91                 throw new ServletException JavaDoc(cnfe);
92             }
93
94             // Using the request, populate the Mock objects, set the WebTestCase
95
// variables and call the LocationCaller to execute the test method
96
setupTestCase(testCase, request, response);
97
98             LocationCaller location = new LocationCaller();
99             Result result;
100
101             try {
102                 location.callTestMethod(testCase);
103                 result = new Result();
104             } catch (Throwable JavaDoc t) {
105                 result = new Result(t);
106                 logger.debug("Test threw exception", t);
107             }
108
109             // Store the result in the servlet context for later retrieval
110
logger.debug("Ran the test [" + testCase.getName() + "] storing result");
111             request.getSession().getServletContext().setAttribute(Constants.RESULT_PARAM, result);
112
113         } else if (type.equals(Constants.REQUEST_TYPE_RESULT)) {
114
115             Result result =
116                 (Result) request.getSession().getServletContext().getAttribute(Constants.RESULT_PARAM);
117
118             if (result == null) {
119                 throw new ServletException JavaDoc("FATAL ERROR ON SERVER - Result object does not exist");
120             }
121
122             outputResult(result, response);
123
124         } else {
125             ServletException JavaDoc se =
126                 new ServletException JavaDoc("FATAL ERROR ON SERVER - Invalid type: " + type);
127             Result result = new Result(se);
128             request.getSession().getServletContext().setAttribute(Constants.RESULT_PARAM, result);
129             throw se;
130         }
131     }
132
133     /**
134      * Creates an instance of the WebTestCase implementaion using the className
135      * and method name given
136      *
137      * @return An instance of the correct test case class
138      * @throws ClassNotFoundException If the test case class could not be found
139      */

140     protected WebTestCase getTestCaseInstance(HttpServletRequest JavaDoc request)
141     throws ClassNotFoundException JavaDoc {
142         String JavaDoc className = request.getParameter(Constants.URL_CLASS_PARAM);
143         String JavaDoc methodName = request.getParameter(Constants.URL_METHOD_PARAM);
144
145         if (className == null) {
146             throw new ClassNotFoundException JavaDoc("FATAL-No class name parameter in request");
147         }
148
149         if (methodName == null) {
150             throw new ClassNotFoundException JavaDoc("FATAL-No method name parameter in request");
151         }
152
153         Class JavaDoc theClass = Class.forName(className);
154
155         try {
156             Constructor JavaDoc constructor =
157                 theClass.getConstructor(WebTestCase.CONSTRUCTOR_PARAM_TYPES);
158
159             return (WebTestCase) constructor.newInstance(new Object JavaDoc[] {methodName});
160         } catch (Exception JavaDoc e) {
161             throw new ClassNotFoundException JavaDoc("Error instantiating the TestCase: " + e.toString());
162         }
163     }
164
165     /**
166      * Sets up the WebTestCase with the Mock objects which are constructed using
167      * the request given
168      */

169     protected void setupTestCase(WebTestCase testCase, HttpServletRequest JavaDoc request,
170             HttpServletResponse JavaDoc response) {
171         URL url = null;
172         String JavaDoc protocol = request.getParameter(Constants.URL_PROTOCOL_PARAM);
173         String JavaDoc server = request.getParameter(Constants.URL_SERVERNAME_PARAM);
174         String JavaDoc servlet = request.getParameter(Constants.URL_SERVLETPATH_PARAM);
175         String JavaDoc context = request.getParameter(Constants.URL_CONTEXT_PARAM);
176         String JavaDoc path = request.getParameter(Constants.URL_PATHINFO_PARAM);
177         String JavaDoc query = request.getParameter(Constants.URL_QUERYSTRING_PARAM);
178
179         if (protocol != null || server != null || servlet != null ||
180                 context != null || path != null || query != null) {
181             url = new URL(request, context, path, protocol, query, server, servlet);
182         }
183
184         testCase.setOrigRequest(request);
185         testCase.setOrigResponse(response);
186         testCase.setRequest(new HttpServletRequestWrapper(request, url));
187         testCase.setResponse(new HttpServletResponseWrapper(response));
188         testCase.setContext(new ServletContextWrapper(
189             request.getSession().getServletContext()));
190
191         PageContext JavaDoc pc = (PageContext JavaDoc) request.getAttribute(Constants.PAGE_CONTEXT_KEY);
192         if (context != null && testCase instanceof JspTestCase) {
193             ((JspTestCase) testCase).pageContext = pc;
194         }
195     }
196
197     /**
198      * Outputs the given result to the stream in the response given
199      *
200      * @param result The result to output
201      * @param response The response to write the result out to
202      * @throws IOException If the result could not be written
203      */

204     protected void outputResult(Result result, HttpServletResponse JavaDoc response)
205     throws IOException JavaDoc {
206         Document xml = result.toXML();
207         XMLOutputter xmlOut = new XMLOutputter(" ", true);
208
209         xmlOut.output(xml, response.getOutputStream());
210     }
211 }
Popular Tags